|
@@ -10,6 +10,7 @@ const moment = require('moment');
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
const jwt = require('jsonwebtoken');
|
|
const jwt = require('jsonwebtoken');
|
|
const { AxiosService } = require('naf-framework-mongoose/lib/service');
|
|
const { AxiosService } = require('naf-framework-mongoose/lib/service');
|
|
|
|
+const { access } = require('fs');
|
|
|
|
|
|
class WeixinAuthService extends AxiosService {
|
|
class WeixinAuthService extends AxiosService {
|
|
constructor(ctx, options) {
|
|
constructor(ctx, options) {
|
|
@@ -20,6 +21,26 @@ class WeixinAuthService extends AxiosService {
|
|
this.wxInfo = ctx.app.config.wxapi;
|
|
this.wxInfo = ctx.app.config.wxapi;
|
|
this.authBackUrl = `${ctx.app.config.baseUrl}/api/live/v0/authBack`;
|
|
this.authBackUrl = `${ctx.app.config.baseUrl}/api/live/v0/authBack`;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ async getPublishList({ site }, { skip = 0, limit = 10 } = {}) {
|
|
|
|
+ let access_token = await this.app.redis.get(`${this.access_tokenKey}_${site}`);
|
|
|
|
+ if (!access_token) {
|
|
|
|
+ access_token = await this.getAccessToken(site);
|
|
|
|
+ }
|
|
|
|
+ const url = `https://api.weixin.qq.com/cgi-bin/freepublish/batchget`;
|
|
|
|
+ const body = {
|
|
|
|
+ offset: skip,
|
|
|
|
+ count: limit,
|
|
|
|
+ };
|
|
|
|
+ const req = await this.httpPost(url, { access_token }, body);
|
|
|
|
+ if (req.errcode && req.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, '消息发布列表请求失败');
|
|
|
|
+ const rObj = {
|
|
|
|
+ data: _.get(req, 'item', []),
|
|
|
|
+ total: _.get(req, 'total_count', 0),
|
|
|
|
+ };
|
|
|
|
+ return rObj;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 网页授权
|
|
* 网页授权
|
|
* @param {String} site 公众号标识,对config中设置
|
|
* @param {String} site 公众号标识,对config中设置
|
|
@@ -65,7 +86,7 @@ class WeixinAuthService extends AxiosService {
|
|
// 验证获取openid结束,接下来应该返回前端
|
|
// 验证获取openid结束,接下来应该返回前端
|
|
const key = `${this.prefix}${state}`;
|
|
const key = `${this.prefix}${state}`;
|
|
let fqueries = await this.app.redis.get(key);
|
|
let fqueries = await this.app.redis.get(key);
|
|
- if (fqueries)fqueries = JSON.parse(fqueries);
|
|
|
|
|
|
+ if (fqueries) fqueries = JSON.parse(fqueries);
|
|
let { redirect_uri } = fqueries;
|
|
let { redirect_uri } = fqueries;
|
|
const queryStr = `?openid=${openid}`;
|
|
const queryStr = `?openid=${openid}`;
|
|
redirect_uri = urljoin(redirect_uri, queryStr);
|
|
redirect_uri = urljoin(redirect_uri, queryStr);
|
|
@@ -79,7 +100,7 @@ class WeixinAuthService extends AxiosService {
|
|
async wxUser({ site }, { openid }) {
|
|
async wxUser({ site }, { openid }) {
|
|
const { appid } = this.wxInfo[site];
|
|
const { appid } = this.wxInfo[site];
|
|
const res = await this.httpGet('/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' ]); // 昵称,头像,openid
|
|
|
|
|
|
+ const object = _.pick(res, ['nickname', 'headimgurl', 'openid']); // 昵称,头像,openid
|
|
return { name: object.nickname, icon: object.headimgurl, openid };
|
|
return { name: object.nickname, icon: object.headimgurl, openid };
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
@@ -90,14 +111,13 @@ class WeixinAuthService extends AxiosService {
|
|
async jsapiAuth({ site }, query) {
|
|
async jsapiAuth({ site }, query) {
|
|
let { url } = query;
|
|
let { url } = query;
|
|
url = decodeURIComponent(url);
|
|
url = decodeURIComponent(url);
|
|
- let jsapi_ticket = await this.app.redis.get(this.jsapiKey);
|
|
|
|
- const { appid, appSecret } = this.wxInfo[site];
|
|
|
|
|
|
+ let jsapi_ticket = await this.app.redis.get(`${this.jsapiKey}_${site}`);
|
|
|
|
+ let access_token = await this.app.redis.get(`${this.access_tokenKey}_${site}`);
|
|
|
|
+ const { appid } = this.wxInfo[site];
|
|
|
|
+ if (!access_token) {
|
|
|
|
+ access_token = await this.getAccessToken(site);
|
|
|
|
+ }
|
|
if (!jsapi_ticket) {
|
|
if (!jsapi_ticket) {
|
|
- // 1,获取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获取失败');
|
|
|
|
- const access_token = _.get(req, 'data.access_token');
|
|
|
|
// 2,获取jsapi_token
|
|
// 2,获取jsapi_token
|
|
const jtUrl = `https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi`;
|
|
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' });
|
|
const jtReq = await this.ctx.curl(jtUrl, { method: 'GET', dataType: 'json' });
|
|
@@ -106,7 +126,7 @@ class WeixinAuthService extends AxiosService {
|
|
// 实际过期时间是7200s(2h),系统默认设置6000s
|
|
// 实际过期时间是7200s(2h),系统默认设置6000s
|
|
const expiresIn = _.get(jtReq, 'data.expires_in', 6000);
|
|
const expiresIn = _.get(jtReq, 'data.expires_in', 6000);
|
|
// 缓存jsapi_ticket,重复使用
|
|
// 缓存jsapi_ticket,重复使用
|
|
- await this.app.redis.set(this.jsapiKey, jsapi_ticket, 'EX', expiresIn);
|
|
|
|
|
|
+ await this.app.redis.set(`${this.jsapiKey}_${site}`, jsapi_ticket, 'EX', expiresIn);
|
|
}
|
|
}
|
|
const noncestr = random(16).toLowerCase();
|
|
const noncestr = random(16).toLowerCase();
|
|
const timestamp = moment().unix();
|
|
const timestamp = moment().unix();
|
|
@@ -114,7 +134,20 @@ class WeixinAuthService extends AxiosService {
|
|
const sign = crypto.createHash('sha1').update(signStr).digest('hex');
|
|
const sign = crypto.createHash('sha1').update(signStr).digest('hex');
|
|
return { jsapi_ticket, noncestr, timestamp, sign, appid, url };
|
|
return { jsapi_ticket, noncestr, timestamp, sign, appid, url };
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取公众号access_token
|
|
|
|
+ * @param {String} site 公众号
|
|
|
|
+ */
|
|
|
|
+ async getAccessToken(site) {
|
|
|
|
+ const { appid, appSecret } = this.wxInfo[site];
|
|
|
|
+ // 1,获取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');
|
|
|
|
+ await this.app.redis.set(`${this.access_tokenKey}_${site}`, access_token, 'EX', expiresIn);
|
|
|
|
+ return access_token;
|
|
|
|
+ }
|
|
|
|
|
|
async createJwt({ openid, nickname, subscribe }) {
|
|
async createJwt({ openid, nickname, subscribe }) {
|
|
const { secret, expiresIn = '1d', issuer = 'weixin' } = this.config.jwt;
|
|
const { secret, expiresIn = '1d', issuer = 'weixin' } = this.config.jwt;
|
|
@@ -199,7 +232,7 @@ class WeixinAuthService extends AxiosService {
|
|
if (!val) {
|
|
if (!val) {
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
|
|
}
|
|
}
|
|
- const [ status, token ] = val.split(':', 2);
|
|
|
|
|
|
+ const [status, token] = val.split(':', 2);
|
|
if (status !== 'scaned' || !token) {
|
|
if (status !== 'scaned' || !token) {
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码状态无效');
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码状态无效');
|
|
}
|
|
}
|
|
@@ -218,7 +251,7 @@ class WeixinAuthService extends AxiosService {
|
|
if (!val) {
|
|
if (!val) {
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
|
|
}
|
|
}
|
|
- const [ status ] = val.split(':', 2);
|
|
|
|
|
|
+ const [status] = val.split(':', 2);
|
|
return { status };
|
|
return { status };
|
|
}
|
|
}
|
|
}
|
|
}
|