Skip to content

Commit

Permalink
fix:通过base64上传增加参数
Browse files Browse the repository at this point in the history
  • Loading branch information
luoluoTH committed Dec 25, 2024
1 parent e662bf9 commit 53dacf2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
8 changes: 7 additions & 1 deletion app/main/api/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,14 @@ module.exports = (win, getClient) => {
readable.push(null)

const formData = new FormData()
if (params) {
Object.entries(params).forEach(([key, value]) => {
if (key !== 'base64' && key !== 'imgInfo') {
formData.append(key, value || undefined)
}
})
}
formData.append("file_name", readable, { ...imgInfo })
if (type) formData.append("type", type || undefined)
const res = await httpApi(
"post",
"upload/img",
Expand Down
40 changes: 26 additions & 14 deletions app/renderer/src/main/src/apiUtils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,36 @@ const {ipcRenderer} = window.require("electron")

export interface HttpUploadImgBaseRequest {
type?: "img" | "headImg" | "comment" | "plugins" | "notepad"
filedHash?: string
}

const isUploadImg = (params: HttpUploadImgBaseRequest) => {
const {type, filedHash} = params
let enable = true
switch (type) {
case "notepad":
if (!filedHash) {
enable = false
yakitNotify("error", "httpUploadImgPath:type为notepad,filedHash必传")
}
break
default:
break
}
return enable
}
export interface HttpUploadImgPathRequest extends HttpUploadImgBaseRequest {
path: string
filedHash?: string
}
/** @name 上传图片(文件路径) */
export const httpUploadImgPath: APIFunc<HttpUploadImgPathRequest, string> = (request, hiddenError) => {
return new Promise(async (resolve, reject) => {
// console.log("http-upload-img-path|api:upload/img", JSON.stringify({...request}))
let enable = true
switch (request.type) {
case "notepad":
if (!request.filedHash) {
enable = false
yakitNotify("error", "httpUploadImgPath:type为notepad,filedHash必传")
}
break
default:
break

if (!isUploadImg({type: request.type, filedHash: request.filedHash})) {
reject("参数错误")
return
}
if (!enable) return
ipcRenderer
.invoke("http-upload-img-path", request)
.then((res) => {
Expand All @@ -55,13 +63,17 @@ export interface HttpUploadImgBase64Request extends HttpUploadImgBaseRequest {
export const httpUploadImgBase64: APIFunc<HttpUploadImgBase64Request, string> = (request, hiddenError) => {
return new Promise(async (resolve, reject) => {
// console.log("http-upload-img-path|api:upload/img", JSON.stringify({...request, base64: "base64"}))
if (!isUploadImg({type: request.type, filedHash: request.filedHash})) {
reject("参数错误")
return
}
ipcRenderer
.invoke("http-upload-img-base64", request)
.then((res) => {
if (res?.code === 200 && res?.data) {
resolve(res.data)
} else {
const message = res?.message || "未知错误"
const message = res?.message || res?.data?.reason || "未知错误"
if (!hiddenError) yakitNotify("error", "上传图片失败:" + message)
reject(message)
}
Expand Down Expand Up @@ -90,7 +102,7 @@ export const httpUploadFile: APIFunc<httpUploadFileFileInfo, string> = (request,
if (res?.code === 200 && res?.data) {
resolve(res.data)
} else {
const message = res?.message || "未知错误"
const message = res?.message || res?.data?.reason || "未知错误"
if (!hiddenError) yakitNotify("error", "上传文件失败:" + message)
reject(message)
}
Expand Down

0 comments on commit 53dacf2

Please sign in to comment.