app.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const { getUserInfo, getSystemConfig } = 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.fetchSystemConfig()
  18. that.fetchUserData()
  19. checkUpdateVersion()
  20. },
  21. globalData: {
  22. wxMiniversion: 'v0.0.0',
  23. userInfo: {},
  24. navBarHeight: 0, // 导航栏高度
  25. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  26. menuBottom: 0, // 胶囊距底部间距(保持底部间距一致)
  27. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  28. tabBarList: [
  29. {
  30. 'pagePath': 'pages/home/home',
  31. 'text': '首页',
  32. 'iconPath': 'image/tabBar/home_0@2x.png',
  33. 'selectedIconPath': 'image/tabBar/home_1@2x.png'
  34. },
  35. {
  36. 'pagePath': 'pages/partner/partner',
  37. 'text': '合作社',
  38. 'iconPath': 'image/tabBar/partner_0@2x.png',
  39. 'selectedIconPath': 'image/tabBar/partner_1@2x.png'
  40. },
  41. {
  42. 'pagePath': 'pages/mine/mine',
  43. 'text': '我的',
  44. 'iconPath': 'image/tabBar/mine_0@2x.png',
  45. 'selectedIconPath': 'image/tabBar/mine_1@2x.png'
  46. }
  47. ],
  48. asyncTabBarList: [
  49. {
  50. 'pagePath': 'pages/news/news',
  51. 'text': '农事天地',
  52. 'iconPath': 'image/tabBar/news_0@2x.png',
  53. 'selectedIconPath': 'image/tabBar/news_1@2x.png'
  54. }
  55. ],
  56. objSystemConfig: {}
  57. },
  58. async fetchUserData() {
  59. try {
  60. const { status, data } = await getUserInfo()
  61. if (status && Object.prototype.toString.call(data) === '[object Object]' && isMobile(data.user_phone)) {
  62. const temp = {
  63. ...data
  64. }
  65. // 设置默认昵称
  66. if (!temp.user_nickname) {
  67. temp.user_nickname = 'bbs' + temp.user_phone.replace(/(\d{7})(.*)/, '$2')
  68. }
  69. if (temp.user_address && temp.user_address.indexOf('/') > -1) {
  70. temp.user_address = temp.user_address.split('/')
  71. }
  72. if (temp.shop_status === 1) {
  73. this.hasPermission()
  74. }
  75. this.globalData.userInfo = temp
  76. }
  77. } catch (e) {}
  78. if (this.fetchUserDataCallback) {
  79. this.fetchUserDataCallback()
  80. this.fetchUserDataCallback = null
  81. }
  82. },
  83. // 根据权限配置tabbar
  84. hasPermission() {
  85. const tabBarList = this.globalData.tabBarList
  86. if (tabBarList.findIndex(item => item.text === '农事天地') === -1) {
  87. tabBarList.splice(2, 0, ...this.globalData.asyncTabBarList)
  88. if (this.addTabBarList) {
  89. this.addTabBarList(tabBarList)
  90. }
  91. }
  92. },
  93. async fetchSystemConfig() {
  94. try {
  95. const { status, data } = await getSystemConfig()
  96. if (status && Object.prototype.toString.call(data) === '[object Object]') {
  97. this.globalData.objSystemConfig = data
  98. }
  99. } catch (err) {}
  100. if (this.fetchSystemConfigCallback) {
  101. this.fetchSystemConfigCallback()
  102. this.fetchSystemConfigCallback = null
  103. }
  104. }
  105. })