app.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. let res = wx.getLaunchOptionsSync()
  18. console.log(res, 'res');
  19. },
  20. async onShow(options) {
  21. this.storeBindings = createStoreBindings(this, {
  22. store,
  23. actions: ['setUser']
  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. }
  65. })
  66. },
  67. getNavbarInfo() {
  68. // 获取系统信息
  69. const systemInfo = wx.getSystemInfoSync();
  70. // 胶囊按钮位置信息
  71. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  72. // 导航栏高度 = 状态栏高度 + 44
  73. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  74. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  75. this.globalData.menuTop = menuButtonInfo.top;
  76. this.globalData.menuHeight = menuButtonInfo.height;
  77. },
  78. globalData: {
  79. userInfo: null,
  80. isIOS: false, // 判断设备是否为苹果
  81. navBarHeight: 0, // 导航栏高度
  82. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  83. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  84. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  85. }
  86. })