app.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // app.js
  2. import {
  3. userLogin
  4. } from '~/api/user'
  5. import {
  6. createStoreBindings
  7. } from 'mobx-miniprogram-bindings'
  8. import {
  9. store
  10. } from '~/store/index'
  11. let storeBindings
  12. App({
  13. onLaunch() {
  14. this.checkIsIos()
  15. this.getNavbarInfo()
  16. },
  17. async onShow(options) {
  18. this.storeBindings = createStoreBindings(this, {
  19. store,
  20. actions: ['setUser']
  21. })
  22. let shareUid = options.query.uid
  23. let uid = wx.getStorageSync('uid')
  24. let userInfo = wx.getStorageSync('user')
  25. if (uid && userInfo) {
  26. this.setUser(userInfo)
  27. } else {
  28. this.login(shareUid)
  29. }
  30. },
  31. login(shareUid) {
  32. wx.login({
  33. success: async (res) => {
  34. if (res.code) {
  35. // 获取openid
  36. let data = {
  37. code: res.code,
  38. shareUid
  39. }
  40. let userRes = await userLogin(data)
  41. this.setUser(userRes.data)
  42. wx.setStorageSync('uid', userRes.data.uid)
  43. wx.setStorageSync('user', userRes.data)
  44. this.globalData.userInfo = userRes.data
  45. if (getApp().callBack) {
  46. getApp().callBack(userRes);
  47. }
  48. }
  49. }
  50. })
  51. },
  52. checkIsIos: function () {
  53. wx.getSystemInfo({
  54. success: (res) => {
  55. if (res.system.search('iOS') != -1) {
  56. this.globalData.isIOS = true
  57. }
  58. }
  59. })
  60. },
  61. getNavbarInfo() {
  62. // 获取系统信息
  63. const systemInfo = wx.getSystemInfoSync();
  64. // 胶囊按钮位置信息
  65. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  66. // 导航栏高度 = 状态栏高度 + 44
  67. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  68. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  69. this.globalData.menuTop = menuButtonInfo.top;
  70. this.globalData.menuHeight = menuButtonInfo.height;
  71. },
  72. globalData: {
  73. userInfo: null,
  74. isIOS: false, // 判断设备是否为苹果
  75. navBarHeight: 0, // 导航栏高度
  76. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  77. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  78. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  79. }
  80. })