Skip to content

Commit

Permalink
feat(notify): support qywx api proxy (#2802)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheekyshibe authored Nov 15, 2024
1 parent e0957a3 commit 845969e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/src/guide/features/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ ww479cadfqfe8c151f,MPKN9gX97w4e4b4h4u7u4i4i4i4iO6mN_dDedBFzqC5c,@all,1000002,2S8
[参考文档 1](https://note.youdao.com/ynoteshare/index.html?id=351e08a72378206f9dd64d2281e9b83b&type=note&_time=1642141216026) | [参考文档 2](https://note.youdao.com/ynoteshare1/index.html?id=1a0c8aff284ad28cbd011b29b3ad0191&type=note)

- `QYWX_AM`: 用于发送企业应用消息的变量,必填。
- `QYWX_PROXY`: 指定企业微信接口的反向代理服务器地址(可信IP),未配置则不使用代理。
- `QYWX_PROXY_PORT`: 指定企业微信接口的反向代理服务器端口(默认 80)
- `AUTHOR_EMAIL`: 博主邮箱,用来区分发布的评论是否是博主本身发布的。如果是博主发布的则不进行提醒通知。
- `SITE_NAME`: 网站名称,用于在消息中显示。
- `SITE_URL`: 网站地址,用于在消息中显示。
Expand Down
16 changes: 13 additions & 3 deletions packages/server/src/service/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ module.exports = class extends think.Service {
}

async qywxAmWechat({ title, content }, self, parent) {
const { QYWX_AM, SITE_NAME, SITE_URL } = process.env;
const { QYWX_AM, QYWX_PROXY, QYWX_PROXY_PORT, SITE_NAME, SITE_URL } = process.env;

if (!QYWX_AM) {
return false;
Expand All @@ -136,6 +136,7 @@ module.exports = class extends think.Service {
postUrl: SITE_URL + self.url + '#' + self.objectId,
},
};

const contentWechat =
think.config('WXTemplate') ||
`💬 {{site.name|safe}}的文章《{{postName}}》有新评论啦
Expand All @@ -154,8 +155,17 @@ module.exports = class extends think.Service {
querystring.set('corpid', `${QYWX_AM_AY[0]}`);
querystring.set('corpsecret', `${QYWX_AM_AY[1]}`);

let baseUrl = 'https://qyapi.weixin.qq.com';
if (QYWX_PROXY) {
if (!QYWX_PROXY_PORT) {
baseUrl = `http://${QYWX_PROXY}`;
} else {
baseUrl = `http://${QYWX_PROXY}:${QYWX_PROXY_PORT}`;
}
}

const { access_token } = await fetch(
`https://qyapi.weixin.qq.com/cgi-bin/gettoken?${querystring.toString()}`,
`${baseUrl}/cgi-bin/gettoken?${querystring.toString()}`,
{
headers: {
'content-type': 'application/json',
Expand All @@ -164,7 +174,7 @@ module.exports = class extends think.Service {
).then((resp) => resp.json());

return fetch(
`https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${access_token}`,
`${baseUrl}/cgi-bin/message/send?access_token=${access_token}`,
{
method: 'POST',
headers: {
Expand Down

0 comments on commit 845969e

Please sign in to comment.