app.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const { getUserInfo } = require('./api/common')
  2. const { isMobile } = require('./utils/validate')
  3. const { checkUpdateVersion } = require('./utils/util')
  4. App({
  5. onLaunch () {
  6. const that = this
  7. // 获取系统信息
  8. const systemInfo = wx.getSystemInfoSync()
  9. // 胶囊按钮位置信息
  10. const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
  11. // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度
  12. that.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 +
  13. menuButtonInfo.height + systemInfo.statusBarHeight
  14. that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right
  15. that.globalData.menuBottom = menuButtonInfo.top - systemInfo.statusBarHeight
  16. that.globalData.menuHeight = menuButtonInfo.height
  17. that.fetchUserData()
  18. checkUpdateVersion()
  19. },
  20. globalData: {
  21. wxMiniversion: 'v0.0.0',
  22. userInfo: {},
  23. navBarHeight: 0, // 导航栏高度
  24. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  25. menuBottom: 0, // 胶囊距底部间距(保持底部间距一致)
  26. menuHeight: 0 // 胶囊高度(自定义内容可与胶囊高度保证一致)
  27. },
  28. async fetchUserData () {
  29. try {
  30. const { status, data } = await getUserInfo()
  31. if (status && Object.prototype.toString.call(data) === '[object Object]' && isMobile(data.user_phone)) {
  32. const temp = {
  33. ...data
  34. }
  35. // 设置默认昵称
  36. if (!temp.user_nickname) {
  37. temp.user_nickname = 'bbs' + temp.user_phone.replace(/(\d{7})(.*)/, '$2')
  38. }
  39. if (temp.user_address && temp.user_address.indexOf('/') > -1) {
  40. temp.user_address = temp.user_address.split('/')
  41. }
  42. this.globalData.userInfo = temp
  43. }
  44. } catch (e) {}
  45. }
  46. })