|
@@ -5,10 +5,12 @@ const app = getApp()
|
|
|
Page({
|
|
|
data: {
|
|
|
motto: 'Hello World',
|
|
|
- type: '',
|
|
|
+ type: '', //0上课,1寝室
|
|
|
sucmsg: false,
|
|
|
warmsg: false,
|
|
|
errmsg: '',
|
|
|
+ list: [],
|
|
|
+ placeList: []
|
|
|
},
|
|
|
//事件处理函数
|
|
|
bindViewTap: function () {
|
|
@@ -17,10 +19,12 @@ Page({
|
|
|
})
|
|
|
},
|
|
|
onLoad: function (option) {
|
|
|
- console.log(app.globalData.userInfo)
|
|
|
+ this.stopDiscovery();
|
|
|
+ this.startDiscover();
|
|
|
this.setData({
|
|
|
type: option.type
|
|
|
})
|
|
|
+ this.getPlaceList();
|
|
|
},
|
|
|
addsuc: function () {
|
|
|
this.setData({
|
|
@@ -30,8 +34,11 @@ Page({
|
|
|
},
|
|
|
addmsg: function (errmsg) {
|
|
|
let msg = '';
|
|
|
- if (errmsg.errCode == 11001) msg = '请打开蓝牙功能';
|
|
|
+ if (errmsg.errCode == '11001' || errmsg.errCode === 11001) msg = '请打开蓝牙功能,重新进入';
|
|
|
+ else if (errmsg.errCode == '11003' || errmsg.errCode === 11003) msg = '已经开始搜索';
|
|
|
+ else if (errmsg.errCode == '11004' || errmsg.errCode === 11004) msg = '还未开始搜索';
|
|
|
else msg = errmsg.errMsg;
|
|
|
+
|
|
|
this.setData({
|
|
|
warmsg: true,
|
|
|
sucmsg: false,
|
|
@@ -41,82 +48,134 @@ Page({
|
|
|
ibeaconBtn: function () {
|
|
|
var that = this;
|
|
|
wx.showLoading({
|
|
|
- title: '正在签到...',
|
|
|
+ title: '正在签到',
|
|
|
mask: true,
|
|
|
})
|
|
|
+ let newdata = {
|
|
|
+ openid: app.globalData.openid,
|
|
|
+ type: that.data.type,
|
|
|
+ ibeacon: that.data.list.map(i => i.minor)
|
|
|
+ };
|
|
|
+ wx.request({
|
|
|
+ url: app.globalData.contentpath + 'api/train/attendance/attendancecreate',
|
|
|
+ header: {//请求头
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ },
|
|
|
+ data: newdata,
|
|
|
+ method: 'POST',
|
|
|
+ dataType: 'json',
|
|
|
+ success: function (res) {
|
|
|
+ if (res.data.errcode == 0) {
|
|
|
+ that.addsuc();
|
|
|
+ } else {
|
|
|
+ console.log('签到失败' + res.data.errmsg);
|
|
|
+ that.addmsg({ errMsg: res.data.errmsg });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: function (err) {
|
|
|
+ console.log('签到失败' + err);
|
|
|
+ that.addmsg(err);
|
|
|
+ },//请求失败
|
|
|
+ complete: function () {
|
|
|
+ wx.hideLoading()
|
|
|
+ }//请求完成后执行的函数
|
|
|
+ })
|
|
|
+ },
|
|
|
+ startDiscover() {
|
|
|
+ // setTimeout(() => this.stopDiscovery(), 5000);
|
|
|
wx.startBeaconDiscovery({
|
|
|
uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],
|
|
|
- success(res) {
|
|
|
- wx.onBeaconUpdate(function (beacons) {
|
|
|
+ success: (res) => {
|
|
|
+ wx.onBeaconUpdate(res => {
|
|
|
wx.getBeacons({
|
|
|
- success: function (res) {
|
|
|
- console.log('getBeacons');
|
|
|
- console.log(res)
|
|
|
- if (res.errMsg != 'getBeacons:ok') {
|
|
|
- that.addmsg({ errMsg: '获取蓝牙设备失败' });
|
|
|
- return;
|
|
|
- }
|
|
|
- var master = 100000;
|
|
|
- var mastername = '0';
|
|
|
+ success: (result) => {
|
|
|
+ const { beacons } = result
|
|
|
var beaconList = [];
|
|
|
- var limit = 5;
|
|
|
- for (var i = 0; i < res.beacons.length; i++) {
|
|
|
- var beacon = res.beacons[i];
|
|
|
+ var limit = 30;
|
|
|
+ for (var i = 0; i < beacons.length; i++) {
|
|
|
+ var beacon = beacons[i];
|
|
|
if (beacon.accuracy < limit) {
|
|
|
- beaconList.push(beacon.minor);
|
|
|
+ const r = this.data.placeList.find(f => f.ibeacon === `${beacon.minor}`)
|
|
|
+ if (r) {
|
|
|
+ if (this.data.type === '0') {
|
|
|
+ const { name } = r
|
|
|
+ beaconList.push(name);
|
|
|
+ } else {
|
|
|
+ const { code, floor } = r
|
|
|
+ let name;
|
|
|
+ let remark = 'C区';
|
|
|
+ if (code.includes('后楼')) {
|
|
|
+ if (code.includes('-')) remark = "B区"
|
|
|
+ if (floor) name = `后楼${remark}${floor.includes('楼') ? floor : `${floor}楼`}`
|
|
|
+ else name = `后楼${remark}${beacon.minor}`
|
|
|
+ } else if (code.includes('前楼')) {
|
|
|
+ if (floor) name = `前楼${floor.includes('楼') ? floor : `${floor}楼`}`
|
|
|
+ else name = `前楼${beacon.minor}`
|
|
|
+ } else name = code
|
|
|
+ beaconList.push(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- console.log(res.beacons)
|
|
|
- console.log('附近--');
|
|
|
- console.log(beaconList);
|
|
|
- wx.stopBeaconDiscovery({
|
|
|
- success: function (res) {
|
|
|
- console.log('停止扫描');
|
|
|
- let newdata = {
|
|
|
- openid: app.globalData.openid,
|
|
|
- type: that.data.type,
|
|
|
- ibeacon: beaconList
|
|
|
- };
|
|
|
- wx.request({
|
|
|
- url: app.globalData.contentpath + 'api/train/attendance/attendancecreate',
|
|
|
- header: {//请求头
|
|
|
- "Content-Type": "application/json"
|
|
|
- },
|
|
|
- data: newdata,
|
|
|
- method: 'POST',
|
|
|
- dataType: 'json',
|
|
|
- success: function (res) {
|
|
|
- //res.data相当于ajax里面的data,为后台返回的数据
|
|
|
- //如果在sucess直接写this就变成了wx.request()的this了
|
|
|
- //必须为getdata函数的this,不然无法重置调用函数
|
|
|
- if (res.data.errcode == 0) {
|
|
|
- that.addsuc();
|
|
|
- } else {
|
|
|
- console.log('签到失败' + res.data.errmsg);
|
|
|
- that.addmsg({ errMsg: res.data.errmsg });
|
|
|
- }
|
|
|
- },
|
|
|
- fail: function (err) {
|
|
|
- console.log('签到失败' + err);
|
|
|
- that.addmsg(err);
|
|
|
- },//请求失败
|
|
|
- complete: function () {
|
|
|
- wx.hideLoading()
|
|
|
- }//请求完成后执行的函数
|
|
|
- })
|
|
|
- // }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
+ this.setData({
|
|
|
+ list: beaconList
|
|
|
+ })
|
|
|
+
|
|
|
+ console.log(result)
|
|
|
+ },
|
|
|
})
|
|
|
})
|
|
|
|
|
|
},
|
|
|
- fail(res) {
|
|
|
- console.log('function in fail')
|
|
|
- console.log(res);
|
|
|
- that.addmsg(res);
|
|
|
+ fail: (error) => {
|
|
|
+ console.log(error)
|
|
|
+ this.addmsg(error);
|
|
|
}
|
|
|
})
|
|
|
+ },
|
|
|
+ stopDiscovery() {
|
|
|
+ wx.stopBeaconDiscovery({
|
|
|
+ success: (res) => {
|
|
|
+ console.log('停止扫描');
|
|
|
+ },
|
|
|
+ })
|
|
|
+ },
|
|
|
+ getPlaceList() {
|
|
|
+ let model;
|
|
|
+ if (this.data.type === '1' || this.data.type === 1) {
|
|
|
+ model = 'bedroom'
|
|
|
+ } else {
|
|
|
+ model = 'location'
|
|
|
+ }
|
|
|
+ let uri = `api/train/${model}`
|
|
|
+ wx.request({
|
|
|
+ url: `${app.globalData.contentpath}${uri}`,
|
|
|
+ header: {//请求头
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ },
|
|
|
+ method: 'GET',
|
|
|
+ dataType: 'json',
|
|
|
+ success: (res) => {
|
|
|
+ if (res.data.errcode == 0) {
|
|
|
+ console.log(res);
|
|
|
+ const { data } = res.data;
|
|
|
+ if (data) {
|
|
|
+ const list = data.filter(f => f.ibeacon)
|
|
|
+ this.setData({
|
|
|
+ placeList: list
|
|
|
+ })
|
|
|
+ console.log(this.data.placeList);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: function (err) {
|
|
|
+ that.addmsg(err);
|
|
|
+ },//请求失败
|
|
|
+ })
|
|
|
}
|
|
|
+
|
|
|
})
|