app.js 6.9 KB

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