app.js 4.0 KB

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