setWifi.js 5.3 KB

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