wifiSet.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <cus-header title='配置网络'></cus-header>
  4. <div class="title">设置Wi-Fi信息</div>
  5. <div class="items">
  6. <div class="item adfacjb">
  7. <div class="il">
  8. <input type="text" v-model="wifiSSID" placeholder="网络名称" placeholder-class="ph">
  9. </div>
  10. <div class="ir" @tap="toSelectWifi">选择Wi-Fi</div>
  11. </div>
  12. <div class="item adfacjb">
  13. <div class="il">
  14. <input :password="isPwd" v-model="wifiPWD" placeholder="请输入Wi-Fi密码" placeholder-class="ph">
  15. </div>
  16. <div class="iimgs">
  17. <image @tap="changePwd" src="@/static/pwd_hide.png" v-if="isPwd"></image>
  18. <image @tap="changePwd" src="@/static/pwd_show.png" v-else></image>
  19. </div>
  20. </div>
  21. </div>
  22. <div class="zt_btn" @tap="connectWifi">连接</div>
  23. <div class="memo">
  24. <div class="m_title">配网说明</div>
  25. <div class="m_p">
  26. 1、请注意设备仅支持2.4G的WiFi<br>
  27. 2、不支持无密码的WiFi连接<br>
  28. 3、不支持二次授权或者需要网页二次登录的WiFi<br>
  29. 4、如果使用手机热点连接,请确保手机热点正常开启<br>
  30. 5、如果使用iPhone热点,请确保热点开启了最大兼容模式<br>
  31. 6、iPhone热点因为低功耗,一段时间不使用,热点将会自动关闭,如果无法连接,请重新打开热点
  32. </div>
  33. </div>
  34. <u-popup :show="show" :round="56" mode="center" @close="show=false">
  35. <div class="wifis">
  36. <div class="top adfacjb">
  37. <div class="tl">选择WiFi</div>
  38. <div class="tr adfac" @tap="sxWiFi">
  39. <image src="@/static/refreash.png"></image>
  40. <text>刷新</text>
  41. </div>
  42. </div>
  43. <div class="list" v-if="wifiList.length">
  44. <div class="item adfacjb" v-for="(item,index) in wifiList" :key="index">
  45. <div class="il adfac">
  46. <image src="@/static/wifi.png"></image>
  47. <text>{{item}}</text>
  48. </div>
  49. <div class="ir" @tap="selectWifi(item)">选择</div>
  50. </div>
  51. </div>
  52. <template v-else>
  53. <page-empty></page-empty>
  54. </template>
  55. </div>
  56. </u-popup>
  57. </view>
  58. </template>
  59. <script>
  60. var xBlufi = require("@/utils/blufi/xBlufi.js");
  61. import pageEmpty from '@/components/pageEmpty/index.vue'
  62. export default {
  63. components:{pageEmpty},
  64. data(){
  65. return {
  66. name: '',
  67. connectedDeviceId: '',
  68. connected: true,
  69. isInitOK: false,
  70. wifiSSID:'',
  71. wifiPWD:'',
  72. isPwd:true,
  73. show:false,
  74. wifiList:[],
  75. type:'', // 配网类型
  76. wifiListCache: [], // 缓存 Wi-Fi 列表
  77. initTimeout: null, // 初始化超时定时器
  78. wifiListFetchTimeout: null, // 获取Wi-Fi列表超时定时器
  79. connectTimeout: null, // 配网超时定时器
  80. phoneType:''
  81. }
  82. },
  83. onLoad: function(options) {
  84. this.type = options.type || '';
  85. var that = this
  86. this.name = options.name;
  87. this.connectedDeviceId = options.deviceId;
  88. xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
  89. // 初始化超时处理
  90. this.initTimeout = setTimeout(() => {
  91. wx.hideLoading();
  92. wx.showModal({
  93. title: '温馨提示',
  94. content: '设备初始化超时,请返回上一页重试;若多次超时,请重启设备。',
  95. showCancel: false
  96. });
  97. }, 5000); // 5秒超时
  98. xBlufi.notifyInitBleEsp32({
  99. deviceId: options.deviceId,
  100. })
  101. uni.getSystemInfo({
  102. success:res=>{
  103. this.phoneType = res.platform;
  104. if(res.platform === 'ios'){
  105. wx.showLoading({title:'设备初始化...'})
  106. }else{
  107. this.startSearchWifi();
  108. }
  109. }
  110. })
  111. },
  112. onUnload() {
  113. // 页面卸载时清除所有定时器
  114. if (this.initTimeout) clearTimeout(this.initTimeout);
  115. if (this.wifiListFetchTimeout) clearTimeout(this.wifiListFetchTimeout);
  116. if (this.connectTimeout) clearTimeout(this.connectTimeout);
  117. },
  118. methods:{
  119. funListenDeviceMsgEvent: function(options) {
  120. let that = this
  121. let ssid_arry = this.wifiList;
  122. switch (options.type) {
  123. case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
  124. that.connected = options.result
  125. if (!options.result) {
  126. console.log('小程序与设备异常断开')
  127. }
  128. break;
  129. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
  130. wx.hideLoading();
  131. if (this.connectTimeout) {
  132. clearTimeout(this.connectTimeout);
  133. this.connectTimeout = null;
  134. }
  135. if (!options.result)
  136. wx.showModal({
  137. title: '配网失败',
  138. content: '请检查:\n1、WiFi名和密码是否正确(可手动输入);\n2、设备是否处于待配网状态。',
  139. showCancel: false,
  140. })
  141. else {
  142. if (options.data.progress == 100) {
  143. uni.setStorageSync(that.wifiSSID,that.wifiPWD)
  144. uni.navigateTo({
  145. url:'/pagesMy/wifiSuccess?pwtype='+this.type
  146. })
  147. }
  148. }
  149. break;
  150. case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
  151. break;
  152. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_NEAR_ROUTER_LISTS:
  153. if(this.phoneType==='ios'){
  154. if (this.wifiListFetchTimeout) {
  155. clearTimeout(this.wifiListFetchTimeout);
  156. this.wifiListFetchTimeout = null;
  157. }
  158. wx.hideLoading();
  159. if ('' === options.data.SSID) break;
  160. if(!ssid_arry.includes(options.data.SSID)) ssid_arry.push(options.data.SSID)
  161. that.wifiList = ssid_arry;
  162. that.wifiListCache = [...ssid_arry]; // 缓存
  163. that.wifiSSID = that.wifiList[0];
  164. that.wifiPWD = uni.getStorageSync(that.wifiSSID)||'';
  165. that.isInitOK = true
  166. }
  167. break;
  168. case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
  169. if (this.initTimeout) {
  170. clearTimeout(this.initTimeout);
  171. this.initTimeout = null;
  172. }
  173. wx.hideLoading();
  174. if(this.phoneType==='ios'){
  175. if (options.result) {
  176. // 检查是否有缓存的Wi-Fi列表
  177. if (this.wifiListCache.length > 0) {
  178. this.wifiList = [...this.wifiListCache];
  179. this.wifiSSID = this.wifiList[0];
  180. this.wifiPWD = uni.getStorageSync(this.wifiSSID) || '';
  181. this.isInitOK = true;
  182. } else {
  183. xBlufi.notifySendGetNearRouterSsid()
  184. wx.showLoading({
  185. title: '获取周围WiFi列表...',
  186. })
  187. // 获取Wi-Fi列表超时处理
  188. this.wifiListFetchTimeout = setTimeout(() => {
  189. wx.hideLoading();
  190. wx.showModal({
  191. title: '温馨提示',
  192. content: '获取Wi-Fi列表超时,可手动输入WiFi和密码。',
  193. showCancel: false
  194. });
  195. }, 7000);
  196. }
  197. } else {
  198. that.connected = false
  199. wx.showModal({
  200. title: '温馨提示',
  201. content: '设备初始化失败,请返回上一页重试;若多次失败,请重启设备。',
  202. showCancel: false, //是否显示取消按钮
  203. })
  204. }
  205. }
  206. break;
  207. }
  208. },
  209. changePwd(){
  210. this.isPwd = !this.isPwd;
  211. },
  212. initWiFi(){
  213. // 如果有缓存直接使用
  214. if (this.wifiListCache?.length > 0) {
  215. this.wifiList = [...this.wifiListCache];
  216. return;
  217. }
  218. this.sxWiFi();
  219. },
  220. sxWiFi(){
  221. if(this.phoneType==='ios'){
  222. xBlufi.notifySendGetNearRouterSsid()
  223. wx.showLoading({
  224. title: '获取周围WiFi列表...',
  225. })
  226. // 获取Wi-Fi列表超时处理
  227. this.wifiListFetchTimeout = setTimeout(() => {
  228. wx.hideLoading();
  229. wx.showModal({
  230. title: '温馨提示',
  231. content: '获取Wi-Fi列表超时,可手动输入WiFi和密码。',
  232. showCancel: false
  233. });
  234. }, 7000);
  235. }else{
  236. wx.showLoading({title:'正在获取WiFi...'})
  237. this.wifiList = [];
  238. this.getWifiList();
  239. }
  240. },
  241. connectWifi(){
  242. let that = this;
  243. if(!this.wifiSSID) return this.$showToast('请选择Wi-Fi')
  244. if(!this.wifiPWD) return this.$showToast('请输入Wi-Fi密码')
  245. wx.showLoading({
  246. title: '正在配网...',
  247. mask: true
  248. })
  249. // 配网超时处理
  250. this.connectTimeout = setTimeout(() => {
  251. wx.hideLoading();
  252. wx.showModal({
  253. title: '配网超时',
  254. content: '请检查:\n1、WiFi名和密码是否正确(可手动输入);\n2、设备是否处于待配网状态。',
  255. showCancel: false
  256. });
  257. }, 7000);
  258. xBlufi.notifySendRouterSsidAndPassword({
  259. ssid: that.wifiSSID,
  260. password: that.wifiPWD
  261. })
  262. },
  263. toSelectWifi(){
  264. this.show = true;
  265. // // 打开弹窗时自动获取Wi-Fi列表(如果没有缓存)
  266. // if (this.wifiList.length === 0 && this.wifiListCache.length === 0) {
  267. // this.initWiFi();
  268. // } else if (this.wifiListCache.length > 0 && this.wifiList.length === 0) {
  269. // // 使用缓存
  270. // this.wifiList = [...this.wifiListCache];
  271. // }
  272. },
  273. selectWifi(item){
  274. this.wifiSSID = item;
  275. this.wifiPWD = uni.getStorageSync(item) || '';
  276. this.show = false;
  277. this.isInitOK = true
  278. },
  279. startSearchWifi() {
  280. wx.showLoading({title:'设备初始化获取WiFi中...'})
  281. this.wifiList = [];
  282. let that = this;
  283. uni.startWifi({
  284. success: (res) => {
  285. console.log('初始化WiFi模块成功', res);
  286. this.getWifiList();
  287. },
  288. fail: (err) => {
  289. wx.hideLoading();
  290. wx.showModal({
  291. title: '温馨提示',
  292. content: '初始化WiFi模块失败,请确保已打开手机WiFi。',
  293. showCancel: false,
  294. success: (res) => {
  295. if(res.confirm){
  296. that.startSearchWifi(); // 尝试重新初始化
  297. }
  298. }
  299. });
  300. console.error('startWifi fail:', err);
  301. }
  302. });
  303. },
  304. getWifiList() {
  305. let that = this;
  306. // 步骤 2: 监听获取到 WiFi 列表数据的事件
  307. uni.onGetWifiList((res) => {
  308. uni.hideLoading();
  309. let list = that.processWifiList(res.wifiList);
  310. that.wifiList = list.map(l=>l.SSID)?.filter(l=>l.indexOf('5G')<0);
  311. that.wifiListCache = [...that.wifiList];
  312. that.wifiSSID = that.wifiList[0];
  313. that.wifiPWD = uni.getStorageSync(that.wifiSSID)||'';
  314. that.isInitOK = true
  315. });
  316. uni.getWifiList({
  317. success: () => {
  318. console.log('获取WiFi列表请求成功');
  319. },
  320. fail: (err) => {
  321. console.error('getWifiList fail:', err);
  322. }
  323. });
  324. },
  325. processWifiList(list) {
  326. const uniqueList = [];
  327. const ssidMap = new Map();
  328. list.forEach(item => {
  329. if (item.SSID && !ssidMap.has(item.SSID)) {
  330. ssidMap.set(item.SSID, true);
  331. uniqueList.push(item);
  332. }
  333. });
  334. // 按信号强度降序排序
  335. return uniqueList.sort((a, b) => b.signalStrength - a.signalStrength);
  336. }
  337. }
  338. }
  339. </script>
  340. <style scoped lang="less">
  341. .page{
  342. padding: 0 40rpx 250rpx;
  343. box-sizing: border-box;
  344. background: #FFFFFF;
  345. &>.title{
  346. font-family: PingFang-SC, PingFang-SC;
  347. font-weight: bold;
  348. font-size: 36rpx;
  349. color: #111111;
  350. line-height: 36rpx;
  351. margin-top: 64rpx;
  352. }
  353. .items{
  354. margin-top: 188rpx;
  355. .item{
  356. margin-top: 80rpx;
  357. padding-bottom: 30rpx;
  358. border-bottom: 1rpx solid #E2E2E2;
  359. .il{
  360. width: calc(100% - 180rpx);
  361. input{
  362. border: none;
  363. font-family: PingFangSC, PingFang SC;
  364. font-weight: 400;
  365. font-size: 28rpx;
  366. color: #333333;
  367. line-height: 42rpx;
  368. }
  369. }
  370. .ir{
  371. width: 160rpx;
  372. height: 64rpx;
  373. background: #D9F159;
  374. border-radius: 32rpx;
  375. font-family: PingFang-SC, PingFang-SC;
  376. font-weight: bold;
  377. font-size: 26rpx;
  378. color: #252525;
  379. line-height: 64rpx;
  380. text-align: center;
  381. }
  382. .iimgs{
  383. image{
  384. width: 36rpx;
  385. height: 36rpx;
  386. }
  387. }
  388. }
  389. }
  390. .zt_btn{
  391. margin-top: 100rpx;
  392. }
  393. .memo{
  394. margin-top: 100rpx;
  395. .m_title{
  396. font-family: PingFangSC, PingFang SC;
  397. font-weight: 400;
  398. font-size: 26rpx;
  399. color: #111111;
  400. line-height: 40rpx;
  401. }
  402. .m_p{
  403. font-family: PingFangSC, PingFang SC;
  404. font-weight: 400;
  405. font-size: 24rpx;
  406. color: #7C8592;
  407. line-height: 54rpx;
  408. margin-top: 10rpx;
  409. }
  410. }
  411. .ph{
  412. color: #7C8592;
  413. }
  414. .wifis{
  415. width: 670rpx;
  416. max-height: 720rpx;
  417. overflow: hidden;
  418. padding-bottom: 20rpx;
  419. .top{
  420. margin-top: 14rpx;
  421. padding: 40rpx;
  422. border-bottom: 1rpx solid #E2E2E2;
  423. .tl{
  424. font-family: PingFang-SC, PingFang-SC;
  425. font-weight: bold;
  426. font-size: 36rpx;
  427. color: #111111;
  428. line-height: 36rpx;
  429. }
  430. .tr{
  431. image{
  432. width: 36rpx;
  433. height: 36rpx;
  434. }
  435. text{
  436. font-family: PingFangSC, PingFang SC;
  437. font-weight: 400;
  438. font-size: 32rpx;
  439. color: #7C8592;
  440. line-height: 36rpx;
  441. margin-left: 10rpx;
  442. }
  443. }
  444. }
  445. .list{
  446. height: 609rpx;
  447. overflow-y: auto;
  448. padding: 30rpx 40rpx;
  449. box-sizing: border-box;
  450. .item{
  451. width: 100%;
  452. height: 120rpx;
  453. border-radius: 16rpx;
  454. border: 1rpx solid #E2E2E2;
  455. margin-top: 20rpx;
  456. padding: 0 24rpx;
  457. box-sizing: border-box;
  458. &:first-child{
  459. margin-top: 0;
  460. }
  461. .il{
  462. width: calc(100% - 134rpx);
  463. image{
  464. width: 42rpx;
  465. height: 42rpx;
  466. }
  467. text{
  468. font-family: PingFangSC, PingFang SC;
  469. font-weight: 400;
  470. font-size: 30rpx;
  471. color: #252525;
  472. line-height: 30rpx;
  473. margin-left: 20rpx;
  474. width: calc(100% - 62rpx);
  475. overflow: hidden;
  476. text-overflow: ellipsis;
  477. white-space: nowrap;
  478. }
  479. }
  480. .ir{
  481. width: 114rpx;
  482. height: 60rpx;
  483. background: #D9F159;
  484. border-radius: 32rpx;
  485. font-family: PingFangSC, PingFang SC;
  486. font-weight: 400;
  487. font-size: 28rpx;
  488. color: #252525;
  489. line-height: 60rpx;
  490. text-align: center;
  491. }
  492. }
  493. }
  494. }
  495. }
  496. </style>