setWifi.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. if (wifiName) {
  34. console.log("获取Wi-Fi信息", wifiName, pwd);
  35. that.setData({
  36. wifiName: wifiName,
  37. wifiPassword: pwd
  38. });
  39. return;
  40. }
  41. const res = wx.getSystemInfoSync(); // 获取系统信息
  42. /// android ios
  43. const platform = res.platform; // 获取平台类型
  44. console.log("平台类型", platform, getApp().globalData.scopeBluetooth, platform === 'ios' && !getApp().globalData.scopeBluetooth);
  45. if (platform === 'ios') {
  46. getApp().getBluetoothStatus()
  47. return
  48. }
  49. that.getWifiName()
  50. },
  51. getBluetoothStatusCallck(v) {
  52. if (v) {
  53. console.log("获取蓝牙权限成功");
  54. this.getWifiName()
  55. }
  56. },
  57. getWifiName() {
  58. var that = this;
  59. wx.getNetworkType({
  60. success(res) {
  61. wx.startWifi({
  62. success(res) {
  63. console.log(res.errMsg)
  64. wx.getConnectedWifi({
  65. success: function (res) {
  66. const wifiName = res.wifi.SSID;
  67. that.setData({
  68. wifiName: wifiName
  69. });
  70. },
  71. fail: function (err) {
  72. console.error('获取Wi-Fi信息失败', err);
  73. }
  74. });
  75. }
  76. })
  77. }
  78. })
  79. },
  80. onWifiNameInput: function (e) {
  81. this.setData({
  82. wifiName: e.detail.value
  83. });
  84. },
  85. onWifiPasswordInput: function (e) {
  86. this.setData({
  87. wifiPassword: e.detail.value
  88. });
  89. },
  90. onFocus(event) {
  91. console.log("onFocus:", event)
  92. if (event.detail.value !== this.data.pwdData) {
  93. this.setData({
  94. wifiPassword: event.detail.value
  95. });
  96. }
  97. },
  98. onBlur(event) {
  99. console.log("onBlur", event)
  100. if (event.detail.value !== this.data.pwdData) {
  101. this.setData({
  102. wifiPassword: event.detail.value
  103. });
  104. }
  105. },
  106. togglePasswordVisibility: function () {
  107. const passwordType = !this.data.passwordType;
  108. // const newType = currentType === 'password' ? 'text' : 'password';
  109. const newIconSrc = passwordType ? '../../img/yj0.png' : '../../img/yj1.png';
  110. this.setData({
  111. passwordType: passwordType,
  112. eyeIconSrc: newIconSrc
  113. });
  114. },
  115. sendWiFiInfo(wifiName, pwd) {
  116. // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
  117. if (!wifiName) {
  118. // if (!wifiName || pwd) {
  119. wx.showToast({
  120. title: '请输入正确的账号',
  121. })
  122. wx.hideLoading()
  123. return;
  124. }
  125. wx.showLoading({
  126. title: 'wifi连接中',
  127. })
  128. let result = BtCmd.sendWiFiInfo(wifiName, pwd)
  129. // _ble.send({ cmd: result });
  130. BtHelper.getInstance().otaSetWifi(result)
  131. },
  132. onConfirm: function () {
  133. const { wifiName, wifiPassword } = this.data;
  134. if (!wifiName) {
  135. wx.showToast({
  136. title: '请输入完整的Wi-Fi信息',
  137. icon: 'none'
  138. });
  139. return;
  140. }
  141. // 这里可以添加进一步的逻辑,比如连接Wi-Fi或保存信息
  142. this.sendWiFiInfo(wifiName, wifiPassword);
  143. },
  144. addNotification() {
  145. let _this = this;
  146. EventManager.addNotification(CmdEvent.eventName, function (event) {
  147. let name = event.cmdEvent;
  148. let otaCmd = event.otaCmd
  149. console.log("设置wifi0:", event)
  150. let kind = event.heiJiaoKind;
  151. console.log("设置wifi:", name, otaCmd, kind)
  152. // let toast = "设置wifi:" + name + " value," + otaCmd + "kind ," + kind
  153. // wx.showToast({
  154. // title: toast,
  155. // icon: 'none'
  156. // })
  157. if (name === EnumCmdEvent.otaWifi && otaCmd === 1) {
  158. store.setStore("wifiInfo", {
  159. wifiName: _this.data.wifiName,
  160. wifiPassword: _this.data.wifiPassword
  161. })
  162. }
  163. }, _this)
  164. },
  165. wifiPageSuccess() {
  166. this.data._onNavigateBack = true
  167. },
  168. // 获取保存的 Wi-Fi 账号和密码
  169. getSavedWiFiInfo() {
  170. const wifiName = wx.getStorageSync('wifiName') || '';
  171. const wifiPassword = wx.getStorageSync('wifiPassword');
  172. this.setData({
  173. wifiName: wifiName,
  174. wifiPassword: wifiPassword
  175. });
  176. },
  177. onLoad: function (options) {
  178. let param = options.param ?? "{}";
  179. let url = JSON.parse(param).url ?? "";
  180. this.data._otaUrl = url;
  181. this.getConnectedWifi();
  182. this.addNotification()
  183. },
  184. onUnload() {
  185. if (!this.data._onNavigateBack) {
  186. BtHelper.getInstance().otaCmd(2)
  187. }
  188. EventManager.removeNotification(CmdEvent.eventName)
  189. },
  190. });