|
| 1 | +import { computed, ref } from 'vue' |
| 2 | +import { useStorage } from '@vueuse/core' |
| 3 | +import { locale } from '@/locales' |
| 4 | +import { appVersion } from '@/store/refs' |
| 5 | + |
| 6 | +// TODO(部署): 由运维部署 COS bucket 后填入实际地址 |
| 7 | +// 约定文件: |
| 8 | +// {COS_BASE}/ccm.json → { "version": "26.1" } |
| 9 | +// {COS_BASE}/ccm-changelog/{ver}.{loc}.md → 对应版本/语言的更新日志 |
| 10 | +// 未部署时 fetch 失败,静默降级为「无更新」 |
| 11 | +const COS_BASE = '' |
| 12 | + |
| 13 | +export interface AppUpdateInfo { |
| 14 | + version: string |
| 15 | +} |
| 16 | + |
| 17 | +export const appUpdateInfo = ref<AppUpdateInfo | null>(null) |
| 18 | + |
| 19 | +export const getCleanVersion = (v: string) => v.split('+')[0] |
| 20 | + |
| 21 | +function compareVersion(a: string, b: string): number { |
| 22 | + const pa = getCleanVersion(a).split('.').map(Number) |
| 23 | + const pb = getCleanVersion(b).split('.').map(Number) |
| 24 | + const len = Math.max(pa.length, pb.length) |
| 25 | + for (let i = 0; i < len; i++) { |
| 26 | + const da = pa[i] || 0 |
| 27 | + const db = pb[i] || 0 |
| 28 | + if (da !== db) return da - db |
| 29 | + } |
| 30 | + return 0 |
| 31 | +} |
| 32 | + |
| 33 | +export const hasUpdate = computed(() => { |
| 34 | + const remote = appUpdateInfo.value?.version |
| 35 | + const local = appVersion.value?.version |
| 36 | + if (!remote || !local) return false |
| 37 | + return compareVersion(remote, local) > 0 |
| 38 | +}) |
| 39 | + |
| 40 | +export async function checkAppUpdate() { |
| 41 | + if (!COS_BASE) return |
| 42 | + try { |
| 43 | + const res = await fetch(`${COS_BASE}/ccm.json`, { cache: 'no-cache' }) |
| 44 | + if (!res.ok) return |
| 45 | + appUpdateInfo.value = await res.json() |
| 46 | + if (appUpdateInfo.value?.version) eagerFetchChangelog(appUpdateInfo.value.version) |
| 47 | + } catch (e) { |
| 48 | + console.error('Failed to get app update info:', e) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export const showChangelogModal = ref(false) |
| 53 | +export const changelogContent = ref('') |
| 54 | +export const changelogTargetVersion = ref('') |
| 55 | +export const changelogAutoPopupDone = ref(false) |
| 56 | +export const lastShownChangelogVersion = useStorage('ccm-last-shown-changelog', '') |
| 57 | + |
| 58 | +function getLocaleFallbackChain(): string[] { |
| 59 | + const current = locale.value |
| 60 | + const chain = [current] |
| 61 | + if (current.includes('-')) chain.push(current.split('-')[0]) |
| 62 | + if (!chain.includes('en')) chain.push('en') |
| 63 | + return chain |
| 64 | +} |
| 65 | + |
| 66 | +async function fetchChangelog(ver: string): Promise<string> { |
| 67 | + if (!COS_BASE) return '' |
| 68 | + const cleanVer = getCleanVersion(ver) |
| 69 | + for (const loc of getLocaleFallbackChain()) { |
| 70 | + try { |
| 71 | + const res = await fetch(`${COS_BASE}/ccm-changelog/${cleanVer}.${loc}.md`, { cache: 'no-cache' }) |
| 72 | + if (res.ok) return await res.text() |
| 73 | + } catch { |
| 74 | + // 网络错误,尝试下一个 locale |
| 75 | + } |
| 76 | + } |
| 77 | + return '' |
| 78 | +} |
| 79 | + |
| 80 | +const changelogCache = new Map<string, Promise<string>>() |
| 81 | +let openChangelogRequestId = 0 |
| 82 | + |
| 83 | +function getChangelogCacheKey(ver: string) { |
| 84 | + return `${getCleanVersion(ver)}|${getLocaleFallbackChain().join('>')}` |
| 85 | +} |
| 86 | + |
| 87 | +export function eagerFetchChangelog(ver: string) { |
| 88 | + const key = getChangelogCacheKey(ver) |
| 89 | + if (!changelogCache.has(key)) changelogCache.set(key, fetchChangelog(ver)) |
| 90 | +} |
| 91 | + |
| 92 | +async function getChangelogCached(ver: string): Promise<string> { |
| 93 | + const key = getChangelogCacheKey(ver) |
| 94 | + const cached = changelogCache.get(key) |
| 95 | + if (cached) return cached |
| 96 | + const promise = fetchChangelog(ver) |
| 97 | + changelogCache.set(key, promise) |
| 98 | + return promise |
| 99 | +} |
| 100 | + |
| 101 | +export async function openChangelog(ver: string, options?: { showAfterLoaded?: boolean; skipIfEmpty?: boolean }) { |
| 102 | + const requestId = ++openChangelogRequestId |
| 103 | + const cleanVer = getCleanVersion(ver) |
| 104 | + const showAfterLoaded = !!options?.showAfterLoaded |
| 105 | + const skipIfEmpty = !!options?.skipIfEmpty |
| 106 | + |
| 107 | + changelogTargetVersion.value = cleanVer |
| 108 | + changelogContent.value = '' |
| 109 | + |
| 110 | + if (!showAfterLoaded) showChangelogModal.value = true |
| 111 | + |
| 112 | + const content = await getChangelogCached(ver) |
| 113 | + if (requestId !== openChangelogRequestId) return false |
| 114 | + if (skipIfEmpty && !content) { |
| 115 | + showChangelogModal.value = false |
| 116 | + return false |
| 117 | + } |
| 118 | + |
| 119 | + changelogContent.value = content |
| 120 | + if (showAfterLoaded) showChangelogModal.value = true |
| 121 | + return true |
| 122 | +} |
0 commit comments