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

feat: use mp oss in website #471

Merged
merged 3 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default antfu({
unocss: true,
typescript: true,
formatters: true,
ignores: [`.github`, `bin`, `md-cli`, `src/assets`],
ignores: [`.github`, `bin`, `md-cli`, `src/assets`, `example`],
}, {
rules: {
'semi': [`error`, `never`],
Expand Down
16 changes: 16 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Example

## worker.js

公众号openapi接口代理服务示例,该项目将请求转发至微信公众号api。

开发调试:

```
cd example
npx wrangler dev worker.js
```

部署:

请将其部署到cloudflare workers。
36 changes: 36 additions & 0 deletions example/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @typedef {object} Env
* @property
*/

export default {
/**
* @param {Request} request
* @param {Env} env
* @param {ExecutionContext} ctx
* @returns {Promise<Response>}
*/
async fetch(request, env, ctx) {
const url = new URL(request.url)
const targetUrl = `https://api.weixin.qq.com`
const proxyRequest = new Request(targetUrl + url.pathname + url.search, {
method: request.method,
headers: request.headers,
body: request.body,
})
const response = await fetch(proxyRequest)
const proxyResponse = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
})
setCorsHeaders(proxyResponse.headers)
return proxyResponse
},
}
// 设置 CORS 头部
function setCorsHeaders(headers) {
headers.set(`Access-Control-Allow-Origin`, `*`)
headers.set(`Access-Control-Allow-Methods`, `GET, POST, PUT, DELETE`)
headers.set(`Access-Control-Allow-Headers`, `*`)
}
20 changes: 14 additions & 6 deletions src/components/CodemirrorEditor/UploadImgDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ const minioOSS = ref({
})

const formMp = ref({
proxyOrigin: ``,
appID: ``,
appsecret: ``,
})
const mpDisabled = ref(window.location.href.startsWith(`http`))
const isWebsite = ref(window.location.href.startsWith(`http`))
const formCustom = ref<{ code: string, editor: CodeMirror.EditorFromTextArea | null }>({
code:
localStorage.getItem(`formCustomConfig`)
Expand Down Expand Up @@ -117,7 +118,6 @@ const options = [
{
value: `mp`,
label: `公众号素材`,
disabled: mpDisabled.value,
},
{
value: `formCustom`,
Expand Down Expand Up @@ -272,6 +272,10 @@ function saveMpConfiguration() {
ElMessage.error(`公众号图床 参数配置不全`)
return
}
if (isWebsite.value && !formMp.value.proxyOrigin) {
ElMessage.error(`代理域名必须配置`)
return
}
localStorage.setItem(`mpConfig`, JSON.stringify(formMp.value))
ElMessage.success(`保存成功`)
}
Expand Down Expand Up @@ -328,7 +332,6 @@ function uploadImage(params: { file: any }) {
:key="item.value"
:label="item.label"
:value="item.value"
:disabled="item?.disabled"
/>
</el-select>
<el-upload
Expand Down Expand Up @@ -646,7 +649,7 @@ function uploadImage(params: { file: any }) {
<template #label>
<el-tooltip placement="top">
<template #content>
由于接口请求跨域问题请在浏览器插件形式中使用
由于接口请求跨域问题建议在浏览器插件形式中使用
</template>
<div>公众号 图床</div>
</el-tooltip>
Expand All @@ -656,8 +659,13 @@ function uploadImage(params: { file: any }) {
:model="formMp"
label-position="right"
label-width="150px"
:disabled="mpDisabled"
>
<el-form-item label="代理域名" :required="false">
<el-input
v-model.trim="formMp.proxyOrigin"
placeholder=""
/>
</el-form-item>
<el-form-item label="appID" :required="true">
<el-input
v-model.trim="formMp.appID"
Expand All @@ -677,7 +685,7 @@ function uploadImage(params: { file: any }) {
如何开启公众号开发者模式并获取应用账号密钥?
</el-link>
</el-form-item>
<el-form-item style="margin-top: -26px;">
<el-form-item style="margin-top: -22px;">
<el-link
type="primary"
href="https://mpmd.pages.dev/tutorial/"
Expand Down
2 changes: 1 addition & 1 deletion src/entrypoints/popup/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MpMd编辑器</title>
<title>公众号文章编辑器</title>
<meta name="manifest.type" content="browser_action" />
</head>
<body>
Expand Down
22 changes: 16 additions & 6 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ interface MpResponse {
errcode: number
errmsg: string
}
async function getMpToken(appID: string, appsecret: string) {
async function getMpToken(appID: string, appsecret: string, proxyOrigin: string) {
const data = localStorage.getItem(`mpToken:${appID}`)
if (data) {
const token = JSON.parse(data)
Expand All @@ -324,7 +324,10 @@ async function getMpToken(appID: string, appsecret: string) {
secret: appsecret,
},
}
const url = `https://api.weixin.qq.com/cgi-bin/stable_token`
let url = `https://api.weixin.qq.com/cgi-bin/stable_token`
if (proxyOrigin) {
url = `${proxyOrigin}/cgi-bin/stable_token`
}
const res = await fetch<any, MpResponse>(url, requestOptions)
if (res.access_token) {
const tokenInfo = {
Expand All @@ -337,13 +340,13 @@ async function getMpToken(appID: string, appsecret: string) {
return ``
}
async function mpFileUpload(file: File) {
const { appID, appsecret } = JSON.parse(
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).catch(e => console.error(e))
const access_token = await getMpToken(appID, appsecret, proxyOrigin).catch(e => console.error(e))
if (!access_token) {
reject(new Error(`获取 access_token 失败,请检查console日志`))
return
Expand All @@ -354,11 +357,18 @@ async function mpFileUpload(file: File) {
method: `POST`,
data: formdata,
}
const 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) {
url = `${proxyOrigin}/cgi-bin/material/add_material?access_token=${access_token}&type=image`
}
const res = await fetch<any, {
url: string
}>(url, requestOptions)
resolve(res.url)
let imageUrl = res.url
if (proxyOrigin && window.location.href.startsWith(`http`)) {
imageUrl = `https://wsrv.nl?url=${encodeURIComponent(imageUrl)}`
}
resolve(imageUrl)
}
catch (e) {
reject(e)
Expand Down
3 changes: 2 additions & 1 deletion wxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default defineConfig({
extensionApi: `chrome`,
manifest: {
name: `公众号文章编辑器`,
version: `0.0.6`,
description: `一款高度简洁的微信 Markdown 编辑器:支持 Markdown 语法、色盘取色、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性`,
version: `0.0.7`,
icons: {
256: `/mpmd/icon-256.png`,
},
Expand Down
Loading