app.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. let shareUid = options.query.uid
  19. this.login(shareUid)
  20. },
  21. login(shareUid) {
  22. this.storeBindings = createStoreBindings(this, {
  23. store,
  24. actions: ['setUser']
  25. })
  26. wx.login({
  27. success: async (res) => {
  28. if (res.code) {
  29. // 获取openid
  30. let data = {
  31. code: res.code,
  32. shareUid
  33. }
  34. let userRes = await userLogin(data)
  35. this.setUser(userRes.data)
  36. wx.setStorageSync('uid', userRes.data.uid)
  37. wx.setStorageSync('user', userRes.data)
  38. this.globalData.userInfo = userRes.data
  39. if (getApp().callBack) {
  40. getApp().callBack(userRes);
  41. }
  42. }
  43. }
  44. })
  45. },
  46. checkIsIos: function () {
  47. wx.getSystemInfo({
  48. success: (res) => {
  49. if (res.system.search('iOS') != -1) {
  50. this.globalData.isIOS = true
  51. }
  52. }
  53. })
  54. },
  55. getNavbarInfo() {
  56. // 获取系统信息
  57. const systemInfo = wx.getSystemInfoSync();
  58. // 胶囊按钮位置信息
  59. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  60. // 导航栏高度 = 状态栏高度 + 44
  61. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  62. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  63. this.globalData.menuTop = menuButtonInfo.top;
  64. this.globalData.menuHeight = menuButtonInfo.height;
  65. },
  66. globalData: {
  67. userInfo: null,
  68. isIOS: false, // 判断设备是否为苹果
  69. navBarHeight: 0, // 导航栏高度
  70. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  71. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  72. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  73. }
  74. })