mirror of
https://gitee.com/beijing_hongye_huicheng/lilishop-ui.git
synced 2026-11-11 19:38:55 +08:00
Compare commits
1 Commits
feat/goods
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
402c9c9ebb |
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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 || {};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user