From 82bfab43c492d99f246522c309f090111e1aa613 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Fri, 8 Nov 2024 17:10:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20=E9=92=89?= =?UTF-8?q?=E9=92=89=E6=9C=BA=E5=99=A8=E4=BA=BA=20=E6=8E=A8=E9=80=81?= =?UTF-8?q?=EF=BC=8C=E8=BF=81=E7=A7=BB=E5=88=B0=20=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=9B=E4=BC=98=E5=8C=96=20=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/push/dingtalk.ts | 30 ++++++++++++++++++++++++------ src/utils/ajax.ts | 9 ++------- src/utils/helper.ts | 5 +++++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/push/dingtalk.ts b/src/push/dingtalk.ts index c95f84b..fc96c23 100644 --- a/src/push/dingtalk.ts +++ b/src/push/dingtalk.ts @@ -12,7 +12,24 @@ import { SendResponse } from '@/interfaces/response' const Debugger = debug('push:dingtalk') +export interface DingtalkConfig { + /** + * 钉钉机器人 access_token。官方文档:https://developers.dingtalk.com/document/app/custom-robot-access + */ + DINGTALK_ACCESS_TOKEN: string + /** + * 加签安全秘钥(HmacSHA256) + */ + DINGTALK_SECRET?: string +} + +export interface DingtalkResponse { + errcode: number + errmsg: string +} + /** + * 钉钉机器人推送 * 在 [dingtalk-robot-sdk](https://github.com/ineo6/dingtalk-robot-sdk) 的基础上重构了一下,用法几乎完全一致。 * 参考文档 [钉钉开放平台 - 自定义机器人接入](https://developers.dingtalk.com/document/app/custom-robot-access) * @@ -31,10 +48,11 @@ export class Dingtalk implements Send { private SECRET?: string private webhook: string = 'https://oapi.dingtalk.com/robot/send' - constructor(ACCESS_TOKEN: string, SECRET?: string) { - this.ACCESS_TOKEN = ACCESS_TOKEN - this.SECRET = SECRET - Debugger('ACCESS_TOKEN: %s , SECRET: %s', ACCESS_TOKEN, SECRET) + constructor(config: DingtalkConfig) { + const { DINGTALK_ACCESS_TOKEN, DINGTALK_SECRET } = config + this.ACCESS_TOKEN = DINGTALK_ACCESS_TOKEN + this.SECRET = DINGTALK_SECRET + Debugger('DINGTALK_ACCESS_TOKEN: %s , DINGTALK_SECRET: %s', this.ACCESS_TOKEN, this.SECRET) if (!this.ACCESS_TOKEN) { throw new Error('ACCESS_TOKEN 是必须的!') } @@ -52,7 +70,7 @@ export class Dingtalk implements Send { return signStr } - private async push(message: MessageTemplateAbs): Promise { + private async push(message: MessageTemplateAbs): Promise> { const timestamp = Date.now() const sign = this.getSign(timestamp) const result = await ajax({ @@ -85,7 +103,7 @@ export class Dingtalk implements Send { * @param [desp] 消息的内容,支持 Markdown * @returns */ - async send(title: string, desp?: string): Promise { + async send(title: string, desp?: string): Promise> { Debugger('title: "%s", desp: "%s"', title, desp) if (!desp) { return this.push(new Text(title)) diff --git a/src/utils/ajax.ts b/src/utils/ajax.ts index 1dc45e8..b731716 100644 --- a/src/utils/ajax.ts +++ b/src/utils/ajax.ts @@ -2,7 +2,7 @@ import axios, { AxiosResponse, Method, AxiosRequestHeaders } from 'axios' import debug from 'debug' import { HttpsProxyAgent } from 'https-proxy-agent' import { SocksProxyAgent } from 'socks-proxy-agent' -import { isHttpURL, isSocksUrl } from './helper' +import { isHttpURL, isSocksUrl, logger } from './helper' const Debugger = debug('push:ajax') @@ -69,14 +69,9 @@ export async function ajax(config: AjaxConfig): Promise