|
- // pages/home/home.js
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- wlanList: [
- ], //当前wlan列表
- isConnectWlan: null, //是否已经连接上WLAN
- connectWlanBssid: '', //当前连接WLAN的SSID
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- this.onStartWifi();
- // todo 获取从微信公众号跳转的参数
- },
-
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
-
- },
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
-
- },
-
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: 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(res) {
- console.log("成功", res.errMsg);
- that.getWifiList();
- },
- fail(res) {
- console.log("失败", res.errMsg);
- wx.showModal({
- title: '温馨提示',
- content: `当前用户未打开WIFI和GPS`,
- showCancel: false,
- success(res) {
- if (res.confirm) {
- console.log('用户点击确定');
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- })
- }
- });
- },
- // 获取当前链接的wifi信息
- getConnectedWifi: function () {
- let that = this;
- wx.getConnectedWifi({
- success(res) {
- console.log(res);
- if(res) {
- // 如果已经连接上WLAN
- that.setData({
- isConnectWlan: true,
- connectWlanBssid: res.wifi.BSSID,
- })
- }
- },
- fail(res) {
- console.log(res.errMsg);
- that.setData({
- isConnectWlan: false
- })
- }
- })
- },
- // 刷新
- onRefresh() {
- this.getWifiList();
- },
- // 搜索不到WLAN
- onHelp() {
- wx.showModal({
- title: '温馨提示',
- content: `请您前往手机-设置-WLAN,打开WLAN开关并且打开手机的GPS`,
- showCancel: false,
- })
- },
- // 保存WLAN信息
- onSave(value) {
- const currentWlanInfo = value.currentTarget.dataset.name;
- console.log("当前点击的WLAN信息", currentWlanInfo);
- wx.showModal({
- title: '温馨提示',
- content: `当前选中WLANBSSID是:
- ${currentWlanInfo.BSSID}
- `,
- cancelText: '关闭',
- confirmText: '复制',
- success: (res) => {
- if (res.confirm) {
- wx.setClipboardData({
- data: `${currentWlanInfo.BSSID}`,
- })
- }
- }
- })
- // todo 调取接口
- },
- // 获取当前连接的周边的wifi列表
- getWifiList() {
- let that = this;
- // 先获取当前连接的wifi信息
- that.getConnectedWifi();
- wx.showLoading({
- title: '获取中WLAN...'
- });
- wx.getWifiList({
- success(res) {
- console.log("getWifiList", res);
- wx.hideLoading();
- wx.showToast({
- title: '获取成功',
- icon: 'success'
- });
- wx.onGetWifiList((result) => {
- // 筛选wifi名称为空的数据
- let wifiList = result.wifiList.filter(item => {
- return item.SSID !== '';
- });
- // ,如果有已连接的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) {
- console.log(res.errMsg);
- wx.hideLoading();
- wx.showModal({
- title: '温馨提示',
- content: '当前用户未打开WIFI和GPS',
- showCancel: false,
- })
- },
- });
- }
- })
|