index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. import Toast from '../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
  5. Page({
  6. data: {
  7. motto: 'Hello World',
  8. type: '', //0上课,1寝室
  9. sucmsg: false,
  10. warmsg: false,
  11. errmsg: '',
  12. toast: {},
  13. },
  14. //事件处理函数
  15. bindViewTap: function () {
  16. wx.navigateTo({
  17. url: '../logs/logs'
  18. })
  19. },
  20. onLoad: function (option) {
  21. this.setData({
  22. type: option.type
  23. })
  24. this.stopDiscovery();
  25. this.startDiscover();
  26. },
  27. addsuc: function () {
  28. this.setData({
  29. sucmsg: true,
  30. warmsg: false,
  31. });
  32. },
  33. addmsg: function (errmsg) {
  34. let msg = '';
  35. if (errmsg.errCode == '11001' || errmsg.errCode === 11001) msg = '请打开蓝牙功能,请开启蓝牙功能后按"签到"按钮';
  36. else if (errmsg.errCode == '11003' || errmsg.errCode === 11003) msg = '已经开始搜索';
  37. else if (errmsg.errCode == '11004' || errmsg.errCode === 11004) msg = '还未开始搜索';
  38. else msg = errmsg.errMsg;
  39. this.setData({
  40. warmsg: true,
  41. sucmsg: false,
  42. errmsg: msg,
  43. });
  44. },
  45. reSearch() {
  46. this.stopDiscovery();
  47. this.setData({
  48. sucmsg: false,
  49. warmsg: false,
  50. errmsg: '重新扫描',
  51. })
  52. this.startDiscover();
  53. },
  54. toSign: function (ibeacon) {
  55. let newdata = {
  56. openid: app.globalData.openid,
  57. type: this.data.type,
  58. ibeacon: [ibeacon]
  59. };
  60. wx.request({
  61. url: app.globalData.contentpath + 'api/train/attendance/attendancecreate',
  62. header: {//请求头
  63. "Content-Type": "application/json"
  64. },
  65. data: newdata,
  66. method: 'POST',
  67. dataType: 'json',
  68. success: (res) => {
  69. if (res.data.errcode == 0) {
  70. this.addsuc();
  71. } else {
  72. console.log('签到失败' + res.data.errmsg);
  73. this.addmsg({ errMsg: res.data.errmsg });
  74. }
  75. },
  76. fail: (err) => {
  77. console.log('签到失败' + err);
  78. this.addmsg(err);
  79. },//请求失败
  80. complete: () => {
  81. const toast = this.data.toast;
  82. toast.clear();
  83. }//请求完成后执行的函数
  84. })
  85. },
  86. async startDiscover() {
  87. const user = app.globalData.userInfo;
  88. if (!user) {
  89. Toast({
  90. duration: 3000,
  91. message: "未找到用户信息,请重新进入小程序"
  92. })
  93. return;
  94. }
  95. let toast = Toast({
  96. type: 'loading',
  97. duration: 0,
  98. message: '准备扫描中',
  99. forbidClick: true,
  100. })
  101. this.setData({
  102. toast: toast
  103. })
  104. wx.startBeaconDiscovery({
  105. uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],
  106. success: (res) => {
  107. toast.setData({
  108. message: '正在搜索设备,请稍后...'
  109. })
  110. const countId = setTimeout(() => {
  111. this.stopDiscovery()
  112. this.addmsg({ errMsg: '未扫描到指定设备,请按"签到"重新扫描' })
  113. }, 30 * 1000)
  114. wx.onBeaconUpdate(res => {
  115. wx.getBeacons({
  116. success: (result) => {
  117. const { beacons } = result
  118. var beaconList = [];
  119. var limit = 30;
  120. for (var i = 0; i < beacons.length; i++) {
  121. var beacon = beacons[i];
  122. if (beacon.accuracy <= limit) {
  123. beaconList.push(`${beacon.minor}`)
  124. }
  125. }
  126. const type = this.data.type;
  127. let needIbeacon = '';
  128. let place = '';
  129. if (type == '0') {
  130. // 教室
  131. needIbeacon = `${user.jsibeacon}`
  132. place = user.jsname
  133. } else {
  134. needIbeacon = `${user.bedroomibeacon}`
  135. place = user.bedroomname
  136. }
  137. if (beaconList.includes(needIbeacon)) {
  138. toast.setData({
  139. message: `已搜索到${place}设备,正在签到,请稍后.....`
  140. })
  141. this.toSign(needIbeacon);
  142. clearTimeout(countId)
  143. this.stopDiscovery();
  144. }
  145. },
  146. })
  147. })
  148. },
  149. fail: async (error) => {
  150. await toast.clear();
  151. this.addmsg(error);
  152. }
  153. })
  154. },
  155. stopDiscovery() {
  156. wx.stopBeaconDiscovery({
  157. success: (res) => {
  158. const toast = this.data.toast;
  159. toast.clear();
  160. console.log('停止扫描');
  161. },
  162. })
  163. },
  164. })