app.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // app.js
  2. const homeApi_empower = "https://aipush.aidsleep.cn";
  3. App({
  4. onLaunch() {
  5. // 在需要隐藏 tabBar 的页面的脚本中调用
  6. wx.hideTabBar({
  7. animation: false, // 是否需要动画效果,默认为 false
  8. success: function () {
  9. console.log('TabBar 已经被隐藏');
  10. },
  11. fail: function (err) {
  12. console.error('隐藏 TabBar 失败', err);
  13. }
  14. });
  15. this.globalData.SystemInfo = wx.getDeviceInfo()
  16. this.data.system = this.globalData.SystemInfo.platform;
  17. // 登录
  18. wx.login({
  19. success: res => {
  20. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  21. wx.request({
  22. url: `${homeApi_empower}/wxssk`,
  23. method: 'POST',
  24. data: {
  25. code: res.code,
  26. },
  27. success: function (loginRes) {
  28. // 处理服务器返回的结果
  29. console.log('登录获取结果:', loginRes.data);
  30. if (loginRes.data) {
  31. wx.setStorageSync("openid", loginRes.data.openid);
  32. wx.setStorageSync("token", loginRes.data.token);
  33. wx.setStorageSync("unionid", loginRes.data.unionid);
  34. // 判断扫码控制权限
  35. wx.request({
  36. url: `${homeApi_empower}/wxstatus`,
  37. data: {
  38. unionid: wx.getStorageSync('unionid'),
  39. openid: wx.getStorageSync('openid'),
  40. token: wx.getStorageSync('token'),
  41. },
  42. method: 'POST',
  43. success: function (intervalRes) {
  44. console.log('intervalRes=' + JSON.stringify(intervalRes));
  45. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  46. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  47. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  48. wx.setStorageSync('hasHotelAuth', true);
  49. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  50. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  51. wx.setStorageSync('hname', intervalRes.data.hname);
  52. wx.setStorageSync('rname', intervalRes.data.rname);
  53. } else {
  54. wx.setStorageSync('hasHotelAuth', false);
  55. }
  56. },
  57. fail: function (error) {
  58. }
  59. });
  60. // 判断今日睡眠报告权限
  61. wx.request({
  62. url: 'https://aipush.aidsleep.cn/srpwithrange',
  63. method: 'POST',
  64. data: {
  65. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  66. udi: wx.getStorageSync('unionid'),
  67. pg: 1,
  68. lm: 1
  69. },
  70. success: (res) => {
  71. var loadData = res.data.data;
  72. if ((Array.isArray(loadData) && loadData.length > 0) || (loadData && !Array.isArray(loadData)) ) {
  73. wx.setStorageSync('hasReportAuth', true);
  74. } else {
  75. wx.setStorageSync('hasReportAuth', false);
  76. }
  77. },
  78. fail: () => {
  79. wx.setStorageSync('hasReportAuth', false);
  80. }
  81. });
  82. }
  83. }
  84. })
  85. }
  86. })
  87. },
  88. onShow() {
  89. wx.hideTabBar()
  90. },
  91. handleStartupParams(options) {
  92. if (options.scene === 1047 || options.scene === 1048) { // 扫码场景
  93. const scene = decodeURIComponent(options.query.scene);
  94. switch (scene) {
  95. case 'page1':
  96. wx.navigateTo({ url: '/pages/page1/page1' });
  97. break;
  98. // 添加更多页面
  99. }
  100. }
  101. },
  102. onHide: function () {
  103. wx.setStorageSync('userName', '');
  104. wx.setStorageSync('isLogin', false);
  105. wx.setStorageSync('deviceList', []);
  106. wx.setStorageSync('doDevices', []);
  107. },
  108. buf2hex: function (buffer) {
  109. return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('')
  110. },
  111. buf2string: function (buffer) {
  112. var arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
  113. var str = ''
  114. for (var i = 0; i < arr.length; i++) {
  115. str += String.fromCharCode(arr[i])
  116. }
  117. return str
  118. },
  119. data: {
  120. service_uuid: "0000FFF0-0000-1000-8000-00805F9B34FB",
  121. characteristic_write_uuid: "0000FFF1-0000-1000-8000-00805F9B34FB",
  122. characteristic_read_uuid: "0000FFF2-0000-1000-8000-00805F9B34FB",
  123. name: "LZ-OTA",
  124. md5Key: "",
  125. system: 'ios',
  126. userName: "",
  127. isLogin: false
  128. },
  129. globalData: {
  130. userInfo: null,
  131. selectedTabIndex: 0
  132. }
  133. })