app.js 2.5 KB

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