From 1032b6e34dcf1ea3e3161af156a1609ff9d7b005 Mon Sep 17 00:00:00 2001 From: "pikachu1995@126.com" Date: Thu, 22 Oct 2026 09:17:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20uniapp=20=E8=AF=BB=E5=8F=96=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E9=A1=B5=E8=AE=BE=E7=BD=AE=E9=85=8D=E7=BD=AE(?= =?UTF-8?q?=E7=BC=93=E5=AD=98+=E9=BB=98=E8=AE=A4=E5=80=BC=E5=85=9C?= =?UTF-8?q?=E5=BA=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 24 +++++++++++++- api/common.js | 11 +++++++ store/index.js | 10 ++++++ utils/categoryPageSetting.js | 62 ++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 utils/categoryPageSetting.js diff --git a/App.vue b/App.vue index 4b6d532..6f30f93 100644 --- a/App.vue +++ b/App.vue @@ -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() diff --git a/api/common.js b/api/common.js index 9ce9069..743a206 100644 --- a/api/common.js +++ b/api/common.js @@ -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, + }); +} diff --git a/store/index.js b/store/index.js index 12f76ff..cccea6e 100644 --- a/store/index.js +++ b/store/index.js @@ -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: {} }) diff --git a/utils/categoryPageSetting.js b/utils/categoryPageSetting.js new file mode 100644 index 0000000..5ec0120 --- /dev/null +++ b/utils/categoryPageSetting.js @@ -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; + } +}