app.js 4.2 KB

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