app.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. let {
  63. scene
  64. } = wx.getLaunchOptionsSync()
  65. // 1023 安卓系统桌面图标,1104微信聊天主界面下拉,「我的小程序」栏(基础库2.2.4-2.29.0版本废弃,2.29.1版本起生效)
  66. this.globalData.desktopTips = this.globalData.isIOS ? scene != 1104 : scene != 1023
  67. }
  68. })
  69. },
  70. getNavbarInfo() {
  71. // 获取系统信息
  72. const systemInfo = wx.getSystemInfoSync();
  73. // 胶囊按钮位置信息
  74. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  75. // 导航栏高度 = 状态栏高度 + 44
  76. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  77. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  78. this.globalData.menuTop = menuButtonInfo.top;
  79. this.globalData.menuHeight = menuButtonInfo.height;
  80. },
  81. globalData: {
  82. userInfo: null,
  83. isIOS: false, // 判断设备是否为苹果
  84. desktopTips: false,
  85. navBarHeight: 0, // 导航栏高度
  86. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  87. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  88. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  89. }
  90. })