app.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. tabBarList: [
  28. {
  29. 'pagePath': 'pages/home/home',
  30. 'text': '首页',
  31. 'iconPath': 'image/tabBar/home_0@2x.png',
  32. 'selectedIconPath': 'image/tabBar/home_1@2x.png'
  33. },
  34. {
  35. 'pagePath': 'pages/partner/partner',
  36. 'text': '合作社',
  37. 'iconPath': 'image/tabBar/partner_0@2x.png',
  38. 'selectedIconPath': 'image/tabBar/partner_1@2x.png'
  39. },
  40. {
  41. 'pagePath': 'pages/mine/mine',
  42. 'text': '我的',
  43. 'iconPath': 'image/tabBar/mine_0@2x.png',
  44. 'selectedIconPath': 'image/tabBar/mine_1@2x.png'
  45. }
  46. ],
  47. asyncTabBarList: [
  48. {
  49. 'pagePath': 'pages/news/news',
  50. 'text': '农事天地',
  51. 'iconPath': 'image/tabBar/news_0@2x.png',
  52. 'selectedIconPath': 'image/tabBar/news_1@2x.png'
  53. }
  54. ]
  55. },
  56. async fetchUserData() {
  57. try {
  58. const { status, data } = await getUserInfo()
  59. if (status && Object.prototype.toString.call(data) === '[object Object]' && isMobile(data.user_phone)) {
  60. const temp = {
  61. ...data
  62. }
  63. // 设置默认昵称
  64. if (!temp.user_nickname) {
  65. temp.user_nickname = 'bbs' + temp.user_phone.replace(/(\d{7})(.*)/, '$2')
  66. }
  67. if (temp.user_address && temp.user_address.indexOf('/') > -1) {
  68. temp.user_address = temp.user_address.split('/')
  69. }
  70. if (temp.shop_status === 1) {
  71. this.hasPermission()
  72. }
  73. this.globalData.userInfo = temp
  74. }
  75. } catch (e) {}
  76. if (this.fetchUserDataCallback) {
  77. this.fetchUserDataCallback()
  78. this.fetchUserDataCallback = null
  79. }
  80. },
  81. // 根据权限配置tabbar
  82. hasPermission() {
  83. const tabBarList = this.globalData.tabBarList
  84. if (tabBarList.findIndex(item => item.text === '农事天地') === -1) {
  85. tabBarList.splice(2, 0, ...this.globalData.asyncTabBarList)
  86. if (this.addTabBarList) {
  87. this.addTabBarList(tabBarList)
  88. }
  89. }
  90. }
  91. })