-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
288 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,34 @@ | ||
import { CoaError } from 'coa-error' | ||
import { $, _, axios, Axios } from 'coa-helper' | ||
import { $, axios, Axios, _ } from 'coa-helper' | ||
import { WxWork } from '../typings' | ||
import { WxWorkStorage } from './WxWorkStorage' | ||
|
||
const baseURL = 'https://qyapi.weixin.qq.com' | ||
|
||
export class WxWorkBin { | ||
|
||
readonly storage: WxWorkStorage | ||
|
||
constructor (storage?: WxWorkStorage) { | ||
this.storage = storage || new WxWorkStorage() | ||
constructor(storage?: WxWorkStorage) { | ||
this.storage = storage ?? new WxWorkStorage() | ||
} | ||
|
||
async get (url: string, params: WxWork.Dic = {}, allow: number[] = []) { | ||
async get(url: string, params: WxWork.Dic = {}, allow: number[] = []) { | ||
const res = await axios(url, { params, baseURL }) | ||
return this.wxResponseResult(res, allow) | ||
} | ||
|
||
async post (url: string, data: WxWork.Dic, params: WxWork.Dic = {}, config: Axios.AxiosRequestConfig = {}, allow: number[] = []) { | ||
async post(url: string, data: WxWork.Dic, params: WxWork.Dic = {}, config: Axios.AxiosRequestConfig = {}, allow: number[] = []) { | ||
const res = await axios(url, { params, data, baseURL, method: 'POST', ...config }) | ||
return this.wxResponseResult(res, allow) | ||
} | ||
|
||
protected wxResponseResult (res: Axios.AxiosResponse, allow: number[]) { | ||
protected wxResponseResult(res: Axios.AxiosResponse, allow: number[]) { | ||
const info: WxWork.Dic = res.data || {} | ||
if (info.errcode) { | ||
const message = _.toString(info.errmsg) || '企业微信服务返回错误' | ||
const code = _.toNumber(info.errcode) || 0 | ||
if (!allow.includes(code)) | ||
CoaError.throw('WxWork.Error.' + info.errcode, message) | ||
if (!allow.includes(code)) CoaError.throw('WxWork.Error.' + info.errcode, message) | ||
} | ||
return $.camelCaseKeys(info) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
export class WxWorkStorage { | ||
private DATA: { [index: string]: { value: any; expire: number } } = {} | ||
|
||
private DATA = {} as { [index: string]: { value: any, expire: number } } | ||
|
||
async get<T> (key: string) { | ||
async get<T>(key: string) { | ||
const { value, expire } = this.DATA[key] || { value: null, expire: 0 } | ||
if (expire < Date.now()) return null | ||
return value | ||
return value as T | ||
} | ||
|
||
async set (key: string, value: any, ms: number) { | ||
async set(key: string, value: any, ms: number) { | ||
const expire = Date.now() + ms | ||
this.DATA[key] = { value, expire } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,71 @@ | ||
export class WxWorkMarkdown { | ||
private data = '' | ||
|
||
text (text: string, color?: 'info' | 'comment' | 'warning') { | ||
if (!color) | ||
this.data += text | ||
else | ||
this.data += `<font color="${color}">${text}</font>` | ||
text(text: string, color?: 'info' | 'comment' | 'warning') { | ||
if (!color) this.data += text | ||
else this.data += `<font color="${color}">${text}</font>` | ||
return this | ||
} | ||
|
||
header3 (text: string) { | ||
header3(text: string) { | ||
return this.text(`### ${text}`) | ||
} | ||
|
||
header4 (text: string) { | ||
header4(text: string) { | ||
return this.text(`#### ${text}`) | ||
} | ||
|
||
header5 (text: string) { | ||
header5(text: string) { | ||
return this.text(`##### ${text}`) | ||
} | ||
|
||
header6 (text: string) { | ||
header6(text: string) { | ||
return this.text(`###### ${text}`) | ||
} | ||
|
||
red (text: string) { | ||
red(text: string) { | ||
return this.text(text, 'warning') | ||
} | ||
|
||
green (text: string) { | ||
green(text: string) { | ||
return this.text(text, 'info') | ||
} | ||
|
||
gray (text: string) { | ||
gray(text: string) { | ||
return this.text(text, 'comment') | ||
} | ||
|
||
bold (text: string) { | ||
bold(text: string) { | ||
return this.text(`**${text}**`) | ||
} | ||
|
||
link (title: string, url: string) { | ||
link(title: string, url: string) { | ||
return this.text(`[${title}](${url})`) | ||
} | ||
|
||
commentText (text: string) { | ||
commentText(text: string) { | ||
return this.text(text, 'comment') | ||
} | ||
|
||
warningText (text: string) { | ||
warningText(text: string) { | ||
return this.text(text, 'warning') | ||
} | ||
|
||
infoText (text: string) { | ||
infoText(text: string) { | ||
return this.text(text, 'info') | ||
} | ||
|
||
br () { | ||
br() { | ||
this.data += '\n' | ||
return this | ||
} | ||
|
||
quote () { | ||
quote() { | ||
this.data += '> ' | ||
return this | ||
} | ||
|
||
value () { | ||
value() { | ||
return this.data | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.