随手精灵小程序,单独为了获取WIFI信息加强围栏告警功能能力
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

282 linhas
8.6KB

  1. import request from '../../utils/request';
  2. import { fenApi } from '../../api/core';
  3. import { getBaseUrl } from '../../utils/utilsServes';
  4. // pages/home/home.js
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. wlanList: [
  11. /* {SSID: '测试', BSSID: 'dc:fe:18:85:a5:7c', frequency: 5259} */
  12. ], //当前wlan列表
  13. isConnectWlan: null, //是否已经连接上WLAN
  14. connectWlanBssid: '', //当前连接WLAN的BSSID
  15. imei: '', //设备imei
  16. fenceId: '', //围栏id
  17. appType: '', //应用类型
  18. env: '', //运行环境
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. this.onStartWifi();
  25. this.checkOptions(options);
  26. this.getAuth();
  27. },
  28. onHide() {
  29. // 点击小程序右上角的关闭按钮时清空authToken
  30. wx.removeStorage({
  31. key: 'authToken',
  32. })
  33. },
  34. // 获取token
  35. getAuth() {
  36. // 获取token时有token清空token,防止获取token接口的时候带上token
  37. if(wx.getStorageSync('authToken')) {
  38. wx.removeStorage({
  39. key: 'authToken'
  40. })
  41. }
  42. const manufactorId = "2cae99d6-0475-42fe-9fa7-ca27e03077de";
  43. const baseUrl = getBaseUrl(this.data.env);
  44. const requestUrl = baseUrl + fenApi.getAuth;
  45. request.post(requestUrl, {
  46. manufactorId: manufactorId
  47. }).then(res => {
  48. if( res.code === 0 ) {
  49. // 先清除原来token,再把新token存在来
  50. if(wx.getStorageSync('authToken')) {
  51. wx.removeStorage('authToken');
  52. }
  53. wx.setStorage({
  54. key: "authToken",
  55. data: res.data,
  56. })
  57. };
  58. }).catch(error => {
  59. console.log("error", error);
  60. })
  61. },
  62. // 检查并获取从微信公众号传过来的参数
  63. checkOptions(options) {
  64. // todo 获取从微信公众号跳转的参数
  65. if(JSON.stringify(options) === '{}') {
  66. console.log("当前页面无携带参数");
  67. } else {
  68. console.log("存在参数", options);
  69. this.setData({
  70. imei: options.imei,
  71. fenceId: options.fenceId,
  72. appType: options.appType,
  73. env: options.env
  74. });
  75. };
  76. },
  77. /**
  78. * 生命周期函数--监听页面初次渲染完成
  79. */
  80. onReady: function () {
  81. },
  82. /**
  83. * 生命周期函数--监听页面显示
  84. */
  85. onShow: function () {
  86. },
  87. /**
  88. * 生命周期函数--监听页面卸载
  89. */
  90. onUnload: function () {
  91. },
  92. /**
  93. * 页面相关事件处理函数--监听用户下拉动作
  94. */
  95. onPullDownRefresh: function () {
  96. },
  97. /**
  98. * 页面上拉触底事件的处理函数
  99. */
  100. onReachBottom: function () {
  101. },
  102. /**
  103. * 用户点击右上角分享
  104. */
  105. onShareAppMessage: function () {
  106. },
  107. navigateTo() {
  108. wx.navigateTo({
  109. url: '/pages/lifecyle/lifecyle'
  110. })
  111. },
  112. switchTab() {
  113. wx.switchTab({
  114. url: "/pages/list/list",
  115. })
  116. },
  117. // 加载wifi模板
  118. onStartWifi() {
  119. let that = this;
  120. wx.startWifi({
  121. success() {
  122. that.getWifiList();
  123. },
  124. fail(res) {
  125. console.log("失败", res.errMsg);
  126. wx.showModal({
  127. title: '温馨提示',
  128. content: `当前用户未打开WIFI和GPS`,
  129. showCancel: false,
  130. })
  131. }
  132. });
  133. },
  134. // 获取当前链接的wifi信息
  135. getConnectedWifi: function () {
  136. let that = this;
  137. wx.getConnectedWifi({
  138. success(res) {
  139. if(res && (res.wifi.frequency / 1000) < 2.5) {
  140. // 如果已经连接上WLAN 并且是2.4G频段
  141. that.setData({
  142. isConnectWlan: true,
  143. connectWlanBssid: res.wifi.BSSID,
  144. })
  145. }
  146. },
  147. fail(res) {
  148. console.log(res.errMsg);
  149. that.setData({
  150. isConnectWlan: false
  151. })
  152. }
  153. })
  154. },
  155. // 刷新
  156. onRefresh() {
  157. this.getWifiList();
  158. this.getAuth();
  159. },
  160. // 搜索不到WLAN
  161. onHelp() {
  162. wx.showModal({
  163. title: '温馨提示',
  164. content: `请您前往手机-设置-WLAN,打开WLAN开关并且打开手机的GPS`,
  165. showCancel: false,
  166. })
  167. },
  168. // 保存WLAN信息
  169. onSave(value) {
  170. const currentWlanInfo = value.currentTarget.dataset.name;
  171. let that = this;
  172. wx.showModal({
  173. title: '温馨提示',
  174. content: `当前选中WLAN是:
  175. ${currentWlanInfo.SSID}
  176. `,
  177. cancelText: '关闭',
  178. confirmText: that.data.fenceId ? `选择` : `复制`,
  179. success: (res) => {
  180. if (res.confirm && that.data.fenceId) {
  181. wx.showToast({
  182. title: '设置中',
  183. icon: 'loading'
  184. })
  185. let reqBody = {
  186. imei: this.data.imei,
  187. fenceId: this.data.fenceId,
  188. wifiName: currentWlanInfo.SSID,
  189. wifiInfo: currentWlanInfo.BSSID
  190. };
  191. // 请求url分为:基础url + 接口Url
  192. const baseUrl = getBaseUrl(this.data.env);
  193. const requestUrl = baseUrl + fenApi.bindFenceWifi;
  194. request.post(requestUrl, {
  195. ...reqBody,
  196. }).then(() => {
  197. wx.showToast({
  198. title: '设置成功',
  199. icon: 'success'
  200. });
  201. // 2023.9.2 新增需求 保存成功后关闭小程序
  202. wx.exitMiniProgram({})
  203. }).catch(() => {
  204. wx.hideToast();
  205. wx.showModal({
  206. title: '温馨提示',
  207. content: '出错了,请您点击刷新按钮重新刷新页面后再重新设置',
  208. showCancel: false
  209. })
  210. })
  211. }
  212. }
  213. })
  214. },
  215. // 获取当前连接的周边的wifi列表
  216. getWifiList() {
  217. let that = this;
  218. // 先获取当前连接的wifi信息
  219. that.getConnectedWifi();
  220. wx.showLoading({
  221. title: '获取中WLAN...'
  222. });
  223. wx.getWifiList({
  224. success(res) {
  225. wx.hideLoading();
  226. wx.showToast({
  227. title: '获取成功',
  228. icon: 'success'
  229. });
  230. wx.onGetWifiList((result) => {
  231. // 筛选wifi名称为空的数据
  232. let wifiList = result.wifiList.filter(item => {
  233. return item.SSID !== '';
  234. }).filter(item => {
  235. // 筛选 2.4GHZ的wifi
  236. return item.frequency ? ( item.frequency / 1000 ) < 2.5 : item;
  237. });
  238. console.log("可用的wifi列表", wifiList);
  239. // ,如果有已连接的wifi则将已连接的wifi移动到第一位,否则不做任何操作
  240. if(that.data.connectWlanBssid !== '') {
  241. //let newWifiList = that.data.wlanList;
  242. // 找到与当前已连接wifi相同bssid数据的下标
  243. const index = wifiList.findIndex(v => v.BSSID === that.data.connectWlanBssid);
  244. //根据该对象在数组的下标从数组中移出
  245. const moveObj = wifiList.splice(index, 1);
  246. //把当前数据插入到数据首位
  247. wifiList.splice(0, 0, ...moveObj);
  248. that.setData({
  249. wlanList: wifiList
  250. })
  251. } else {
  252. that.setData({
  253. wlanList: wifiList
  254. })
  255. }
  256. });
  257. },
  258. fail(res) {
  259. wx.hideLoading();
  260. wx.showModal({
  261. title: '温馨提示',
  262. content: '当前用户未打开WIFI和GPS',
  263. showCancel: false,
  264. })
  265. },
  266. });
  267. }
  268. })