import { AxiosResponse } from 'axios' ; import debug from 'debug' ; import CryptoJS from 'crypto-js' ; import { Send } from '../interfaces/send' ; import { MessageTemplateAbs } from './dingtalk/template' ; import { Text } from './dingtalk/Text' ; import { Markdown } from './dingtalk/Markdown' ; import { warn } from '@/utils/helper' ; import { ajax } from '@/utils/ajax' ; const Debugger = debug ( 'push:dingtalk' ) ; author CaoMeiYouRen
 *  date export class export class Dingtalk implements Send { private ACCESS_TOKEN : string ; private private SECRET ? : string ; private webhook : string = 'https://oapi.dingtalk.com/robot/send' ; constructor ( ACCESS_TOKEN , SECRET ? ) { this . ACCESS_TOKEN = ACCESS_TOKEN ; this . SECRET = SECRET ; Debugger ( 'ACCESS_TOKEN: %s , SECRET: %s' , ACCESS_TOKEN , SECRET ) ; if ( ! this . ACCESS_TOKEN ) { throw new Error ( 'ACCESS_TOKEN 是必须的！' ) ; } if ( ! this . SECRET ) { warn ( '未提供 SECRET ！' ) ; } } private getSign ( timeStamp ) { 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 ; } private async push ( message ) { const timestamp = Date . now ( ) ; const sign = this . getSign ( timestamp ) ; const result = await ajax ( { url : this . webhook , method : 'POST' , headers : { 'Content-Type' : 'application/json' , } , query : { timestamp , sign , access_token : this . ACCESS_TOKEN , } , data : message . get ( ) , } ) ; 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 ; } author CaoMeiYouRen
     *  date param title param desp returns async send ( title , desp ? ) { Debugger ( 'title: "%s", desp: "%s"' , title , desp ) ; if ( ! desp ) { return this . push ( new Text ( title ) ) ; } const markDown = new Markdown ( ) ; markDown . setTitle ( title ) . add ( `# ${ title }` ) . add ( `${ desp }` ) ; return this . push ( markDown ) ; } }