|
- import request from '../../utils/request';
- import { fenApi } from '../../api/core';
- import { getBaseUrl } from '../../utils/utilsServes';
-
- Page({
-
-
-
- data: {
- wlanList: [],
- isConnectWlan: null,
- connectWlanBssid: '',
- imei: '',
- fenceId: '',
- appType: '',
- env: '',
- },
-
-
-
- onLoad: function (options) {
- this.onStartWifi();
- this.checkOptions(options);
- this.getAuth();
- },
- onHide() {
-
- wx.removeStorage({
- key: 'authToken',
- })
- },
-
- getAuth() {
-
- 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 ) {
-
- if(wx.getStorageSync('authToken')) {
- wx.removeStorage('authToken');
- }
- wx.setStorage({
- key: "authToken",
- data: res.data,
- })
- };
- }).catch(error => {
- console.log("error", error);
- })
- },
-
- checkOptions(options) {
-
- 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",
- })
- },
-
- onStartWifi() {
- let that = this;
- wx.startWifi({
- success() {
- that.getWifiList();
- },
- fail(res) {
- console.log("失败", res.errMsg);
- wx.showModal({
- title: '温馨提示',
- content: `当前用户未打开WIFI和GPS`,
- showCancel: false,
- })
- }
- });
- },
-
- getConnectedWifi: function () {
- let that = this;
- wx.getConnectedWifi({
- success(res) {
- if(res) {
-
- that.setData({
- isConnectWlan: true,
- connectWlanBssid: res.wifi.BSSID,
- })
- }
- },
- fail(res) {
- console.log(res.errMsg);
- that.setData({
- isConnectWlan: false
- })
- }
- })
- },
-
- onRefresh() {
- this.getWifiList();
- this.getAuth();
-
- },
-
- onHelp() {
- wx.showModal({
- title: '温馨提示',
- content: `请您前往手机-设置-WLAN,打开WLAN开关并且打开手机的GPS`,
- showCancel: false,
- })
- },
-
- 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,
- wifiInfo: currentWlanInfo.BSSID
- };
-
- 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}`,
- })
- }
- }
- })
- },
-
- getWifiList() {
- let that = this;
-
- that.getConnectedWifi();
- wx.showLoading({
- title: '获取中WLAN...'
- });
- wx.getWifiList({
- success(res) {
- wx.hideLoading();
- wx.showToast({
- title: '获取成功',
- icon: 'success'
- });
- wx.onGetWifiList((result) => {
-
- let wifiList = result.wifiList.filter(item => {
- return item.SSID !== '';
- });
-
- if(that.data.connectWlanBssid !== '') {
-
-
- 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,
- })
- },
- });
- }
- })
|