Просмотр исходного кода

fix: resolveAuthId fallback - find old APIGAuth records by api and migrate user pointer

gangvy 5 месяцев назад
Родитель
Сommit
01917e7c4a
1 измененных файлов с 42 добавлено и 3 удалено
  1. 42 3
      src/app/app.ts

+ 42 - 3
src/app/app.ts

@@ -269,7 +269,7 @@ export class App implements OnInit {
 
   // ─── 通过 user+apigid 查询或创建 APIGAuth ───
   async resolveAuthId(user: string, apig: string): Promise<string | null> {
-    // 1. 查询已有 APIGAuth
+    // 1. 查询已有 APIGAuth(按 user 指针)
     try {
       const query: any = {
         where: JSON.stringify({
@@ -290,9 +290,48 @@ export class App implements OnInit {
       console.warn('[resolveAuthId] 查询失败:', e.message);
     }
 
-    // 2. 创建新 APIGAuth
+    // 2. 回退:按 api 查询所有记录(兼容旧 company 记录或不同 user 记录)
+    //    找到余额最多的旧记录,迁移 user 指针到当前用户
     try {
-      console.log('[resolveAuthId] 未找到 APIGAuth,创建新记录...');
+      const fallbackQuery: any = {
+        where: JSON.stringify({
+          api: { __type: 'Pointer', className: 'APIG', objectId: apig }
+        }),
+        order: '-count',
+        limit: '10'
+      };
+      const fbResp = await fetch(API_BASE + '/parse/classes/APIGAuth?' + new URLSearchParams(fallbackQuery), {
+        headers: { 'X-Parse-Application-Id': APP_ID }
+      });
+      const fbData = await fbResp.json();
+      if (fbData.results?.length > 0) {
+        // 选余额最高的记录,迁移到当前 user
+        const best = fbData.results[0];
+        console.log('[resolveAuthId] 找到旧记录,迁移到当前用户:', best.objectId, 'count:', best.count);
+        try {
+          await fetch(API_BASE + '/parse/classes/APIGAuth/' + best.objectId, {
+            method: 'PUT',
+            headers: {
+              'X-Parse-Application-Id': APP_ID,
+              'Content-Type': 'application/json'
+            },
+            body: JSON.stringify({
+              user: { __type: 'Pointer', className: '_User', objectId: user }
+            })
+          });
+          console.log('[resolveAuthId] 迁移成功');
+        } catch (e: any) {
+          console.warn('[resolveAuthId] 迁移 user 指针失败(不影响使用):', e.message);
+        }
+        return best.objectId;
+      }
+    } catch (e: any) {
+      console.warn('[resolveAuthId] 回退查询失败:', e.message);
+    }
+
+    // 3. 创建新 APIGAuth
+    try {
+      console.log('[resolveAuthId] 未找到任何 APIGAuth,创建新记录...');
       const createResp = await fetch(API_BASE + '/parse/classes/APIGAuth', {
         method: 'POST',
         headers: {