From 0a7f09d733e6e30ea727db722051d8a595730453 Mon Sep 17 00:00:00 2001 From: cool Date: Tue, 15 Aug 2023 15:55:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=BE=AE=E4=BF=A1token?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/user/service/wx.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/modules/user/service/wx.ts b/src/modules/user/service/wx.ts index 14482d6..416484f 100644 --- a/src/modules/user/service/wx.ts +++ b/src/modules/user/service/wx.ts @@ -1,5 +1,5 @@ import { Config, Provide } from '@midwayjs/decorator'; -import { BaseService, CoolCommException } from '@cool-midway/core'; +import { BaseService, CoolCache, CoolCommException } from '@cool-midway/core'; import axios from 'axios'; import * as crypto from 'crypto'; import { v1 as uuid } from 'uuid'; @@ -20,12 +20,12 @@ export class UserWxService extends BaseService { * @param url 当前网页的URL,不包含#及其后面部分(必须是调用JS接口页面的完整URL) */ public async getWxMpConfig(url: string) { - const access_token = await this.getWxToken(); + const token = await this.getWxToken(); const ticket = await axios.get( 'https://api.weixin.qq.com/cgi-bin/ticket/getticket', { params: { - access_token: access_token.data.access_token, + access_token: token.access_token, type: 'jsapi', }, } @@ -66,16 +66,18 @@ export class UserWxService extends BaseService { * @param appid * @param secret */ + @CoolCache(3600) public async getWxToken(type = 'mp') { //@ts-ignore const conf = this.config.wx[type]; - return await axios.get('https://api.weixin.qq.com/cgi-bin/token', { + const result = await axios.get('https://api.weixin.qq.com/cgi-bin/token', { params: { grant_type: 'client_credential', appid: conf.appid, secret: conf.secret, }, }); + return result.data; } /**