deviceConnect1.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const app = getApp();
  2. import routeUtil from '../../utils/routeUtil.js';
  3. import routePath from '../../utils/routePath.js'
  4. Page({
  5. data: {
  6. nvabarData: {
  7. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  8. title: '设置网络', //导航栏 中间的标题
  9. },
  10. scopeBluetooth: app.globalData.scopeBluetooth,
  11. isShowPwd: false,
  12. is5GWifi: false,
  13. ssid: "",
  14. pwdData: "",
  15. connectDevice: "",
  16. },
  17. onLoad(options) {
  18. var that = this;
  19. var param = options.param;
  20. that.data.connectDevice = param;
  21. },
  22. onShow() {
  23. var that = this;
  24. that.getWifiStatus();
  25. },
  26. getBluetoothStatus() {
  27. app.getBluetoothStatus();
  28. },
  29. ///Wifi名称
  30. setSsid: function (e) {
  31. var that = this;
  32. let str = false;
  33. if (e.detail.value.indexOf("5G") !== -1) {
  34. str = true;
  35. };
  36. that.setData({
  37. ssid: e.detail.value,
  38. is5GWifi: str
  39. });
  40. },
  41. onFocus(event) {
  42. console.log("onFocus:", event)
  43. if (event.detail.value !== this.data.pwdData) {
  44. this.setPwd(event);
  45. }
  46. },
  47. onBlur(event) {
  48. console.log("onBlur", event)
  49. if (event.detail.value !== this.data.pwdData) {
  50. this.setPwd(event);
  51. }
  52. },
  53. ///Wifi名称
  54. setPwd: function (e) {
  55. console.log("设置密码:", e.detail.value)
  56. var that = this;
  57. app.globalData.pwdData = e.detail.value;
  58. that.setData({
  59. pwdData: e.detail.value,
  60. });
  61. },
  62. ///获取网络状态
  63. getWifiStatus() {
  64. var that = this;
  65. wx.getNetworkType({
  66. success(res) {
  67. if (res.networkType === "wifi") {
  68. wx.startWifi({
  69. success(res) {
  70. //获取当前已连接wifi名
  71. wx.getConnectedWifi({
  72. success: function (res) {
  73. var ssid = res.wifi.SSID;
  74. // 5Gwifi
  75. if (res.wifi.SSID.indexOf("5G") !== -1) {
  76. that.setData({
  77. is5GWifi: true,
  78. ssid: ssid,
  79. });
  80. } else {
  81. that.setData({
  82. is5GWifi: false,
  83. ssid: ssid,
  84. });
  85. };
  86. app.globalData.ssid = ssid;
  87. wx.getStorage({
  88. key: 'wifiInfo',
  89. success(wifiRes) {
  90. console.log("wifiInfo:", wifiRes)
  91. const resData = JSON.parse(wifiRes.data ?? "{}")?? {};
  92. console.log("wifiInfo2:", resData)
  93. if (resData.ssid === res.wifi.SSID) {
  94. that.setData({
  95. pwdData: resData.password ?? "",
  96. });
  97. }
  98. }
  99. })
  100. },
  101. })
  102. }
  103. })
  104. }
  105. }
  106. })
  107. },
  108. ///下一步
  109. next() {
  110. var that = this;
  111. if (that.data.ssid === "") {
  112. wx.showToast({
  113. title: '请输入WIFI名称',
  114. icon: 'none',
  115. duration: 2000
  116. })
  117. return;
  118. };
  119. // if (that.data.pwdData === "") {
  120. // wx.showToast({
  121. // title: '请输入WIFI密码',
  122. // icon: 'none',
  123. // duration: 2000
  124. // })
  125. // return;
  126. // };
  127. app.globalData.ssid = that.data.ssid;
  128. app.globalData.pwdData = that.data.pwdData;
  129. routeUtil.jumpParam(routePath.deviceConnect2, that.data.connectDevice);
  130. },
  131. notRoter() {
  132. wx.navigateBack({
  133. delta: 1
  134. });
  135. },
  136. showPwd() {
  137. var that = this;
  138. that.setData({
  139. isShowPwd: !that.data.isShowPwd
  140. })
  141. },
  142. })