app.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. }
  63. })
  64. },
  65. getNavbarInfo() {
  66. // 获取系统信息
  67. const systemInfo = wx.getSystemInfoSync();
  68. // 胶囊按钮位置信息
  69. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  70. // 导航栏高度 = 状态栏高度 + 44
  71. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  72. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  73. this.globalData.menuTop = menuButtonInfo.top;
  74. this.globalData.menuHeight = menuButtonInfo.height;
  75. },
  76. globalData: {
  77. userInfo: null,
  78. isIOS: false, // 判断设备是否为苹果
  79. navBarHeight: 0, // 导航栏高度
  80. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  81. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  82. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  83. }
  84. })