# 一、实现逻辑

1、先上传当前自己的定位经纬度

2、学生查询老师的最后一次签到记录，如果发现签到记录signComplete为false说明有新的签到

3、得到签到的第几次课

4、系统获得学生的定位经纬度

5、判断两点经纬度转换成距离是否在100米以内，如果是则可以签到否则不能签到

1、老师端选择结束签到

2、老师更新最后一次签到signComplete为true

# 三、代码实现

 // 签到 sign:function(){ wx.showToast({ title: '正在获取位置信息..', icon:'loading', duration:10000 }); // 1、获取位置 wx.getLocation({ type: 'wgs84', success: function (resLocation) { // 2、设置当前用户的经纬度信息 var queryUser = Bmob.Query('_User'); queryUser.get(app.globalData.currentUser.objectId).then(resUser => { var point = Bmob.GeoPoint({ latitude: resLocation.latitude, longitude: resLocation.longitude }) resUser.set('location', point); resUser.save(); //3、查询签到表老师最近一次签到 var queryTeacher = Bmob.Query('_User'); queryTeacher.equalTo("userType", '==', 1); queryTeacher.find().then(resTeacher => { var teacher = resTeacher[0]; var teacherPointer = Bmob.Pointer('_User') var teacherPoiID = teacherPointer.set(teacher.objectId) var teacherQuerySign = Bmob.Query('sign_record'); //userId 字段名称关联用户表 ，类型Pointer teacherQuerySign.equalTo("userId", "==", teacherPoiID); teacherQuerySign.order("-numberNo"); teacherQuerySign.limit(1); teacherQuerySign.find().then(resTeacherSign => { wx.hideToast(); // 如果没有记录，说明学生当前不用签到，老师可以发起签到 if (resTeacherSign.length==0){ // 如果当前用户是老师 if(app.globalData.currentUser.userType==1){ wx.showModal({ title: '提示', content: '您确定发起签到吗', success: function (resModal) { if (resModal.confirm) { wx.showToast({ title: '发起中..', icon: 'loading', duration: 10000 }); teacherQuerySign.set('numberNo',1); teacherQuerySign.set('signComplete', false); teacherQuerySign.save().then(resTeacherSignSave => { teacherQuerySign.get(resTeacherSignSave.objectId).then(resSignSaveUser => { var userIdPointer = Bmob.Pointer('_User'); var pointerUserId = userIdPointer.set(app.globalData.currentUser.objectId); resSignSaveUser.set('userId', pointerUserId); resSignSaveUser.save(); // 签到结束刷新签到记录 that.getSignRecord(); wx.hideToast(); }); }).catch(err => { console.log(err) }); } } }); } // 如果是学生 else{ wx.showToast({ title: '当前无需签到~', duration:2500 }); return; } } //有签到记录 else{ var teacherSignRecordItem = resTeacherSign[0]; if (app.globalData.currentUser.userType == 1) { wx.hideToast(); wx.showModal({ title: '提示', content: '您确定发起签到吗', success: function (resModal) { if (resModal.confirm) { wx.showToast({ title: '发起中..', icon: 'loading', duration: 10000 }); teacherQuerySign.set('numberNo', teacherSignRecordItem.numberNo + 1); teacherQuerySign.set('signComplete', false); teacherQuerySign.save().then(resTeacherSignSave => { teacherQuerySign.get(resTeacherSignSave.objectId).then(resSignSaveUser => { var userIdPointer = Bmob.Pointer('_User'); var pointerUserId = userIdPointer.set(app.globalData.currentUser.objectId); resSignSaveUser.set('userId', pointerUserId); resSignSaveUser.save(); // 签到结束刷新签到记录 that.getSignRecord(); wx.hideToast(); }); }).catch(err => { console.log(err) }); } } }) } // 如果是学生 else { // 如果最后一次签到已经结束则提示最近一次签到结束 if (teacherSignRecordItem.signComplete == true){ wx.showToast({ title: '最新签到已结束~', duration: 2500 }); return; } else{ wx.showToast({ title: '查询是否签到中..', icon: 'loading', duration: 10000 }); // 查询是不是已经签到过了 var queryHaveSign = Bmob.Query('sign_record'); var pointerHaveSign = Bmob.Pointer('_User') var poiIDHaveSign = pointerHaveSign.set(app.globalData.currentUser.objectId); queryHaveSign.equalTo("userId", '==', poiIDHaveSign); queryHaveSign.equalTo("numberNo", '==', teacherSignRecordItem.numberNo); queryHaveSign.find().then(resHaveSign => { wx.hideToast(); // 如果最新一次课已经签到 if (resHaveSign.length>0){ wx.showToast({ title: '您已签到过了~', duration: 2500 }); return; } // 如果最新一次课没有签到 else{ wx.showToast({ title: '获取老师位置中..', icon: 'loading', duration: 10000 }); // 判断和老师端的距离，小于100米才能签到 var teacherLocation = teacher.location; var distance = Util.getDistance(resLocation.latitude, resLocation.longitude, teacherLocation.latitude, teacherLocation.longitude); //距离老师签到点100米内可以签到否则不能 if (distance < 100) { teacherQuerySign.set('numberNo', teacherSignRecordItem.numberNo); teacherQuerySign.set('signComplete', false); teacherQuerySign.save().then(resTeacherSignSave => { teacherQuerySign.get(resTeacherSignSave.objectId).then(resSignSaveUser => { var userIdPointer = Bmob.Pointer('_User'); var pointerUserId = userIdPointer.set(app.globalData.currentUser.objectId); resSignSaveUser.set('userId', pointerUserId); resSignSaveUser.save(); // 签到结束刷新签到记录 that.getSignRecord(); wx.hideToast(); wx.showToast({ title: '恭喜你完成签到啦~', duration: 2500 }); }); }).catch(err => { console.log(err) }); } else { wx.hideToast(); wx.showToast({ title: '您距离老师太远啦~', duration: 2500 }); return; } } }).catch(err=>{}); } } } }).catch(err => { }); }).catch(err=>{}); }); } }); },

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120