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.

166 lines
4.6 KiB

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