|
@@ -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);
|
|
|
this.ctx.redirect(url);
|
|
|
}
|
|
|
|
|
@@ -51,20 +51,19 @@ class WeixinAuthService extends AxiosService {
|
|
|
* @param {Object} query 参数
|
|
|
*/
|
|
|
async authBack(query) {
|
|
|
- console.log('in function:back');
|
|
|
const { code, state } = query;
|
|
|
if (!code) throw new BusinessError(ErrorCode.SERVICE_FAULT, '授权未成功');
|
|
|
const { appid, appSecret } = this.wxInfo;
|
|
|
- const url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
|
|
|
- const params = {
|
|
|
- appid,
|
|
|
- secret: appSecret,
|
|
|
- code,
|
|
|
- grant_type: 'authorization_code',
|
|
|
- };
|
|
|
- console.log(params);
|
|
|
- const req = await this.httpGet(url, params);
|
|
|
- console.log(req);
|
|
|
+
|
|
|
+ const url = '/api/fetch';
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ const req = await this.httpGet(url, { code });
|
|
|
if (req.errcode && req.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'openid获取失败');
|
|
|
const openid = _.get(req, 'openid');
|
|
|
if (!openid) {
|
|
@@ -72,7 +71,7 @@ class WeixinAuthService extends AxiosService {
|
|
|
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('/api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN', { appid, openid });
|
|
|
const object = _.pick(res, [ 'nickname', 'headimgurl', 'openid' ]);
|
|
|
console.log(object);
|
|
|
|