lrf402788946 4 years ago
parent
commit
dec014907c

+ 1 - 0
.gitignore

@@ -12,3 +12,4 @@ $RECYCLE.BIN/
 
 # Node.js
 node_modules/
+miniprogram

+ 0 - 1
app.json

@@ -11,6 +11,5 @@
     "navigationBarTitleText": "培训会考勤",
     "navigationBarTextStyle": "black"
   },
-  "style": "v2",
   "sitemapLocation": "sitemap.json"
 }

+ 5 - 5
pages/bind/index.wxml

@@ -1,8 +1,8 @@
 <!--index.wxml-->
 <view class="container">
- <form>
-  <input bindinput="bindName" placeholder="请输入姓名"/>
-  <input bindinput="bindMobile" placeholder="请输入手机号"/>
-  <button bindtap="toBind" type="primary">绑定</button>
- </form>
+  <form>
+    <input bindinput="bindName" placeholder="请输入姓名" />
+    <input bindinput="bindMobile" placeholder="请输入手机号" />
+    <button bindtap="toBind" type="primary">绑定</button>
+  </form>
 </view>

+ 11 - 8
pages/index/index.js

@@ -1,6 +1,7 @@
 //index.js
 //获取应用实例
 const app = getApp()
+import Toast from '../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
 
 Page({
   data: {
@@ -15,10 +16,11 @@ Page({
   classBtn: function () {
     const openid = app.globalData.openid;
     if (!openid) {
-      wx.showToast({
-        title: '未找到用户信息',
-        icon: 'none'
-      })
+      Toast.fail('未找到用户信息,请重新进入');
+      // wx.showToast({
+      //   title: '未找到用户信息',
+      //   icon: 'none'
+      // })
       return;
     }
     const user = app.globalData.userInfo;
@@ -33,10 +35,11 @@ Page({
   beedroomBtn: function () {
     const openid = app.globalData.openid;
     if (!openid) {
-      wx.showToast({
-        title: '未找到用户信息',
-        icon: 'none'
-      })
+      Toast.fail('未找到用户信息,请重新进入');
+      // wx.showToast({
+      //   title: '未找到用户信息',
+      //   icon: 'none'
+      // })
       return;
     }
     const user = app.globalData.userInfo;

+ 7 - 1
pages/index/index.json

@@ -1,3 +1,9 @@
 {
-  "usingComponents": {}
+  "usingComponents": {
+    "van-row": "/miniprogram/miniprogram_npm/vant-weapp/row/index",
+    "van-col": "/miniprogram/miniprogram_npm/vant-weapp/col/index",
+    "van-button": "/miniprogram/miniprogram_npm/vant-weapp/button/index",
+    "van-toast": "/miniprogram/miniprogram_npm/vant-weapp/toast/index",
+    "van-icon": "/miniprogram/miniprogram_npm/vant-weapp/icon/index"
+  }
 }

+ 17 - 1
pages/index/index.wxml

@@ -1,9 +1,25 @@
 <!--index.wxml-->
-<view class="container">
+<!-- <view class="container">
   <view class="userinfo">
     <button bindtap="classBtn" type="primary">上课考勤</button>
   </view>
+
   <view class="usermotto">
     <button bindtap="beedroomBtn" type="primary">寝室考勤</button>
   </view>
+</view> -->
+<view class="main">
+  <van-row>
+    <van-col span="24">
+      <van-button type="primary" size="large" bindtap="classBtn">上课考勤</van-button>
+    </van-col>
+  </van-row>
+  <view style="padding-top:30%">
+    <van-row>
+      <van-col span="24">
+        <van-button type="primary" size="large" bindtap="beedroomBtn">寝室考勤</van-button>
+      </van-col>
+    </van-row>
+  </view>
 </view>
+<van-toast id="van-toast" />

+ 9 - 0
pages/index/index.wxss

@@ -20,3 +20,12 @@
   margin-top: 50px;
 }
 
+.main {
+  height: 100%;
+  display: flex;
+  flex-direction: column;
+  padding:45% 15%;
+  /* align-items: center; */
+  justify-content: space-between;
+  box-sizing: border-box;
+}

+ 122 - 0
pages/work/index copy.js

@@ -0,0 +1,122 @@
+//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);
+      }
+    })
+  }
+})

+ 76 - 88
pages/work/index.js

@@ -1,6 +1,7 @@
 //index.js
 //获取应用实例
 const app = getApp()
+import Toast from '../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
 
 Page({
   data: {
@@ -9,8 +10,7 @@ Page({
     sucmsg: false,
     warmsg: false,
     errmsg: '',
-    list: [],
-    placeList: []
+    toast: {},
   },
   //事件处理函数
   bindViewTap: function () {
@@ -19,12 +19,11 @@ Page({
     })
   },
   onLoad: function (option) {
-    this.stopDiscovery();
-    this.startDiscover();
     this.setData({
       type: option.type
     })
-    this.getPlaceList();
+    this.stopDiscovery();
+    this.startDiscover();
   },
   addsuc: function () {
     this.setData({
@@ -34,28 +33,32 @@ Page({
   },
   addmsg: function (errmsg) {
     let msg = '';
-    if (errmsg.errCode == '11001' || 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,
       errmsg: msg,
     });
   },
-  ibeaconBtn: function () {
-    var that = this;
-    wx.showLoading({
-      title: '正在签到',
-      mask: true,
+  reSearch() {
+    this.stopDiscovery();
+    this.setData({
+      sucmsg: false,
+      warmsg: false,
+      errmsg: '重新扫描',
     })
+    this.startDiscover();
+  },
+  toSign: function (ibeacon) {
     let newdata = {
       openid: app.globalData.openid,
-      type: that.data.type,
-      ibeacon: that.data.list.map(i => i.minor)
+      type: this.data.type,
+      ibeacon: [ibeacon]
     };
+
     wx.request({
       url: app.globalData.contentpath + 'api/train/attendance/attendancecreate',
       header: {//请求头
@@ -64,28 +67,53 @@ Page({
       data: newdata,
       method: 'POST',
       dataType: 'json',
-      success: function (res) {
+      success: (res) => {
         if (res.data.errcode == 0) {
-          that.addsuc();
+          this.addsuc();
         } else {
           console.log('签到失败' + res.data.errmsg);
-          that.addmsg({ errMsg: res.data.errmsg });
+          this.addmsg({ errMsg: res.data.errmsg });
         }
       },
-      fail: function (err) {
+      fail: (err) => {
         console.log('签到失败' + err);
-        that.addmsg(err);
+        this.addmsg(err);
       },//请求失败
-      complete: function () {
-        wx.hideLoading()
+      complete: () => {
+        const toast = this.data.toast;
+        toast.clear();
       }//请求完成后执行的函数
+
     })
   },
-  startDiscover() {
-    // setTimeout(() => this.stopDiscovery(), 5000);
+  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) => {
@@ -94,42 +122,36 @@ Page({
               var limit = 30;
               for (var i = 0; i < beacons.length; i++) {
                 var beacon = beacons[i];
-                if (beacon.accuracy < limit) {
-                  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);
-                    }
-
-                  }
+                if (beacon.accuracy <= limit) {
+                  beaconList.push(`${beacon.minor}`)
                 }
               }
-              this.setData({
-                list: beaconList
-              })
-
-              console.log(result)
+              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: (error) => {
-        console.log(error)
+      fail: async (error) => {
+        await toast.clear();
         this.addmsg(error);
       }
     })
@@ -137,45 +159,11 @@ Page({
   stopDiscovery() {
     wx.stopBeaconDiscovery({
       success: (res) => {
+        const toast = this.data.toast;
+        toast.clear();
         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);
-      },//请求失败
-    })
-  }
 
 })

+ 3 - 1
pages/work/index.json

@@ -1,3 +1,5 @@
 {
-  "usingComponents": {}
+  "usingComponents": {
+    "van-toast": "/miniprogram/miniprogram_npm/vant-weapp/toast/index"
+  }
 }

+ 8 - 7
pages/work/index.wxml

@@ -1,21 +1,21 @@
 <!--index.wxml-->
 <view class="page">
-	<view class="fadeIn {{hideToast ? 'fadeOut' : ''}}" wx:if="{{toast}}">
+	<!-- <view class="fadeIn {{hideToast ? 'fadeOut' : ''}}" wx:if="{{toast}}">
 		<view class="weui-mask_transparent"></view>
 		<view class="weui-toast">
 			<i class="weui-icon-success-no-circle weui-icon_toast"></i>
 			<view class="weui-toast__content">已完成</view>
 		</view>
-	</view>
-	<view class="userinfo" bindtap="ibeaconBtn">
+	</view> -->
+	<view class="userinfo" bindtap="reSearch">
 		<image class="qiandaoImage" src="../images/qiandao.jpg"></image>
 		<p class="qiandao">签到</p>
 		<!-- <button bindtap="ibeaconBtn" type="primary">签到</button> -->
 	</view>
-	<view class="weui-msg">
+	<!-- <view class="weui-msg">
 		<h2>已扫描到的蓝牙</h2>
-		<div wx:for="{{list}}">{{item}}</div>
-	</view>
+		<div wx:for="{{list}}">{{item.name}}</div>
+	</view> -->
 	<view wx:if="{{warmsg}}" class="weui-msg">
 		<view class="weui-msg__icon-area"><i class="weui-icon-warn weui-icon_msg"></i></view>
 		<view class="weui-msg__text-area">
@@ -30,4 +30,5 @@
 			<view class="weui-msg__desc"></view>
 		</view>
 	</view>
-</view>
+</view>
+<van-toast id="van-toast" />

+ 58 - 59
project.config.json

@@ -1,61 +1,60 @@
 {
-	"description": "项目配置文件",
-	"packOptions": {
-		"ignore": []
-	},
-	"setting": {
-		"urlCheck": false,
-		"es6": true,
-		"enhance": false,
-		"postcss": true,
-		"preloadBackgroundData": false,
-		"minified": true,
-		"newFeature": true,
-		"coverView": true,
-		"nodeModules": false,
-		"autoAudits": false,
-		"showShadowRootInWxmlPanel": true,
-		"scopeDataCheck": false,
-		"uglifyFileName": false,
-		"checkInvalidKey": true,
-		"checkSiteMap": true,
-		"uploadWithSourceMap": true,
-		"compileHotReLoad": false,
-		"babelSetting": {
-			"ignore": [],
-			"disablePlugins": [],
-			"outputPath": ""
-		},
-		"useIsolateContext": true,
-		"useCompilerModule": false,
-		"userConfirmedUseCompilerModuleSwitch": false
-	},
-	"compileType": "miniprogram",
-	"libVersion": "2.11.0",
-	"appid": "wx3b5ac763ada73e0e",
-	"projectname": "train-ibea",
-	"debugOptions": {
-		"hidedInDevtools": []
-	},
-	"isGameTourist": false,
-	"simulatorType": "wechat",
-	"simulatorPluginLibVersion": {},
-	"condition": {
-		"search": {
-			"current": -1,
-			"list": []
-		},
-		"conversation": {
-			"current": -1,
-			"list": []
-		},
-		"game": {
-			"currentL": -1,
-			"list": []
-		},
-		"miniprogram": {
-			"current": -1,
-			"list": []
-		}
-	}
+  "description": "项目配置文件",
+  "packOptions": {
+    "ignore": []
+  },
+  "setting": {
+    "urlCheck": false,
+    "es6": true,
+    "enhance": false,
+    "postcss": true,
+    "preloadBackgroundData": false,
+    "minified": true,
+    "newFeature": true,
+    "coverView": true,
+    "nodeModules": false,
+    "autoAudits": false,
+    "showShadowRootInWxmlPanel": true,
+    "scopeDataCheck": false,
+    "uglifyFileName": false,
+    "checkInvalidKey": true,
+    "checkSiteMap": true,
+    "uploadWithSourceMap": true,
+    "compileHotReLoad": false,
+    "babelSetting": {
+      "ignore": [],
+      "disablePlugins": [],
+      "outputPath": ""
+    },
+    "useIsolateContext": true,
+    "useCompilerModule": false,
+    "userConfirmedUseCompilerModuleSwitch": false,
+    "useMultiFrameRuntime": false,
+    "useApiHook": true
+  },
+  "compileType": "miniprogram",
+  "libVersion": "2.11.0",
+  "appid": "wx3b5ac763ada73e0e",
+  "projectname": "train-ibea",
+  "debugOptions": {
+    "hidedInDevtools": []
+  },
+  "isGameTourist": false,
+  "simulatorType": "wechat",
+  "simulatorPluginLibVersion": {},
+  "condition": {
+    "search": {
+      "list": []
+    },
+    "conversation": {
+      "list": []
+    },
+    "game": {
+      "currentL": -1,
+      "list": []
+    },
+    "miniprogram": {
+      "list": []
+    }
+  }
 }