|
@@ -15,7 +15,8 @@ class WeixinAuthService extends AxiosService {
|
|
|
constructor(ctx) {
|
|
|
super(ctx, {}, _.get(ctx.app.config, 'wxapi'));
|
|
|
this.prefix = 'visit-auth:';
|
|
|
- this.jsapiKey = 'visit-access_token';
|
|
|
+ this.jsapiKey = 'visit-jsapi_ticket';
|
|
|
+ this.access_tokenKey = 'visit-access_token';
|
|
|
this.wxInfo = ctx.app.config.wxapi;
|
|
|
this.authBackUrl = `${ctx.app.config.baseUrl}/api/visit/authBack`;
|
|
|
}
|
|
@@ -35,14 +36,6 @@ class WeixinAuthService extends AxiosService {
|
|
|
const val = JSON.stringify({ ...others, redirect_uri });
|
|
|
await this.app.redis.set(key, val, 'EX', 600);
|
|
|
const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${this.authBackUrl}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
this.ctx.redirect(url);
|
|
|
}
|
|
|
|
|
@@ -62,15 +55,16 @@ class WeixinAuthService extends AxiosService {
|
|
|
grant_type: 'authorization_code',
|
|
|
};
|
|
|
const req = await this.httpGet(url, params);
|
|
|
- console.log(req);
|
|
|
if (req.errcode && req.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'openid获取失败');
|
|
|
const openid = _.get(req, 'openid');
|
|
|
+ const access_token = await this.app.redis.get(this.access_tokenKey);
|
|
|
+ if (!access_token) await this.app.redis.set(this.access_tokenKey, _.get(req, 'access_token'), 'EX', 6000);
|
|
|
if (!openid) {
|
|
|
this.ctx.logger.error(JSON.stringify(req.data));
|
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '未获取到openid');
|
|
|
}
|
|
|
|
|
|
- const res = await this.httpGet('https://api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN', { appid, openid });
|
|
|
+ const res = await this.httpGet('https://api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN', { access_token, appid, openid });
|
|
|
const object = _.pick(res, [ 'nickname', 'headimgurl', 'openid' ]);
|
|
|
console.log(object);
|
|
|
|
|
@@ -110,11 +104,15 @@ class WeixinAuthService extends AxiosService {
|
|
|
let jsapi_ticket = await this.app.redis.get(this.jsapiKey);
|
|
|
const { appid, appSecret } = this.wxInfo;
|
|
|
if (!jsapi_ticket) {
|
|
|
-
|
|
|
- const atUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appSecret}`;
|
|
|
- const req = await this.ctx.curl(atUrl, { method: 'GET', dataType: 'json' });
|
|
|
- if (req.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'access_token获取失败');
|
|
|
- const access_token = _.get(req, 'data.access_token');
|
|
|
+
|
|
|
+ let access_token = await this.app.redis.get(this.access_tokenKey);
|
|
|
+ if (!access_token) {
|
|
|
+
|
|
|
+ const atUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appSecret}`;
|
|
|
+ const req = await this.ctx.curl(atUrl, { method: 'GET', dataType: 'json' });
|
|
|
+ if (req.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'access_token获取失败');
|
|
|
+ access_token = _.get(req, 'data.access_token');
|
|
|
+ }
|
|
|
|
|
|
const jtUrl = `https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi`;
|
|
|
const jtReq = await this.ctx.curl(jtUrl, { method: 'GET', dataType: 'json' });
|