Skip to content

Commit

Permalink
feat: 完成钉钉推送
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Feb 27, 2021
1 parent b8a5568 commit 071fb8b
Show file tree
Hide file tree
Showing 18 changed files with 430 additions and 91 deletions.
53 changes: 0 additions & 53 deletions build/post-build-type.js

This file was deleted.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"browser": "dist/index.browser.js",
"types": "dist/index.d.ts",
"files": [
"dist"
Expand All @@ -20,7 +19,7 @@
"build": "cross-env NODE_ENV=production rollup -c",
"postbuild": "npm run build:type",
"build:type": "api-extractor run",
"postbuild:type": "node build/post-build-type.js",
"postbuild:type": "rimraf \"dist/**/!(*.js?(.map)|index.d.ts)\"",
"analyzer": "cross-env NODE_ENV=production ANALYZER=true rollup -c",
"dev": "cross-env NODE_ENV=development ts-node-dev src/index.ts",
"dev:rollup": "cross-env NODE_ENV=development rollup -c",
Expand All @@ -40,6 +39,7 @@
"@rollup/plugin-typescript": "^8.2.0",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@types/crypto-js": "^4.0.1",
"@types/debug": "^4.1.5",
"@types/lodash": "^4.14.168",
"@types/module-alias": "^2.0.0",
Expand All @@ -52,12 +52,12 @@
"conventional-changelog-cmyr-config": "^1.2.3",
"cross-env": "^7.0.3",
"cz-conventional-changelog": "^3.3.0",
"debug": "^4.3.1",
"eslint": "^7.20.0",
"eslint-config-cmyr": "^1.0.10",
"husky": "^5.1.1",
"lint-staged": "^10.5.4",
"lodash": "^4.17.21",
"module-alias": "^2.2.2",
"rimraf": "^3.0.2",
"rollup": "^2.39.1",
"rollup-plugin-analyzer": "^4.0.0",
Expand All @@ -70,7 +70,9 @@
},
"dependencies": {
"axios": "^0.21.1",
"module-alias": "^2.2.2",
"colors": "^1.4.0",
"crypto-js": "^4.0.0",
"debug": "^4.3.1",
"qs": "^6.9.6"
},
"config": {
Expand All @@ -93,4 +95,4 @@
"git add"
]
}
}
}
33 changes: 16 additions & 17 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import _ from 'lodash'
import path from 'path'
import { dependencies, name } from './package.json'
const external = Object.keys(dependencies) // 默认不打包 dependencies
external.push('debug')
const outputName = _.upperFirst(_.camelCase(name))// 导出的模块名称 PascalCase
const env = process.env
const __PROD__ = env.NODE_ENV === 'production'
Expand Down Expand Up @@ -113,7 +112,7 @@ export default [
plugins: getPlugins({
isBrowser: false,
isDeclaration: false,
isMin: true,
isMin: false,
}),
},
{
Expand All @@ -131,19 +130,19 @@ export default [
isMin: false,
}),
},
{
input: 'src/index.ts',
external,
output: {
file: 'dist/index.browser.js', // 生成 browser
format: 'umd',
name: outputName,
sourcemap: true,
},
plugins: getPlugins({
isBrowser: true,
isDeclaration: false,
isMin: true,
}),
},
// { // 本包不推荐在浏览器中使用,故不生成浏览器版本
// input: 'src/index.ts',
// external,
// output: {
// file: 'dist/index.browser.js', // 生成 browser
// format: 'umd',
// name: outputName,
// sourcemap: true,
// },
// plugins: getPlugins({
// isBrowser: true,
// isDeclaration: false,
// isMin: true,
// }),
// },
]
10 changes: 3 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { ServerChan } from './push/server-chan'
import { ServerChanTurbo } from './push/server-chan-turbo'

