setWifi.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. const {
  2. BtHelper
  3. } = require("../../../devices/bt_helper");
  4. import eventBus from '../../../utils/eventBus'
  5. import {
  6. EnumCmdEvent,
  7. CmdEvent
  8. } from '../../../devices/cmd_key_event';
  9. import store from '../../../utils/store';
  10. import {
  11. BtCmd
  12. } from '../../../devices/bluetooth/bt_cmd';
  13. Page({
  14. data: {
  15. navbarData: {
  16. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  17. title: '猫王音箱 - WI-FI配置', //导航栏 中间的标题
  18. // callback: true,
  19. },
  20. wifiName: '',
  21. wifiPassword: '',
  22. _otaUrl: "",
  23. eyeIconSrc: '../../../img/yj0.png',
  24. passwordType: true,
  25. _onNavigateBack: false,
  26. },
  27. callback() {
  28. console.log("返回");
  29. wx.navigateBack({
  30. delta: 1
  31. });
  32. },
  33. getConnectedWifi: function () {
  34. const that = this;
  35. let wifiDic = store.getStore("wifiInfo")
  36. let wifiName = wifiDic.wifiName
  37. let pwd = wifiDic.wifiPassword
  38. // if (wifiName && pwd) {
  39. if (wifiName) {
  40. console.log("获取Wi-Fi信息", wifiName, pwd);
  41. that.setData({
  42. wifiName: wifiName,
  43. wifiPassword: pwd
  44. });
  45. return;
  46. }
  47. const res = wx.getSystemInfoSync(); // 获取系统信息
  48. /// android ios
  49. const platform = res.platform; // 获取平台类型
  50. console.log("平台类型", platform, getApp().globalData.scopeBluetooth, platform === 'ios' && !getApp().globalData.scopeBluetooth);
  51. if (platform === 'ios') {
  52. getApp().getBluetoothStatus()
  53. return
  54. }
  55. that.getWifiName()
  56. },
  57. getBluetoothStatusCallck(v) {
  58. if (v) {
  59. console.log("获取蓝牙权限成功");
  60. this.getWifiName()
  61. }
  62. },
  63. getWifiName() {
  64. var that = this;
  65. wx.getNetworkType({
  66. success(res) {
  67. wx.startWifi({
  68. success(res) {
  69. console.log(res.errMsg)
  70. wx.getConnectedWifi({
  71. success: function (res) {
  72. const wifiName = res.wifi.SSID;
  73. that.setData({
  74. wifiName: wifiName
  75. });
  76. },
  77. fail: function (err) {
  78. console.error('获取Wi-Fi信息失败', err);
  79. }
  80. });
  81. }
  82. })
  83. }
  84. })
  85. },
  86. onWifiNameInput: function (e) {
  87. this.setData({
  88. wifiName: e.detail.value
  89. });
  90. },
  91. onWifiPasswordInput: function (e) {
  92. this.setData({
  93. wifiPassword: e.detail.value
  94. });
  95. },
  96. onFocus(event) {
  97. console.log("onFocus:", event)
  98. if (event.detail.value !== this.data.pwdData) {
  99. this.setData({
  100. wifiPassword: event.detail.value
  101. });
  102. }
  103. },
  104. onBlur(event) {
  105. console.log("onBlur", event)
  106. if (event.detail.value !== this.data.pwdData) {
  107. this.setData({
  108. wifiPassword: event.detail.value
  109. });
  110. }
  111. },
  112. togglePasswordVisibility: function () {
  113. const passwordType = !this.data.passwordType;
  114. // const newType = currentType === 'password' ? 'text' : 'password';
  115. const newIconSrc = passwordType ? '../../../img/yj0.png' : '../../../img/yj1.png';
  116. this.setData({
  117. passwordType: passwordType,
  118. eyeIconSrc: newIconSrc
  119. });
  120. },
  121. sendWiFiInfo(wifiName, pwd) {
  122. // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
  123. if (!wifiName) {
  124. // if (!wifiName || pwd) {
  125. wx.showToast({
  126. title: '请输入正确的账号',
  127. })
  128. wx.hideLoading()
  129. return;
  130. }
  131. wx.showLoading({
  132. title: 'wifi连接中',
  133. })
  134. let result = BtCmd.sendWiFiInfo(wifiName, pwd)
  135. // _ble.send({ cmd: result });
  136. BtHelper.getInstance().otaSetWifi(result)
  137. },
  138. onConfirm: function () {
  139. const {
  140. wifiName,
  141. wifiPassword
  142. } = 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. eventBus.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. eventBus.removeNotification(CmdEvent.eventName)
  198. },
  199. });