// pages/setWifi/setWifi.js const { BtHelper } = require("../../devices/bt_helper"); import EventManager from '../../utils/event_bus' import { EnumCmdEvent, CmdEvent } from '../../devices/cmd_key_event'; import store from '../../utils/store'; import { BtCmd } from '../../devices/bluetooth/bt_cmd'; Page({ data: { navbarData: { showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示 title: '猫王音箱 - WI-FI配置', //导航栏 中间的标题 // callback: true, }, wifiName: '', wifiPassword: '', _otaUrl: "", eyeIconSrc: '../../img/yj0.png', passwordType: true, _onNavigateBack: false, }, callback() { console.log("返回"); wx.navigateBack({ delta: 1 }); }, getConnectedWifi: function () { const that = this; let wifiDic = store.getStore("wifiInfo") let wifiName = wifiDic.wifiName let pwd = wifiDic.wifiPassword if (wifiName && pwd) { console.log("获取Wi-Fi信息", wifiName, pwd); that.setData({ wifiName: wifiName, wifiPassword: pwd }); return; } wx.startWifi({ success(res) { console.log(res.errMsg) wx.getConnectedWifi({ success: function (res) { const wifiName = res.wifi.SSID; that.setData({ wifiName: wifiName }); }, fail: function (err) { console.error('获取Wi-Fi信息失败', err); // wx.showToast({ // title: '获取Wi-Fi信息失败', // icon: 'none' // }); } }); } }) }, onWifiNameInput: function (e) { this.setData({ wifiName: e.detail.value }); }, onWifiPasswordInput: function (e) { this.setData({ wifiPassword: e.detail.value }); }, togglePasswordVisibility: function () { const passwordType = !this.data.passwordType; // const newType = currentType === 'password' ? 'text' : 'password'; const newIconSrc = passwordType ? '../../img/yj0.png' : '../../img/yj1.png'; this.setData({ passwordType: passwordType, eyeIconSrc: newIconSrc }); }, sendWiFiInfo(wifiName, pwd) { // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)]; if (!wifiName || !pwd) { wx.showToast({ title: '请输入正确的账号密码', }) wx.hideLoading() return; } wx.showLoading({ title: 'wifi连接中', }) let result = BtCmd.sendWiFiInfo(wifiName, pwd) // _ble.send({ cmd: result }); BtHelper.getInstance().otaSetWifi(result) }, onConfirm: function () { const { wifiName, wifiPassword } = this.data; if (!wifiName || !wifiPassword) { wx.showToast({ title: '请输入完整的Wi-Fi信息', icon: 'none' }); return; } // 这里可以添加进一步的逻辑,比如连接Wi-Fi或保存信息 this.sendWiFiInfo(wifiName, wifiPassword); }, addNotification() { let _this = this; EventManager.addNotification(CmdEvent.eventName, function (event) { let name = event.cmdEvent; let otaCmd = event.otaCmd console.log("设置wifi0:", event) let kind = event.heiJiaoKind; console.log("设置wifi:", name, otaCmd, kind) // let toast = "设置wifi:" + name + " value," + otaCmd + "kind ," + kind // wx.showToast({ // title: toast, // icon: 'none' // }) if (name === EnumCmdEvent.otaWifi && otaCmd === 1) { store.setStore("wifiInfo", { wifiName: _this.data.wifiName, wifiPassword: _this.data.wifiPassword }) } }, _this) }, wifiPageSuccess() { this.data._onNavigateBack = true }, // 获取保存的 Wi-Fi 账号和密码 getSavedWiFiInfo() { const wifiName = wx.getStorageSync('wifiName') || ''; const wifiPassword = wx.getStorageSync('wifiPassword') || ''; this.setData({ wifiName: wifiName, wifiPassword: wifiPassword }); }, onLoad: function (options) { let param = options.param ?? "{}"; let url = JSON.parse(param).url ?? ""; this.data._otaUrl = url; this.getConnectedWifi(); this.addNotification() }, onUnload() { if (!this.data._onNavigateBack) { BtHelper.getInstance().otaCmd(2) } EventManager.removeNotification(CmdEvent.eventName) }, });