|
@@ -34,15 +34,15 @@ class WeixinAuthService extends AxiosService {
|
|
|
const key = `${this.prefix}${state}`;
|
|
|
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`;
|
|
|
- let backUrl;
|
|
|
- if (this.authBackUrl.startsWith('http')) {
|
|
|
- backUrl = encodeURI(`${this.authBackUrl}?state=${state}`);
|
|
|
- } else {
|
|
|
- backUrl = encodeURI(`${this.ctx.protocol}://${this.ctx.host}${this.authBackUrl}?state=${state}`);
|
|
|
- }
|
|
|
- const url = `${baseUrl}/api/auth?appid=${appid}&response_type=code&redirect_uri=${backUrl}#wechat`;
|
|
|
- console.log(url);
|
|
|
+ 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`;
|
|
|
+ // let backUrl;
|
|
|
+ // if (this.authBackUrl.startsWith('http')) {
|
|
|
+ // backUrl = encodeURI(`${this.authBackUrl}?state=${state}`);
|
|
|
+ // } else {
|
|
|
+ // backUrl = encodeURI(`${this.ctx.protocol}://${this.ctx.host}${this.authBackUrl}?state=${state}`);
|
|
|
+ // }
|
|
|
+ // const url = `${baseUrl}/api/auth?appid=${appid}&response_type=code&redirect_uri=${backUrl}#wechat`;
|
|
|
+ // console.log(url);
|
|
|
this.ctx.redirect(url);
|
|
|
}
|
|
|
|
|
@@ -54,16 +54,14 @@ class WeixinAuthService extends AxiosService {
|
|
|
const { code, state } = query;
|
|
|
if (!code) throw new BusinessError(ErrorCode.SERVICE_FAULT, '授权未成功');
|
|
|
const { appid, appSecret } = this.wxInfo;
|
|
|
- // const url = '/api.weixin.qq.com/sns/oauth2/access_token';
|
|
|
- const url = '/api/fetch';
|
|
|
- // const params = {
|
|
|
- // appid,
|
|
|
- // secret: appSecret,
|
|
|
- // code,
|
|
|
- // grant_type: 'authorization_code',
|
|
|
- // };
|
|
|
- // const req = await this.httpGet(url, params);
|
|
|
- const req = await this.httpGet(url, { code });
|
|
|
+ const url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
|
|
|
+ const params = {
|
|
|
+ appid,
|
|
|
+ secret: appSecret,
|
|
|
+ code,
|
|
|
+ grant_type: 'authorization_code',
|
|
|
+ };
|
|
|
+ const req = await this.httpGet(url, params);
|
|
|
if (req.errcode && req.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'openid获取失败');
|
|
|
const openid = _.get(req, 'openid');
|
|
|
if (!openid) {
|
|
@@ -71,7 +69,7 @@ class WeixinAuthService extends AxiosService {
|
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '未获取到openid');
|
|
|
}
|
|
|
// 获取微信的用户信息
|
|
|
- const res = await this.httpGet('/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', { appid, openid });
|
|
|
const object = _.pick(res, [ 'nickname', 'headimgurl', 'openid' ]); // 昵称,头像,openid
|
|
|
console.log(object);
|
|
|
// 验证获取openid结束,接下来应该返回前端
|