app.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. console.log(shareUid, 'shareUid');
  31. let uid = wx.getStorageSync('uid')
  32. if (uid) {
  33. let userInfo = await getMyInfo()
  34. this.setUser(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.getLaunchOptionsSync()
  98. let types = [1047, 1048]
  99. var gradeObj = Object.keys({
  100. "PRESCHOOL": "学前班",
  101. "PRIMARY_FIRST_GRADE": "一年级",
  102. "PRIMARY_SECOND_GRADE": "二年级",
  103. "PRIMARY_THREE_GRADE": "三年级",
  104. "PRIMARY_SENIOR_GRADE": "四年级",
  105. "PRIMARY_FIVE_GRADE": "五年级",
  106. "PRIMARY_SIX_GRADE": "六年级",
  107. })
  108. // 1047 扫描小程序码 1048长按图片识别小程序码
  109. if (query.scene && types.includes(scene)) {
  110. let params = decodeURIComponent(query.scene).split('&')
  111. console.log('eee', params, gradeObj[params[2]]);
  112. bindDevice({
  113. deviceCode: params[0],
  114. channelCode: params[1],
  115. grade: gradeObj[params[2]],
  116. uid: wx.getStorageSync('uid')
  117. }).then(res => {
  118. console.log(res);
  119. })
  120. }
  121. },
  122. getNavbarInfo() {
  123. // 获取系统信息
  124. const systemInfo = wx.getSystemInfoSync();
  125. // 胶囊按钮位置信息
  126. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  127. // 导航栏高度 = 状态栏高度 + 44
  128. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  129. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  130. this.globalData.menuTop = menuButtonInfo.top;
  131. this.globalData.menuHeight = menuButtonInfo.height;
  132. },
  133. updateApplet() {
  134. // 获取小程序更新机制兼容
  135. if (wx.canIUse('getUpdateManager')) {
  136. const updateManager = wx.getUpdateManager()
  137. updateManager.onCheckForUpdate(function (res) {
  138. // 请求完新版本信息的回调
  139. if (res.hasUpdate) {
  140. updateManager.onUpdateReady(function () {
  141. wx.showModal({
  142. title: '更新提示',
  143. content: '新版本已经准备好,是否重启应用?',
  144. success: function (res) {
  145. if (res.confirm) {
  146. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  147. updateManager.applyUpdate()
  148. }
  149. }
  150. })
  151. })
  152. updateManager.onUpdateFailed(function () {
  153. // 新的版本下载失败
  154. wx.showModal({
  155. title: '已经有新版本了哟~',
  156. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  157. })
  158. })
  159. }
  160. })
  161. }
  162. },
  163. globalData: {
  164. userInfo: null,
  165. isIOS: false, // 判断设备是否为苹果
  166. desktopTips: false,
  167. navBarHeight: 0, // 导航栏高度
  168. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  169. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  170. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  171. windowWidth: 0,
  172. windowHeight: 0
  173. }
  174. })