index copy.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. motto: 'Hello World',
  7. type: '',
  8. sucmsg: false,
  9. warmsg: false,
  10. errmsg: '',
  11. },
  12. //事件处理函数
  13. bindViewTap: function () {
  14. wx.navigateTo({
  15. url: '../logs/logs'
  16. })
  17. },
  18. onLoad: function (option) {
  19. console.log(app.globalData.userInfo)
  20. this.setData({
  21. type: option.type
  22. })
  23. },
  24. addsuc: function () {
  25. this.setData({
  26. sucmsg: true,
  27. warmsg: false,
  28. });
  29. },
  30. addmsg: function (errmsg) {
  31. let msg = '';
  32. if (errmsg.errCode == 11001) msg = '请打开蓝牙功能';
  33. else msg = errmsg.errMsg;
  34. this.setData({
  35. warmsg: true,
  36. sucmsg: false,
  37. errmsg: msg,
  38. });
  39. },
  40. ibeaconBtn: function () {
  41. var that = this;
  42. wx.showLoading({
  43. title: '正在签到',
  44. mask: true,
  45. })
  46. wx.startBeaconDiscovery({
  47. uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],
  48. success(res) {
  49. wx.onBeaconUpdate(function (beacons) {
  50. wx.getBeacons({
  51. success: function (res) {
  52. console.log('getBeacons');
  53. console.log(res)
  54. if (res.errMsg != 'getBeacons:ok') {
  55. that.addmsg({ errMsg: '获取蓝牙设备失败' });
  56. return;
  57. }
  58. var master = 100000;
  59. var mastername = '0';
  60. var beaconList = [];
  61. var limit = 5;
  62. for (var i = 0; i < res.beacons.length; i++) {
  63. var beacon = res.beacons[i];
  64. if (beacon.accuracy < limit) {
  65. beaconList.push(beacon.minor);
  66. }
  67. }
  68. console.log(res.beacons)
  69. console.log('附近--');
  70. console.log(beaconList);
  71. wx.stopBeaconDiscovery({
  72. success: function (res) {
  73. console.log('停止扫描');
  74. let newdata = {
  75. openid: app.globalData.openid,
  76. type: that.data.type,
  77. ibeacon: beaconList
  78. };
  79. wx.request({
  80. url: app.globalData.contentpath + 'api/train/attendance/attendancecreate',
  81. header: {//请求头
  82. "Content-Type": "application/json"
  83. },
  84. data: newdata,
  85. method: 'POST',
  86. dataType: 'json',
  87. success: function (res) {
  88. //res.data相当于ajax里面的data,为后台返回的数据
  89. //如果在sucess直接写this就变成了wx.request()的this了
  90. //必须为getdata函数的this,不然无法重置调用函数
  91. if (res.data.errcode == 0) {
  92. that.addsuc();
  93. } else {
  94. console.log('签到失败' + res.data.errmsg);
  95. that.addmsg({ errMsg: res.data.errmsg });
  96. }
  97. },
  98. fail: function (err) {
  99. console.log('签到失败' + err);
  100. that.addmsg(err);
  101. },//请求失败
  102. complete: function () {
  103. wx.hideLoading()
  104. }//请求完成后执行的函数
  105. })
  106. // }
  107. }
  108. });
  109. }
  110. })
  111. })
  112. },
  113. fail(res) {
  114. console.log('function in fail')
  115. console.log(res);
  116. that.addmsg(res);
  117. }
  118. })
  119. }
  120. })