wzz 1 месяц назад
Родитель
Сommit
eb0d96dab9
2 измененных файлов с 24 добавлено и 13 удалено
  1. 15 3
      src/pages/selfCenter/upload.vue
  2. 9 10
      src/utils/request.js

+ 15 - 3
src/pages/selfCenter/upload.vue

@@ -30,7 +30,7 @@
 </template>
 
 <script>
-import { uploadImage, currentOpenId } from '@/utils/request.js'
+import { request, uploadImage, currentOpenId } from '@/utils/request.js'
 
 const MAX_COUNT = 9
 
@@ -89,9 +89,21 @@ export default {
       this.images = failedPaths
       this.showResult(successCount, failedPaths.length)
     },
+    // 两步:1) uni.uploadFile 传图换 attach_id  2) 带着 openId + attach_id 调 report&action=add 登记
     async uploadOne(filePath) {
-      const res = await uploadImage(filePath, { openId: currentOpenId() })
-      return res.code === 200
+      const uploadRes = await uploadImage(filePath)
+      // TODO-DIAG: 确认第一步返回结构(attach_id 在哪个字段)的临时日志,确认后删除
+      console.log('UPLOAD_DEBUG step1 result=', JSON.stringify(uploadRes))
+      if (uploadRes.notJson || uploadRes.code !== 200) return false
+      const data = uploadRes.data
+      const attachId = data && (data.id || data.attach_id)
+      if (!attachId) return false
+      const addRes = await request('report', 'add', {
+        openId: currentOpenId(),
+        attach_id: attachId,
+      })
+      console.log('UPLOAD_DEBUG step2 result=', JSON.stringify(addRes))
+      return addRes.code === 200
     },
     showResult(successCount, failCount) {
       let title

+ 9 - 10
src/utils/request.js

@@ -27,25 +27,24 @@ export function request(method, action, data = {}, source) {
 	})
 }
 
-// 图片文件上传,走 uni.uploadFile(multipart),签名规则和 apiUrl 保持一致。
-// 确认过是一步到位:直接传到 report&action=add,openId 作为普通表单字段,
-// 图片文件本身的字段名就叫 attach_id(不是常见的 file)。
-export function uploadImage(filePath, formData) {
+// 图片文件上传第一步:uni.uploadFile(multipart) 传到 method=upload,用来换 attach_id。
+// 第二步再由调用方 request('report','add',{ openId, attach_id }) 登记。
+// 返回值:能解析成 JSON 就原样返回后台的内容;解析不了(空内容/HTML 等)时带上 notJson: true 和后台原始状态码、
+// 原始内容,不编造 code,调用方据此判断失败(空内容的 200 不能算成功)。
+export function uploadImage(filePath) {
 	return new Promise((resolve) => {
 		uni.uploadFile({
-			url: apiUrl('report', 'add'),
+			url: apiUrl('upload'),
 			filePath,
-			name: 'attach_id',
-			formData,
+			name: 'file',
 			success: (res) => {
 				try {
 					resolve(JSON.parse(res.data))
 				} catch (e) {
-					// 不编造 code/msg,原样把后台这次真实返回的状态码和内容带出去,方便看后台到底返回了什么
-					resolve({ code: res.statusCode, msg: res.data })
+					resolve({ notJson: true, code: res.statusCode, msg: res.data })
 				}
 			},
-			fail: (err) => resolve({ code: -1, msg: err.errMsg || '请求失败' })
+			fail: (err) => resolve({ notJson: true, code: -1, msg: err.errMsg || '请求失败' })
 		})
 	})
 }