You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.7 KiB

1 year ago
1 year ago
1 year ago
  1. // app.js
  2. const app=getApp()
  3. const EventEmitter2 = require("./components/eventemitter2/index.js").EventEmitter2;
  4. const emitter = new EventEmitter2();
  5. const WXAPI = require('./utils/request')
  6. const plugin = requirePlugin("myPlugin")
  7. App({
  8. onLaunch() {
  9. this.globalData.sysinfo = wx.getSystemInfoSync()
  10. // wx885b65d99da00084
  11. // wx20de9fe5943934a1 公寓 '458f9df0-3405-66a9-1cdb-49d5f4197e6a'
  12. plugin.init('wx885b65d99da00084', '4f24f9d7-3cae-4f3b-b48C-5429544f617b')
  13. .then(function(res){
  14. console.log('res', res)
  15. if(res.code != 200){
  16. wx.showToast({
  17. title: res.data.msg,
  18. })
  19. }
  20. })
  21. wx.login({
  22. success (res) {
  23. if (res.code) {
  24. let data={
  25. code:res.code
  26. }
  27. WXAPI.getOpenId(data).then(res=>{
  28. console.log(res)
  29. wx.setStorageSync('openid', res.openid)
  30. })
  31. } else {
  32. console.log('登录失败!' + res.errMsg)
  33. }
  34. }
  35. })
  36. // 展示本地存储能力
  37. var logs = wx.getStorageSync('token') || ''
  38. var userRole = wx.getStorageSync('userRole') || ''
  39. var signature= wx.getStorageSync('signature') || ''
  40. // 登录
  41. if(logs!=''&&userRole==0){
  42. this.globalData.token=logs.access_token
  43. setTimeout(()=>{
  44. wx.switchTab({
  45. url: '/pages/index/index',
  46. })
  47. },100)
  48. }
  49. if(signature&&userRole==1){
  50. wx.navigateTo({
  51. url: '/pages/indexUser/indexUser',
  52. })
  53. }
  54. },
  55. getDeviceByDevName(devName) {
  56. return this.globalData.foundBleDevs[devName]
  57. },
  58. addDeviceWithDevName(devName, dev) {
  59. this.globalData.foundBleDevs[devName] = dev
  60. },
  61. connectTo: function(options) {
  62. console.debug('connectTo', options)
  63. if (typeof options !== 'object') {
  64. var message = '请求传参应为 object 类型,但实际传了 ' + (typeof options) + ' 类型';
  65. throw new RequestError('invalid params', message);
  66. }
  67. var success = options.success || noop;
  68. var fail = options.fail || noop;
  69. var complete = options.complete || noop;
  70. // 成功回调
  71. var callSuccess = function() {
  72. success.apply(null, arguments);
  73. complete.apply(null, arguments);
  74. };
  75. // 失败回调
  76. var callFail = function(error) {
  77. fail.call(null, error);
  78. complete.call(null, error);
  79. };
  80. var that = this
  81. if (options.devName == undefined) {
  82. throw new RequestError('invalid params', 'options.devName');
  83. }
  84. var bleDev = that.globalData.foundBleDevs[options.devName]
  85. console.log('bleDev', bleDev)
  86. if(bleDev == undefined){
  87. throw new RequestError('dev not found', options.devName);
  88. }
  89. wx.showLoading({
  90. mask: true,
  91. title: '连接中...',
  92. })
  93. wx.createBLEConnection({
  94. deviceId: bleDev.deviceId,
  95. timeout: 8000,
  96. success: function(res) {
  97. console.log('createBLEConnection success', res)
  98. wx.getBLEDeviceServices({
  99. deviceId: bleDev.deviceId,
  100. success: function(res) {
  101. wx.getBLEDeviceCharacteristics({
  102. deviceId: bleDev.deviceId,
  103. serviceId: config.uuid.serviceUuid.toUpperCase(),
  104. success: function(res) {
  105. wx.notifyBLECharacteristicValueChange({
  106. deviceId: bleDev.deviceId,
  107. serviceId: config.uuid.serviceUuid.toUpperCase(),
  108. characteristicId: config.uuid.notifyUuid.toUpperCase(),
  109. state: true,
  110. success: function(res) {
  111. callSuccess()
  112. },
  113. fail: function(res) {
  114. callFail(res)
  115. }
  116. })
  117. },
  118. fail: function(res) {
  119. callFail(res)
  120. }
  121. })
  122. },
  123. fail: function(res) {
  124. callFail(res)
  125. }
  126. })
  127. },
  128. fail: function(res) {
  129. console.log('createBLEConnection fail', res)
  130. wx.hideLoading()
  131. if (res.errMsg.indexOf('already connect') > 0) {
  132. callSuccess()
  133. } else {
  134. callFail(res)
  135. }
  136. }
  137. })
  138. },
  139. globalData: {
  140. userInfo: null,
  141. token:'',
  142. baseurl:'https://schoolsafety.zhiweisz.cn/',
  143. emitter:emitter,//全局订阅函数
  144. ble: "", //全局蓝牙实例
  145. blueStatus: false,
  146. userInfo: null,
  147. fingerprintId: -1,//指纹ID
  148. backdata: -1,//返回状态
  149. openlock:-1,//蓝牙开锁返回状态
  150. stop29:false,
  151. zgBlueStatus:false,
  152. blueDeviceId:'',
  153. deviceNum:'',
  154. tenantIdEnter:wx.getStorageSync('tenantId') || '100',
  155. sysinfo: null,
  156. foundBleDevs: {},
  157. isSearch:false,
  158. isConnected:false,
  159. isInitReady:false
  160. }
  161. })