export {
ServerChan,
ServerChanTurbo,
}
export * from './push/dingtalk'
export * from './push/server-chan'
export * from './push/server-chan-turbo'
12 changes: 12 additions & 0 deletions src/interfaces/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 要求所有 push 方法都至少实现了 send 接口
*
* @author CaoMeiYouRen
* @date 2021-02-27
* @export
* @interface Send
*/
interface Send {
send(...args: any[]): Promise<any>
}
export { Send }
61 changes: 61 additions & 0 deletions src/push/dingtalk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Send } from '../interfaces/send'
import { ajax } from '@/utils/ajax'
import { AxiosResponse } from 'axios'
import debug from 'debug'
import colors from 'colors'
import CryptoJS from 'crypto-js'
import { Message } from './dingtalk/index'

const Debugger = debug('push:dingtalk')

class RobotOption {
accessToken?: string
secret?: string
}

export class Dingtalk implements Send {
private accessToken?: string
private secret?: string
private webhook: string = 'https://oapi.dingtalk.com/robot/send'
constructor(option: RobotOption = {}) {
Object.assign(this, option)
if (!this.accessToken) {
throw new Error('accessToken is required!')
}
if (!this.secret) {
console.warn(colors.yellow('Secret is undefined'))
}
}

private getSign(timeStamp: number) {
let signStr = ''
if (this.secret) {
signStr = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(`${timeStamp}\n${this.secret}`, this.secret))
Debugger('Sign string is %s, result is %s', `${timeStamp}\n${this.secret}`, signStr)
}
return signStr
}

public async send(message: Message): Promise<AxiosResponse<any>> {
const timestamp = Date.now()
const sign = this.getSign(timestamp)
const result = await ajax({
url: this.webhook,
query: {
timestamp,
sign,
access_token: this.accessToken,
},
data: message.get(),
headers: {
'Content-Type': 'application/json',
},
})
Debugger('Result is %s, %s。', result.data.errcode, result.data.errmsg)
if (result.data.errcode === 310000) {
console.error('Send Failed:', result.data)
Debugger('Please check safe config : %O', result.data)
}
return result
}
}
79 changes: 79 additions & 0 deletions src/push/dingtalk/ActionCard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { MessageTemplateAbs } from './template'

class ActionCard extends MessageTemplateAbs {
title: string
text: string
hideAvatar: number
btnOrientation?: number
singleTitle: string
singleURL: string
btns: { title: string, actionURL: string }[]
constructor() {
super()
this.msgtype = 'actionCard'

this.title = ''
this.text = ''
// 0-正常发消息者头像,1-隐藏发消息者头像
this.hideAvatar = 0
// 0-按钮竖直排列,1-按钮横向排列
this.btnOrientation = 0

// 单个按钮的方案。(设置此项和singleURL后btns无效)
this.singleTitle = ''
this.singleURL = ''
this.btns = []
}

setBtns(btns: ConcatArray<{ title: string, actionURL: string }>) {
this.btns = this.btns.concat(btns)
return this
}

setSingleTitle(title: string) {
this.singleTitle = title
return this
}

setSingleURL(url: string) {
this.singleURL = url
return this
}

setTitle(title: string) {
this.title = title
return this
}

setText(content: string) {
this.text = content
return this
}

setBtnOrientation(flag: number) {
this.btnOrientation = flag
return this
}

setHideAvatar(flag: number) {
this.hideAvatar = flag
return this
}

get() {
return this.render({
actionCard: {
title: this.title,
text: this.text,
hideAvatar: this.hideAvatar,
btnOrientation: this.btnOrientation,
btns: this.btns,
singleTitle: this.singleTitle,
singleURL: this.singleURL,
},
})
}
}

export { ActionCard }
28 changes: 28 additions & 0 deletions src/push/dingtalk/FeedCard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { MessageTemplateAbs } from './template'
import { Link } from './Link'

class FeedCard extends MessageTemplateAbs {
links: Link[]
constructor(links: Link[]) {
super()
this.msgtype = 'feedCard'

this.links = links || []
}

addLinks(links: Link[]) {
this.links = this.links.concat(links)
return this
}

get() {
return this.render({
feedCard: {
links: this.links,
},
})
}
}

export { FeedCard }
Loading

0 comments on commit 071fb8b

Please sign in to comment.