|
|
@@ -1,3 +1,6 @@ |
|
|
|
import request from '../../utils/request'; |
|
|
|
import { fenApi } from '../../api/core'; |
|
|
|
import { getBaseUrl } from '../../utils/utilsServes'; |
|
|
|
// pages/home/home.js |
|
|
|
Page({ |
|
|
|
|
|
|
@@ -5,10 +8,13 @@ Page({ |
|
|
|
* 页面的初始数据 |
|
|
|
*/ |
|
|
|
data: { |
|
|
|
wlanList: [ |
|
|
|
], //当前wlan列表 |
|
|
|
wlanList: [], //当前wlan列表 |
|
|
|
isConnectWlan: null, //是否已经连接上WLAN |
|
|
|
connectWlanBssid: '', //当前连接WLAN的SSID |
|
|
|
connectWlanBssid: '', //当前连接WLAN的BSSID |
|
|
|
imei: '', //设备imei |
|
|
|
fenceId: '', //围栏id |
|
|
|
appType: '', //应用类型 |
|
|
|
env: '', //运行环境 |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
@@ -16,14 +22,64 @@ Page({ |
|
|
|
*/ |
|
|
|
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 () { |
|
|
|
|
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
@@ -33,13 +89,6 @@ Page({ |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* 生命周期函数--监听页面隐藏 |
|
|
|
*/ |
|
|
|
onHide: function () { |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* 生命周期函数--监听页面卸载 |
|
|
|
*/ |
|
|
@@ -81,8 +130,7 @@ Page({ |
|
|
|
onStartWifi() { |
|
|
|
let that = this; |
|
|
|
wx.startWifi({ |
|
|
|
success(res) { |
|
|
|
console.log("成功", res.errMsg); |
|
|
|
success() { |
|
|
|
that.getWifiList(); |
|
|
|
}, |
|
|
|
fail(res) { |
|
|
@@ -91,13 +139,6 @@ Page({ |
|
|
|
title: '温馨提示', |
|
|
|
content: `当前用户未打开WIFI和GPS`, |
|
|
|
showCancel: false, |
|
|
|
success(res) { |
|
|
|
if (res.confirm) { |
|
|
|
console.log('用户点击确定'); |
|
|
|
} else if (res.cancel) { |
|
|
|
console.log('用户点击取消'); |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
}); |
|
|
@@ -107,7 +148,6 @@ Page({ |
|
|
|
let that = this; |
|
|
|
wx.getConnectedWifi({ |
|
|
|
success(res) { |
|
|
|
console.log(res); |
|
|
|
if(res) { |
|
|
|
// 如果已经连接上WLAN |
|
|
|
that.setData({ |
|
|
@@ -127,6 +167,8 @@ Page({ |
|
|
|
// 刷新 |
|
|
|
onRefresh() { |
|
|
|
this.getWifiList(); |
|
|
|
this.getAuth(); |
|
|
|
|
|
|
|
}, |
|
|
|
// 搜索不到WLAN |
|
|
|
onHelp() { |
|
|
@@ -139,23 +181,50 @@ Page({ |
|
|
|
// 保存WLAN信息 |
|
|
|
onSave(value) { |
|
|
|
const currentWlanInfo = value.currentTarget.dataset.name; |
|
|
|
console.log("当前点击的WLAN信息", currentWlanInfo); |
|
|
|
let that = this; |
|
|
|
wx.showModal({ |
|
|
|
title: '温馨提示', |
|
|
|
content: `当前选中WLANBSSID是: |
|
|
|
${currentWlanInfo.BSSID} |
|
|
|
content: `当前选中WLAN是: |
|
|
|
${currentWlanInfo.SSID} |
|
|
|
`, |
|
|
|
cancelText: '关闭', |
|
|
|
confirmText: '复制', |
|
|
|
confirmText: that.data.fenceId ? `选择` : `复制`, |
|
|
|
success: (res) => { |
|
|
|
if (res.confirm) { |
|
|
|
if (res.confirm && that.data.fenceId) { |
|
|
|
wx.showToast({ |
|
|
|
title: '设置中', |
|
|
|
icon: 'loading' |
|
|
|
}) |
|
|
|
let reqBody = { |
|
|
|
imei: this.data.imei, |
|
|
|
fenceId: this.data.fenceId, |
|
|
|
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' |
|
|
|
}) |
|
|
|
}).catch(() => { |
|
|
|
wx.hideToast(); |
|
|
|
wx.showModal({ |
|
|
|
title: '温馨提示', |
|
|
|
content: '出错了,请您点击刷新按钮重新刷新页面后再重新设置', |
|
|
|
showCancel: false |
|
|
|
}) |
|
|
|
}) |
|
|
|
} else { |
|
|
|
wx.setClipboardData({ |
|
|
|
data: `${currentWlanInfo.BSSID}`, |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
// todo 调取接口 |
|
|
|
}, |
|
|
|
// 获取当前连接的周边的wifi列表 |
|
|
|
getWifiList() { |
|
|
@@ -167,7 +236,6 @@ Page({ |
|
|
|
}); |
|
|
|
wx.getWifiList({ |
|
|
|
success(res) { |
|
|
|
console.log("getWifiList", res); |
|
|
|
wx.hideLoading(); |
|
|
|
wx.showToast({ |
|
|
|
title: '获取成功', |
|
|
@@ -198,7 +266,6 @@ Page({ |
|
|
|
}); |
|
|
|
}, |
|
|
|
fail(res) { |
|
|
|
console.log(res.errMsg); |
|
|
|
wx.hideLoading(); |
|
|
|
wx.showModal({ |
|
|
|
title: '温馨提示', |
|
|
|