|
@@ -13,7 +13,7 @@ const { AxiosService } = require('naf-framework-mongoose/lib/service');
|
|
|
|
|
|
class WeixinAuthService extends AxiosService {
|
|
class WeixinAuthService extends AxiosService {
|
|
constructor(ctx) {
|
|
constructor(ctx) {
|
|
- super(ctx, {}, _.get(ctx.app.config, 'wxapi'));
|
|
|
|
|
|
+ super(ctx, {}, {}); // _.get(ctx.app.config, 'wxapi')
|
|
this.prefix = 'visit-auth:';
|
|
this.prefix = 'visit-auth:';
|
|
this.jsapiKey = 'visit-access_token';
|
|
this.jsapiKey = 'visit-access_token';
|
|
this.wxInfo = ctx.app.config.wxapi;
|
|
this.wxInfo = ctx.app.config.wxapi;
|
|
@@ -34,15 +34,15 @@ class WeixinAuthService extends AxiosService {
|
|
const key = `${this.prefix}${state}`;
|
|
const key = `${this.prefix}${state}`;
|
|
const val = JSON.stringify({ ...others, redirect_uri });
|
|
const val = JSON.stringify({ ...others, redirect_uri });
|
|
await this.app.redis.set(key, val, 'EX', 600);
|
|
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);
|
|
this.ctx.redirect(url);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -55,7 +55,7 @@ class WeixinAuthService extends AxiosService {
|
|
const { code, state } = query;
|
|
const { code, state } = query;
|
|
if (!code) throw new BusinessError(ErrorCode.SERVICE_FAULT, '授权未成功');
|
|
if (!code) throw new BusinessError(ErrorCode.SERVICE_FAULT, '授权未成功');
|
|
const { appid, appSecret } = this.wxInfo;
|
|
const { appid, appSecret } = this.wxInfo;
|
|
- const url = '/api.weixin.qq.com/sns/oauth2/access_token';
|
|
|
|
|
|
+ const url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
|
|
const params = {
|
|
const params = {
|
|
appid,
|
|
appid,
|
|
secret: appSecret,
|
|
secret: appSecret,
|
|
@@ -72,7 +72,7 @@ class WeixinAuthService extends AxiosService {
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '未获取到openid');
|
|
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
|
|
const object = _.pick(res, [ 'nickname', 'headimgurl', 'openid' ]); // 昵称,头像,openid
|
|
console.log(object);
|
|
console.log(object);
|
|
// 验证获取openid结束,接下来应该返回前端
|
|
// 验证获取openid结束,接下来应该返回前端
|