ai-coding
2026-07-29 160537ccf9380a8002885f1edf73ae970d247cd4
fix(feedback): 图片压缩改为基于原图并跳过非JPG格式

Co-Authored-By: 何宏文 <hehw@xiaopeng.com>
1个文件已修改
27 ■■■■■ 已修改文件
src/robotaxi/pages/feedback/hooks/use-media-upload.ts 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/robotaxi/pages/feedback/hooks/use-media-upload.ts
@@ -73,30 +73,39 @@
   * 压缩图片到体积限制内。
   */
  const ensureImageSizeUnderLimit = useCallback(async (filePath: string) => {
    // iOS 端 Taro.compressImage 仅支持 JPG/JPEG,其他格式直接使用原图上传。
    if (!/\.(?:jpe?g)(?:[?#].*)?$/i.test(filePath)) {
      return filePath;
    }
    const sizeLimitBytes = options.imageSizeLimitBytes || DEFAULT_IMAGE_SIZE_LIMIT_BYTES;
    try {
      let currentFilePath = filePath;
      const currentSize = await getFileSize(currentFilePath);
      const originalFilePath = filePath;
      let lastCompressedFilePath = filePath;
      const currentSize = await getFileSize(originalFilePath);
      if (currentSize <= sizeLimitBytes) {
        return currentFilePath;
        return originalFilePath;
      }
      for (const quality of IMAGE_COMPRESS_QUALITIES) {
        try {
          // 每次都基于原图压缩,避免 80 -> 60 -> 40 的代际压缩导致画质累积劣化。
          const compressResult: any = await Taro.compressImage({
            src: currentFilePath,
            src: originalFilePath,
            quality
          });
          const compressedPath = compressResult?.tempFilePath || currentFilePath;
          const compressedPath = compressResult?.tempFilePath;
          if (!compressedPath) {
            continue;
          }
          lastCompressedFilePath = compressedPath;
          const compressedSize = await getFileSize(compressedPath);
          currentFilePath = compressedPath;
          if (compressedSize <= sizeLimitBytes) {
            return currentFilePath;
            return compressedPath;
          }
        } catch (error) {
          // 单次压缩失败时继续尝试更低质量。
          // 单次压缩失败时继续基于原图尝试更低质量。
        }
      }
      return currentFilePath;
      return lastCompressedFilePath;
    } catch (error) {
      return filePath;
    }