app.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. messageDetailStorageKey: 'messageDetail',
  58. arrShopType: [
  59. {
  60. name: '大户',
  61. value: '0'
  62. },
  63. {
  64. name: '合作社',
  65. value: '1'
  66. },
  67. {
  68. name: '家庭农场',
  69. value: '2'
  70. }
  71. ]
  72. },
  73. async fetchUserData() {
  74. try {
  75. const { status, data } = await getUserInfo()
  76. if (status && Object.prototype.toString.call(data) === '[object Object]' && isMobile(data.user_phone)) {
  77. const temp = {
  78. ...data
  79. }
  80. // 设置默认昵称
  81. if (!temp.user_nickname) {
  82. temp.user_nickname = 'SNNY' + temp.user_phone.replace(/(\d{7})(.*)/, '$2')
  83. }
  84. if (temp.user_address && temp.user_address.indexOf('/') > -1) {
  85. temp.user_address = temp.user_address.split('/')
  86. }
  87. if (temp.shop_status === 1) {
  88. this.hasPermission()
  89. }
  90. this.globalData.userInfo = temp
  91. }
  92. } catch (e) {}
  93. if (this.fetchUserDataCallback) {
  94. this.fetchUserDataCallback()
  95. this.fetchUserDataCallback = null
  96. }
  97. },
  98. // 根据权限配置tabbar
  99. hasPermission() {
  100. const tabBarList = this.globalData.tabBarList
  101. if (tabBarList.findIndex(item => item.text === '农事天地') === -1) {
  102. tabBarList.splice(2, 0, ...this.globalData.asyncTabBarList)
  103. if (this.addTabBarList) {
  104. this.addTabBarList(tabBarList)
  105. }
  106. }
  107. },
  108. async fetchSystemConfig() {
  109. try {
  110. const { status, data } = await getSystemConfig()
  111. if (status && Object.prototype.toString.call(data) === '[object Object]') {
  112. this.globalData.objSystemConfig = data
  113. }
  114. } catch (err) {}
  115. if (this.fetchSystemConfigCallback) {
  116. this.fetchSystemConfigCallback()
  117. this.fetchSystemConfigCallback = null
  118. }
  119. }
  120. })