qiangdanxitong/frontend/src/stores/siteSettings.ts

127 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getSystemConfigsByGroup } from '@/api/system'
export const useSiteSettingsStore = defineStore('siteSettings', () => {
const siteName = ref('DesignHub')
const logoUrl = ref('')
// 加载网站设置
const loadSiteSettings = async () => {
try {
const response = await getSystemConfigsByGroup('site')
const configs = response.data || []
const siteNameConfig = configs.find((c: any) => c.configKey === 'site.name')
const logoConfig = configs.find((c: any) => c.configKey === 'site.logo')
if (siteNameConfig) {
siteName.value = siteNameConfig.configValue || 'DesignHub'
} else {
siteName.value = localStorage.getItem('siteName') || 'DesignHub'
}
if (logoConfig) {
logoUrl.value = logoConfig.configValue || ''
} else {
logoUrl.value = localStorage.getItem('logoUrl') || ''
}
// 更新浏览器标题和favicon
updateBrowserInfo()
} catch (error) {
console.error('加载网站设置失败:', error)
// 如果API失败,从localStorage读取(向后兼容)
siteName.value = localStorage.getItem('siteName') || 'DesignHub'
logoUrl.value = localStorage.getItem('logoUrl') || ''
updateBrowserInfo()
}
}
// 更新网站设置
const updateSiteSettings = (newSiteName: string, newLogoUrl: string) => {
siteName.value = newSiteName || 'DesignHub'
logoUrl.value = newLogoUrl || ''
// 保存到localStorage
localStorage.setItem('siteName', siteName.value)
if (logoUrl.value) {
localStorage.setItem('logoUrl', logoUrl.value)
} else {
localStorage.removeItem('logoUrl')
}
// 更新浏览器标题和favicon
updateBrowserInfo()
// 触发自定义事件,通知其他组件更新
window.dispatchEvent(new CustomEvent('siteSettingsUpdated', {
detail: {
siteName: siteName.value,
logoUrl: logoUrl.value
}
}))
}
// 更新浏览器标题和favicon
const updateBrowserInfo = () => {
// 更新浏览器标题
document.title = siteName.value || 'DesignHub'
// 更新favicon(如果有logo)
if (logoUrl.value) {
// 移除所有现有的favicon链接
const existingLinks = document.querySelectorAll("link[rel*='icon']")
existingLinks.forEach(link => link.remove())
// 创建新的favicon链接
const link = document.createElement('link')
link.rel = 'icon'
// 处理base64和普通URL
if (logoUrl.value.startsWith('data:')) {
// base64格式
const mimeMatch = logoUrl.value.match(/data:([^;]+)/)
link.type = mimeMatch?.[1] || 'image/png'
link.href = logoUrl.value
} else {
// 普通URL,添加时间戳强制刷新
link.type = 'image/x-icon'
link.href = `${logoUrl.value}?t=${Date.now()}`
}
// 添加到head
document.getElementsByTagName('head')[0].appendChild(link)
// 同时添加apple-touch-icon(移动端支持)
const appleLink = document.createElement('link')
appleLink.rel = 'apple-touch-icon'
if (logoUrl.value.startsWith('data:')) {
appleLink.href = logoUrl.value
} else {
appleLink.href = `${logoUrl.value}?t=${Date.now()}`
}
document.getElementsByTagName('head')[0].appendChild(appleLink)
} else {
// 如果没有logo,移除所有favicon链接
const existingLinks = document.querySelectorAll("link[rel*='icon']")
existingLinks.forEach(link => {
// 保留默认的vite.svg favicon(如果存在)
const href = (link as HTMLLinkElement).href
if (!href.includes('vite.svg')) {
link.remove()
}
})
}
}
return {
siteName,
logoUrl,
loadSiteSettings,
updateSiteSettings,
updateBrowserInfo
}
})