随手精灵小程序,单独为了获取WIFI信息加强围栏告警功能能力
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

home.js 9.1KB

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