Browse Source

fix(case): 案件 ID 改用 Long/字符串,修复新建案件打开报错

- 后端 OpenCaseDTO/UpdateCasePwdDTO 的 id 改为 Long,匹配雪花 ID
- 前端案件 ID 全链路按字符串处理,去掉 Number() 精度丢失
- 排序、查找、删除均改为字符串比较
cc 3 weeks ago
parent
commit
7684cb6ccf

+ 9 - 4
ai-electron/frontend/src/core/layouts/default/setting/components/ChangeCasePasswordModal.vue

@@ -51,7 +51,7 @@
   const confirmPwd = ref('');
   const newPwdError = ref('');
   const confirmPwdError = ref('');
-  const currentCaseId = ref<number | null>(null);
+  const currentCaseId = ref<string | null>(null);
   const currentCaseName = ref('');
 
   const [registerModal, { closeModal, setModalProps, getOpen }] = useModalInner();
@@ -60,6 +60,11 @@
     return value?.trim().toLocaleLowerCase() || '';
   }
 
+  /** 案件 ID 是雪花 Long,超出 JS number 安全整数范围:按定长字符串比较,避免精度丢失 */
+  const normalizeCaseId = (value?: string | number | null) => String(value ?? '0').padStart(20, '0');
+  const compareCaseIdDesc = (left: { id?: string }, right: { id?: string }) =>
+    normalizeCaseId(right.id) > normalizeCaseId(left.id) ? 1 : normalizeCaseId(right.id) < normalizeCaseId(left.id) ? -1 : 0;
+
   function resetForm() {
     newPwd.value = '';
     confirmPwd.value = '';
@@ -67,7 +72,7 @@
     confirmPwdError.value = '';
     const caseInfo = caseStore.getCaseInfo;
     currentCaseName.value = caseInfo?.name || '';
-    currentCaseId.value = caseInfo?.id ? Number(caseInfo.id) : null;
+    currentCaseId.value = caseInfo?.id ? String(caseInfo.id) : null;
   }
 
   async function resolveCurrentCaseId() {
@@ -80,10 +85,10 @@
 
     const caseList = await list();
     const matchedCase = [...(caseList || [])]
-      .sort((left, right) => Number(right.id || 0) - Number(left.id || 0))
+      .sort(compareCaseIdDesc)
       .find((item) => normalizeCaseName(item.name) === normalizeCaseName(currentCaseName.value));
 
-    currentCaseId.value = matchedCase?.id ? Number(matchedCase.id) : null;
+    currentCaseId.value = matchedCase?.id ? String(matchedCase.id) : null;
     return currentCaseId.value;
   }
 

+ 7 - 5
ai-electron/frontend/src/core/store/modules/case.ts

