重构:重构 WeChatWork 服务以改进错误处理。

- 在 wechatwork.service.ts 引入 fs 模块
- 从 axios 请求配置中删除 maxBodyLength 属性
- 在 refreshAccessToken 方法中添加对带有 errcode 属性的响应的错误处理
- 在 refreshAccessToken 方法中更新访问令牌和令牌过期时间
- 修正 fetchFile 方法注释中的拼写错误。
This commit is contained in:
zhl 2023-04-06 10:47:23 +08:00
parent 73fe7817fd
commit 7e24383a49

View File

@ -1,7 +1,7 @@
import axios, { AxiosRequestConfig } from 'axios'
import { singleton } from 'decorators/singleton'
import fs from 'fs'
const fs = require('fs')
const WX_API_HOST = 'https://qyapi.weixin.qq.com'
@singleton
export class WechatWorkService {
@ -25,10 +25,8 @@ export class WechatWorkService {
*/
public async refreshAccessToken() {
const url = `${WX_API_HOST}/cgi-bin/gettoken`
// use axios get url
let config: AxiosRequestConfig = {
method: 'get',
maxBodyLength: Infinity,
url,
params: {
corpid: this.wxCorpId,
@ -38,6 +36,11 @@ export class WechatWorkService {
let response = await axios.request(config).then(response => {
return response.data
})
if (response.errcode) {
throw new Error(response.errmsg)
}
this.accessToken = response.access_token
this.tokenExpire = Date.now() + response.expires_in * 1000
}
/**
*