wifiSet.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. // wx.showLoading({title:'设备初始化...'})
  90. // 初始化超时处理
  91. this.initTimeout = setTimeout(() => {
  92. wx.hideLoading();
  93. wx.showModal({
  94. title: '提示',
  95. content: '设备初始化超时,请重试',
  96. showCancel: false
  97. });
  98. }, 10000); // 10秒超时
  99. xBlufi.notifyInitBleEsp32({
  100. deviceId: options.deviceId,
  101. })
  102. uni.getSystemInfo({
  103. success:res=>{
  104. this.phoneType = res.platform;
  105. if(res.platform === 'android'){
  106. this.startSearchWifi();
  107. }
  108. }
  109. })
  110. },
  111. onUnload() {
  112. // 页面卸载时清除所有定时器
  113. if (this.initTimeout) clearTimeout(this.initTimeout);
  114. if (this.wifiListFetchTimeout) clearTimeout(this.wifiListFetchTimeout);
  115. if (this.connectTimeout) clearTimeout(this.connectTimeout);
  116. },
  117. methods:{
  118. funListenDeviceMsgEvent: function(options) {
  119. let that = this
  120. let ssid_arry = this.wifiList;
  121. switch (options.type) {
  122. case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
  123. that.connected = options.result
  124. if (!options.result) {
  125. console.log('小程序与设备异常断开')
  126. }
  127. break;
  128. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
  129. wx.hideLoading();
  130. if (this.connectTimeout) {
  131. clearTimeout(this.connectTimeout);
  132. this.connectTimeout = null;
  133. }
  134. if (!options.result)
  135. wx.showModal({
  136. title: '配网失败',
  137. content: '请检查WiFi和密码是否正确',
  138. showCancel: false,
  139. })
  140. else {
  141. if (options.data.progress == 100) {
  142. uni.setStorageSync(that.wifiSSID,that.wifiPWD)
  143. uni.navigateTo({
  144. url:'/pagesMy/wifiSuccess?pwtype='+this.type
  145. })
  146. }
  147. }
  148. break;
  149. case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
  150. break;
  151. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_NEAR_ROUTER_LISTS:
  152. if(this.phoneType==='ios'){
  153. if (this.wifiListFetchTimeout) {
  154. clearTimeout(this.wifiListFetchTimeout);
  155. this.wifiListFetchTimeout = null;
  156. }
  157. wx.hideLoading();
  158. if ('' === options.data.SSID) break;
  159. if(!ssid_arry.includes(options.data.SSID)) ssid_arry.push(options.data.SSID)
  160. that.wifiList = ssid_arry;
  161. that.wifiListCache = [...ssid_arry]; // 缓存
  162. that.wifiSSID = that.wifiList[0];
  163. that.wifiPWD = uni.getStorageSync(that.wifiSSID)||'';
  164. that.isInitOK = true
  165. }
  166. break;
  167. case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
  168. if (this.initTimeout) {
  169. clearTimeout(this.initTimeout);
  170. this.initTimeout = null;
  171. }
  172. wx.hideLoading();
  173. if(this.phoneType==='ios'){
  174. if (options.result) {
  175. // 检查是否有缓存的Wi-Fi列表
  176. if (this.wifiListCache.length > 0) {
  177. this.wifiList = [...this.wifiListCache];
  178. this.wifiSSID = this.wifiList[0];
  179. this.wifiPWD = uni.getStorageSync(this.wifiSSID) || '';
  180. this.isInitOK = true;
  181. } else {
  182. xBlufi.notifySendGetNearRouterSsid()
  183. wx.showLoading({
  184. title: '获取周围WiFi列表...',
  185. })
  186. // 获取Wi-Fi列表超时处理
  187. this.wifiListFetchTimeout = setTimeout(() => {
  188. wx.hideLoading();
  189. wx.showModal({
  190. title: '提示',
  191. content: '获取Wi-Fi列表超时,请重试',
  192. showCancel: false
  193. });
  194. }, 10000);
  195. }
  196. } else {
  197. that.connected = false
  198. wx.showModal({
  199. title: '温馨提示',
  200. content: `设备初始化失败`,
  201. showCancel: false, //是否显示取消按钮
  202. })
  203. }
  204. }
  205. break;
  206. }
  207. },
  208. changePwd(){
  209. this.isPwd = !this.isPwd;
  210. },
  211. initWiFi(){
  212. // 如果有缓存直接使用
  213. if (this.wifiListCache?.length > 0) {
  214. this.wifiList = [...this.wifiListCache];
  215. return;
  216. }
  217. this.sxWiFi();
  218. },
  219. sxWiFi(){
  220. if(this.phoneType==='ios'){
  221. xBlufi.notifySendGetNearRouterSsid()
  222. wx.showLoading({
  223. title: '获取周围WiFi列表...',
  224. })
  225. // 获取Wi-Fi列表超时处理
  226. this.wifiListFetchTimeout = setTimeout(() => {
  227. wx.hideLoading();
  228. wx.showModal({
  229. title: '提示',
  230. content: '获取Wi-Fi列表超时,请重试',
  231. showCancel: false
  232. });
  233. }, 10000);
  234. }else{
  235. this.startSearchWifi();
  236. }
  237. },
  238. connectWifi(){
  239. let that = this;
  240. if(!this.wifiSSID) return this.$showToast('请选择Wi-Fi')
  241. if(!this.wifiPWD) return this.$showToast('请输入Wi-Fi密码')
  242. wx.showLoading({
  243. title: '正在配网...',
  244. mask: true
  245. })
  246. // 配网超时处理
  247. this.connectTimeout = setTimeout(() => {
  248. wx.hideLoading();
  249. wx.showModal({
  250. title: '配网超时',
  251. content: '请检查WiFi密码或设备状态',
  252. showCancel: false
  253. });
  254. }, 10000);
  255. xBlufi.notifySendRouterSsidAndPassword({
  256. ssid: that.wifiSSID,
  257. password: that.wifiPWD
  258. })
  259. },
  260. toSelectWifi(){
  261. this.show = true;
  262. // // 打开弹窗时自动获取Wi-Fi列表(如果没有缓存)
  263. // if (this.wifiList.length === 0 && this.wifiListCache.length === 0) {
  264. // this.initWiFi();
  265. // } else if (this.wifiListCache.length > 0 && this.wifiList.length === 0) {
  266. // // 使用缓存
  267. // this.wifiList = [...this.wifiListCache];
  268. // }
  269. },
  270. selectWifi(item){
  271. this.wifiSSID = item;
  272. this.wifiPWD = uni.getStorageSync(item) || '';
  273. this.show = false;
  274. this.isInitOK = true
  275. },
  276. startSearchWifi() {
  277. wx.showLoading({title:'设备初始化获取WiFi...'})
  278. this.wifiList = [];
  279. uni.startWifi({
  280. success: (res) => {
  281. console.log('初始化WiFi模块成功', res);
  282. this.getWifiList();
  283. },
  284. fail: (err) => {
  285. console.error('startWifi fail:', err);
  286. }
  287. });
  288. },
  289. getWifiList() {
  290. let that = this;
  291. // 步骤 2: 监听获取到 WiFi 列表数据的事件
  292. uni.onGetWifiList((res) => {
  293. let list = that.processWifiList(res.wifiList);
  294. that.wifiList = list.map(l=>l.SSID)?.filter(l=>l.indexOf('5G')<0);
  295. that.wifiListCache = [...that.wifiList];
  296. that.wifiSSID = that.wifiList[0];
  297. that.wifiPWD = uni.getStorageSync(that.wifiSSID)||'';
  298. that.isInitOK = true
  299. uni.hideLoading();
  300. });
  301. uni.getWifiList({
  302. success: () => {
  303. console.log('获取WiFi列表请求成功');
  304. },
  305. fail: (err) => {
  306. console.error('getWifiList fail:', err);
  307. }
  308. });
  309. },
  310. processWifiList(list) {
  311. const uniqueList = [];
  312. const ssidMap = new Map();
  313. list.forEach(item => {
  314. if (item.SSID && !ssidMap.has(item.SSID)) {
  315. ssidMap.set(item.SSID, true);
  316. uniqueList.push(item);
  317. }
  318. });
  319. // 按信号强度降序排序
  320. return uniqueList.sort((a, b) => b.signalStrength - a.signalStrength);
  321. }
  322. }
  323. }
  324. </script>
  325. <style scoped lang="less">
  326. .page{
  327. padding: 0 40rpx 250rpx;
  328. box-sizing: border-box;
  329. background: #FFFFFF;
  330. &>.title{
  331. font-family: PingFang-SC, PingFang-SC;
  332. font-weight: bold;
  333. font-size: 36rpx;
  334. color: #111111;
  335. line-height: 36rpx;
  336. margin-top: 64rpx;
  337. }
  338. .items{
  339. margin-top: 188rpx;
  340. .item{
  341. margin-top: 80rpx;
  342. padding-bottom: 30rpx;
  343. border-bottom: 1rpx solid #E2E2E2;
  344. .il{
  345. width: calc(100% - 180rpx);
  346. input{
  347. border: none;
  348. font-family: PingFangSC, PingFang SC;
  349. font-weight: 400;
  350. font-size: 28rpx;
  351. color: #333333;
  352. line-height: 42rpx;
  353. }
  354. }
  355. .ir{
  356. width: 160rpx;
  357. height: 64rpx;
  358. background: #D9F159;
  359. border-radius: 32rpx;
  360. font-family: PingFang-SC, PingFang-SC;
  361. font-weight: bold;
  362. font-size: 26rpx;
  363. color: #252525;
  364. line-height: 64rpx;
  365. text-align: center;
  366. }
  367. .iimgs{
  368. image{
  369. width: 36rpx;
  370. height: 36rpx;
  371. }
  372. }
  373. }
  374. }
  375. .zt_btn{
  376. margin-top: 100rpx;
  377. }
  378. .memo{
  379. margin-top: 100rpx;
  380. .m_title{
  381. font-family: PingFangSC, PingFang SC;
  382. font-weight: 400;
  383. font-size: 26rpx;
  384. color: #111111;
  385. line-height: 40rpx;
  386. }
  387. .m_p{
  388. font-family: PingFangSC, PingFang SC;
  389. font-weight: 400;
  390. font-size: 24rpx;
  391. color: #7C8592;
  392. line-height: 54rpx;
  393. margin-top: 10rpx;
  394. }
  395. }
  396. .ph{
  397. color: #7C8592;
  398. }
  399. .wifis{
  400. width: 670rpx;
  401. max-height: 720rpx;
  402. overflow: hidden;
  403. padding-bottom: 20rpx;
  404. .top{
  405. margin-top: 14rpx;
  406. padding: 40rpx;
  407. border-bottom: 1rpx solid #E2E2E2;
  408. .tl{
  409. font-family: PingFang-SC, PingFang-SC;
  410. font-weight: bold;
  411. font-size: 36rpx;
  412. color: #111111;
  413. line-height: 36rpx;
  414. }
  415. .tr{
  416. image{
  417. width: 36rpx;
  418. height: 36rpx;
  419. }
  420. text{
  421. font-family: PingFangSC, PingFang SC;
  422. font-weight: 400;
  423. font-size: 32rpx;
  424. color: #7C8592;
  425. line-height: 36rpx;
  426. margin-left: 10rpx;
  427. }
  428. }
  429. }
  430. .list{
  431. height: 609rpx;
  432. overflow-y: auto;
  433. padding: 30rpx 40rpx;
  434. box-sizing: border-box;
  435. .item{
  436. width: 100%;
  437. height: 120rpx;
  438. border-radius: 16rpx;
  439. border: 1rpx solid #E2E2E2;
  440. margin-top: 20rpx;
  441. padding: 0 24rpx;
  442. box-sizing: border-box;
  443. &:first-child{
  444. margin-top: 0;
  445. }
  446. .il{
  447. width: calc(100% - 134rpx);
  448. image{
  449. width: 42rpx;
  450. height: 42rpx;
  451. }
  452. text{
  453. font-family: PingFangSC, PingFang SC;
  454. font-weight: 400;
  455. font-size: 30rpx;
  456. color: #252525;
  457. line-height: 30rpx;
  458. margin-left: 20rpx;
  459. width: calc(100% - 62rpx);
  460. overflow: hidden;
  461. text-overflow: ellipsis;
  462. white-space: nowrap;
  463. }
  464. }
  465. .ir{
  466. width: 114rpx;
  467. height: 60rpx;
  468. background: #D9F159;
  469. border-radius: 32rpx;
  470. font-family: PingFangSC, PingFang SC;
  471. font-weight: 400;
  472. font-size: 28rpx;
  473. color: #252525;
  474. line-height: 60rpx;
  475. text-align: center;
  476. }
  477. }
  478. }
  479. }
  480. }
  481. </style>