wifiSet.vue 9.0 KB

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