setWifi.js 5.3 KB

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