app.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. if (res.system.search('iOS') != -1) {
  65. this.globalData.isIOS = true
  66. }
  67. let {
  68. scene
  69. } = wx.getLaunchOptionsSync()
  70. // 1023 安卓系统桌面图标,1104微信聊天主界面下拉,「我的小程序」栏(基础库2.2.4-2.29.0版本废弃,2.29.1版本起生效)
  71. if (this.globalData.isIOS ? scene != 1104 : scene != 1023) {
  72. let preTime = wx.getStorageSync('preDesktopTime')
  73. let flag = !preTime ? true : (new Date() - preTime) / 43200000 > 1 ? true : false
  74. if (flag || !preTime) {
  75. this.globalData.desktopTips = true
  76. wx.setStorage({
  77. key: "preDesktopTime",
  78. data: new Date()
  79. })
  80. }
  81. }
  82. }
  83. })
  84. },
  85. getNavbarInfo() {
  86. // 获取系统信息
  87. const systemInfo = wx.getSystemInfoSync();
  88. // 胶囊按钮位置信息
  89. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  90. // 导航栏高度 = 状态栏高度 + 44
  91. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  92. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  93. this.globalData.menuTop = menuButtonInfo.top;
  94. this.globalData.menuHeight = menuButtonInfo.height;
  95. },
  96. updateApplet() {
  97. // 获取小程序更新机制兼容
  98. if (wx.canIUse('getUpdateManager')) {
  99. const updateManager = wx.getUpdateManager()
  100. updateManager.onCheckForUpdate(function (res) {
  101. // 请求完新版本信息的回调
  102. if (res.hasUpdate) {
  103. updateManager.onUpdateReady(function () {
  104. wx.showModal({
  105. title: '更新提示',
  106. content: '新版本已经准备好,是否重启应用?',
  107. success: function (res) {
  108. if (res.confirm) {
  109. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  110. updateManager.applyUpdate()
  111. }
  112. }
  113. })
  114. })
  115. updateManager.onUpdateFailed(function () {
  116. // 新的版本下载失败
  117. wx.showModal({
  118. title: '已经有新版本了哟~',
  119. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  120. })
  121. })
  122. }
  123. })
  124. }
  125. },
  126. globalData: {
  127. userInfo: null,
  128. isIOS: false, // 判断设备是否为苹果
  129. desktopTips: false,
  130. navBarHeight: 0, // 导航栏高度
  131. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  132. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  133. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  134. }
  135. })