app.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 + menuButtonInfo.height + systemInfo.statusBarHeight
  13. that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right
  14. that.globalData.menuBottom = menuButtonInfo.top - systemInfo.statusBarHeight
  15. that.globalData.menuHeight = menuButtonInfo.height
  16. that.fetchSystemConfig()
  17. that.fetchUserData()
  18. checkUpdateVersion()
  19. wx.removeStorage({ key: this.globalData.businessGoodsEditsStorageKey })
  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. businessGoodsEditsStorageKey: 'businessGoodsEdit'
  73. },
  74. async fetchUserData() {
  75. try {
  76. const { status, data } = await getUserInfo()
  77. if (status && Object.prototype.toString.call(data) === '[object Object]' && isMobile(data.user_phone)) {
  78. const temp = {
  79. ...data
  80. }
  81. // 设置默认昵称
  82. if (!temp.user_nickname) {
  83. temp.user_nickname = 'SNNY' + temp.user_phone.replace(/(\d{7})(.*)/, '$2')
  84. }
  85. if (temp.user_address && temp.user_address.indexOf('/') > -1) {
  86. temp.user_address = temp.user_address.split('/')
  87. }
  88. if (temp.shop_status === 1) {
  89. this.hasPermission()
  90. }
  91. this.globalData.userInfo = temp
  92. }
  93. } catch (e) {}
  94. if (this.fetchUserDataCallback) {
  95. this.fetchUserDataCallback()
  96. this.fetchUserDataCallback = null
  97. }
  98. },
  99. // 根据权限配置tabbar
  100. hasPermission() {
  101. const tabBarList = this.globalData.tabBarList
  102. if (tabBarList.findIndex(item => item.text === '农事天地') === -1) {
  103. tabBarList.splice(2, 0, ...this.globalData.asyncTabBarList)
  104. if (this.addTabBarList) {
  105. this.addTabBarList(tabBarList)
  106. }
  107. }
  108. },
  109. async fetchSystemConfig() {
  110. try {
  111. const { status, data } = await getSystemConfig()
  112. if (status && Object.prototype.toString.call(data) === '[object Object]') {
  113. this.globalData.objSystemConfig = data
  114. }
  115. } catch (err) {}
  116. if (this.fetchSystemConfigCallback) {
  117. this.fetchSystemConfigCallback()
  118. this.fetchSystemConfigCallback = null
  119. }
  120. }
  121. })