mirror of
https://github.com/doocs/md.git
synced 2025-02-02 19:27:46 +08:00
refactor: update mpFileUpload
This commit is contained in:
parent
48e96440cc
commit
6d8744b8ba
@ -344,37 +344,37 @@ async function mpFileUpload(file: File) {
|
|||||||
const { appID, appsecret, proxyOrigin } = JSON.parse(
|
const { appID, appsecret, proxyOrigin } = JSON.parse(
|
||||||
localStorage.getItem(`mpConfig`)!,
|
localStorage.getItem(`mpConfig`)!,
|
||||||
)
|
)
|
||||||
/* eslint-disable no-async-promise-executor */
|
|
||||||
return new Promise<string>(async (resolve, reject) => {
|
const access_token = await getMpToken(appID, appsecret, proxyOrigin)
|
||||||
try {
|
if (!access_token) {
|
||||||
const access_token = await getMpToken(appID, appsecret, proxyOrigin).catch(e => console.error(e))
|
throw new Error(`获取 access_token 失败`)
|
||||||
if (!access_token) {
|
}
|
||||||
reject(new Error(`获取 access_token 失败,请检查console日志`))
|
|
||||||
return
|
const formdata = new FormData()
|
||||||
}
|
formdata.append(`media`, file, file.name)
|
||||||
const formdata = new FormData()
|
|
||||||
formdata.append(`media`, file, file.name)
|
const requestOptions = {
|
||||||
const requestOptions = {
|
method: `POST`,
|
||||||
method: `POST`,
|
data: formdata,
|
||||||
data: formdata,
|
}
|
||||||
}
|
|
||||||
let url = `https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=${access_token}&type=image`
|
let url = `https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=${access_token}&type=image`
|
||||||
if (proxyOrigin) {
|
if (proxyOrigin) {
|
||||||
url = `${proxyOrigin}/cgi-bin/material/add_material?access_token=${access_token}&type=image`
|
url = `${proxyOrigin}/cgi-bin/material/add_material?access_token=${access_token}&type=image`
|
||||||
}
|
}
|
||||||
const res = await fetch<any, {
|
|
||||||
url: string
|
const res = await fetch<any, { url: string }>(url, requestOptions)
|
||||||
}>(url, requestOptions)
|
|
||||||
let imageUrl = res.url
|
if (!res.url) {
|
||||||
if (proxyOrigin && window.location.href.startsWith(`http`)) {
|
throw new Error(`上传失败,未获取到URL`)
|
||||||
imageUrl = `https://wsrv.nl?url=${encodeURIComponent(imageUrl)}`
|
}
|
||||||
}
|
|
||||||
resolve(imageUrl)
|
let imageUrl = res.url
|
||||||
}
|
if (proxyOrigin && window.location.href.startsWith(`http`)) {
|
||||||
catch (e) {
|
imageUrl = `https://wsrv.nl?url=${encodeURIComponent(imageUrl)}`
|
||||||
reject(e)
|
}
|
||||||
}
|
|
||||||
})
|
return imageUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user