setWifi.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // pages/setWifi/setWifi.js
  2. const { BtHelper } = require("../../devices/bt_helper");
  3. import EventManager from '../../utils/event_bus'
  4. import { EnumCmdEvent, CmdEvent } from '../../devices/cmd_key_event';
  5. import store from '../../utils/store';
  6. import { BtCmd } from '../../devices/bluetooth/bt_cmd';
  7. Page({
  8. data: {
  9. navbarData: {
  10. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  11. title: '猫王音箱 - WI-FI配置', //导航栏 中间的标题
  12. // callback: true,
  13. },
  14. wifiName: '',
  15. wifiPassword: '',
  16. _otaUrl: "",
  17. eyeIconSrc: '../../img/yj0.png',
  18. passwordType: true,
  19. _onNavigateBack: false,
  20. },
  21. callback() {
  22. console.log("返回");
  23. wx.navigateBack({
  24. delta: 1
  25. });
  26. },
  27. getConnectedWifi: function () {
  28. const that = this;
  29. let wifiDic = store.getStore("wifiInfo")
  30. let wifiName = wifiDic.wifiName
  31. let pwd = wifiDic.wifiPassword
  32. if (wifiName && pwd) {
  33. console.log("获取Wi-Fi信息", wifiName, pwd);
  34. that.setData({
  35. wifiName: wifiName,
  36. wifiPassword: pwd
  37. });
  38. return;
  39. }
  40. const res = wx.getSystemInfoSync(); // 获取系统信息
  41. /// android ios
  42. const platform = res.platform; // 获取平台类型
  43. console.log("平台类型", platform, getApp().globalData.scopeBluetooth, platform === 'ios' && !getApp().globalData.scopeBluetooth);
  44. if (platform === 'ios') {
  45. getApp().getBluetoothStatus()
  46. return
  47. }
  48. that.getWifiName()
  49. },
  50. getBluetoothStatusCallck(v) {
  51. if (v) {
  52. console.log("获取蓝牙权限成功");
  53. this.getWifiName()
  54. }
  55. },
  56. getWifiName() {
  57. var that = this;
  58. wx.startWifi({
  59. success(res) {
  60. console.log(res.errMsg)
  61. wx.getConnectedWifi({
  62. success: function (res) {
  63. const wifiName = res.wifi.SSID;
  64. that.setData({
  65. wifiName: wifiName
  66. });
  67. },
  68. fail: function (err) {
  69. console.error('获取Wi-Fi信息失败', err);
  70. // wx.showToast({
  71. // title: '获取Wi-Fi信息失败',
  72. // icon: 'none'
  73. // });
  74. }
  75. });
  76. // wx.getWifiList({
  77. // success: function (res) {
  78. // let wifiList = res.wifiList ?? [];
  79. // console.log('已连接的 Wi-Fi 列表:', res);
  80. // // 检查是否有已连接的 Wi-Fi
  81. // const connectedWifi = wifiList.find(wifi => wifi.SSID && wifi.connected);
  82. // if (connectedWifi) {
  83. // const wifiName = connectedWifi.SSID;
  84. // that.setData({
  85. // wifiName: wifiName
  86. // });
  87. // } else {
  88. // console.error('当前没有连接到 Wi-Fi');
  89. // wx.showToast({
  90. // title: '当前没有连接到 Wi-Fi',
  91. // icon: 'none'
  92. // });
  93. // }
  94. // },
  95. // fail: function (err) {
  96. // console.error('获取 Wi-Fi 列表失败', err);
  97. // wx.showToast({
  98. // title: '获取 Wi-Fi 列表失败',
  99. // icon: 'none'
  100. // });
  101. // }
  102. // });
  103. }
  104. })
  105. },
  106. onWifiNameInput: function (e) {
  107. this.setData({
  108. wifiName: e.detail.value
  109. });
  110. },
  111. onWifiPasswordInput: function (e) {
  112. this.setData({
  113. wifiPassword: e.detail.value
  114. });
  115. },
  116. togglePasswordVisibility: function () {
  117. const passwordType = !this.data.passwordType;
  118. // const newType = currentType === 'password' ? 'text' : 'password';
  119. const newIconSrc = passwordType ? '../../img/yj0.png' : '../../img/yj1.png';
  120. this.setData({
  121. passwordType: passwordType,
  122. eyeIconSrc: newIconSrc
  123. });
  124. },
  125. sendWiFiInfo(wifiName, pwd) {
  126. // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
  127. if (!wifiName || !pwd) {
  128. wx.showToast({
  129. title: '请输入正确的账号密码',
  130. })
  131. wx.hideLoading()
  132. return;
  133. }
  134. wx.showLoading({
  135. title: 'wifi连接中',
  136. })
  137. let result = BtCmd.sendWiFiInfo(wifiName, pwd)
  138. // _ble.send({ cmd: result });
  139. BtHelper.getInstance().otaSetWifi(result)
  140. },
  141. onConfirm: function () {
  142. const { wifiName, wifiPassword } = this.data;
  143. if (!wifiName) {
  144. wx.showToast({
  145. title: '请输入完整的Wi-Fi信息',
  146. icon: 'none'
  147. });
  148. return;
  149. }
  150. // 这里可以添加进一步的逻辑,比如连接Wi-Fi或保存信息
  151. this.sendWiFiInfo(wifiName, wifiPassword);
  152. },
  153. addNotification() {
  154. let _this = this;
  155. EventManager.addNotification(CmdEvent.eventName, function (event) {
  156. let name = event.cmdEvent;
  157. let otaCmd = event.otaCmd
  158. console.log("设置wifi0:", event)
  159. let kind = event.heiJiaoKind;
  160. console.log("设置wifi:", name, otaCmd, kind)
  161. // let toast = "设置wifi:" + name + " value," + otaCmd + "kind ," + kind
  162. // wx.showToast({
  163. // title: toast,
  164. // icon: 'none'
  165. // })
  166. if (name === EnumCmdEvent.otaWifi && otaCmd === 1) {
  167. store.setStore("wifiInfo", {
  168. wifiName: _this.data.wifiName,
  169. wifiPassword: _this.data.wifiPassword
  170. })
  171. }
  172. }, _this)
  173. },
  174. wifiPageSuccess() {
  175. this.data._onNavigateBack = true
  176. },
  177. // 获取保存的 Wi-Fi 账号和密码
  178. getSavedWiFiInfo() {
  179. const wifiName = wx.getStorageSync('wifiName') || '';
  180. const wifiPassword = wx.getStorageSync('wifiPassword');
  181. this.setData({
  182. wifiName: wifiName,
  183. wifiPassword: wifiPassword
  184. });
  185. },
  186. onLoad: function (options) {
  187. let param = options.param ?? "{}";
  188. let url = JSON.parse(param).url ?? "";
  189. this.data._otaUrl = url;
  190. this.getConnectedWifi();
  191. this.addNotification()
  192. },
  193. onUnload() {
  194. if (!this.data._onNavigateBack) {
  195. BtHelper.getInstance().otaCmd(2)
  196. }
  197. EventManager.removeNotification(CmdEvent.eventName)
  198. },
  199. });