wifiSet.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. }, 10000); // 10秒超时
  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: '请检查WiFi和密码是否正确',
  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列表超时,请重试',
  193. showCancel: false
  194. });
  195. }, 10000);
  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列表超时,请重试',
  232. showCancel: false
  233. });
  234. }, 10000);
  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: '请检查WiFi密码或设备状态',
  255. showCancel: false
  256. });
  257. }, 10000);
  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. uni.startWifi({
  283. success: (res) => {
  284. console.log('初始化WiFi模块成功', res);
  285. this.getWifiList();
  286. },
  287. fail: (err) => {
  288. console.error('startWifi fail:', err);
  289. }
  290. });
  291. },
  292. getWifiList() {
  293. let that = this;
  294. // 步骤 2: 监听获取到 WiFi 列表数据的事件
  295. uni.onGetWifiList((res) => {
  296. uni.hideLoading();
  297. let list = that.processWifiList(res.wifiList);
  298. that.wifiList = list.map(l=>l.SSID)?.filter(l=>l.indexOf('5G')<0);
  299. that.wifiListCache = [...that.wifiList];
  300. that.wifiSSID = that.wifiList[0];
  301. that.wifiPWD = uni.getStorageSync(that.wifiSSID)||'';
  302. that.isInitOK = true
  303. });
  304. uni.getWifiList({
  305. success: () => {
  306. console.log('获取WiFi列表请求成功');
  307. },
  308. fail: (err) => {
  309. console.error('getWifiList fail:', err);
  310. }
  311. });
  312. },
  313. processWifiList(list) {
  314. const uniqueList = [];
  315. const ssidMap = new Map();
  316. list.forEach(item => {
  317. if (item.SSID && !ssidMap.has(item.SSID)) {
  318. ssidMap.set(item.SSID, true);
  319. uniqueList.push(item);
  320. }
  321. });
  322. // 按信号强度降序排序
  323. return uniqueList.sort((a, b) => b.signalStrength - a.signalStrength);
  324. }
  325. }
  326. }
  327. </script>
  328. <style scoped lang="less">
  329. .page{
  330. padding: 0 40rpx 250rpx;
  331. box-sizing: border-box;
  332. background: #FFFFFF;
  333. &>.title{
  334. font-family: PingFang-SC, PingFang-SC;
  335. font-weight: bold;
  336. font-size: 36rpx;
  337. color: #111111;
  338. line-height: 36rpx;
  339. margin-top: 64rpx;
  340. }
  341. .items{
  342. margin-top: 188rpx;
  343. .item{
  344. margin-top: 80rpx;
  345. padding-bottom: 30rpx;
  346. border-bottom: 1rpx solid #E2E2E2;
  347. .il{
  348. width: calc(100% - 180rpx);
  349. input{
  350. border: none;
  351. font-family: PingFangSC, PingFang SC;
  352. font-weight: 400;
  353. font-size: 28rpx;
  354. color: #333333;
  355. line-height: 42rpx;
  356. }
  357. }
  358. .ir{
  359. width: 160rpx;
  360. height: 64rpx;
  361. background: #D9F159;
  362. border-radius: 32rpx;
  363. font-family: PingFang-SC, PingFang-SC;
  364. font-weight: bold;
  365. font-size: 26rpx;
  366. color: #252525;
  367. line-height: 64rpx;
  368. text-align: center;
  369. }
  370. .iimgs{
  371. image{
  372. width: 36rpx;
  373. height: 36rpx;
  374. }
  375. }
  376. }
  377. }
  378. .zt_btn{
  379. margin-top: 100rpx;
  380. }
  381. .memo{
  382. margin-top: 100rpx;
  383. .m_title{
  384. font-family: PingFangSC, PingFang SC;
  385. font-weight: 400;
  386. font-size: 26rpx;
  387. color: #111111;
  388. line-height: 40rpx;
  389. }
  390. .m_p{
  391. font-family: PingFangSC, PingFang SC;
  392. font-weight: 400;
  393. font-size: 24rpx;
  394. color: #7C8592;
  395. line-height: 54rpx;
  396. margin-top: 10rpx;
  397. }
  398. }
  399. .ph{
  400. color: #7C8592;
  401. }
  402. .wifis{
  403. width: 670rpx;
  404. max-height: 720rpx;
  405. overflow: hidden;
  406. padding-bottom: 20rpx;
  407. .top{
  408. margin-top: 14rpx;
  409. padding: 40rpx;
  410. border-bottom: 1rpx solid #E2E2E2;
  411. .tl{
  412. font-family: PingFang-SC, PingFang-SC;
  413. font-weight: bold;
  414. font-size: 36rpx;
  415. color: #111111;
  416. line-height: 36rpx;
  417. }
  418. .tr{
  419. image{
  420. width: 36rpx;
  421. height: 36rpx;
  422. }
  423. text{
  424. font-family: PingFangSC, PingFang SC;
  425. font-weight: 400;
  426. font-size: 32rpx;
  427. color: #7C8592;
  428. line-height: 36rpx;
  429. margin-left: 10rpx;
  430. }
  431. }
  432. }
  433. .list{
  434. height: 609rpx;
  435. overflow-y: auto;
  436. padding: 30rpx 40rpx;
  437. box-sizing: border-box;
  438. .item{
  439. width: 100%;
  440. height: 120rpx;
  441. border-radius: 16rpx;
  442. border: 1rpx solid #E2E2E2;
  443. margin-top: 20rpx;
  444. padding: 0 24rpx;
  445. box-sizing: border-box;
  446. &:first-child{
  447. margin-top: 0;
  448. }
  449. .il{
  450. width: calc(100% - 134rpx);
  451. image{
  452. width: 42rpx;
  453. height: 42rpx;
  454. }
  455. text{
  456. font-family: PingFangSC, PingFang SC;
  457. font-weight: 400;
  458. font-size: 30rpx;
  459. color: #252525;
  460. line-height: 30rpx;
  461. margin-left: 20rpx;
  462. width: calc(100% - 62rpx);
  463. overflow: hidden;
  464. text-overflow: ellipsis;
  465. white-space: nowrap;
  466. }
  467. }
  468. .ir{
  469. width: 114rpx;
  470. height: 60rpx;
  471. background: #D9F159;
  472. border-radius: 32rpx;
  473. font-family: PingFangSC, PingFang SC;
  474. font-weight: 400;
  475. font-size: 28rpx;
  476. color: #252525;
  477. line-height: 60rpx;
  478. text-align: center;
  479. }
  480. }
  481. }
  482. }
  483. }
  484. </style>