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.

167 lines
4.6 KiB

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