Selaa lähdekoodia

修改显示,扫描方式.错误提示

lrf402788946 4 vuotta sitten
vanhempi
commit
75f2aaeca1
6 muutettua tiedostoa jossa 194 lisäystä ja 94 poistoa
  1. 33 18
      app.js
  2. 7 1
      pages/bind/index.js
  3. 16 0
      pages/index/index.js
  4. 124 65
      pages/work/index.js
  5. 13 9
      pages/work/index.wxml
  6. 1 1
      project.config.json

+ 33 - 18
app.js

@@ -15,25 +15,40 @@ App({
       success: res => {
         // 发送 res.code 到后台换取 openId, sessionKey, unionId
         if (res.code) {
+          that.globalData.code = res.code;
           //获取openId
           // 需要放到服务端去请求
-          wx.request({
-            url: that.globalData.contentpath + 'api/train/appAuth?js_code=' + res.code,
-            method: 'GET',
-            header: { 'content-type': 'application/json' },
-            success: function (result) {
-              const res = result.data;
-              that.globalData.openid = res.openid;
-              that.globalData.userInfo = res.user;
-            },
-            fail: function (error) {
-              console.info("获取用户openId失败");
-              console.info(error);
-            },
-            complete() {
-              wx.hideLoading()
-            },
-          })
+          try {
+            wx.request({
+              url: that.globalData.contentpath + 'api/train/appAuth?js_code=' + res.code,
+              method: 'GET',
+              header: { 'content-type': 'application/json' },
+              success: function (result) {
+                console.log(result);
+                if (result.statusCode === 200) {
+                  const res = result.data;
+                  that.globalData.openid = res.openid;
+                  that.globalData.userInfo = res.user;
+                } else {
+                  wx.showToast({ title: '未请求到用户', icon: 'none', duration: 0, mask: true })
+                }
+
+              },
+              fail: function (error) {
+                wx.showToast({ title: '未请求到用户', icon: 'none', duration: 0, mask: true })
+                console.info("获取用户openId失败");
+                console.info(error);
+              },
+              complete() {
+                wx.hideLoading()
+              },
+            })
+          } catch (error) {
+            wx.showToast({
+              title: '用户信息无效',
+              icon: "none"
+            })
+          }
         }
       }
     })
@@ -42,7 +57,7 @@ App({
     userInfo: null,
     openid: null,
     unionid: null,
-    contentpath: 'https://jytz.jilinjobs.cn/',
+    contentpath: 'https://jytz.jilinjobs.cn/', //http://127.0.0.1:2001/
     cookie: null,
     code: null,
   }

+ 7 - 1
pages/bind/index.js

@@ -23,6 +23,12 @@ Page({
   },
 
   async toBind() {
+    if (!app.globalData.openid) {
+      wx.showToast({
+        title: '缺少用户信息,请重新进入',
+        icon: 'none'
+      })
+    }
     const obj = { name: this.data.name, mobile: this.data.mobile, appopenid: app.globalData.openid };
     wx.request({
       url: app.globalData.contentpath + 'api/train/user/appbind',
@@ -50,7 +56,7 @@ Page({
         } else {
           wx.showToast({
             title: r.errmsg || "绑定失败",
-            icon: 'errror',
+            icon: 'none',
             duration: 3000
           })
         }

+ 16 - 0
pages/index/index.js

@@ -13,6 +13,14 @@ Page({
     })
   },
   classBtn: function () {
+    const openid = app.globalData.openid;
+    if (!openid) {
+      wx.showToast({
+        title: '未找到用户信息',
+        icon: 'none'
+      })
+      return;
+    }
     const user = app.globalData.userInfo;
     if (!user) {
       this.toBind();
@@ -23,6 +31,14 @@ Page({
     })
   },
   beedroomBtn: function () {
+    const openid = app.globalData.openid;
+    if (!openid) {
+      wx.showToast({
+        title: '未找到用户信息',
+        icon: 'none'
+      })
+      return;
+    }
     const user = app.globalData.userInfo;
     if (!user) {
       this.toBind();

+ 124 - 65
pages/work/index.js

@@ -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);
+      },//请求失败
+    })
   }
+
 })

+ 13 - 9
pages/work/index.wxml

@@ -1,29 +1,33 @@
 <!--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">
-  <image class="qiandaoImage" src="../images/qiandao.jpg" ></image>
-  <p class="qiandao">签到</p>
-    <!-- <button bindtap="ibeaconBtn" type="primary">签到</button> -->
-  </view>
-  <view wx:if="{{warmsg}}" class="weui-msg">
+	<view class="userinfo" bindtap="ibeaconBtn">
+		<image class="qiandaoImage" src="../images/qiandao.jpg"></image>
+		<p class="qiandao">签到</p>
+		<!-- <button bindtap="ibeaconBtn" type="primary">签到</button> -->
+	</view>
+	<view class="weui-msg">
+		<h2>已扫描到的蓝牙</h2>
+		<div wx:for="{{list}}">{{item}}</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">
 			<h2 class="weui-msg__title">签到失败</h2>
 			<view class="weui-msg__desc">{{errmsg}}</view>
 		</view>
 	</view>
-  <view wx:if="{{sucmsg}}" class="weui-msg">
+	<view wx:if="{{sucmsg}}" class="weui-msg">
 		<view class="weui-msg__icon-area"><i class="weui-icon-success weui-icon_msg"></i></view>
 		<view class="weui-msg__text-area">
 			<h2 class="weui-msg__title">签到成功</h2>
 			<view class="weui-msg__desc"></view>
 		</view>
 	</view>
-</view>
+</view>

+ 1 - 1
project.config.json

@@ -4,7 +4,7 @@
 		"ignore": []
 	},
 	"setting": {
-		"urlCheck": true,
+		"urlCheck": false,
 		"es6": true,
 		"enhance": false,
 		"postcss": true,