app.js 7.0 KB

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