123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- //index.js
- //获取应用实例
- const app = getApp()
- import Toast from '../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
- Page({
- data: {
- motto: 'Hello World',
- type: '', //0上课,1寝室
- sucmsg: false,
- warmsg: false,
- errmsg: '',
- toast: {},
- },
- //事件处理函数
- bindViewTap: function () {
- wx.navigateTo({
- url: '../logs/logs'
- })
- },
- onLoad: function (option) {
- this.setData({
- type: option.type
- })
- this.stopDiscovery();
- this.startDiscover();
- },
- addsuc: function () {
- this.setData({
- sucmsg: true,
- warmsg: false,
- });
- },
- addmsg: function (errmsg) {
- let 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,
- errmsg: msg,
- });
- },
- reSearch() {
- this.stopDiscovery();
- this.setData({
- sucmsg: false,
- warmsg: false,
- errmsg: '重新扫描',
- })
- this.startDiscover();
- },
- toSign: function (ibeacon) {
- let newdata = {
- openid: app.globalData.openid,
- type: this.data.type,
- ibeacon: [ibeacon]
- };
- wx.request({
- url: app.globalData.contentpath + 'api/train/attendance/attendancecreate',
- header: {//请求头
- "Content-Type": "application/json"
- },
- data: newdata,
- method: 'POST',
- dataType: 'json',
- success: (res) => {
- if (res.data.errcode == 0) {
- this.addsuc();
- } else {
- console.log('签到失败' + res.data.errmsg);
- this.addmsg({ errMsg: res.data.errmsg });
- }
- },
- fail: (err) => {
- console.log('签到失败' + err);
- this.addmsg(err);
- },//请求失败
- complete: () => {
- const toast = this.data.toast;
- toast.clear();
- }//请求完成后执行的函数
- })
- },
- async startDiscover() {
- const user = app.globalData.userInfo;
- if (!user) {
- Toast({
- duration: 3000,
- message: "未找到用户信息,请重新进入小程序"
- })
- return;
- }
- let toast = Toast({
- type: 'loading',
- duration: 0,
- message: '准备扫描中',
- forbidClick: true,
- })
- this.setData({
- toast: toast
- })
- wx.startBeaconDiscovery({
- uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],
- success: (res) => {
- toast.setData({
- message: '正在搜索设备,请稍后...'
- })
- const countId = setTimeout(() => {
- this.stopDiscovery()
- this.addmsg({ errMsg: '未扫描到指定设备,请按"签到"重新扫描' })
- }, 30 * 1000)
- wx.onBeaconUpdate(res => {
- wx.getBeacons({
- success: (result) => {
- const { beacons } = result
- var beaconList = [];
- var limit = 30;
- for (var i = 0; i < beacons.length; i++) {
- var beacon = beacons[i];
- if (beacon.accuracy <= limit) {
- beaconList.push(`${beacon.minor}`)
- }
- }
- const type = this.data.type;
- let needIbeacon = '';
- let place = '';
- if (type == '0') {
- // 教室
- needIbeacon = `${user.jsibeacon}`
- place = user.jsname
- } else {
- needIbeacon = `${user.bedroomibeacon}`
- place = user.bedroomname
- }
- if (beaconList.includes(needIbeacon)) {
- toast.setData({
- message: `已搜索到${place}设备,正在签到,请稍后.....`
- })
- this.toSign(needIbeacon);
- clearTimeout(countId)
- this.stopDiscovery();
- }
- },
- })
- })
- },
- fail: async (error) => {
- await toast.clear();
- this.addmsg(error);
- }
- })
- },
- stopDiscovery() {
- wx.stopBeaconDiscovery({
- success: (res) => {
- const toast = this.data.toast;
- toast.clear();
- console.log('停止扫描');
- },
- })
- },
- })
|