app.js 6.4 KB

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