增加品牌方案管理
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
const brandApi = require('../api/brand')
|
||||
|
||||
const BRAND_CACHE_KEY = 'brand_config_cache'
|
||||
|
||||
const DEFAULT_CONFIG = {
|
||||
brandName: 'Novalon健身房',
|
||||
slogan: '专业健身,从这里开始',
|
||||
primaryColor: '#00E676',
|
||||
primaryColorRgb: '0,230,118',
|
||||
secondaryColor: '#1A1A1A',
|
||||
secondaryColorRgb: '26,26,26',
|
||||
logoUrl: '',
|
||||
backgroundImageUrl: '',
|
||||
updatedAt: ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 品牌方案 Store
|
||||
*
|
||||
* 响应式策略:fetchConfig 更新后通过 uni.$emit('brand:updated', config) 广播,
|
||||
* 页面通过 uni.$on 监听并在 data 中维护副本,确保 UI 自动刷新。
|
||||
*/
|
||||
const brandStore = {
|
||||
_config: null,
|
||||
_loaded: false,
|
||||
|
||||
/** 获取当前品牌配置(优先内存,其次缓存,最后默认值) */
|
||||
get config() {
|
||||
if (this._config) return this._config
|
||||
try {
|
||||
const cached = uni.getStorageSync(BRAND_CACHE_KEY)
|
||||
if (cached && cached.primaryColor) {
|
||||
this._config = cached
|
||||
return cached
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
return { ...DEFAULT_CONFIG }
|
||||
},
|
||||
|
||||
/** 是否已从后端加载完成 */
|
||||
get isLoaded() {
|
||||
return this._loaded
|
||||
},
|
||||
|
||||
/**
|
||||
* 从后端拉取品牌配置
|
||||
* 策略:对比 updatedAt,有变化时更新缓存并广播事件通知全部页面
|
||||
* @returns {Promise<boolean>} 是否有变化
|
||||
*/
|
||||
async fetchConfig() {
|
||||
try {
|
||||
const res = await brandApi.getBrandConfig()
|
||||
if (res) {
|
||||
const newConfig = {
|
||||
brandName: res.brandName || DEFAULT_CONFIG.brandName,
|
||||
slogan: res.slogan || DEFAULT_CONFIG.slogan,
|
||||
primaryColor: res.primaryColor || DEFAULT_CONFIG.primaryColor,
|
||||
primaryColorRgb: res.primaryColorRgb || DEFAULT_CONFIG.primaryColorRgb,
|
||||
secondaryColor: res.secondaryColor || DEFAULT_CONFIG.secondaryColor,
|
||||
secondaryColorRgb: res.secondaryColorRgb || DEFAULT_CONFIG.secondaryColorRgb,
|
||||
logoUrl: res.logoUrl || '',
|
||||
backgroundImageUrl: res.backgroundImageUrl || '',
|
||||
updatedAt: res.updatedAt || ''
|
||||
}
|
||||
|
||||
if (!this._config || this._config.updatedAt !== newConfig.updatedAt) {
|
||||
this._config = newConfig
|
||||
try {
|
||||
uni.setStorageSync(BRAND_CACHE_KEY, newConfig)
|
||||
} catch (e) { /* ignore */ }
|
||||
this._loaded = true
|
||||
// 广播事件通知所有页面刷新
|
||||
uni.$emit('brand:updated', newConfig)
|
||||
return true
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[BrandStore] 获取品牌配置失败:', e)
|
||||
}
|
||||
|
||||
this._loaded = true
|
||||
return false
|
||||
},
|
||||
|
||||
/** 应用 TabBar 品牌色 */
|
||||
applyTabBar() {
|
||||
try {
|
||||
const cfg = this.config
|
||||
uni.setTabBarStyle({
|
||||
color: '#7A7E84',
|
||||
selectedColor: cfg.primaryColor,
|
||||
backgroundColor: cfg.secondaryColor,
|
||||
borderStyle: 'black'
|
||||
})
|
||||
} catch (e) { /* 非 TabBar 页面忽略 */ }
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = brandStore
|
||||
Reference in New Issue
Block a user