Skip to content

Commit

Permalink
refactor: 重构 PushPlus 为新版接口
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 8, 2024
1 parent 2c85ecf commit 72b3457
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions src/push/push-plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/
*
Expand All @@ -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) {
Expand All @@ -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<SendResponse> {
Debugger('title: "%s", content: "%s", template: "%s", channel: "%s"', title, content, template, channel)
send(title: string, desp: string = '', option?: PushPlusOption): Promise<SendResponse<PushPlusResponse>> {
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',
Expand All @@ -63,6 +108,7 @@ export class PushPlus implements Send {
content: content || title,
template,
channel,
...args,
},
})
}
Expand Down

0 comments on commit 72b3457

Please sign in to comment.