app.js 5.2 KB

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