mirror of
https://github.com/VickScarlet/lifeRestart.git
synced 2026-08-19 18:43:53 +08:00
feature: bili-toy cloud fallback local
This commit is contained in:
+12
-3
@@ -40,6 +40,13 @@
|
||||
width: 100%;
|
||||
> button { flex: 1 0; }
|
||||
}
|
||||
&.loading {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
> * { width: auto; }
|
||||
animation: pulse 1.8s ease-in-out infinite;
|
||||
}
|
||||
}
|
||||
.saving {
|
||||
position: fixed;
|
||||
@@ -47,9 +54,11 @@
|
||||
left: 0;
|
||||
opacity: 0;
|
||||
user-select: none;
|
||||
transition: opacity 0.3s;
|
||||
&.active {
|
||||
opacity: 1;
|
||||
transition: opacity 0.1s;
|
||||
animation: pulse 1.8s ease-in-out infinite;
|
||||
}
|
||||
}
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 0.5; }
|
||||
50% { opacity: 1; }
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
import { toastMsg } from '@/toast'
|
||||
import './Github.css'
|
||||
const repo = 'https://github.com/VickScarlet/remake'
|
||||
export function Github() {
|
||||
const handleClick = () => {
|
||||
if (import.meta.env.VITE_CHANNEL === 'bili') toastMsg(repo, 'github')
|
||||
else window.open(repo)
|
||||
}
|
||||
return (
|
||||
<button
|
||||
className="github-btn"
|
||||
onClick={() => window.open('https://github.com/VickScarlet/remake')}
|
||||
>
|
||||
<button className="github-btn" onClick={handleClick}>
|
||||
<svg
|
||||
fill="currentColor"
|
||||
width="800px"
|
||||
|
||||
@@ -62,7 +62,6 @@ export default function Summary() {
|
||||
const handleEnd = () => {
|
||||
const achievements = end()
|
||||
toastAchvs(achievements)
|
||||
console.log('Achievements', achievements)
|
||||
}
|
||||
return (
|
||||
<div className="screen summary">
|
||||
|
||||
Vendored
+48
-8
@@ -1,6 +1,7 @@
|
||||
const PREFIX = 'bili-toy-remake'
|
||||
const storage = {
|
||||
get: async (keys: string[]) => {
|
||||
var result = await toy.getCloudStorage(keys)
|
||||
const result = await toy.getCloudStorage(keys)
|
||||
if (result && result.data && typeof result.data === 'object') {
|
||||
return result.data
|
||||
}
|
||||
@@ -24,19 +25,58 @@ export async function init() {
|
||||
}
|
||||
}
|
||||
|
||||
export async function get(key: string) {
|
||||
function getCloudInstance(key: string) {
|
||||
if (!cloudMap.has(key)) {
|
||||
const cloud = ToyCloudSaveCore.createCloudSave({ storage, prefix: key })
|
||||
cloudMap.set(key, cloud)
|
||||
}
|
||||
return await cloudMap.get(key)!.load()
|
||||
return cloudMap.get(key) || null
|
||||
}
|
||||
|
||||
export async function set(key: string, value: string) {
|
||||
if (!cloudMap.has(key)) {
|
||||
const cloud = ToyCloudSaveCore.createCloudSave({ storage, prefix: key })
|
||||
cloudMap.set(key, cloud)
|
||||
export async function get(key: string): Promise<string | null> {
|
||||
const localKey = `${PREFIX}-${key}`
|
||||
const localData = localStorage.getItem(localKey)
|
||||
|
||||
const cloud = getCloudInstance(key)
|
||||
if (!cloud) return localData
|
||||
try {
|
||||
const cloudData = await cloud.load()
|
||||
|
||||
if (cloudData !== null) {
|
||||
localStorage.setItem(localKey, cloudData)
|
||||
return cloudData
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[Bili Storage] 业务 Key '${key}' 云端拉取失败,降级使用本地档:`,
|
||||
error,
|
||||
)
|
||||
}
|
||||
return localData
|
||||
}
|
||||
|
||||
export async function set(key: string, value: string): Promise<boolean> {
|
||||
const localKey = `${PREFIX}-${key}`
|
||||
|
||||
try {
|
||||
localStorage.setItem(localKey, value)
|
||||
} catch (localError) {
|
||||
console.error(
|
||||
`[Bili Storage] 本地 LocalStorage 写入崩溃(可能满 5MB 额度):`,
|
||||
localError,
|
||||
)
|
||||
}
|
||||
|
||||
const cloud = getCloudInstance(key)
|
||||
if (!cloud) return true
|
||||
|
||||
try {
|
||||
await cloud.save(value)
|
||||
} catch (cloudError) {
|
||||
console.error(
|
||||
`[Bili Storage] 业务 Key '${key}' 本地已保存,但同步至 B 站云端失败:`,
|
||||
cloudError,
|
||||
)
|
||||
}
|
||||
await cloudMap.get(key)!.save(value)
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user