代码提交-3-13
This commit is contained in:
@@ -41,6 +41,7 @@
|
||||
import { addRecord } from "@/api/inspection/record.js"
|
||||
import { addWatermarkToImage } from "@/utils/watermark.js"
|
||||
import appConfig from "@/config"
|
||||
import { getToken } from "@/utils/auth"
|
||||
|
||||
// ------- URL 安全拼接,避免出现 /undefined/common/upload -------
|
||||
function joinURL(base, path) {
|
||||
@@ -110,25 +111,26 @@
|
||||
},
|
||||
|
||||
// 选择文件(支持多张)
|
||||
async upload(e) {
|
||||
if (!e?.tempFiles?.length) return
|
||||
if (this.isUploading) {
|
||||
this.showMessage("warning", "图片正在上传中,请稍等")
|
||||
return
|
||||
}
|
||||
|
||||
this.isUploading = true
|
||||
try {
|
||||
for (const tf of e.tempFiles) {
|
||||
await this.handleImageUpload(tf)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
this.showMessage("error", `图片上传失败:${err.message || err}`)
|
||||
} finally {
|
||||
this.isUploading = false
|
||||
}
|
||||
},
|
||||
async upload(e) {
|
||||
if (!e?.tempFiles?.length) return
|
||||
if (this.isUploading) {
|
||||
this.showMessage("warning", "图片正在上传中,请稍等")
|
||||
return
|
||||
}
|
||||
|
||||
this.isUploading = true
|
||||
try {
|
||||
for (const tf of e.tempFiles) {
|
||||
await this.handleImageUpload(tf)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
const msg = err?.errMsg || err?.message || (typeof err === "string" ? err : JSON.stringify(err))
|
||||
this.showMessage("error", `图片上传失败:${msg}`)
|
||||
} finally {
|
||||
this.isUploading = false
|
||||
}
|
||||
},
|
||||
|
||||
// 单张:加水印 -> 压缩(H5) -> 预览 -> 上传
|
||||
async handleImageUpload(tempFile) {
|
||||
@@ -260,19 +262,45 @@
|
||||
url: UPLOAD_URL,
|
||||
name: "file",
|
||||
file, // H5 传 File;不要传 filePath
|
||||
header: { Authorization: "Bearer " + getToken() },
|
||||
success: (res) => {
|
||||
try {
|
||||
if (res.statusCode && res.statusCode !== 200) {
|
||||
throw new Error(`HTTP ${res.statusCode}`)
|
||||
}
|
||||
const data = typeof res.data === "string" ? JSON.parse(res.data) : res.data
|
||||
if (data.code === 200) {
|
||||
if (data && data.code === 200) {
|
||||
resolve(data.fileName || data.url)
|
||||
} else {
|
||||
reject(new Error(data.msg || "上传失败"))
|
||||
reject(new Error((data && data.msg) || "上传失败"))
|
||||
}
|
||||
} catch (e) {
|
||||
reject(new Error("上传响应解析失败"))
|
||||
}
|
||||
},
|
||||
fail: (err) => reject(err)
|
||||
fail: async (err) => {
|
||||
const msg = err?.errMsg || "uploadFile fail"
|
||||
if (msg.includes("file error") || msg.includes("fail")) {
|
||||
try {
|
||||
const form = new FormData()
|
||||
form.append("file", file)
|
||||
const resp = await fetch(UPLOAD_URL, {
|
||||
method: "POST",
|
||||
headers: { Authorization: "Bearer " + getToken() },
|
||||
body: form
|
||||
})
|
||||
if (!resp.ok) return reject(new Error(`HTTP ${resp.status}`))
|
||||
const data = await resp.json()
|
||||
if (data && data.code === 200) {
|
||||
return resolve(data.fileName || data.url)
|
||||
}
|
||||
return reject(new Error((data && data.msg) || "上传失败"))
|
||||
} catch (e) {
|
||||
return reject(new Error(e?.message || msg))
|
||||
}
|
||||
}
|
||||
reject(new Error(msg))
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
@@ -298,7 +326,7 @@
|
||||
|
||||
dialogConfirm() {
|
||||
if (this.msgType === "success") {
|
||||
uni.reLaunch({ url: "/pages/work/index" })
|
||||
uni.reLaunch({ url: "/pages/work/inspection/recordList/index" })
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user