123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- // 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;
- }
- const res = wx.getSystemInfoSync(); // 获取系统信息
- /// android ios
- const platform = res.platform; // 获取平台类型
- console.log("平台类型", platform, getApp().globalData.scopeBluetooth, platform === 'ios' && !getApp().globalData.scopeBluetooth);
- if (platform === 'ios') {
- getApp().getBluetoothStatus()
- return
- }
- that.getWifiName()
- },
- getBluetoothStatusCallck(v) {
- if (v) {
- console.log("获取蓝牙权限成功");
- this.getWifiName()
- }
- },
- getWifiName() {
- var that = this;
- 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'
- // });
- }
- });
- // wx.getWifiList({
- // success: function (res) {
- // let wifiList = res.wifiList ?? [];
- // console.log('已连接的 Wi-Fi 列表:', res);
- // // 检查是否有已连接的 Wi-Fi
- // const connectedWifi = wifiList.find(wifi => wifi.SSID && wifi.connected);
- // if (connectedWifi) {
- // const wifiName = connectedWifi.SSID;
- // that.setData({
- // wifiName: wifiName
- // });
- // } else {
- // console.error('当前没有连接到 Wi-Fi');
- // wx.showToast({
- // title: '当前没有连接到 Wi-Fi',
- // icon: 'none'
- // });
- // }
- // },
- // 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) {
- 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)
- },
- });
|