import request from '../../utils/request'; import { fenApi } from '../../api/core'; import { getBaseUrl } from '../../utils/utilsServes'; // pages/home/home.js Page({ /** * 页面的初始数据 */ data: { wlanList: [ /* {SSID: '测试', BSSID: 'dc:fe:18:85:a5:7c', frequency: 5259} */ ], //当前wlan列表 isConnectWlan: null, //是否已经连接上WLAN connectWlanBssid: '', //当前连接WLAN的BSSID imei: '', //设备imei fenceId: '', //围栏id appType: '', //应用类型 env: '', //运行环境 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.onStartWifi(); this.checkOptions(options); this.getAuth(); }, onHide() { // 点击小程序右上角的关闭按钮时清空authToken wx.removeStorage({ key: 'authToken', }) }, // 获取token getAuth() { // 获取token时有token清空token,防止获取token接口的时候带上token if(wx.getStorageSync('authToken')) { wx.removeStorage({ key: 'authToken' }) } const manufactorId = "2cae99d6-0475-42fe-9fa7-ca27e03077de"; const baseUrl = getBaseUrl(this.data.env); const requestUrl = baseUrl + fenApi.getAuth; request.post(requestUrl, { manufactorId: manufactorId }).then(res => { if( res.code === 0 ) { // 先清除原来token,再把新token存在来 if(wx.getStorageSync('authToken')) { wx.removeStorage('authToken'); } wx.setStorage({ key: "authToken", data: res.data, }) }; }).catch(error => { console.log("error", error); }) }, // 检查并获取从微信公众号传过来的参数 checkOptions(options) { // todo 获取从微信公众号跳转的参数 if(JSON.stringify(options) === '{}') { console.log("当前页面无携带参数"); } else { console.log("存在参数", options); this.setData({ imei: options.imei, fenceId: options.fenceId, appType: options.appType, env: options.env }); }; }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, navigateTo() { wx.navigateTo({ url: '/pages/lifecyle/lifecyle' }) }, switchTab() { wx.switchTab({ url: "/pages/list/list", }) }, // 加载wifi模板 onStartWifi() { let that = this; wx.startWifi({ success() { that.getWifiList(); }, fail(res) { console.log("失败", res.errMsg); wx.showModal({ title: '温馨提示', content: `当前用户未打开WIFI和GPS`, showCancel: false, }) } }); }, // 获取当前链接的wifi信息 getConnectedWifi: function () { let that = this; wx.getConnectedWifi({ success(res) { if(res && (res.wifi.frequency / 1000) < 2.5) { // 如果已经连接上WLAN 并且是2.4G频段 that.setData({ isConnectWlan: true, connectWlanBssid: res.wifi.BSSID, }) } }, fail(res) { console.log(res.errMsg); that.setData({ isConnectWlan: false }) } }) }, // 刷新 onRefresh() { this.getWifiList(); this.getAuth(); }, // 搜索不到WLAN onHelp() { wx.showModal({ title: '温馨提示', content: `请您前往手机-设置-WLAN,打开WLAN开关并且打开手机的GPS`, showCancel: false, }) }, // 保存WLAN信息 onSave(value) { const currentWlanInfo = value.currentTarget.dataset.name; let that = this; wx.showModal({ title: '温馨提示', content: `当前选中WLAN是: ${currentWlanInfo.SSID} `, cancelText: '关闭', confirmText: that.data.fenceId ? `选择` : `复制`, success: (res) => { if (res.confirm && that.data.fenceId) { wx.showToast({ title: '设置中', icon: 'loading' }) let reqBody = { imei: this.data.imei, fenceId: this.data.fenceId, wifiName: currentWlanInfo.SSID, wifiInfo: currentWlanInfo.BSSID }; // 请求url分为:基础url + 接口Url const baseUrl = getBaseUrl(this.data.env); const requestUrl = baseUrl + fenApi.bindFenceWifi; request.post(requestUrl, { ...reqBody, }).then(() => { wx.showToast({ title: '设置成功', icon: 'success' }); // 2023.9.2 新增需求 保存成功后关闭小程序 wx.exitMiniProgram({}) }).catch(() => { wx.hideToast(); wx.showModal({ title: '温馨提示', content: '出错了,请您点击刷新按钮重新刷新页面后再重新设置', showCancel: false }) }) } } }) }, // 获取当前连接的周边的wifi列表 getWifiList() { let that = this; // 先获取当前连接的wifi信息 that.getConnectedWifi(); wx.showLoading({ title: '获取中WLAN...' }); wx.getWifiList({ success(res) { wx.hideLoading(); wx.showToast({ title: '获取成功', icon: 'success' }); wx.onGetWifiList((result) => { // 筛选wifi名称为空的数据 let wifiList = result.wifiList.filter(item => { return item.SSID !== ''; }).filter(item => { // 筛选 2.4GHZ的wifi return item.frequency ? ( item.frequency / 1000 ) < 2.5 : item; }); console.log("可用的wifi列表", wifiList); // ,如果有已连接的wifi则将已连接的wifi移动到第一位,否则不做任何操作 if(that.data.connectWlanBssid !== '') { //let newWifiList = that.data.wlanList; // 找到与当前已连接wifi相同bssid数据的下标 const index = wifiList.findIndex(v => v.BSSID === that.data.connectWlanBssid); //根据该对象在数组的下标从数组中移出 const moveObj = wifiList.splice(index, 1); //把当前数据插入到数据首位 wifiList.splice(0, 0, ...moveObj); that.setData({ wlanList: wifiList }) } else { that.setData({ wlanList: wifiList }) } }); }, fail(res) { wx.hideLoading(); wx.showModal({ title: '温馨提示', content: '当前用户未打开WIFI和GPS', showCancel: false, }) }, }); } })