@@ -308,7 +308,7 @@ interface MpResponse {
308308 errcode : number
309309 errmsg : string
310310}
311- async function getMpToken ( appID : string , appsecret : string ) {
311+ async function getMpToken ( appID : string , appsecret : string , proxyOrigin : string ) {
312312 const data = localStorage . getItem ( `mpToken:${ appID } ` )
313313 if ( data ) {
314314 const token = JSON . parse ( data )
@@ -324,7 +324,10 @@ async function getMpToken(appID: string, appsecret: string) {
324324 secret : appsecret ,
325325 } ,
326326 }
327- const url = `https://api.weixin.qq.com/cgi-bin/stable_token`
327+ let url = `https://api.weixin.qq.com/cgi-bin/stable_token`
328+ if ( proxyOrigin ) {
329+ url = `${ proxyOrigin } /cgi-bin/stable_token`
330+ }
328331 const res = await fetch < any , MpResponse > ( url , requestOptions )
329332 if ( res . access_token ) {
330333 const tokenInfo = {
@@ -337,13 +340,13 @@ async function getMpToken(appID: string, appsecret: string) {
337340 return ``
338341}
339342async function mpFileUpload ( file : File ) {
340- const { appID, appsecret } = JSON . parse (
343+ const { appID, appsecret, proxyOrigin } = JSON . parse (
341344 localStorage . getItem ( `mpConfig` ) ! ,
342345 )
343346 /* eslint-disable no-async-promise-executor */
344347 return new Promise < string > ( async ( resolve , reject ) => {
345348 try {
346- const access_token = await getMpToken ( appID , appsecret ) . catch ( e => console . error ( e ) )
349+ const access_token = await getMpToken ( appID , appsecret , proxyOrigin ) . catch ( e => console . error ( e ) )
347350 if ( ! access_token ) {
348351 reject ( new Error ( `获取 access_token 失败,请检查console日志` ) )
349352 return
@@ -354,11 +357,18 @@ async function mpFileUpload(file: File) {
354357 method : `POST` ,
355358 data : formdata ,
356359 }
357- const url = `https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=${ access_token } &type=image`
360+ let url = `https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=${ access_token } &type=image`
361+ if ( proxyOrigin ) {
362+ url = `${ proxyOrigin } /cgi-bin/material/add_material?access_token=${ access_token } &type=image`
363+ }
358364 const res = await fetch < any , {
359365 url : string
360366 } > ( url , requestOptions )
361- resolve ( res . url )
367+ let imageUrl = res . url
368+ if ( proxyOrigin && window . location . href . startsWith ( `http` ) ) {
369+ imageUrl = `https://wsrv.nl?url=${ encodeURIComponent ( imageUrl ) } `
370+ }
371+ resolve ( imageUrl )
362372 }
363373 catch ( e ) {
364374 reject ( e )
0 commit comments