feat: 优化企业微信服务的access_token获取方法。
- 添加获取access_token的方法到WechatWorkService中 - 重构fetchApprovalDetail和fetchFile方法,改用新的getAccessToken方法替代accessToken - 在fetchFile方法中删除不必要的fs.createWriteStream调用
This commit is contained in:
parent
7e24383a49
commit
bbf0b434f1
@ -18,7 +18,16 @@ export class WechatWorkService {
|
||||
this.wxCorpId = process.env.WX_CORP_ID
|
||||
this.wxCorpSecret = process.env.WX_CORP_SECRET
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取access_token
|
||||
* @returns access_token
|
||||
*/
|
||||
public async getAccessToken() {
|
||||
if (!this.accessToken || this.tokenExpire < Date.now()) {
|
||||
await this.refreshAccessToken()
|
||||
}
|
||||
return this.accessToken
|
||||
}
|
||||
/**
|
||||
* 获取微信企业号的access_token
|
||||
* https://developer.work.weixin.qq.com/resource/devtool
|
||||
@ -49,11 +58,12 @@ export class WechatWorkService {
|
||||
*/
|
||||
public async fetchApprovalDetail(spNo: string) {
|
||||
const url = `${WX_API_HOST}/cgi-bin/oa/getapprovaldetail`
|
||||
const access_token = await this.getAccessToken()
|
||||
let config: AxiosRequestConfig = {
|
||||
method: 'post',
|
||||
url,
|
||||
params: {
|
||||
access_token: this.accessToken,
|
||||
access_token,
|
||||
},
|
||||
data: {
|
||||
sp_no: spNo,
|
||||
@ -71,12 +81,13 @@ export class WechatWorkService {
|
||||
*/
|
||||
public async fetchFile(mediaId: string) {
|
||||
const url = `${WX_API_HOST}/cgi-bin/media/get`
|
||||
const access_token = await this.getAccessToken()
|
||||
let config: AxiosRequestConfig = {
|
||||
method: 'get',
|
||||
url,
|
||||
responseType: 'stream',
|
||||
params: {
|
||||
access_token: this.accessToken,
|
||||
access_token,
|
||||
media_id: mediaId,
|
||||
},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user