随手精灵小程序,单独为了获取WIFI信息加强围栏告警功能能力
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

211 lines
5.8KB

  1. // pages/home/home.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. wlanList: [
  8. ], //当前wlan列表
  9. isConnectWlan: null, //是否已经连接上WLAN
  10. connectWlanBssid: '', //当前连接WLAN的SSID
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad: function (options) {
  16. this.onStartWifi();
  17. // todo 获取从微信公众号跳转的参数
  18. },
  19. /**
  20. * 生命周期函数--监听页面初次渲染完成
  21. */
  22. onReady: function () {
  23. },
  24. /**
  25. * 生命周期函数--监听页面显示
  26. */
  27. onShow: function () {
  28. },
  29. /**
  30. * 生命周期函数--监听页面隐藏
  31. */
  32. onHide: function () {
  33. },
  34. /**
  35. * 生命周期函数--监听页面卸载
  36. */
  37. onUnload: function () {
  38. },
  39. /**
  40. * 页面相关事件处理函数--监听用户下拉动作
  41. */
  42. onPullDownRefresh: function () {
  43. },
  44. /**
  45. * 页面上拉触底事件的处理函数
  46. */
  47. onReachBottom: function () {
  48. },
  49. /**
  50. * 用户点击右上角分享
  51. */
  52. onShareAppMessage: function () {
  53. },
  54. navigateTo() {
  55. wx.navigateTo({
  56. url: '/pages/lifecyle/lifecyle'
  57. })
  58. },
  59. switchTab() {
  60. wx.switchTab({
  61. url: "/pages/list/list",
  62. })
  63. },
  64. // 加载wifi模板
  65. onStartWifi() {
  66. let that = this;
  67. wx.startWifi({
  68. success(res) {
  69. console.log("成功", res.errMsg);
  70. that.getWifiList();
  71. },
  72. fail(res) {
  73. console.log("失败", res.errMsg);
  74. wx.showModal({
  75. title: '温馨提示',
  76. content: `当前用户未打开WIFI和GPS`,
  77. showCancel: false,
  78. success(res) {
  79. if (res.confirm) {
  80. console.log('用户点击确定');
  81. } else if (res.cancel) {
  82. console.log('用户点击取消');
  83. }
  84. }
  85. })
  86. }
  87. });
  88. },
  89. // 获取当前链接的wifi信息
  90. getConnectedWifi: function () {
  91. let that = this;
  92. wx.getConnectedWifi({
  93. success(res) {
  94. console.log(res);
  95. if(res) {
  96. // 如果已经连接上WLAN
  97. that.setData({
  98. isConnectWlan: true,
  99. connectWlanBssid: res.wifi.BSSID,
  100. })
  101. }
  102. },
  103. fail(res) {
  104. console.log(res.errMsg);
  105. that.setData({
  106. isConnectWlan: false
  107. })
  108. }
  109. })
  110. },
  111. // 刷新
  112. onRefresh() {
  113. this.getWifiList();
  114. },
  115. // 搜索不到WLAN
  116. onHelp() {
  117. wx.showModal({
  118. title: '温馨提示',
  119. content: `请您前往手机-设置-WLAN,打开WLAN开关并且打开手机的GPS`,
  120. showCancel: false,
  121. })
  122. },
  123. // 保存WLAN信息
  124. onSave(value) {
  125. const currentWlanInfo = value.currentTarget.dataset.name;
  126. console.log("当前点击的WLAN信息", currentWlanInfo);
  127. wx.showModal({
  128. title: '温馨提示',
  129. content: `当前选中WLANBSSID是:
  130. ${currentWlanInfo.BSSID}
  131. `,
  132. cancelText: '关闭',
  133. confirmText: '复制',
  134. success: (res) => {
  135. if (res.confirm) {
  136. wx.setClipboardData({
  137. data: `${currentWlanInfo.BSSID}`,
  138. })
  139. }
  140. }
  141. })
  142. // todo 调取接口
  143. },
  144. // 获取当前连接的周边的wifi列表
  145. getWifiList() {
  146. let that = this;
  147. // 先获取当前连接的wifi信息
  148. that.getConnectedWifi();
  149. wx.showLoading({
  150. title: '获取中WLAN...'
  151. });
  152. wx.getWifiList({
  153. success(res) {
  154. console.log("getWifiList", res);
  155. wx.hideLoading();
  156. wx.showToast({
  157. title: '获取成功',
  158. icon: 'success'
  159. });
  160. wx.onGetWifiList((result) => {
  161. // 筛选wifi名称为空的数据
  162. let wifiList = result.wifiList.filter(item => {
  163. return item.SSID !== '';
  164. });
  165. // ,如果有已连接的wifi则将已连接的wifi移动到第一位,否则不做任何操作
  166. if(that.data.connectWlanBssid !== '') {
  167. //let newWifiList = that.data.wlanList;
  168. // 找到与当前已连接wifi相同bssid数据的下标
  169. const index = wifiList.findIndex(v => v.BSSID === that.data.connectWlanBssid);
  170. //根据该对象在数组的下标从数组中移出
  171. const moveObj = wifiList.splice(index, 1);
  172. //把当前数据插入到数据首位
  173. wifiList.splice(0, 0, ...moveObj);
  174. that.setData({
  175. wlanList: wifiList
  176. })
  177. } else {
  178. that.setData({
  179. wlanList: wifiList
  180. })
  181. }
  182. });
  183. },
  184. fail(res) {
  185. console.log(res.errMsg);
  186. wx.hideLoading();
  187. wx.showModal({
  188. title: '温馨提示',
  189. content: '当前用户未打开WIFI和GPS',
  190. showCancel: false,
  191. })
  192. },
  193. });
  194. }
  195. })