|
| 1 | +import { ref } from "vue"; |
| 2 | +import { useStorage } from "@vueuse/core"; |
| 3 | +import { locale } from "@/locales"; |
| 4 | +import { aquaMaiVersionConfig } from "@/client/api"; |
| 5 | +import { GetGetConfigTypeEnum } from "@/client/aquaMaiVersionConfigApiGen"; |
| 6 | + |
| 7 | +const CHANGELOG_BUCKET_BASE = 'https://munet-version-config-1251600285.cos.ap-shanghai.myqcloud.com/mcm-changelog'; |
| 8 | + |
| 9 | +// --- Mod 更新 --- |
| 10 | + |
| 11 | +export const modUpdateInfo = ref<Awaited<ReturnType<typeof aquaMaiVersionConfig.getGetConfig>>['data']>([{ |
| 12 | + type: GetGetConfigTypeEnum.Builtin, |
| 13 | +}]) |
| 14 | + |
| 15 | +export const updateModUpdateInfo = async () => { |
| 16 | + try { |
| 17 | + modUpdateInfo.value = await Promise.any([ |
| 18 | + (async () => { |
| 19 | + const res = await aquaMaiVersionConfig.getGetConfig({ |
| 20 | + cache: 'no-cache', |
| 21 | + }); |
| 22 | + return res.data; |
| 23 | + })(), |
| 24 | + (async () => { |
| 25 | + const res = await fetch('https://munet-version-config-1251600285.cos.ap-shanghai.myqcloud.com/aquamai.json', { |
| 26 | + cache: 'no-cache', |
| 27 | + }); |
| 28 | + if (!res.ok) { |
| 29 | + throw new Error(`Failed to fetch mod update info: ${res.status} ${res.statusText}`); |
| 30 | + } |
| 31 | + return await res.json(); |
| 32 | + })(), |
| 33 | + ]); |
| 34 | + } catch (e) { |
| 35 | + console.error('Failed to get mod update info:', e); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// --- 应用更新 --- |
| 40 | + |
| 41 | +export const appUpdateInfo = ref<{ version: string } | null>(null); |
| 42 | + |
| 43 | +export const updateAppUpdateInfo = async () => { |
| 44 | + try { |
| 45 | + const res = await fetch('https://munet-version-config-1251600285.cos.ap-shanghai.myqcloud.com/mcm.json', { |
| 46 | + cache: 'no-cache', |
| 47 | + }); |
| 48 | + if (!res.ok) return; |
| 49 | + appUpdateInfo.value = await res.json(); |
| 50 | + // 预加载新版本的更新日志 |
| 51 | + if (appUpdateInfo.value?.version) { |
| 52 | + eagerFetchChangelog(appUpdateInfo.value.version); |
| 53 | + } |
| 54 | + } catch (e) { |
| 55 | + console.error('Failed to get app update info:', e); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// --- 更新日志 --- |
| 60 | + |
| 61 | +export const showChangelogModal = ref(false); |
| 62 | +export const changelogContent = ref(''); |
| 63 | +export const changelogTargetVersion = ref(''); |
| 64 | +export const changelogAutoPopupDone = ref(false); |
| 65 | +export const lastShownChangelogVersion = useStorage('lastShownChangelogVersion', ''); |
| 66 | + |
| 67 | +export const getCleanVersion = (v: string) => v.split('+')[0]; |
| 68 | + |
| 69 | +/** |
| 70 | + * 构建 locale 回退链:当前语言 → 基础语言 → en |
| 71 | + * 例如 zh-TW → zh → en |
| 72 | + */ |
| 73 | +function getLocaleFallbackChain(): string[] { |
| 74 | + const current = locale.value; |
| 75 | + const chain = [current]; |
| 76 | + if (current.includes('-')) { |
| 77 | + chain.push(current.split('-')[0]); |
| 78 | + } |
| 79 | + if (!chain.includes('en')) { |
| 80 | + chain.push('en'); |
| 81 | + } |
| 82 | + return chain; |
| 83 | +} |
| 84 | + |
| 85 | +async function fetchChangelog(ver: string): Promise<string> { |
| 86 | + const cleanVer = getCleanVersion(ver); |
| 87 | + for (const loc of getLocaleFallbackChain()) { |
| 88 | + try { |
| 89 | + const res = await fetch(`${CHANGELOG_BUCKET_BASE}/${cleanVer}.${loc}.md`, { cache: 'no-cache' }); |
| 90 | + if (res.ok) return await res.text(); |
| 91 | + } catch { |
| 92 | + // 网络错误,尝试下一个 locale |
| 93 | + } |
| 94 | + } |
| 95 | + return ''; |
| 96 | +} |
| 97 | + |
| 98 | +// 更新日志缓存:版本号 + 语言链 → fetch promise |
| 99 | +const changelogCache = new Map<string, Promise<string>>(); |
| 100 | +let openChangelogRequestId = 0; |
| 101 | + |
| 102 | +function getChangelogCacheKey(ver: string) { |
| 103 | + const cleanVer = getCleanVersion(ver); |
| 104 | + return `${cleanVer}|${getLocaleFallbackChain().join('>')}`; |
| 105 | +} |
| 106 | + |
| 107 | +export function eagerFetchChangelog(ver: string) { |
| 108 | + const key = getChangelogCacheKey(ver); |
| 109 | + if (!changelogCache.has(key)) { |
| 110 | + changelogCache.set(key, fetchChangelog(ver)); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +async function getChangelogCached(ver: string): Promise<string> { |
| 115 | + const key = getChangelogCacheKey(ver); |
| 116 | + const cached = changelogCache.get(key); |
| 117 | + if (cached) return cached; |
| 118 | + const promise = fetchChangelog(ver); |
| 119 | + changelogCache.set(key, promise); |
| 120 | + return promise; |
| 121 | +} |
| 122 | + |
| 123 | +export async function openChangelog(ver: string, options?: { |
| 124 | + showAfterLoaded?: boolean; |
| 125 | + skipIfEmpty?: boolean; |
| 126 | +}) { |
| 127 | + const requestId = ++openChangelogRequestId; |
| 128 | + const cleanVer = getCleanVersion(ver); |
| 129 | + const showAfterLoaded = !!options?.showAfterLoaded; |
| 130 | + const skipIfEmpty = !!options?.skipIfEmpty; |
| 131 | + |
| 132 | + changelogTargetVersion.value = cleanVer; |
| 133 | + changelogContent.value = ''; |
| 134 | + |
| 135 | + if (!showAfterLoaded) { |
| 136 | + showChangelogModal.value = true; |
| 137 | + } |
| 138 | + |
| 139 | + const content = await getChangelogCached(ver); |
| 140 | + if (requestId !== openChangelogRequestId) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + if (skipIfEmpty && !content) { |
| 144 | + showChangelogModal.value = false; |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + changelogContent.value = content; |
| 149 | + if (showAfterLoaded) { |
| 150 | + showChangelogModal.value = true; |
| 151 | + } |
| 152 | + return true; |
| 153 | +} |
0 commit comments