app.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.checkIsIos()
  16. this.getNavbarInfo()
  17. },
  18. async onShow(options) {
  19. if (!this.storeBindings) {
  20. this.storeBindings = createStoreBindings(this, {
  21. store,
  22. actions: ['setUser']
  23. })
  24. }
  25. let shareUid = options.query.uid || ''
  26. let uid = wx.getStorageSync('uid')
  27. if (uid) {
  28. let userInfo = await getMyInfo()
  29. this.setUser(userInfo.user)
  30. if (getApp().callBack) {
  31. getApp().callBack();
  32. }
  33. } else {
  34. this.login(shareUid)
  35. }
  36. },
  37. login(shareUid) {
  38. wx.login({
  39. success: async (res) => {
  40. if (res.code) {
  41. // 获取openid
  42. let data = {
  43. code: res.code,
  44. shareUid
  45. }
  46. let userRes = await userLogin(data)
  47. this.setUser(userRes.data)
  48. wx.setStorageSync('uid', userRes.data.uid)
  49. wx.setStorageSync('user', userRes.data)
  50. this.globalData.userInfo = userRes.data
  51. if (getApp().callBack) {
  52. getApp().callBack(userRes);
  53. }
  54. }
  55. }
  56. })
  57. },
  58. checkIsIos: function () {
  59. wx.getSystemInfo({
  60. success: (res) => {
  61. if (res.system.search('iOS') != -1) {
  62. this.globalData.isIOS = true
  63. }
  64. let {
  65. scene
  66. } = wx.getLaunchOptionsSync()
  67. // 1023 安卓系统桌面图标,1104微信聊天主界面下拉,「我的小程序」栏(基础库2.2.4-2.29.0版本废弃,2.29.1版本起生效)
  68. if (this.globalData.isIOS ? scene != 1104 : scene != 1023) {
  69. let preTime = wx.getStorageSync('preDesktopTime')
  70. let flag = !preTime ? true : (new Date() - preTime) / 43200000 > 1 ? true : false
  71. if (flag || !preTime) {
  72. this.globalData.desktopTips = true
  73. wx.setStorage({
  74. key: "preDesktopTime",
  75. data: new Date()
  76. })
  77. }
  78. }
  79. }
  80. })
  81. },
  82. getNavbarInfo() {
  83. // 获取系统信息
  84. const systemInfo = wx.getSystemInfoSync();
  85. // 胶囊按钮位置信息
  86. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  87. // 导航栏高度 = 状态栏高度 + 44
  88. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  89. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  90. this.globalData.menuTop = menuButtonInfo.top;
  91. this.globalData.menuHeight = menuButtonInfo.height;
  92. },
  93. globalData: {
  94. userInfo: null,
  95. isIOS: false, // 判断设备是否为苹果
  96. desktopTips: false,
  97. navBarHeight: 0, // 导航栏高度
  98. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  99. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  100. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  101. }
  102. })