feat: 支持动态修改管理后台站点标题
- 从本地存储读取自定义标题,替代硬编码默认标题 - 在布局组件中提供更新标题的方法并注入到设置页面 - 修改站点标题时同步更新本地存储和页面标题
This commit is contained in:
@@ -71,7 +71,8 @@ export const router = createRouter({
|
||||
});
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
const defaultTitle = 'CWD 评论系统';
|
||||
const storedTitle = localStorage.getItem('cwd_admin_site_title');
|
||||
const defaultTitle = storedTitle || 'CWD 评论系统';
|
||||
if (to.meta && to.meta.title) {
|
||||
document.title = (to.meta.title + ' - ' + defaultTitle) as string;
|
||||
} else {
|
||||
|
||||
@@ -179,6 +179,7 @@ import packageJson from "../../../package.json";
|
||||
|
||||
const DOMAIN_STORAGE_KEY = "cwd_admin_domain_filter";
|
||||
const API_BASE_URL_KEY = "cwd_admin_api_base_url";
|
||||
const SITE_TITLE_KEY = "cwd_admin_site_title";
|
||||
|
||||
const router = useRouter();
|
||||
const route = useRoute();
|
||||
@@ -191,7 +192,7 @@ const apiVersion = ref("");
|
||||
const checkedApiBaseUrl = ref("");
|
||||
const apiVersionError = ref("");
|
||||
const versionModalVisible = ref(false);
|
||||
const layoutTitle = ref("CWD 评论系统");
|
||||
const layoutTitle = ref(localStorage.getItem(SITE_TITLE_KEY) || "CWD 评论系统");
|
||||
|
||||
const themeTitle = computed(() => {
|
||||
if (theme.value === "light") return "明亮模式";
|
||||
@@ -255,16 +256,29 @@ async function loadVersion() {
|
||||
}
|
||||
}
|
||||
|
||||
function updateTitle(newTitle: string) {
|
||||
layoutTitle.value = newTitle;
|
||||
localStorage.setItem(SITE_TITLE_KEY, newTitle);
|
||||
const pageTitle = route.meta.title;
|
||||
if (pageTitle) {
|
||||
document.title = `${pageTitle} - ${newTitle}`;
|
||||
} else {
|
||||
document.title = newTitle;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadDisplaySettings() {
|
||||
try {
|
||||
const res = await fetchAdminDisplaySettings();
|
||||
layoutTitle.value = res.layoutTitle || "CWD 评论系统";
|
||||
const title = res.layoutTitle || "CWD 评论系统";
|
||||
updateTitle(title);
|
||||
} catch {
|
||||
layoutTitle.value = "CWD 评论系统";
|
||||
// 忽略错误,保持当前值或默认值
|
||||
}
|
||||
}
|
||||
|
||||
provide("domainFilter", domainFilter);
|
||||
provide("updateSiteTitle", updateTitle);
|
||||
|
||||
onMounted(() => {
|
||||
loadDomains();
|
||||
|
||||
@@ -510,7 +510,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref, type Ref } from "vue";
|
||||
import { onMounted, ref, type Ref, inject } from "vue";
|
||||
import {
|
||||
fetchCommentSettings,
|
||||
saveCommentSettings,
|
||||
@@ -1038,6 +1038,8 @@ async function saveFeature() {
|
||||
}
|
||||
}
|
||||
|
||||
const updateSiteTitle = inject<(title: string) => void>("updateSiteTitle");
|
||||
|
||||
async function saveDisplay() {
|
||||
savingDisplay.value = true;
|
||||
message.value = "";
|
||||
@@ -1046,6 +1048,10 @@ async function saveDisplay() {
|
||||
layoutTitle: adminLayoutTitle.value,
|
||||
});
|
||||
|
||||
if (updateSiteTitle) {
|
||||
updateSiteTitle(adminLayoutTitle.value);
|
||||
}
|
||||
|
||||
showToast(res.message || "保存成功", "success");
|
||||
} catch (e: any) {
|
||||
message.value = e.message || "保存失败";
|
||||
|
||||
Reference in New Issue
Block a user