wifiSet.vue 11 KB

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