setWifi.js 4.4 KB

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