shell.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import appSettings from '@/core/config/app-settings'
  2. import { buildRestPath } from '@/core/config/api-paths'
  3. import { get } from '@/core/http/http-client'
  4. import { STORAGE } from '@/core/constants/storage-keys'
  5. import {
  6. fetchAppConfig,
  7. readCachedAppConfig,
  8. persistAppConfig
  9. } from '@/features/shell/app-config-gateway'
  10. const pxToRpx = (value) =>
  11. String(value).replace(/\d*\.?\d+px/g, (m) => `${parseFloat(m) * 2}rpx`)
  12. function transformModuleStyles(modules) {
  13. const items = modules?.homeModules?.item
  14. if (!Array.isArray(items)) return
  15. items.forEach((row) => {
  16. ;['style', 'params'].forEach((bucket) => {
  17. const target = row[bucket]
  18. if (!target) return
  19. Object.keys(target).forEach((key) => {
  20. if (target[key]) target[key] = pxToRpx(target[key])
  21. })
  22. })
  23. })
  24. }
  25. export default {
  26. namespaced: true,
  27. state: {
  28. appStyle: {},
  29. appConfig: {},
  30. appUrl: { api: appSettings.appurl, oss: appSettings.cdnurl },
  31. appInfo: { adVersion: '0', serviceVersion: '0' },
  32. adData: { openAdverts: {}, pageAdverts: [], categoryAdverts: [], firstAdverts: [], otherAdverts: [] },
  33. modulesData: { homeModules: {}, categoryModules: [], searchModules: [] },
  34. config: { screenshot: false, position: true, map: true },
  35. version: appSettings.versionName,
  36. versionCode: appSettings.versionCode
  37. },
  38. mutations: {
  39. patchConfig(state, payload) {
  40. Object.keys(payload || {}).forEach((key) => {
  41. if (key in state.config) state.config[key] = payload[key]
  42. })
  43. },
  44. hydrateAppConfigFromCache(state) {
  45. const cached = readCachedAppConfig()
  46. if (cached) state.appConfig = cached
  47. },
  48. applyAppConfig(state, payload) {
  49. if (!payload || typeof payload !== 'object') return
  50. state.appConfig = { ...state.appConfig, ...payload }
  51. persistAppConfig(state.appConfig)
  52. },
  53. applyServices(state, payload) {
  54. if (payload.appConfig && typeof payload.appConfig === 'object') {
  55. state.appConfig = { ...state.appConfig, ...payload.appConfig }
  56. persistAppConfig(state.appConfig)
  57. }
  58. state.appStyle = payload.appStyle
  59. state.modulesData = payload.modulesData
  60. },
  61. applyAds(state, payload) {
  62. state.adData = payload
  63. }
  64. },
  65. actions: {
  66. async bootstrap({ commit, dispatch, rootState }) {
  67. commit('hydrateAppConfigFromCache')
  68. const cachedUser = uni.getStorageSync(STORAGE.user)
  69. try {
  70. const { msg } = await get(buildRestPath('token/check'))
  71. if (msg === 'ok') {
  72. if (cachedUser) Object.assign(rootState.user, cachedUser)
  73. rootState.user.isLogin = true
  74. } else {
  75. dispatch('clearSession', null, { root: true })
  76. }
  77. } catch (_) {
  78. // #ifdef APP-PLUS
  79. plus.navigator.closeSplashscreen()
  80. // #endif
  81. }
  82. await Promise.all([
  83. dispatch('fetchAds'),
  84. dispatch('fetchServices'),
  85. dispatch('fetchAppConfig')
  86. ])
  87. },
  88. async fetchAppConfig({ state, commit }) {
  89. try {
  90. const cfg = await fetchAppConfig({
  91. lang: undefined,
  92. version: state.version
  93. })
  94. if (cfg) commit('applyAppConfig', cfg)
  95. } catch (e) {
  96. console.warn('[shell] fetchAppConfig', e)
  97. }
  98. },
  99. async fetchServices({ state, commit }) {
  100. try {
  101. const { data } = await get(buildRestPath('common/init'), { version: state.version })
  102. transformModuleStyles(data.modulesData)
  103. commit('applyServices', data)
  104. } catch (e) {
  105. console.warn('[shell] fetchServices', e)
  106. }
  107. },
  108. async fetchAds({ state, commit }) {
  109. try {
  110. const { data } = await get(buildRestPath('common/adverts'), { version: state.version })
  111. if (data?.syspopup) {
  112. uni.showModal({ content: data.syspopup })
  113. return
  114. }
  115. // #ifdef APP-PLUS
  116. setTimeout(() => plus.navigator.closeSplashscreen(), 100)
  117. // #endif
  118. commit('applyAds', data.data || data)
  119. } catch (e) {
  120. console.warn('[shell] fetchAds', e)
  121. }
  122. }
  123. }
  124. }