From d91274a6ddf1efc74675dc68d044d918ded06399 Mon Sep 17 00:00:00 2001 From: imeepos Date: Mon, 8 Sep 2025 16:22:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=E5=AE=A1=E6=A0=B8=E5=8A=9F=E8=83=BD=E7=9A=84=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E5=92=8C=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **类型安全优化**: - 为 handleAuditFailure 函数添加完整的 Record 类型 - 添加 AuditConclusion.PASS 枚举项以保持类型完整性 - 使用可选链操作符安全访问 auditResult.conclusion **错误处理优化**: - 改进 loadingState 类型检查,使用 typeof 确保类型安全 - 优化错误信息的条件判断逻辑 **文档更新**: - 添加 hooks 和 sdk 相关的 prompt.md 文档文件 --- src/hooks/prompt.md | 40 ++++++++++++++++++++ src/pages/home/index.tsx | 11 +++--- src/sdk/prompt.md | 82 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 src/hooks/prompt.md create mode 100644 src/sdk/prompt.md diff --git a/src/hooks/prompt.md b/src/hooks/prompt.md new file mode 100644 index 0000000..c59ea30 --- /dev/null +++ b/src/hooks/prompt.md @@ -0,0 +1,40 @@ + + +baseUrl: /api/v1/content-moderation + +接口:图片内容审核 +地址::platform/audit-image +返回:{taskId} + +平台: +export enum PlatformType { + WECHAT = 'wechat', // 微信小程序 - 腾讯微信生态 + ALIPAY = 'alipay', // 支付宝小程序 - 蚂蚁金服生态 + BAIDU = 'baidu', // 百度智能小程序 - 百度生态 + BYTEDANCE = 'bytedance', // 字节跳动小程序 - 抖音、今日头条等 + JD = 'jd', // 京东小程序 - 京东购物生态 + QQ = 'qq', // QQ小程序 - 腾讯QQ生态 + FEISHU = 'feishu', // 飞书小程序 - 企业办公生态 + KUAISHOU = 'kuaishou', // 快手小程序 - 快手短视频生态 + H5 = 'h5', // H5网页版 - 浏览器端应用 + RN = 'rn', // React Native应用 - 原生移动应用 +} + +接口:查询审核结果 +地址::platform/result/:taskId +返回:{status: AuditStatus, conclusion: AuditConclusion;} + +export enum AuditStatus { + PENDING = 'pending', // 待审核 + PROCESSING = 'processing', // 审核中 + COMPLETED = 'completed', // 审核完成 + FAILED = 'failed', // 审核失败 + TIMEOUT = 'timeout', // 审核超时 +} + +export enum AuditConclusion { + PASS = 'pass', // 通过 + REJECT = 'reject', // 拒绝 + REVIEW = 'review', // 人工复审 + UNCERTAIN = 'uncertain', // 不确定 +} \ No newline at end of file diff --git a/src/pages/home/index.tsx b/src/pages/home/index.tsx index d835148..0e2cf9b 100644 --- a/src/pages/home/index.tsx +++ b/src/pages/home/index.tsx @@ -76,13 +76,14 @@ export default function Home() { // 处理审核失败 const handleAuditFailure = (auditResult: ImageAuditResult) => { - const messages = { + const messages: Record = { [AuditConclusion.REJECT]: '图片内容不符合规范,请重新选择符合规范的图片', [AuditConclusion.REVIEW]: '图片需要人工审核,请稍后重试或更换其他图片', - [AuditConclusion.UNCERTAIN]: '图片审核结果不确定,请重新选择图片' + [AuditConclusion.UNCERTAIN]: '图片审核结果不确定,请重新选择图片', + [AuditConclusion.PASS]: '图片审核通过' // 虽然不会用到,但为了类型完整性 } - const message = messages[auditResult.conclusion!] || '图片审核失败,请重新选择图片' + const message = auditResult.conclusion ? messages[auditResult.conclusion] : '图片审核失败,请重新选择图片' Taro.showModal({ title: '图片审核未通过', @@ -201,13 +202,13 @@ export default function Home() { // 统一错误处理 Taro.hideLoading() - if (loadingState === 'uploading') { + if (typeof loadingState === 'string' && loadingState === 'uploading') { Taro.showToast({ title: '图片上传失败,请重试', icon: 'error', duration: 2000 }) - } else if (loadingState === 'auditing') { + } else if (typeof loadingState === 'string' && loadingState === 'auditing') { Taro.showToast({ title: '图片审核失败,请重试', icon: 'error', diff --git a/src/sdk/prompt.md b/src/sdk/prompt.md new file mode 100644 index 0000000..311d3bf --- /dev/null +++ b/src/sdk/prompt.md @@ -0,0 +1,82 @@ + +请求地址: +const baseUrl = 'https://bowongai-test--text-video-agent-fastapi-app.modal.run'; +const url = `${baseUrl}/api/file/upload/s3`; + + +```实例代码 + async upload(params: UploadParams): Promise { + const { filePath, name, type, onProgress } = params; + const url = `${this.baseUrl}/api/file/upload/s3`; + + try { + // 获取文件信息 + const fileInfo = await Taro.getFileInfo({ filePath }); + if (!isGetFileInfoSuccessCallbackResult(fileInfo)) { + throw new Error(fileInfo.errMsg) + } + // 自动生成文件名(如果未提供) + const fileName = name || `upload_${Date.now()}.${this._getFileExtension(filePath)}`; + + // 自动判断文件类型(如果未提供) + const fileType = type || this._getMimeType(filePath); + + console.log('开始上传文件:', { + fileName, + fileSize: fileInfo.size, + fileType + }); + + // 使用 Taro.uploadFile 进行文件上传 + const response = await new Promise((resolve, reject) => { + const uploadTask = Taro.uploadFile({ + url, + filePath, + name: 'file', // 服务端接收的字段名 + header: { + 'Accept': 'application/json' + }, + formData: { + filename: fileName, + type: fileType + }, + success: resolve, + fail: reject + }); + + // 监听上传进度 + if (onProgress) { + uploadTask.progress((res) => { + const progress = Math.round((res.totalBytesSent / res.totalBytesExpectedToSend) * 100); + onProgress(progress); + }); + } + }); + + // 检查 HTTP 状态码 + if (response.statusCode !== 200) { + throw new Error(`HTTP ${response.statusCode}: 文件上传失败`); + } + + // 解析响应数据 + let result: ApiResponse; + try { + result = JSON.parse(response.data) as ApiResponse; + } catch (parseError) { + throw new Error('服务器响应格式错误'); + } + + // 检查业务状态码 + if (!result.status) { + throw new Error(result.msg || '文件上传失败'); + } + + console.log('文件上传成功:', result.data); + return result.data; + + } catch (error) { + console.error('文件上传失败:', error); + throw error; + } + } +``` \ No newline at end of file