setWifi.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.getNetworkType({
  59. success(res) {
  60. wx.startWifi({
  61. success(res) {
  62. console.log(res.errMsg)
  63. wx.getConnectedWifi({
  64. success: function (res) {
  65. const wifiName = res.wifi.SSID;
  66. that.setData({
  67. wifiName: wifiName
  68. });
  69. },
  70. fail: function (err) {
  71. console.error('获取Wi-Fi信息失败', err);
  72. }
  73. });
  74. }
  75. })
  76. }
  77. })
  78. },
  79. onWifiNameInput: function (e) {
  80. this.setData({
  81. wifiName: e.detail.value
  82. });
  83. },
  84. onWifiPasswordInput: function (e) {
  85. this.setData({
  86. wifiPassword: e.detail.value
  87. });
  88. },
  89. togglePasswordVisibility: function () {
  90. const passwordType = !this.data.passwordType;
  91. // const newType = currentType === 'password' ? 'text' : 'password';
  92. const newIconSrc = passwordType ? '../../img/yj0.png' : '../../img/yj1.png';
  93. this.setData({
  94. passwordType: passwordType,
  95. eyeIconSrc: newIconSrc
  96. });
  97. },
  98. sendWiFiInfo(wifiName, pwd) {
  99. // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
  100. if (!wifiName || !pwd) {
  101. wx.showToast({
  102. title: '请输入正确的账号密码',
  103. })
  104. wx.hideLoading()
  105. return;
  106. }
  107. wx.showLoading({
  108. title: 'wifi连接中',
  109. })
  110. let result = BtCmd.sendWiFiInfo(wifiName, pwd)
  111. // _ble.send({ cmd: result });
  112. BtHelper.getInstance().otaSetWifi(result)
  113. },
  114. onConfirm: function () {
  115. const { wifiName, wifiPassword } = this.data;
  116. if (!wifiName) {
  117. wx.showToast({
  118. title: '请输入完整的Wi-Fi信息',
  119. icon: 'none'
  120. });
  121. return;
  122. }
  123. // 这里可以添加进一步的逻辑,比如连接Wi-Fi或保存信息
  124. this.sendWiFiInfo(wifiName, wifiPassword);
  125. },
  126. addNotification() {
  127. let _this = this;
  128. EventManager.addNotification(CmdEvent.eventName, function (event) {
  129. let name = event.cmdEvent;
  130. let otaCmd = event.otaCmd
  131. console.log("设置wifi0:", event)
  132. let kind = event.heiJiaoKind;
  133. console.log("设置wifi:", name, otaCmd, kind)
  134. // let toast = "设置wifi:" + name + " value," + otaCmd + "kind ," + kind
  135. // wx.showToast({
  136. // title: toast,
  137. // icon: 'none'
  138. // })
  139. if (name === EnumCmdEvent.otaWifi && otaCmd === 1) {
  140. store.setStore("wifiInfo", {
  141. wifiName: _this.data.wifiName,
  142. wifiPassword: _this.data.wifiPassword
  143. })
  144. }
  145. }, _this)
  146. },
  147. wifiPageSuccess() {
  148. this.data._onNavigateBack = true
  149. },
  150. // 获取保存的 Wi-Fi 账号和密码
  151. getSavedWiFiInfo() {
  152. const wifiName = wx.getStorageSync('wifiName') || '';
  153. const wifiPassword = wx.getStorageSync('wifiPassword');
  154. this.setData({
  155. wifiName: wifiName,
  156. wifiPassword: wifiPassword
  157. });
  158. },
  159. onLoad: function (options) {
  160. let param = options.param ?? "{}";
  161. let url = JSON.parse(param).url ?? "";
  162. this.data._otaUrl = url;
  163. this.getConnectedWifi();
  164. this.addNotification()
  165. },
  166. onUnload() {
  167. if (!this.data._onNavigateBack) {
  168. BtHelper.getInstance().otaCmd(2)
  169. }
  170. EventManager.removeNotification(CmdEvent.eventName)
  171. },
  172. });