fix(buyer): 优化首屏站点及主题请求去重,并在顶栏退出时联动后端登出注销会话

This commit is contained in:
pikachu1995@126.com
2026-11-11 14:46:15 +08:00
parent 2252bcb8af
commit 402c9c9ebb
4 changed files with 53 additions and 37 deletions

View File

@@ -8,8 +8,7 @@
import { onMounted } from "vue";
import { useRoute } from "vue-router";
import storage from "@/plugins/storage";
import { getBaseSite, getThemeSetting } from "@/api/common.js";
import { fetchAndApplyTheme } from "@/utils/theme";
import { loadSiteSettings } from "@/composables/useSiteSettings";
import { useAppStore } from "@/stores/app";
defineOptions({ name: "App" });
@@ -49,27 +48,15 @@ function init() {
});
}
getSite();
getTheme();
}
function getSite() {
getBaseSite().then((res) => {
if (res.success && res.result.settingValue) {
try {
const data = JSON.parse(res.result.settingValue);
applySiteInfo(data);
} catch {
// 站点配置解析失败时使用缓存
}
loadSiteSettings().then((data) => {
if (data) {
applySiteInfo(data);
}
}).catch((err) => {
console.warn('获取站点配置失败', err);
});
}
function getTheme() {
fetchAndApplyTheme(getThemeSetting).catch((err) => {
console.warn('获取主题配置失败', err);
console.warn("获取站点配置失败", err);
});
}

View File

@@ -37,6 +37,8 @@
<script setup>
import { computed } from 'vue';
import Cookies from 'js-cookie';
import storage from '@/plugins/storage';
import { logout } from '@/api/account';
const props = defineProps({
options: {
@@ -69,12 +71,22 @@ const username = computed(() => {
return '';
});
function handleLogout() {
function clearLocalAuth() {
storage.removeItem('userInfo');
storage.removeItem('accessToken');
storage.removeItem('refreshToken');
storage.removeItem('cartNum');
Cookies.remove('userInfo');
Cookies.remove('accessToken');
window.location.reload();
}
function handleLogout() {
logout().finally(() => {
clearLocalAuth();
});
}
const customStyle = computed(() => {
const s = props.componentStyle || {};
const opt = props.options || {};

View File

@@ -2,11 +2,14 @@ import { computed, onMounted, ref } from "vue";
import { getBaseSite } from "@/api/common.js";
import config from "@/config";
// 同页的页头、页脚共享请求;每次重新进入页面时刷新,避免旧缓存覆盖后台设置。
// 全局共享站点配置;应用生命周期内仅拉取一次,同页与切页均复用请求与结果。
const settings = ref(null);
let pendingRequest;
let pendingRequest = null;
export function loadSiteSettings() {
if (settings.value) {
return Promise.resolve(settings.value);
}
if (!pendingRequest) {
pendingRequest = getBaseSite()
.then((res) => {
@@ -16,7 +19,9 @@ export function loadSiteSettings() {
return settings.value;
})
.catch(() => settings.value)
.finally(() => { pendingRequest = null; });
.finally(() => {
pendingRequest = null;
});
}
return pendingRequest;
}

View File

@@ -69,27 +69,39 @@ export function normalizeTheme(data = {}) {
};
}
let pendingThemeRequest = null;
export async function fetchAndApplyTheme(requestFn) {
const cachedTheme = loadCachedTheme();
if (cachedTheme) {
applyThemeToDocument(normalizeTheme(cachedTheme));
}
try {
const res = await requestFn();
if (res?.success && res?.result?.settingValue) {
const data = JSON.parse(res.result.settingValue);
const normalized = normalizeTheme(data);
applyThemeToDocument(normalized);
cacheTheme(normalized);
return normalized;
}
} catch (error) {
console.warn(
"[theme] 主题色加载失败,请确认 buyer-api 已启动且站点主题配置可用",
error
);
if (pendingThemeRequest) {
return pendingThemeRequest;
}
return cachedTheme ? normalizeTheme(cachedTheme) : normalizeTheme();
pendingThemeRequest = (async () => {
try {
const res = await requestFn();
if (res?.success && res?.result?.settingValue) {
const data = JSON.parse(res.result.settingValue);
const normalized = normalizeTheme(data);
applyThemeToDocument(normalized);
cacheTheme(normalized);
return normalized;
}
} catch (error) {
console.warn(
"[theme] 主题色加载失败,请确认 buyer-api 已启动且站点主题配置可用",
error
);
} finally {
pendingThemeRequest = null;
}
return cachedTheme ? normalizeTheme(cachedTheme) : normalizeTheme();
})();
return pendingThemeRequest;
}