wifiSet.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. }
  77. },
  78. onLoad: function(options) {
  79. this.type = options.type || '';
  80. var that = this
  81. this.name = options.name;
  82. this.connectedDeviceId = options.deviceId;
  83. xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
  84. xBlufi.notifyInitBleEsp32({
  85. deviceId: options.deviceId,
  86. })
  87. wx.showLoading({
  88. title: '设备初始化中',
  89. })
  90. },
  91. // onUnload: function() {
  92. // let that = this
  93. // xBlufi.notifyConnectBle({
  94. // isStart: false,
  95. // deviceId: that.connectedDeviceId,
  96. // name: that.name,
  97. // });
  98. // xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
  99. // },
  100. methods:{
  101. funListenDeviceMsgEvent: function(options) {
  102. let that = this
  103. let ssid_arry = this.wifiList;
  104. switch (options.type) {
  105. // case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
  106. // that.connected = options.result
  107. // if (!options.result) {
  108. // wx.showModal({
  109. // title: '很抱歉提醒你!',
  110. // content: '小程序与设备异常断开',
  111. // showCancel: false, //是否显示取消按钮
  112. // })
  113. // }
  114. // break;
  115. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
  116. wx.hideLoading();
  117. if (!options.result)
  118. wx.showModal({
  119. title: '温馨提示',
  120. content: '配网失败,请重试',
  121. showCancel: false,
  122. })
  123. else {
  124. if (options.data.progress == 100) {
  125. uni.setStorageSync(that.wifiSSID,that.wifiPWD)
  126. // uni.navigateTo({
  127. // url:'/pagesMy/wifiSuccess'
  128. // })
  129. let result = {
  130. "macAddress": this.connectedDeviceId||'',
  131. "board": this.name||"cx-mxb-wifi",
  132. "appVersion": "1.8.2" ,
  133. };
  134. console.log(result,'配网成功')
  135. uni.navigateTo({
  136. url:'/pagesMy/scanResult?scanParams='+JSON.stringify(result)+'&type=lanya'+'&pwtype='+this.type
  137. })
  138. }
  139. }
  140. break;
  141. // case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
  142. // wx.showModal({
  143. // title: '收到自定义设备数据',
  144. // content: `【${options.data}】`,
  145. // showCancel: false,
  146. // })
  147. // break;
  148. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_NEAR_ROUTER_LISTS:
  149. wx.hideLoading();
  150. if ('' === options.data.SSID) break;
  151. if(!ssid_arry.includes(options.data.SSID)) ssid_arry.push(options.data.SSID)
  152. that.wifiList = ssid_arry;
  153. that.wifiSSID = that.wifiList[0];
  154. that.wifiPWD = uni.getStorageSync(that.wifiSSID)||'';
  155. that.isInitOK = true
  156. break;
  157. case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
  158. wx.hideLoading();
  159. if (options.result) {
  160. xBlufi.notifySendGetNearRouterSsid()
  161. wx.showLoading({
  162. title: '获取周围WiFi列表...',
  163. })
  164. } else {
  165. that.connected = false
  166. wx.showModal({
  167. title: '温馨提示',
  168. content: `设备初始化失败`,
  169. showCancel: false, //是否显示取消按钮
  170. })
  171. }
  172. break;
  173. }
  174. },
  175. changePwd(){
  176. this.isPwd = !this.isPwd;
  177. },
  178. initWiFi(){
  179. // let that = this;
  180. // xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
  181. // xBlufi.notifyInitBleEsp32({
  182. // deviceId: that.connectedDeviceId
  183. // })
  184. },
  185. connectWifi(){
  186. let that = this;
  187. if(!this.wifiSSID) return this.$showToast('请选择Wi-Fi')
  188. if(!this.wifiPWD) return this.$showToast('请输入Wi-Fi密码')
  189. wx.showLoading({
  190. title: '正在配网...',
  191. mask: true
  192. })
  193. xBlufi.notifySendRouterSsidAndPassword({
  194. ssid: that.wifiSSID,
  195. password: that.wifiPWD
  196. })
  197. },
  198. toSelectWifi(){
  199. this.show = true;
  200. },
  201. selectWifi(item){
  202. this.wifiSSID = item;
  203. this.wifiPWD = uni.getStorageSync(item) || '';
  204. this.show = false;
  205. this.isInitOK = true
  206. }
  207. }
  208. }
  209. </script>
  210. <style scoped lang="less">
  211. .page{
  212. padding: 0 40rpx 250rpx;
  213. box-sizing: border-box;
  214. background: #FFFFFF;
  215. &>.title{
  216. font-family: PingFang-SC, PingFang-SC;
  217. font-weight: bold;
  218. font-size: 36rpx;
  219. color: #111111;
  220. line-height: 36rpx;
  221. margin-top: 64rpx;
  222. }
  223. .items{
  224. margin-top: 188rpx;
  225. .item{
  226. margin-top: 80rpx;
  227. padding-bottom: 30rpx;
  228. border-bottom: 1rpx solid #E2E2E2;
  229. .il{
  230. width: calc(100% - 180rpx);
  231. input{
  232. border: none;
  233. font-family: PingFangSC, PingFang SC;
  234. font-weight: 400;
  235. font-size: 28rpx;
  236. color: #333333;
  237. line-height: 42rpx;
  238. }
  239. }
  240. .ir{
  241. width: 160rpx;
  242. height: 64rpx;
  243. background: #D9F159;
  244. border-radius: 32rpx;
  245. font-family: PingFang-SC, PingFang-SC;
  246. font-weight: bold;
  247. font-size: 26rpx;
  248. color: #252525;
  249. line-height: 64rpx;
  250. text-align: center;
  251. }
  252. .iimgs{
  253. image{
  254. width: 36rpx;
  255. height: 36rpx;
  256. }
  257. }
  258. }
  259. }
  260. .zt_btn{
  261. margin-top: 100rpx;
  262. }
  263. .memo{
  264. margin-top: 100rpx;
  265. .m_title{
  266. font-family: PingFangSC, PingFang SC;
  267. font-weight: 400;
  268. font-size: 26rpx;
  269. color: #111111;
  270. line-height: 40rpx;
  271. }
  272. .m_p{
  273. font-family: PingFangSC, PingFang SC;
  274. font-weight: 400;
  275. font-size: 24rpx;
  276. color: #7C8592;
  277. line-height: 54rpx;
  278. margin-top: 10rpx;
  279. }
  280. }
  281. .ph{
  282. color: #7C8592;
  283. }
  284. .wifis{
  285. width: 670rpx;
  286. max-height: 720rpx;
  287. overflow: hidden;
  288. padding-bottom: 20rpx;
  289. .top{
  290. margin-top: 14rpx;
  291. padding: 40rpx;
  292. border-bottom: 1rpx solid #E2E2E2;
  293. .tl{
  294. font-family: PingFang-SC, PingFang-SC;
  295. font-weight: bold;
  296. font-size: 36rpx;
  297. color: #111111;
  298. line-height: 36rpx;
  299. }
  300. .tr{
  301. image{
  302. width: 36rpx;
  303. height: 36rpx;
  304. }
  305. text{
  306. font-family: PingFangSC, PingFang SC;
  307. font-weight: 400;
  308. font-size: 32rpx;
  309. color: #7C8592;
  310. line-height: 36rpx;
  311. margin-left: 10rpx;
  312. }
  313. }
  314. }
  315. .list{
  316. height: 609rpx;
  317. overflow-y: auto;
  318. padding: 30rpx 40rpx;
  319. box-sizing: border-box;
  320. .item{
  321. width: 100%;
  322. height: 120rpx;
  323. border-radius: 16rpx;
  324. border: 1rpx solid #E2E2E2;
  325. margin-top: 20rpx;
  326. padding: 0 24rpx;
  327. box-sizing: border-box;
  328. &:first-child{
  329. margin-top: 0;
  330. }
  331. .il{
  332. width: calc(100% - 134rpx);
  333. image{
  334. width: 42rpx;
  335. height: 42rpx;
  336. }
  337. text{
  338. font-family: PingFangSC, PingFang SC;
  339. font-weight: 400;
  340. font-size: 30rpx;
  341. color: #252525;
  342. line-height: 30rpx;
  343. margin-left: 20rpx;
  344. width: calc(100% - 62rpx);
  345. overflow: hidden;
  346. text-overflow: ellipsis;
  347. white-space: nowrap;
  348. }
  349. }
  350. .ir{
  351. width: 114rpx;
  352. height: 60rpx;
  353. background: #D9F159;
  354. border-radius: 32rpx;
  355. font-family: PingFangSC, PingFang SC;
  356. font-weight: 400;
  357. font-size: 28rpx;
  358. color: #252525;
  359. line-height: 60rpx;
  360. text-align: center;
  361. }
  362. }
  363. }
  364. }
  365. }
  366. </style>