index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <!-- 使用 type="home" 属性设置首页,其他页面不需要设置,默认为page;推荐使用json5,更强大,且允许注释 -->
  2. <route lang="json5" type="page">
  3. {
  4. style: {
  5. navigationBarTitleText: '配网向导',
  6. },
  7. }
  8. </route>
  9. <template>
  10. <view class="page-form">
  11. <view class="form-title">
  12. <view>配网步骤</view>
  13. </view>
  14. <view class="tips-box">
  15. <wd-img :src="deviceImg" width="100" mode="widthFix"></wd-img>
  16. <view class="check-box">
  17. <view class="check-result">{{ checkResult }}</view>
  18. <view v-if="currentWifi" class="check-result">{{ `设备当前连接WiFi: ${currentWifi}` }}</view>
  19. <wd-button @click="getConnectedWifi()">重新检查</wd-button>
  20. </view>
  21. <view v-if="!checkWiFiFlag">{{ `请先连接以 "${targetWifi}" 开头的WiFi` }}</view>
  22. </view>
  23. <view class="form-title">
  24. <view>家庭路由器信息</view>
  25. <view class="opt-item" @click="getWifiList()">
  26. <wd-icon name="refresh" size="30rpx"></wd-icon>
  27. <text>刷新无线列表</text>
  28. </view>
  29. </view>
  30. <wd-form ref="form" :model="formData">
  31. <wd-cell-group custom-class="form-group" border>
  32. <wd-select-picker
  33. v-model="formData.ssid"
  34. label="无线名称"
  35. label-width="100px"
  36. prop="ssid"
  37. type="radio"
  38. filterable
  39. placeholder="请选择家里的无线名称"
  40. :columns="wifiList"
  41. value-key="ssid"
  42. label-key="ssid"
  43. :loading="loading"
  44. :max="1"
  45. :show-confirm="false"
  46. :rules="[{ required: true, message: '请选择家里的无线名称' }]"
  47. />
  48. <wd-input
  49. v-model="formData.pwd"
  50. label="无线密码"
  51. label-width="100px"
  52. prop="pwd"
  53. placeholder="请输入家里的无线密码"
  54. clearable
  55. show-password
  56. :rules="[{ required: true, pattern: /^.{8,}$/, message: '请输入不少于8位无线密码' }]"
  57. />
  58. </wd-cell-group>
  59. <view class="form-footer">
  60. <wd-button type="primary" size="large" :disabled="loading" @click="handleSubmit()" block>提交</wd-button>
  61. </view>
  62. </wd-form>
  63. </view>
  64. </template>
  65. <script lang="ts" setup>
  66. import { testConnect, getWifiStatus, getWifis, connectWifi } from '@/service/api/index'
  67. import { useToast } from 'wot-design-uni'
  68. const loading = ref(false)
  69. const deviceImg = ref('/static/images/device.png')
  70. const toast = useToast()
  71. const targetWifi = "maoer-hub"
  72. const checkWiFiFlag = ref(false)
  73. const checkIPFlag = ref(false)
  74. // 设备当前连接的wifi
  75. const currentWifi = ref("")
  76. // 手机已连接的wifi信息
  77. const wifiInfo: any = ref({})
  78. // 表单信息
  79. const form = ref()
  80. const formData: any = ref({
  81. ssid: "",
  82. pwd: "",
  83. })
  84. const wifiList: any = ref([])
  85. // 检查结果
  86. const checkResult = computed(() => {
  87. return checkWiFiFlag.value ? (checkIPFlag.value ? "已检测到设备" : "未检测到设备") : "未连接指定WiFi"
  88. })
  89. // 检查手机wifi连接是否正确, 匹配前缀
  90. function checkWifi() {
  91. if (wifiInfo.value.SSID.startsWith(targetWifi)) {
  92. checkWiFiFlag.value = true
  93. toast.success('您已连接指定WiFi!')
  94. }
  95. }
  96. // 获取已连接中的 Wi-Fi 信息。
  97. function getConnectedWifi(isFirst = false) {
  98. uni.startWifi().then(() => {
  99. setTimeout(() => {
  100. uni.getConnectedWifi({
  101. success: (res) => {
  102. // 当前手机连接的wifi信息
  103. wifiInfo.value = res.wifi || {}
  104. checkWifi()
  105. if (checkWiFiFlag.value) checkIP(isFirst)
  106. },
  107. fail: (err) => {
  108. toast.error('获取WiFi信息失败')
  109. }
  110. })
  111. }, 1000)
  112. }).catch((err)=> {})
  113. }
  114. // 局域网通信: 检查IP地址是否请求成功
  115. function checkIP(isFirst: boolean = false) {
  116. testConnect().then((res: any) => {
  117. checkIPFlag.value = true
  118. getDeviceWifiStatus()
  119. if (isFirst) getWifiList()
  120. }).catch((err)=> {})
  121. }
  122. // 局域网通信: 获取设备当前连接wifi
  123. function getDeviceWifiStatus() {
  124. getWifiStatus().then((res: any) => {
  125. console.log('当前连接WiFi: ', res);
  126. currentWifi.value = res.body || ""
  127. }).catch((err)=> {})
  128. }
  129. // 局域网通信: 获取可连接wifi列表
  130. function getWifiList() {
  131. toast.loading('加载无线列表中...')
  132. loading.value = true
  133. getWifis().then((res: any) => {
  134. console.log('wifi列表: ', res.body);
  135. wifiList.value = (res.body || []).filter((i: any) => i.ssid)
  136. toast.success('已刷新无线列表')
  137. }).catch((err)=> {})
  138. .finally(() => {
  139. loading.value = false
  140. toast.close()
  141. })
  142. }
  143. // 局域网通信: 设置打印助手的WiFi连接信息
  144. function handleSubmit() {
  145. form.value.validate().then(({ valid, errors }) => {
  146. if (valid) {
  147. loading.value = true
  148. uni.showLoading({
  149. title: '正在提交...'
  150. });
  151. connectWifi(formData.value).then((res: any) => {
  152. console.log('res: ', res);
  153. toast.success('连接成功')
  154. currentWifi.value = res.body || ""
  155. }).catch((err) => {
  156. console.log('err: ', err);
  157. toast.error('连接失败')
  158. getDeviceWifiStatus()
  159. })
  160. .finally(() => {
  161. loading.value = false
  162. uni.hideLoading()
  163. })
  164. }
  165. })
  166. }
  167. onLoad(() => {
  168. getConnectedWifi(true)
  169. })
  170. </script>
  171. <style lang="scss" scoped>
  172. .tips-box {
  173. width: calc(100% - 60rpx);
  174. padding: 40rpx 30rpx;
  175. text-align: center;
  176. background-color: #fff;
  177. border-radius: 16rpx;
  178. }
  179. .check-box {
  180. margin: 40rpx 0;
  181. .check-result {
  182. margin-bottom: 30rpx;
  183. }
  184. }
  185. </style>