@@ -73,16 +73,18 @@ export const useUserStore = defineStore('app-user', {
       const normalizedName = String(info?.name || '').trim();
       if (normalizedName) {
         if (caseId && !info.id) {
-          info.id = Number(caseId);
+          // 案件 ID 是雪花 Long,必须保持字符串,Number() 会丢失精度
+          info.id = String(caseId);
         }
         return info;
       }
 
       const cachedCaseInfo = getAuthCache<CaseInfo>(CASE_INFO_KEY) || ({} as CaseInfo);
-      const targetCaseId = Number(caseId || info?.id || cachedCaseInfo?.id || 0);
+      const rawCaseId = caseId || info?.id || cachedCaseInfo?.id;
+      const targetCaseId = rawCaseId === undefined || rawCaseId === null ? '' : String(rawCaseId).trim();
       const cachedCaseName = String(this.getPageCacheByKey('currentCaseName', '') || cachedCaseInfo?.name || '').trim();
 
-      if (cachedCaseName && (!targetCaseId || Number(cachedCaseInfo?.id || 0) === targetCaseId)) {
+      if (cachedCaseName && (!targetCaseId || String(cachedCaseInfo?.id || '') === targetCaseId)) {
         return {
           ...cachedCaseInfo,
           ...info,
@@ -97,7 +99,7 @@ export const useUserStore = defineStore('app-user', {
 
       try {
         const caseList = await listCases();
-        const matchedCase = (caseList || []).find((item) => Number(item.id || 0) === targetCaseId);
+        const matchedCase = (caseList || []).find((item) => String(item.id || '') === targetCaseId);
         if (matchedCase?.name) {
           return {
             ...matchedCase,
@@ -140,7 +142,7 @@ export const useUserStore = defineStore('app-user', {
     async hydrateCurrentCaseName() {
       const caseInfo = this.getCaseInfo || ({} as CaseInfo);
       const caseName = String(caseInfo?.name || this.getPageCacheByKey('currentCaseName', '') || '').trim();
-      const caseId = Number(caseInfo?.id || 0);
+      const caseId = String(caseInfo?.id || '').trim();
       if (caseName || !caseId) {
         return caseName;
       }

+ 11 - 11
ai-electron/frontend/src/plat/api/case/caseApi.ts

@@ -8,9 +8,9 @@ const { adminPath } = useGlobSetting();
  */
 export interface CaseInfo {
   /**
-   * 案件 ID
+   * 案件 ID(后端雪花算法 Long,JSON 中以字符串返回;前端必须保持字符串,避免 number 精度丢失)
    */
-  id?: number;
+  id?: string;
   /**
    * 案件名称
    */
@@ -69,9 +69,9 @@ export interface CreateCaseDTO {
  */
 export interface UpdateCaseDTO {
   /**
-   * 案件 ID(更新时必填)
+   * 案件 ID(更新时必填,雪花 Long 用字符串传递)
    */
-  id: number;
+  id: string;
   /**
    * 案件名称
    */
@@ -95,9 +95,9 @@ export interface UpdateCaseDTO {
  */
 export interface UpdateCasePwdDTO {
   /**
-   * 案件 ID
+   * 案件 ID(雪花 Long 用字符串传递)
    */
-  id: number;
+  id: string;
   /**
    * 新密码
    */
@@ -113,9 +113,9 @@ export interface UpdateCasePwdDTO {
  */
 export interface OpenCaseDTO {
   /**
-   * 案件 ID
+   * 案件 ID(雪花 Long 用字符串传递)
    */
-  id: number;
+  id: string;
   /**
    * 案件密码。
    * @deprecated 案件已归属创建者(列表只返回自己的案件),后端**不再校验**案件密码,
@@ -129,9 +129,9 @@ export interface OpenCaseDTO {
  */
 export interface DeleteCaseParams {
   /**
-   * 案件 ID
+   * 案件 ID(雪花 Long 用字符串传递)
    */
-  id: number;
+  id: string;
   /**
    * 案件密码
    */
@@ -168,7 +168,7 @@ export const update = (data: UpdateCaseDTO) =>
  * @param id 案件 ID
  * @param pwd 案件密码
  */
-export const deleteById = (id: number) => defHttp.get<void>({ url: adminPath + '/case/delete', params: { id } });
+export const deleteById = (id: string) => defHttp.get<void>({ url: adminPath + '/case/delete', params: { id } });
 
 /**
  * 更新案件密码

+ 12 - 4
ai-electron/frontend/src/plat/views/case/CaseForm.vue

@@ -45,6 +45,14 @@
   }
 
   const normalizeCaseName = (value?: string) => value?.trim().toLocaleLowerCase() || '';
+
+  /**
+   * 案件 ID 是后端雪花 Long,超出 JS number 安全整数范围;
+   * 这里按定长字符串比较,避免 Number() 精度丢失导致按 ID 排序/查找错位。
+   */
+  const normalizeCaseId = (value?: string | number | null) => String(value ?? '0').padStart(20, '0');
+  const compareCaseIdDesc = (left: CaseInfo, right: CaseInfo) =>
+    normalizeCaseId(right.id) > normalizeCaseId(left.id) ? 1 : normalizeCaseId(right.id) < normalizeCaseId(left.id) ? -1 : 0;
   const defaultCasePwd = '123456';
 
   const typeOptions = [
@@ -170,7 +178,7 @@
     const latestCaseList = await list();
     const normalizedName = normalizeCaseName(caseName);
     return [...(latestCaseList || [])]
-      .sort((left, right) => Number(right.id || 0) - Number(left.id || 0))
+      .sort(compareCaseIdDesc)
       .find((item) => normalizeCaseName(item.name) === normalizedName);
   }
 
@@ -184,7 +192,7 @@
         // 编辑模式:需要id,调用更新接口
         const submittedPwd = data.pwd?.trim() || '';
         const updateData: UpdateCaseDTO = {
-          id: Number(currentCaseId.value),
+          id: currentCaseId.value,
           name: caseName,
           pwd: submittedPwd,
           type: data.type || typeOptions[0]?.value,
@@ -194,7 +202,7 @@
 
         // 如果是打开案件时触发的编辑,保存后直接打开案件
         if (needPwdForOpen.value) {
-          emit('openCase', { id: Number(currentCaseId.value), pwd: submittedPwd });
+          emit('openCase', { id: currentCaseId.value, pwd: submittedPwd });
         }
       } else {
         // 新增模式
@@ -212,7 +220,7 @@
           waitingAutoOpen.value = true;
           safeSetModalProps({ confirmLoading: true, loading: true, closable: false });
           emit('openCase', {
-            id: Number(createdCase.id),
+            id: String(createdCase.id),
             pwd: createdPwd,
             onFinished: (success: boolean) => {
               waitingAutoOpen.value = false;

+ 15 - 10
ai-electron/frontend/src/plat/views/case/case.vue

@@ -217,9 +217,9 @@
   const loading = ref(false);
   const deleteModalVisible = ref(false);
   const deleteLoading = ref(false);
-  const currentDeleteId = ref<number>(0);
+  const currentDeleteId = ref<string>('');
   /** 正在打开的案件 id:打开期间禁用其它打开操作并显示全局 loading */
-  const openingCaseId = ref<number | null>(null);
+  const openingCaseId = ref<string | null>(null);
   const [showOpeningLoading, hideOpeningLoading, setOpeningTip] = useLoading({
     tip: '正在打开案件…',
   });
@@ -251,11 +251,16 @@
     const timestamp = new Date(rawTime.replace(/-/g, '/')).getTime();
     return Number.isNaN(timestamp) ? 0 : timestamp;
   }
+  /** 案件 ID 是雪花 Long,超出 JS number 安全整数范围:按定长字符串比较,避免精度丢失 */
+  const normalizeCaseId = (value?: string) => String(value ?? '0').padStart(20, '0');
+  const compareCaseIdDesc = (left: CaseInfo, right: CaseInfo) =>
+    normalizeCaseId(right.id) > normalizeCaseId(left.id) ? 1 : normalizeCaseId(right.id) < normalizeCaseId(left.id) ? -1 : 0;
+
   const filteredCaseList = computed(() => {
     const sortedList = [...caseList.value].sort((left, right) => {
       const timeDiff = getCaseSortTime(right) - getCaseSortTime(left);
       if (timeDiff !== 0) return timeDiff;
-      return Number(right.id || 0) - Number(left.id || 0);
+      return compareCaseIdDesc(left, right);
     });
 
     if (!searchKeyword.value) {
@@ -281,7 +286,7 @@
     return classMap[type || ''] || '';
   }
 
-  async function handleDelete(id: number) {
+  async function handleDelete(id: string) {
     currentDeleteId.value = id;
     deleteModalVisible.value = true;
   }
@@ -291,7 +296,7 @@
     try {
       // 案件已归属个人,后端不再校验案件密码
       await deleteById(currentDeleteId.value);
-      const index = caseList.value.findIndex((item) => item.id === currentDeleteId.value);
+      const index = caseList.value.findIndex((item) => String(item.id || '') === String(currentDeleteId.value));
       if (index > -1) {
         caseList.value.splice(index, 1);
       }
@@ -305,21 +310,21 @@
 
   function cancelDelete() {
     deleteModalVisible.value = false;
-    currentDeleteId.value = 0;
+    currentDeleteId.value = '';
   }
 
   /** 打开案件:案件归属当前用户,无需再输案件密码 */
-  async function handleOpen(id: number) {
+  async function handleOpen(id: string) {
     await doOpenCase(id);
   }
 
-  async function doOpenCase(id: number) {
+  async function doOpenCase(id: string) {
     // 打开案件要初始化数据源、重建路由并跳转,期间给一层全局 loading,避免界面像卡死
     if (openingCaseId.value !== null) {
       return false;
     }
 
-    const matchedCase = caseList.value.find((item) => Number(item.id) === Number(id));
+    const matchedCase = caseList.value.find((item) => String(item.id || '') === String(id));
     openingCaseId.value = id;
     setOpeningTip(matchedCase?.name ? `正在打开「${matchedCase.name}」…` : '正在打开案件…');
     showOpeningLoading();
@@ -367,7 +372,7 @@
     loadCaseList();
   }
 
-  async function handleOpenCreatedCase(payload: { id: number; onFinished?: (success: boolean) => void }) {
+  async function handleOpenCreatedCase(payload: { id: string; onFinished?: (success: boolean) => void }) {
     const success = await doOpenCase(payload.id);
     payload.onFinished?.(success);
   }

+ 2 - 2
ai-server/src/main/java/com/zsjz/ai/common/model/plat/dto/OpenCaseDTO.java

@@ -12,8 +12,8 @@ import lombok.Data;
 @Data
 public class OpenCaseDTO {
 
-    /** 案件 ID */
-    private Integer id;
+    /** 案件 ID(雪花算法 Long,前端以字符串传递,避免 JS number 精度丢失) */
+    private Long id;
 
     /** 案件密码 */
     private String pwd;

+ 2 - 1
ai-server/src/main/java/com/zsjz/ai/common/model/plat/dto/UpdateCasePwdDTO.java

@@ -7,6 +7,7 @@ import lombok.Data;
  */
 @Data
 public class UpdateCasePwdDTO {
-    private Integer id;
+    /** 案件 ID(雪花算法 Long,前端以字符串传递,避免 JS number 精度丢失) */
+    private Long id;
     private String newPwd;
 }

+ 1 - 1
ai-server/src/main/java/com/zsjz/ai/module/plat/service/CaseInfoService.java

@@ -224,7 +224,7 @@ public class CaseInfoService extends ServiceImpl<CaseInfoMapper, CaseInfo> {
      *
      * <p>人员库缓存是可选加速项,初始化失败只记日志,不阻断开案。
      */
-    public void open(Integer id) {
+    public void open(Long id) {
         Long ownerId = requireUserId();
         CaseInfo ci = getById(id);
         Assert.notNull(ci, () -> ServerException.spe("案件不存在!"));