|
@@ -290,40 +290,65 @@ export class App implements OnInit {
|
|
|
console.warn('[resolveAuthId] 查询失败:', e.message);
|
|
console.warn('[resolveAuthId] 查询失败:', e.message);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 2. 回退:按 api 查询所有记录(兼容旧 company 记录或不同 user 记录)
|
|
|
|
|
- // 找到余额最多的旧记录,迁移 user 指针到当前用户
|
|
|
|
|
|
|
+ // 2. 回退:按 api 查询所有记录(兼容旧 company / 不同 user 的记录)
|
|
|
|
|
+ // 合并所有记录的 count 和 used 到一条主记录,绑定当前 user,删除多余记录
|
|
|
try {
|
|
try {
|
|
|
const fallbackQuery: any = {
|
|
const fallbackQuery: any = {
|
|
|
where: JSON.stringify({
|
|
where: JSON.stringify({
|
|
|
api: { __type: 'Pointer', className: 'APIG', objectId: apig }
|
|
api: { __type: 'Pointer', className: 'APIG', objectId: apig }
|
|
|
}),
|
|
}),
|
|
|
order: '-count',
|
|
order: '-count',
|
|
|
- limit: '10'
|
|
|
|
|
|
|
+ limit: '20'
|
|
|
};
|
|
};
|
|
|
const fbResp = await fetch(API_BASE + '/parse/classes/APIGAuth?' + new URLSearchParams(fallbackQuery), {
|
|
const fbResp = await fetch(API_BASE + '/parse/classes/APIGAuth?' + new URLSearchParams(fallbackQuery), {
|
|
|
headers: { 'X-Parse-Application-Id': APP_ID }
|
|
headers: { 'X-Parse-Application-Id': APP_ID }
|
|
|
});
|
|
});
|
|
|
const fbData = await fbResp.json();
|
|
const fbData = await fbResp.json();
|
|
|
- if (fbData.results?.length > 0) {
|
|
|
|
|
- // 选余额最高的记录,迁移到当前 user
|
|
|
|
|
- const best = fbData.results[0];
|
|
|
|
|
- console.log('[resolveAuthId] 找到旧记录,迁移到当前用户:', best.objectId, 'count:', best.count);
|
|
|
|
|
|
|
+ const records = fbData.results || [];
|
|
|
|
|
+
|
|
|
|
|
+ if (records.length > 0) {
|
|
|
|
|
+ // 汇总所有记录的 count 和 used
|
|
|
|
|
+ let totalCount = 0;
|
|
|
|
|
+ let totalUsed = 0;
|
|
|
|
|
+ for (const r of records) {
|
|
|
|
|
+ totalCount += (r.count || 0);
|
|
|
|
|
+ totalUsed += (r.used || 0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 选第一条(count 最高)作为主记录
|
|
|
|
|
+ const primary = records[0];
|
|
|
|
|
+ console.log(`[resolveAuthId] 找到 ${records.length} 条旧记录,合并余额: count=${totalCount}, used=${totalUsed}`);
|
|
|
|
|
+
|
|
|
|
|
+ // 更新主记录:绑定当前 user + 合并余额
|
|
|
try {
|
|
try {
|
|
|
- await fetch(API_BASE + '/parse/classes/APIGAuth/' + best.objectId, {
|
|
|
|
|
|
|
+ await fetch(API_BASE + '/parse/classes/APIGAuth/' + primary.objectId, {
|
|
|
method: 'PUT',
|
|
method: 'PUT',
|
|
|
- headers: {
|
|
|
|
|
- 'X-Parse-Application-Id': APP_ID,
|
|
|
|
|
- 'Content-Type': 'application/json'
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ headers: { 'X-Parse-Application-Id': APP_ID, 'Content-Type': 'application/json' },
|
|
|
body: JSON.stringify({
|
|
body: JSON.stringify({
|
|
|
- user: { __type: 'Pointer', className: '_User', objectId: user }
|
|
|
|
|
|
|
+ user: { __type: 'Pointer', className: '_User', objectId: user },
|
|
|
|
|
+ count: totalCount,
|
|
|
|
|
+ used: totalUsed
|
|
|
})
|
|
})
|
|
|
});
|
|
});
|
|
|
- console.log('[resolveAuthId] 迁移成功');
|
|
|
|
|
|
|
+ console.log('[resolveAuthId] 主记录已更新:', primary.objectId);
|
|
|
} catch (e: any) {
|
|
} catch (e: any) {
|
|
|
- console.warn('[resolveAuthId] 迁移 user 指针失败(不影响使用):', e.message);
|
|
|
|
|
|
|
+ console.warn('[resolveAuthId] 更新主记录失败:', e.message);
|
|
|
}
|
|
}
|
|
|
- return best.objectId;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 删除多余记录
|
|
|
|
|
+ for (let i = 1; i < records.length; i++) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ await fetch(API_BASE + '/parse/classes/APIGAuth/' + records[i].objectId, {
|
|
|
|
|
+ method: 'DELETE',
|
|
|
|
|
+ headers: { 'X-Parse-Application-Id': APP_ID }
|
|
|
|
|
+ });
|
|
|
|
|
+ console.log('[resolveAuthId] 删除重复记录:', records[i].objectId);
|
|
|
|
|
+ } catch (e: any) {
|
|
|
|
|
+ console.warn('[resolveAuthId] 删除失败:', records[i].objectId, e.message);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return primary.objectId;
|
|
|
}
|
|
}
|
|
|
} catch (e: any) {
|
|
} catch (e: any) {
|
|
|
console.warn('[resolveAuthId] 回退查询失败:', e.message);
|
|
console.warn('[resolveAuthId] 回退查询失败:', e.message);
|