feat: uniapp 读取分类页设置配置(缓存+默认值兜底)

This commit is contained in:
pikachu1995@126.com
2026-10-22 09:17:58 +08:00
parent 0a47f84c32
commit 1032b6e34d
4 changed files with 106 additions and 1 deletions

24
App.vue
View File

@@ -3,13 +3,18 @@ import config from '@/config/config'
import { getClipboardData } from '@/js_sdk/h5-copy/h5-copy.js'
import APPUpdate from '@/plugins/APPUpdate'
import storage from '@/utils/storage'
import { getThemeSetting } from '@/api/common.js'
import { getThemeSetting, getCategoryPageSetting } from '@/api/common.js'
import {
applyTabBarStyle,
cacheTheme,
loadCachedTheme,
normalizeTheme,
} from '@/utils/theme'
import {
cacheCategoryPageSetting,
loadCachedCategoryPageSetting,
normalizeCategoryPageSetting,
} from '@/utils/categoryPageSetting'
import { onLaunch, onShow } from '@dcloudio/uni-app'
import { useStore } from '@/store'
import { handleAppLaunchScene } from '@/utils/distributionBind.js'
@@ -24,6 +29,7 @@ wx.onAppRoute((res) => {
onLaunch((val) => {
initTheme()
initCategoryPageSetting()
handleAppLaunchScene(val)
if (val?.query?.inviter) {
storage.setInviter(val.query.inviter)
@@ -81,6 +87,22 @@ function initTheme() {
.catch(() => {})
}
function initCategoryPageSetting() {
const cached = loadCachedCategoryPageSetting()
if (cached) {
store.commit('SET_CATEGORY_PAGE_SETTING', cached)
}
getCategoryPageSetting()
.then((res) => {
if (res.data?.success) {
const setting = normalizeCategoryPageSetting(res.data.result)
store.commit('SET_CATEGORY_PAGE_SETTING', setting)
cacheCategoryPageSetting(setting)
}
})
.catch(() => {})
}
function applyUpdateWeChat() {
const updateManager = uni.getUpdateManager()

View File

@@ -41,3 +41,14 @@ export function getThemeSetting() {
message: false,
});
}
/**
* 获取分类页配置
*/
export function getCategoryPageSetting() {
return http.request({
url: `${api.buyer}/common/site/category-page`,
method: Method.GET,
message: false,
});
}

View File

@@ -2,8 +2,13 @@ import { createStore } from 'vuex'
import storage from '@/utils/storage'
import config from '@/config/config'
import { DEFAULT_THEME, loadCachedTheme } from '@/utils/theme'
import {
DEFAULT_CATEGORY_PAGE_SETTING,
loadCachedCategoryPageSetting,
} from '@/utils/categoryPageSetting'
const cachedTheme = loadCachedTheme()
const cachedCategoryPageSetting = loadCachedCategoryPageSetting()
const store = createStore({
state: {
@@ -17,6 +22,8 @@ const store = createStore({
uuid: storage.getUuid(),
token: '',
theme: cachedTheme || { ...DEFAULT_THEME },
categoryPageSetting:
cachedCategoryPageSetting || { ...DEFAULT_CATEGORY_PAGE_SETTING },
},
getters: {
mainColor: (state) => state.theme.mainColor,
@@ -44,6 +51,9 @@ const store = createStore({
aiderLightColor: theme.aiderLightColor || DEFAULT_THEME.aiderLightColor,
}
},
SET_CATEGORY_PAGE_SETTING(state, setting) {
state.categoryPageSetting = setting || { ...DEFAULT_CATEGORY_PAGE_SETTING }
},
},
actions: {}
})

View File

@@ -0,0 +1,62 @@
export const DEFAULT_CATEGORY_PAGE_SETTING = {
template: 1,
showFields: {
subTitle: true,
spec: true,
tags: true,
selfTag: true,
price: true,
linePrice: true,
sales: true,
cart: true,
},
};
const STORAGE_KEY = "category_page_setting";
const EXPIRATION_KEY = "category_page_setting_expiration_time";
/**
* 归一化分类页设置。入参为 buyer 接口 result(Setting 实体或 null),
* null / 解析失败 / 缺键 / template 非 1-4 一律回退默认值。
*/
export function normalizeCategoryPageSetting(setting = null) {
const fallback = () => JSON.parse(JSON.stringify(DEFAULT_CATEGORY_PAGE_SETTING));
if (!setting || !setting.settingValue) return fallback();
let raw;
try {
raw = JSON.parse(setting.settingValue);
} catch {
return fallback();
}
if (!raw || typeof raw !== "object") return fallback();
const result = fallback();
if ([1, 2, 3, 4].includes(raw.template)) {
result.template = raw.template;
}
if (raw.showFields && typeof raw.showFields === "object") {
for (const key of Object.keys(result.showFields)) {
if (typeof raw.showFields[key] === "boolean") {
result.showFields[key] = raw.showFields[key];
}
}
}
return result;
}
export function cacheCategoryPageSetting(data) {
const expirationTime = new Date().setHours(new Date().getHours() + 1);
uni.setStorageSync(EXPIRATION_KEY, expirationTime);
uni.setStorageSync(STORAGE_KEY, data);
}
export function loadCachedCategoryPageSetting() {
const expirationTime = uni.getStorageSync(EXPIRATION_KEY);
const cacheValid =
expirationTime && new Date() <= new Date(Number(expirationTime));
if (!cacheValid) return null;
try {
return uni.getStorageSync(STORAGE_KEY) || null;
} catch {
return null;
}
}