wifiSet.vue 11 KB

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