随手精灵小程序,单独为了获取WIFI信息加强围栏告警功能能力
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
5.7KB

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