diff --git a/src/pages/api/ApiDebugger.vue b/src/pages/api/ApiDebugger.vue index f30a589..8117f39 100644 --- a/src/pages/api/ApiDebugger.vue +++ b/src/pages/api/ApiDebugger.vue @@ -181,17 +181,17 @@ * - -
+ +
- 上传图片(JPG/BMP/PNG) + {{ getUploadButtonTextByField(field) }} @@ -423,7 +423,7 @@
-
@@ -749,12 +749,12 @@ watch( if (newProductId === oldProductId) { return } - + // 如果正在选择产品,不重复执行 if (isSelectingProduct.value) { return } - + if (newProductId && userProducts.value.length > 0) { await autoSelectProduct(newProductId) } else if (!newProductId && userProducts.value.length > 0 && !selectedProduct.value) { @@ -771,13 +771,13 @@ const autoSelectProduct = async (productId) => { console.log('正在选择产品,跳过重复请求') return } - + // 如果已经选择了相同的产品,不重复选择 if (lastSelectedProductId.value === productId && selectedProduct.value) { console.log('产品已选择,跳过重复选择:', productId) return } - + // 如果用户产品列表为空,等待加载完成 if (!userProducts.value.length) { console.log('等待用户产品列表加载完成...') @@ -883,13 +883,13 @@ const loadApiKeys = async () => { const selectProduct = async (product) => { // 防止重复选择相同产品 const productId = product.product_id || product.id - if (selectedProduct.value && + if (selectedProduct.value && (selectedProduct.value.id === productId || selectedProduct.value.product_id === productId) && !isSelectingProduct.value) { console.log('产品已选择,跳过重复加载:', productId) return } - + // 确保API密钥已经加载 if (!debugForm.accessId || !debugForm.secretKey) { ElMessage.warning('正在加载API密钥,请稍候...') @@ -934,24 +934,51 @@ const loadFormConfig = async (apiCode) => { } } -// 处理图片上传并转换为base64 -const handleImageUpload = (file, fieldName) => { +const getFieldValidationText = (field) => { + return typeof field?.validation === 'string' ? field.validation : '' +} + +const isBase64ImageOnlyField = (field) => { + return getFieldValidationText(field).includes('Base64图片') +} + +const isBase64UploadField = (field) => { + if (field?.type !== 'textarea') return false + const validationText = getFieldValidationText(field) + return validationText.includes('Base64图片') || validationText.includes('Base64编码') || validationText.toLowerCase().includes('base64') +} + +const getUploadAcceptByField = (field) => { + if (isBase64ImageOnlyField(field)) { + return 'image/jpeg,image/jpg,image/png,image/bmp' + } + return 'image/jpeg,image/jpg,image/png,image/bmp,application/pdf,.pdf' +} + +const getUploadButtonTextByField = (field) => { + return isBase64ImageOnlyField(field) ? '上传图片(JPG/BMP/PNG)' : '上传文件(JPG/BMP/PNG/PDF)' +} + +// 处理文件上传并转换为base64(支持按字段规则限制类型) +const handleImageUpload = (file, fieldName, field) => { const fileObj = file.raw || file - + // 验证文件类型 - const allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/bmp'] + const allowedTypes = isBase64ImageOnlyField(field) + ? ['image/jpeg', 'image/jpg', 'image/png', 'image/bmp'] + : ['image/jpeg', 'image/jpg', 'image/png', 'image/bmp', 'application/pdf'] if (!allowedTypes.includes(fileObj.type)) { - ElMessage.error('只支持 JPG、BMP、PNG 格式的图片') + ElMessage.error(isBase64ImageOnlyField(field) ? '只支持 JPG、BMP、PNG 格式的图片' : '只支持 JPG、BMP、PNG、PDF 格式的文件') return false } - + // 验证文件大小(限制为5MB) const maxSize = 5 * 1024 * 1024 // 5MB if (fileObj.size > maxSize) { - ElMessage.error('图片大小不能超过 5MB') + ElMessage.error('文件大小不能超过 5MB') return false } - + // 读取文件并转换为base64 const reader = new FileReader() reader.onload = (e) => { @@ -959,13 +986,13 @@ const handleImageUpload = (file, fieldName) => { // 移除 data:image/xxx;base64, 前缀,只保留纯base64数据 const base64Data = base64String.includes(',') ? base64String.split(',')[1] : base64String formData.value[fieldName] = base64Data - ElMessage.success('图片上传成功,已转换为base64') + ElMessage.success('文件上传成功,已转换为base64') } reader.onerror = () => { - ElMessage.error('图片读取失败,请重试') + ElMessage.error('文件读取失败,请重试') } reader.readAsDataURL(fileObj) - + return false // 阻止自动上传 } @@ -1142,7 +1169,7 @@ const convertFieldTypes = (data) => { if (!formFields.value || formFields.value.length === 0) { return data } - + const processedData = { ...data } formFields.value.forEach(field => { const value = processedData[field.name] @@ -1150,7 +1177,7 @@ const convertFieldTypes = (data) => { if (value === '' || value === null || value === undefined) { return } - + // 根据字段类型进行转换 if (field.type === 'number') { // 将字符串转换为数字(整数) @@ -1160,7 +1187,7 @@ const convertFieldTypes = (data) => { } } }) - + return processedData } @@ -1197,7 +1224,7 @@ const encryptWithAES = async (data, secretKey) => { // 解析JSON字符串(如果是字符串) let parsedData = typeof data === 'string' ? JSON.parse(data) : data - + // 根据字段类型进行类型转换 parsedData = convertFieldTypes(parsedData) @@ -1282,7 +1309,7 @@ const handleDebug = async () => { debugResult.value = null decryptedData.value = null await nextTick() // 确保DOM更新 - + const startTime = new Date() try {