Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update mpFileUpload #524

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,37 +344,37 @@ async function mpFileUpload(file: File) {
const { appID, appsecret, proxyOrigin } = JSON.parse(
localStorage.getItem(`mpConfig`)!,
)
/* eslint-disable no-async-promise-executor */
return new Promise<string>(async (resolve, reject) => {
try {
const access_token = await getMpToken(appID, appsecret, proxyOrigin).catch(e => console.error(e))
if (!access_token) {
reject(new Error(`获取 access_token 失败,请检查console日志`))
return
}
const formdata = new FormData()
formdata.append(`media`, file, file.name)
const requestOptions = {
method: `POST`,
data: formdata,
}
let url = `https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=${access_token}&type=image`
if (proxyOrigin) {
url = `${proxyOrigin}/cgi-bin/material/add_material?access_token=${access_token}&type=image`
}
const res = await fetch<any, {
url: string
}>(url, requestOptions)
let imageUrl = res.url
if (proxyOrigin && window.location.href.startsWith(`http`)) {
imageUrl = `https://wsrv.nl?url=${encodeURIComponent(imageUrl)}`
}
resolve(imageUrl)
}
catch (e) {
reject(e)
}
})

const access_token = await getMpToken(appID, appsecret, proxyOrigin)
if (!access_token) {
throw new Error(`获取 access_token 失败`)
}

const formdata = new FormData()
formdata.append(`media`, file, file.name)

const requestOptions = {
method: `POST`,
data: formdata,
}

let url = `https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=${access_token}&type=image`
if (proxyOrigin) {
url = `${proxyOrigin}/cgi-bin/material/add_material?access_token=${access_token}&type=image`
}

const res = await fetch<any, { url: string }>(url, requestOptions)

if (!res.url) {
throw new Error(`上传失败,未获取到URL`)
}

let imageUrl = res.url
if (proxyOrigin && window.location.href.startsWith(`http`)) {
imageUrl = `https://wsrv.nl?url=${encodeURIComponent(imageUrl)}`
}

return imageUrl
}

// -----------------------------------------------------------------------
Expand Down
Loading