mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2024-11-01 22:20:30 +08:00
完善微信token获取
This commit is contained in:
parent
89edb2e84e
commit
0a7f09d733
@ -1,5 +1,5 @@
|
|||||||
import { Config, Provide } from '@midwayjs/decorator';
|
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 axios from 'axios';
|
||||||
import * as crypto from 'crypto';
|
import * as crypto from 'crypto';
|
||||||
import { v1 as uuid } from 'uuid';
|
import { v1 as uuid } from 'uuid';
|
||||||
@ -20,12 +20,12 @@ export class UserWxService extends BaseService {
|
|||||||
* @param url 当前网页的URL,不包含#及其后面部分(必须是调用JS接口页面的完整URL)
|
* @param url 当前网页的URL,不包含#及其后面部分(必须是调用JS接口页面的完整URL)
|
||||||
*/
|
*/
|
||||||
public async getWxMpConfig(url: string) {
|
public async getWxMpConfig(url: string) {
|
||||||
const access_token = await this.getWxToken();
|
const token = await this.getWxToken();
|
||||||
const ticket = await axios.get(
|
const ticket = await axios.get(
|
||||||
'https://api.weixin.qq.com/cgi-bin/ticket/getticket',
|
'https://api.weixin.qq.com/cgi-bin/ticket/getticket',
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
access_token: access_token.data.access_token,
|
access_token: token.access_token,
|
||||||
type: 'jsapi',
|
type: 'jsapi',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -66,16 +66,18 @@ export class UserWxService extends BaseService {
|
|||||||
* @param appid
|
* @param appid
|
||||||
* @param secret
|
* @param secret
|
||||||
*/
|
*/
|
||||||
|
@CoolCache(3600)
|
||||||
public async getWxToken(type = 'mp') {
|
public async getWxToken(type = 'mp') {
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
const conf = this.config.wx[type];
|
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: {
|
params: {
|
||||||
grant_type: 'client_credential',
|
grant_type: 'client_credential',
|
||||||
appid: conf.appid,
|
appid: conf.appid,
|
||||||
secret: conf.secret,
|
secret: conf.secret,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
return result.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user