Skip to content

Commit

Permalink
feat: 增加debug参数
Browse files Browse the repository at this point in the history
  • Loading branch information
jay committed Jun 28, 2023
1 parent ea112e1 commit 4249761
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ module.exports = {
'functions': 'always-multiline',
}],
'eqeqeq': ['error', 'always'],
'complexity': ['error', { max: 16 }],
'complexity': ['error', { max: 17 }],
},
};
14 changes: 11 additions & 3 deletions lib/miniprogram.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ export class MiniProgramService {

private readonly logger = new Logger(MiniProgramService.name);

constructor (private options: WeChatModuleOptions) { }
private debug = false;

constructor (private options: WeChatModuleOptions) {
if (options && options.debug) {
this.debug = true;
}
}

/**
* 获取接口调用凭据
Expand Down Expand Up @@ -484,8 +490,10 @@ export class MiniProgramService {
*/
public verifyMessagePush (@Req() req: Request, @Res() res: Response, token?: string) {
token = token || this.options?.token;
this.logger.debug(`verifyMessagePush() token = ${token}`);
this.logger.debug(`verifyMessagePush() query = ${JSON.stringify(req.query)}`);
if (this.debug) {
this.logger.debug(`verifyMessagePush() token = ${token}`);
this.logger.debug(`verifyMessagePush() query = ${JSON.stringify(req.query)}`);
}
const signature = (req.query && req.query.signature) || '';
const timestamp = (req.query && req.query.timestamp) || '';
const nonce = (req.query && req.query.nonce) || '';
Expand Down
10 changes: 5 additions & 5 deletions lib/wechat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ export class WeChatService {

constructor (private options: WeChatModuleOptions) {
this.mp = new MiniProgramService(options);
this.pay = new WePayService();
if (options && options.cacheAdapter) {
if (options.debug) {
this.debug = true;
}
this.cacheAdapter = options.cacheAdapter as ICache;
}
this.pay = new WePayService(this.debug);
}

/**
Expand Down Expand Up @@ -117,12 +117,12 @@ export class WeChatService {
const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${secret}`;
const res = await axios.get<AccountAccessTokenResult>(url);
const ret = res && res.data;
this.logger.debug(`请求公众号或者小程序access_token:[errcode=${ret.errcode}][errmsg=${ret.errmsg}]`);
if (this.debug) this.logger.debug(`请求公众号或者小程序access_token:[errcode=${ret.errcode}][errmsg=${ret.errmsg}]`);
if (ret.access_token) {
// eslint-disable-next-line camelcase
ret.expires_in += (Math.floor(Date.now() / 1000) - 120);
if (this.cacheAdapter) {
this.logger.debug(`保存access_token到缓存[${WeChatService.KEY_ACCESS_TOKEN}_${appId}]`);
if (this.debug) this.logger.debug(`保存access_token到缓存[${WeChatService.KEY_ACCESS_TOKEN}_${appId}]`);
this.cacheAdapter.set(`${WeChatService.KEY_ACCESS_TOKEN}_${appId}`, ret, 7100);
}
}
Expand Down Expand Up @@ -158,12 +158,12 @@ export class WeChatService {
};
const res = await axios.post<AccountAccessTokenResult>(url, data);
const ret = res && res.data;
this.logger.debug(`请求公众号或者小程序access_token:[errcode=${ret.errcode}][errmsg=${ret.errmsg}]`);
if (this.debug) this.logger.debug(`请求公众号或者小程序access_token:[errcode=${ret.errcode}][errmsg=${ret.errmsg}]`);
if (ret.access_token) {
// eslint-disable-next-line camelcase
ret.expires_in += (Math.floor(Date.now() / 1000) - 120);
if (this.cacheAdapter) {
this.logger.debug(`保存access_token到缓存[${WeChatService.KEY_ACCESS_TOKEN}_${appId}]`);
if (this.debug) this.logger.debug(`保存access_token到缓存[${WeChatService.KEY_ACCESS_TOKEN}_${appId}]`);
this.cacheAdapter.set(`${WeChatService.KEY_ACCESS_TOKEN}_${appId}`, ret, 7100);
}
}
Expand Down
17 changes: 11 additions & 6 deletions lib/wepay.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export class WePayService {

private readonly logger = new Logger(WePayService.name);

constructor (private readonly debug = false) {
}

/**
* 获取平台证书列表
* @param mchId
Expand Down Expand Up @@ -211,16 +214,18 @@ export class WePayService {
rawBody = await getRawBody(req);
} catch (error) {
const message = (error as Error).message as string;
this.logger.debug(message);
if (this.debug) this.logger.debug(`getRawBody error:${message}`);
if (message === 'stream is not readable') {
rawBody = req.body;
}
}
this.logger.debug(`Wechatpay-Signature = ${signature}`);
this.logger.debug(`Wechatpay-Serial = ${platformSerial}`);
this.logger.debug(`Wechatpay-Timestamp = ${timestamp}`);
this.logger.debug(`Wechatpay-Nonce = ${nonce}`);
this.logger.debug(`Body = ${typeof rawBody === 'string' ? rawBody : JSON.stringify(rawBody)}`);
if (this.debug) {
this.logger.debug(`Wechatpay-Signature = ${signature}`);
this.logger.debug(`Wechatpay-Serial = ${platformSerial}`);
this.logger.debug(`Wechatpay-Timestamp = ${timestamp}`);
this.logger.debug(`Wechatpay-Nonce = ${nonce}`);
this.logger.debug(`Body = ${typeof rawBody === 'string' ? rawBody : JSON.stringify(rawBody)}`);
}
let verified = false;
const responseData = { code: 'FAIL', message: '' };
let result: Trade = {} as Trade;
Expand Down

0 comments on commit 4249761

Please sign in to comment.