From 72b3457593119dc69fb99e336844f2130b42b271 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Fri, 8 Nov 2024 19:17:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20PushPlus=20?= =?UTF-8?q?=E4=B8=BA=E6=96=B0=E7=89=88=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/push/push-plus.ts | 56 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/src/push/push-plus.ts b/src/push/push-plus.ts index e25a9ef..63d2516 100644 --- a/src/push/push-plus.ts +++ b/src/push/push-plus.ts @@ -9,6 +9,47 @@ export type TemplateType = 'html' | 'txt' | 'json' | 'markdown' | 'cloudMonitor' export type ChannelType = 'wechat' | 'webhook' | 'cp' | 'sms' | 'mail' +export interface PushPlusConfig { + /** + * 请前往 https://www.pushplus.plus/message 领取 + */ + PUSH_PLUS_TOKEN: string +} + +export interface PushPlusOption { + /** + * 模板类型 + */ + template: TemplateType + /** + * 渠道类型 + */ + channel: ChannelType + /** + * 群组编码,不填仅发送给自己;channel为webhook时无效 + */ + topic?: string + /** + * webhook编码,仅在channel使用webhook渠道和CP渠道时需要填写 + */ + webhook?: string + /** + * 发送结果回调地址 + */ + callbackUrl?: string + /** + * 毫秒时间戳。格式如:1632993318000。服务器时间戳大于此时间戳,则消息不会发送 + */ + timestamp?: number +} + +export interface PushPlusResponse { + // 200 为正确 + code: number + msg: string + data: any +} + /** * pushplus 推送加开放平台,仅支持一对一推送。官方文档:https://www.pushplus.plus/doc/ * @@ -25,13 +66,15 @@ export class PushPlus implements Send { * @private */ private PUSH_PLUS_TOKEN: string + /** * * @author CaoMeiYouRen - * @date 2021-03-03 - * @param PUSH_PLUS_TOKEN 请前往 https://www.pushplus.plus/message 领取 + * @date 2024-11-08 + * @param config 请前往 https://www.pushplus.plus/message 领取 */ - constructor(PUSH_PLUS_TOKEN: string) { + constructor(config: PushPlusConfig) { + const { PUSH_PLUS_TOKEN } = config this.PUSH_PLUS_TOKEN = PUSH_PLUS_TOKEN Debugger('set PUSH_PLUS_TOKEN: "%s"', PUSH_PLUS_TOKEN) if (!this.PUSH_PLUS_TOKEN) { @@ -49,8 +92,10 @@ export class PushPlus implements Send { * @param [channel='wechat'] 发送渠道 * @returns */ - send(title: string, content?: string, template: TemplateType = 'html', channel: ChannelType = 'wechat'): Promise { - Debugger('title: "%s", content: "%s", template: "%s", channel: "%s"', title, content, template, channel) + send(title: string, desp: string = '', option?: PushPlusOption): Promise> { + Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option) + const { template, channel, ...args } = option || {} + const content = desp || title return ajax({ url: 'http://www.pushplus.plus/send', method: 'POST', @@ -63,6 +108,7 @@ export class PushPlus implements Send { content: content || title, template, channel, + ...args, }, }) }