随手精灵小程序,单独为了获取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.

278 linhas
8.2KB

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