feat: 初始化刷新后覆盖本地funds数据到云端
This commit is contained in:
32
app/page.jsx
32
app/page.jsx
@@ -1459,14 +1459,19 @@ export default function HomePage() {
|
||||
userIdRef.current = user?.id || null;
|
||||
}, [user]);
|
||||
|
||||
const getFundCodesSignature = useCallback((value) => {
|
||||
const getFundCodesSignature = useCallback((value, extraFields = []) => {
|
||||
try {
|
||||
const list = JSON.parse(value || '[]');
|
||||
const list = Array.isArray(value) ? value : JSON.parse(value || '[]');
|
||||
if (!Array.isArray(list)) return '';
|
||||
const fields = Array.from(new Set([
|
||||
'jzrq',
|
||||
'dwjz',
|
||||
...(Array.isArray(extraFields) ? extraFields : [])
|
||||
]));
|
||||
const items = list.map((item) => {
|
||||
if (!item?.code) return null;
|
||||
// 加入 jzrq 和 dwjz 以检测净值更新
|
||||
return `${item.code}:${item.jzrq || ''}:${item.dwjz || ''}`;
|
||||
const extras = fields.map((field) => item?.[field] || '').join(':');
|
||||
return `${item.code}:${extras}`;
|
||||
}).filter(Boolean);
|
||||
return Array.from(new Set(items)).join('|');
|
||||
} catch (e) {
|
||||
@@ -3027,6 +3032,25 @@ export default function HomePage() {
|
||||
if (nextFunds.length) {
|
||||
const codes = Array.from(new Set(nextFunds.map((f) => f.code)));
|
||||
if (codes.length) await refreshAll(codes);
|
||||
// 刷新完成后,强制同步本地localStorage 的 funds 数据到云端
|
||||
const currentUserId = userIdRef.current || user?.id;
|
||||
if (currentUserId) {
|
||||
try {
|
||||
const latestFunds = JSON.parse(localStorage.getItem('funds') || '[]');
|
||||
const localSig = getFundCodesSignature(latestFunds, ['gztime']);
|
||||
const cloudSig = getFundCodesSignature(Array.isArray(cloudData.funds) ? cloudData.funds : [], ['gztime']);
|
||||
if (localSig !== cloudSig) {
|
||||
await syncUserConfig(
|
||||
currentUserId,
|
||||
false,
|
||||
{ funds: Array.isArray(latestFunds) ? latestFunds : [] },
|
||||
true
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('刷新后强制同步 funds 到云端失败', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const payload = collectLocalPayload();
|
||||
|
||||
Reference in New Issue
Block a user