杂务:整理文档并收口 YumiNail 修复
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
import { readonly, shallowRef } from 'vue'
|
||||
import { fileApi } from '@/api/file'
|
||||
import { shopApi, type ShopVO } from '@/api/shop'
|
||||
import {
|
||||
applyShopTheme,
|
||||
DEFAULT_SHOP_THEME,
|
||||
resolveShopTheme,
|
||||
SHOP_THEME_CHANGED_EVENT,
|
||||
type ResolvedShopTheme
|
||||
} from '@/utils/shop-theme'
|
||||
|
||||
const currentTheme = shallowRef<ResolvedShopTheme>(DEFAULT_SHOP_THEME)
|
||||
let cleanupThemeSync: (() => void) | undefined
|
||||
let refreshSequence = 0
|
||||
|
||||
export function useShopTheme() {
|
||||
return {
|
||||
currentTheme: readonly(currentTheme),
|
||||
refreshShopTheme,
|
||||
resetShopTheme,
|
||||
setupShopThemeSync
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshShopTheme() {
|
||||
const token = readToken()
|
||||
const userInfo = readStoredUserInfo()
|
||||
const shopId = userInfo?.shopId
|
||||
|
||||
if (!token || !shopId) {
|
||||
resetShopTheme()
|
||||
return
|
||||
}
|
||||
|
||||
const sequence = ++refreshSequence
|
||||
try {
|
||||
const shop = await shopApi.getShopById(shopId)
|
||||
if (sequence !== refreshSequence) {
|
||||
return
|
||||
}
|
||||
await applyThemeFromShop(shop)
|
||||
} catch {
|
||||
if (sequence === refreshSequence) {
|
||||
resetShopTheme()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function resetShopTheme() {
|
||||
refreshSequence += 1
|
||||
currentTheme.value = DEFAULT_SHOP_THEME
|
||||
applyShopTheme(DEFAULT_SHOP_THEME)
|
||||
}
|
||||
|
||||
function setupShopThemeSync() {
|
||||
if (cleanupThemeSync || typeof window === 'undefined') {
|
||||
return cleanupThemeSync || (() => {})
|
||||
}
|
||||
|
||||
const handleThemeEvent = () => {
|
||||
void refreshShopTheme()
|
||||
}
|
||||
const handleStorage = (event: StorageEvent) => {
|
||||
if (['accessToken', 'userInfo', 'refreshToken'].includes(event.key || '')) {
|
||||
void refreshShopTheme()
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener(SHOP_THEME_CHANGED_EVENT, handleThemeEvent)
|
||||
window.addEventListener('storage', handleStorage)
|
||||
window.addEventListener('focus', handleThemeEvent)
|
||||
|
||||
cleanupThemeSync = () => {
|
||||
window.removeEventListener(SHOP_THEME_CHANGED_EVENT, handleThemeEvent)
|
||||
window.removeEventListener('storage', handleStorage)
|
||||
window.removeEventListener('focus', handleThemeEvent)
|
||||
cleanupThemeSync = undefined
|
||||
}
|
||||
return cleanupThemeSync
|
||||
}
|
||||
|
||||
async function applyThemeFromShop(shop: ShopVO) {
|
||||
const theme = resolveShopTheme(shop)
|
||||
currentTheme.value = theme
|
||||
const backgroundImageUrl = await resolveBackgroundImageUrl(theme.backgroundImageFileId)
|
||||
applyShopTheme(theme, backgroundImageUrl)
|
||||
}
|
||||
|
||||
async function resolveBackgroundImageUrl(fileId?: string) {
|
||||
if (!fileId) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
return await fileApi.getSignedUrl(fileId, 120) as unknown as string
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function readToken() {
|
||||
if (typeof window === 'undefined') {
|
||||
return ''
|
||||
}
|
||||
return localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken') || ''
|
||||
}
|
||||
|
||||
function readStoredUserInfo(): { shopId?: string | number } | null {
|
||||
if (typeof window === 'undefined') {
|
||||
return null
|
||||
}
|
||||
const raw = localStorage.getItem('userInfo')
|
||||
if (!raw) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
return JSON.parse(raw) as { shopId?: string | number }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user