//index.js //获取应用实例 const app = getApp() Page({ data: { motto: 'Hello World', type: '', sucmsg: false, warmsg: false, errmsg: '', }, //事件处理函数 bindViewTap: function () { wx.navigateTo({ url: '../logs/logs' }) }, onLoad: function (option) { console.log(app.globalData.userInfo) this.setData({ type: option.type }) }, addsuc: function () { this.setData({ sucmsg: true, warmsg: false, }); }, addmsg: function (errmsg) { let msg = ''; if (errmsg.errCode == 11001) msg = '请打开蓝牙功能'; else msg = errmsg.errMsg; this.setData({ warmsg: true, sucmsg: false, errmsg: msg, }); }, ibeaconBtn: function () { var that = this; wx.showLoading({ title: '正在签到', mask: true, }) wx.startBeaconDiscovery({ uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'], success(res) { wx.onBeaconUpdate(function (beacons) { 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'; var beaconList = []; var limit = 5; for (var i = 0; i < res.beacons.length; i++) { var beacon = res.beacons[i]; if (beacon.accuracy < limit) { beaconList.push(beacon.minor); } } 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() }//请求完成后执行的函数 }) // } } }); } }) }) }, fail(res) { console.log('function in fail') console.log(res); that.addmsg(res); } }) } })