app.js 2.9 KB

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