app.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // app.js
  2. import {
  3. loginLog,
  4. userLogin,
  5. getMyInfo,
  6. bindDevice
  7. } from '~/api/user'
  8. import {
  9. createStoreBindings
  10. } from 'mobx-miniprogram-bindings'
  11. import {
  12. store
  13. } from '~/store/index'
  14. let storeBindings
  15. App({
  16. async onLaunch() {
  17. this.updateApplet()
  18. this.checkIsIos()
  19. this.getNavbarInfo()
  20. await loginLog()
  21. },
  22. async onShow(options) {
  23. if (!this.storeBindings) {
  24. this.storeBindings = createStoreBindings(this, {
  25. store,
  26. actions: ['setUser']
  27. })
  28. }
  29. let shareUid = options.query.uid || ''
  30. let uid = wx.getStorageSync('uid')
  31. if (uid) {
  32. let userInfo = await getMyInfo()
  33. this.setUser(userInfo.user)
  34. this.deviceLogin()
  35. if (getApp().callBack) {
  36. getApp().callBack();
  37. }
  38. } else {
  39. this.login(shareUid)
  40. }
  41. },
  42. login(shareUid) {
  43. wx.login({
  44. success: async (res) => {
  45. if (res.code) {
  46. // 获取openid
  47. let data = {
  48. code: res.code,
  49. shareUid
  50. }
  51. let userRes = await userLogin(data)
  52. this.setUser(userRes.data)
  53. wx.setStorageSync('uid', userRes.data.uid)
  54. wx.setStorageSync('user', userRes.data)
  55. this.globalData.userInfo = userRes.data
  56. this.deviceLogin()
  57. if (getApp().callBack) {
  58. getApp().callBack(userRes);
  59. }
  60. }
  61. }
  62. })
  63. },
  64. checkIsIos: function () {
  65. wx.getSystemInfo({
  66. success: (res) => {
  67. // 设备显示区域的宽度,单位px
  68. this.globalData.windowWidth = res.windowWidth
  69. this.globalData.windowHeight = res.windowHeight
  70. if (res.system.search('iOS') != -1) {
  71. this.globalData.isIOS = true
  72. }
  73. let {
  74. scene
  75. } = wx.getLaunchOptionsSync()
  76. // 1023 安卓系统桌面图标,1104微信聊天主界面下拉,「我的小程序」栏(基础库2.2.4-2.29.0版本废弃,2.29.1版本起生效)
  77. if (scene != 1023) {
  78. let preTime = wx.getStorageSync('preDesktopTime')
  79. let flag = !preTime ? true : (new Date() - preTime) / 43200000 > 1 ? true : false
  80. if (flag || !preTime) {
  81. this.globalData.desktopTips = true
  82. wx.setStorage({
  83. key: "preDesktopTime",
  84. data: new Date()
  85. })
  86. }
  87. }
  88. }
  89. })
  90. },
  91. // 音箱端登录
  92. deviceLogin() {
  93. let {
  94. scene,
  95. query
  96. } = wx.getLaunchOptionsSync()
  97. let types = [1047, 1048]
  98. var gradeObj = Object.keys({
  99. "PRESCHOOL": "学前班",
  100. "PRIMARY_FIRST_GRADE": "一年级",
  101. "PRIMARY_SECOND_GRADE": "二年级",
  102. "PRIMARY_THREE_GRADE": "三年级",
  103. "PRIMARY_SENIOR_GRADE": "四年级",
  104. "PRIMARY_FIVE_GRADE": "五年级",
  105. "PRIMARY_SIX_GRADE": "六年级",
  106. })
  107. // 1047 扫描小程序码 1048长按图片识别小程序码
  108. if (query.scene && types.includes(scene)) {
  109. let params = decodeURIComponent(query.scene).split('&')
  110. console.log('音箱端参数', params, gradeObj[params[2]]);
  111. bindDevice({
  112. deviceCode: params[0],
  113. channelCode: params[1],
  114. grade: gradeObj[params[2]],
  115. uid: wx.getStorageSync('uid')
  116. }).then(res => {
  117. console.log(res);
  118. })
  119. }
  120. },
  121. getNavbarInfo() {
  122. // 获取系统信息
  123. const systemInfo = wx.getSystemInfoSync();
  124. // 胶囊按钮位置信息
  125. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  126. // 导航栏高度 = 状态栏高度 + 44
  127. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  128. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  129. this.globalData.menuTop = menuButtonInfo.top;
  130. this.globalData.menuHeight = menuButtonInfo.height;
  131. },
  132. updateApplet() {
  133. // 获取小程序更新机制兼容
  134. if (wx.canIUse('getUpdateManager')) {
  135. const updateManager = wx.getUpdateManager()
  136. updateManager.onCheckForUpdate(function (res) {
  137. // 请求完新版本信息的回调
  138. if (res.hasUpdate) {
  139. updateManager.onUpdateReady(function () {
  140. wx.showModal({
  141. title: '更新提示',
  142. content: '新版本已经准备好,是否重启应用?',
  143. success: function (res) {
  144. if (res.confirm) {
  145. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  146. updateManager.applyUpdate()
  147. }
  148. }
  149. })
  150. })
  151. updateManager.onUpdateFailed(function () {
  152. // 新的版本下载失败
  153. wx.showModal({
  154. title: '已经有新版本了哟~',
  155. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  156. })
  157. })
  158. }
  159. })
  160. }
  161. },
  162. globalData: {
  163. userInfo: null,
  164. isIOS: false, // 判断设备是否为苹果
  165. desktopTips: false,
  166. navBarHeight: 0, // 导航栏高度
  167. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  168. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  169. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  170. windowWidth: 0,
  171. windowHeight: 0
  172. }
  173. })