Skip to content

Commit d37aedb

Browse files
authored
feat: use mp oss in website (#471)
* add example cloudflare worker proxy * use wsrv.nl to host image request in website
1 parent b83968b commit d37aedb

File tree

7 files changed

+86
-15
lines changed

7 files changed

+86
-15
lines changed

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default antfu({
55
unocss: true,
66
typescript: true,
77
formatters: true,
8-
ignores: [`.github`, `bin`, `md-cli`, `src/assets`],
8+
ignores: [`.github`, `bin`, `md-cli`, `src/assets`, `example`],
99
}, {
1010
rules: {
1111
'semi': [`error`, `never`],

example/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Example
2+
3+
## worker.js
4+
5+
公众号openapi接口代理服务示例,该项目将请求转发至微信公众号api。
6+
7+
开发调试:
8+
9+
```
10+
cd example
11+
npx wrangler dev worker.js
12+
```
13+
14+
部署:
15+
16+
请将其部署到cloudflare workers。

example/worker.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @typedef {object} Env
3+
* @property
4+
*/
5+
6+
export default {
7+
/**
8+
* @param {Request} request
9+
* @param {Env} env
10+
* @param {ExecutionContext} ctx
11+
* @returns {Promise<Response>}
12+
*/
13+
async fetch(request, env, ctx) {
14+
const url = new URL(request.url)
15+
const targetUrl = `https://api.weixin.qq.com`
16+
const proxyRequest = new Request(targetUrl + url.pathname + url.search, {
17+
method: request.method,
18+
headers: request.headers,
19+
body: request.body,
20+
})
21+
const response = await fetch(proxyRequest)
22+
const proxyResponse = new Response(response.body, {
23+
status: response.status,
24+
statusText: response.statusText,
25+
headers: response.headers,
26+
})
27+
setCorsHeaders(proxyResponse.headers)
28+
return proxyResponse
29+
},
30+
}
31+
// 设置 CORS 头部
32+
function setCorsHeaders(headers) {
33+
headers.set(`Access-Control-Allow-Origin`, `*`)
34+
headers.set(`Access-Control-Allow-Methods`, `GET, POST, PUT, DELETE`)
35+
headers.set(`Access-Control-Allow-Headers`, `*`)
36+
}

src/components/CodemirrorEditor/UploadImgDialog.vue

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ const minioOSS = ref({
6363
})
6464
6565
const formMp = ref({
66+
proxyOrigin: ``,
6667
appID: ``,
6768
appsecret: ``,
6869
})
69-
const mpDisabled = ref(window.location.href.startsWith(`http`))
70+
const isWebsite = ref(window.location.href.startsWith(`http`))
7071
const formCustom = ref<{ code: string, editor: CodeMirror.EditorFromTextArea | null }>({
7172
code:
7273
localStorage.getItem(`formCustomConfig`)
@@ -117,7 +118,6 @@ const options = [
117118
{
118119
value: `mp`,
119120
label: `公众号素材`,
120-
disabled: mpDisabled.value,
121121
},
122122
{
123123
value: `formCustom`,
@@ -272,6 +272,10 @@ function saveMpConfiguration() {
272272
ElMessage.error(`公众号图床 参数配置不全`)
273273
return
274274
}
275+
if (isWebsite.value && !formMp.value.proxyOrigin) {
276+
ElMessage.error(`代理域名必须配置`)
277+
return
278+
}
275279
localStorage.setItem(`mpConfig`, JSON.stringify(formMp.value))
276280
ElMessage.success(`保存成功`)
277281
}
@@ -328,7 +332,6 @@ function uploadImage(params: { file: any }) {
328332
:key="item.value"
329333
:label="item.label"
330334
:value="item.value"
331-
:disabled="item?.disabled"
332335
/>
333336
</el-select>
334337
<el-upload
@@ -646,7 +649,7 @@ function uploadImage(params: { file: any }) {
646649
<template #label>
647650
<el-tooltip placement="top">
648651
<template #content>
649-
由于接口请求跨域问题请在浏览器插件形式中使用
652+
由于接口请求跨域问题建议在浏览器插件形式中使用
650653
</template>
651654
<div>公众号 图床</div>
652655
</el-tooltip>
@@ -656,8 +659,13 @@ function uploadImage(params: { file: any }) {
656659
:model="formMp"
657660
label-position="right"
658661
label-width="150px"
659-
:disabled="mpDisabled"
660662
>
663+
<el-form-item label="代理域名" :required="false">
664+
<el-input
665+
v-model.trim="formMp.proxyOrigin"
666+
placeholder=""
667+
/>
668+
</el-form-item>
661669
<el-form-item label="appID" :required="true">
662670
<el-input
663671
v-model.trim="formMp.appID"
@@ -677,7 +685,7 @@ function uploadImage(params: { file: any }) {
677685
如何开启公众号开发者模式并获取应用账号密钥?
678686
</el-link>
679687
</el-form-item>
680-
<el-form-item style="margin-top: -26px;">
688+
<el-form-item style="margin-top: -22px;">
681689
<el-link
682690
type="primary"
683691
href="https://mpmd.pages.dev/tutorial/"

src/entrypoints/popup/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<title>MpMd编辑器</title>
6+
<title>公众号文章编辑器</title>
77
<meta name="manifest.type" content="browser_action" />
88
</head>
99
<body>

src/utils/file.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
339342
async 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)

wxt.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export default defineConfig({
77
extensionApi: `chrome`,
88
manifest: {
99
name: `公众号文章编辑器`,
10-
version: `0.0.6`,
10+
description: `一款高度简洁的微信 Markdown 编辑器:支持 Markdown 语法、色盘取色、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性`,
11+
version: `0.0.7`,
1112
icons: {
1213
256: `/mpmd/icon-256.png`,
1314
},

0 commit comments

Comments
 (0)