weixin.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. 'use strict';
  2. const assert = require('assert');
  3. const uuid = require('uuid');
  4. const random = require('string-random');
  5. const crypto = require('crypto');
  6. const urljoin = require('url-join');
  7. const _ = require('lodash');
  8. const moment = require('moment');
  9. const { BusinessError, ErrorCode } = require('naf-core').Error;
  10. const jwt = require('jsonwebtoken');
  11. const { AxiosService } = require('naf-framework-mongoose/lib/service');
  12. const { access } = require('fs');
  13. class WeixinAuthService extends AxiosService {
  14. constructor(ctx, options) {
  15. super(ctx, {}, _.get(ctx.app.config, 'wxapi', {}));
  16. this.prefix = 'auth:';
  17. this.jsapiKey = 'jsapi_ticket';
  18. this.access_tokenKey = 'access_token';
  19. this.wxInfo = ctx.app.config.wxapi;
  20. this.authBackUrl = `${ctx.app.config.baseUrl}/api/live/v0/authBack`;
  21. }
  22. async getPublishList({ site }, { skip = 0, limit = 10 } = {}) {
  23. let access_token = await this.app.redis.get(`${this.access_tokenKey}_${site}`);
  24. if (!access_token && access_token === '') {
  25. access_token = await this.getAccessToken(site);
  26. }
  27. const url = `https://api.weixin.qq.com/cgi-bin/freepublish/batchget`;
  28. const body = {
  29. offset: skip,
  30. count: limit,
  31. };
  32. const req = await this.httpPost(url, { access_token }, body);
  33. if (req.errcode && req.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, '消息发布列表请求失败');
  34. const rObj = {
  35. data: _.get(req, 'item', []),
  36. total: _.get(req, 'total_count', 0),
  37. };
  38. return rObj;
  39. }
  40. /**
  41. * 网页授权
  42. * @param {String} site 公众号标识,对config中设置
  43. * @param {Object} query 参数
  44. */
  45. async auth({ site }, query) {
  46. const { redirect_uri, ...others } = query;
  47. const { baseUrl } = this.wxInfo;
  48. const { appid } = this.wxInfo[site];
  49. if (!appid) {
  50. throw new BusinessError(ErrorCode.SERVICE_FAULT, '缺少公众号设置');
  51. }
  52. // 用于redis
  53. const state = uuid.v4();
  54. const key = `${this.prefix}${state}`;
  55. const val = JSON.stringify({ ...others, redirect_uri, site });
  56. await this.app.redis.set(key, val, 'EX', 600);
  57. // 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`;
  58. let backUrl;
  59. if (this.authBackUrl.startsWith('http')) {
  60. backUrl = encodeURI(`${this.authBackUrl}?state=${state}`);
  61. } else {
  62. backUrl = encodeURI(`${this.ctx.protocol}://${this.ctx.host}${this.authBackUrl}?state=${state}`);
  63. }
  64. const to_uri = `${baseUrl}/api/auth?appid=${appid}&response_type=code&redirect_uri=${backUrl}#wechat`;
  65. this.ctx.redirect(to_uri);
  66. }
  67. /**
  68. * 网页授权回调,获取openid
  69. * @param {Object} query 参数
  70. */
  71. async authBack(query) {
  72. const { code, state } = query;
  73. if (!code) throw new BusinessError(ErrorCode.SERVICE_FAULT, '授权未成功');
  74. const req = await this.httpGet('/api/fetch', { code });
  75. if (req.errcode && req.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'openid获取失败');
  76. const openid = _.get(req, 'openid');
  77. if (!openid) {
  78. this.ctx.logger.error(JSON.stringify(req.data));
  79. throw new BusinessError(ErrorCode.SERVICE_FAULT, '未获取到openid');
  80. }
  81. // 验证获取openid结束,接下来应该返回前端
  82. const key = `${this.prefix}${state}`;
  83. let fqueries = await this.app.redis.get(key);
  84. if (fqueries) fqueries = JSON.parse(fqueries);
  85. let { redirect_uri } = fqueries;
  86. const queryStr = `?openid=${openid}`;
  87. redirect_uri = urljoin(redirect_uri, queryStr);
  88. this.ctx.redirect(redirect_uri);
  89. }
  90. /**
  91. * 换微信用户信息
  92. * @param {String} site 公众号标识,对config中设置
  93. * @param {Object} {openid}
  94. */
  95. async wxUser({ site }, { openid }) {
  96. const { appid } = this.wxInfo[site];
  97. const res = await this.httpGet('/api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN', { appid, openid });
  98. const object = _.pick(res, ['nickname', 'headimgurl', 'openid']); // 昵称,头像,openid
  99. return { name: object.nickname, icon: object.headimgurl, openid };
  100. }
  101. /**
  102. * JsApi验证
  103. * @param {String} site 公众号标识,对config中设置
  104. * @param {Object} query 参数
  105. */
  106. async jsapiAuth({ site }, query) {
  107. let { url } = query;
  108. url = decodeURIComponent(url);
  109. let jsapi_ticket = await this.app.redis.get(`${this.jsapiKey}_${site}`);
  110. let access_token = await this.app.redis.get(`${this.access_tokenKey}_${site}`);
  111. const { appid } = this.wxInfo[site];
  112. if (!access_token) {
  113. access_token = await this.getAccessToken(site);
  114. }
  115. if (!jsapi_ticket) {
  116. // 2,获取jsapi_token
  117. const jtUrl = `https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi`;
  118. const jtReq = await this.ctx.curl(jtUrl, { method: 'GET', dataType: 'json' });
  119. if (jtReq.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'jsapi_ticket获取失败');
  120. jsapi_ticket = _.get(jtReq, 'data.ticket');
  121. // 实际过期时间是7200s(2h),系统默认设置6000s
  122. const expiresIn = _.get(jtReq, 'data.expires_in', 6000);
  123. // 缓存jsapi_ticket,重复使用
  124. await this.app.redis.set(`${this.jsapiKey}_${site}`, jsapi_ticket, 'EX', expiresIn);
  125. }
  126. const noncestr = random(16).toLowerCase();
  127. const timestamp = moment().unix();
  128. const signStr = `jsapi_ticket=${jsapi_ticket}&noncestr=${noncestr}&timestamp=${timestamp}&url=${url}`;
  129. const sign = crypto.createHash('sha1').update(signStr).digest('hex');
  130. return { jsapi_ticket, noncestr, timestamp, sign, appid, url };
  131. }
  132. /**
  133. * 获取公众号access_token
  134. * @param {String} site 公众号
  135. */
  136. async getAccessToken(site) {
  137. const { appid, appSecret } = this.wxInfo[site];
  138. const expires_in = 6000;
  139. // 1,获取access_token
  140. const atUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appSecret}`;
  141. const req = await this.ctx.curl(atUrl, { method: 'GET', dataType: 'json' });
  142. if (req.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'access_token获取失败');
  143. const access_token = _.get(req, 'data.access_token');
  144. await this.app.redis.set(`${this.access_tokenKey}_${site}`, access_token, 'EX', expires_in);
  145. return access_token;
  146. }
  147. async createJwt({ openid, nickname, subscribe }) {
  148. const { secret, expiresIn = '1d', issuer = 'weixin' } = this.config.jwt;
  149. const subject = openid;
  150. const userinfo = { nickname, subscribe };
  151. const token = await jwt.sign(userinfo, secret, { expiresIn, issuer, subject });
  152. return token;
  153. }
  154. /**
  155. * 创建二维码
  156. * 随机生成二维码,并保存在Redis中,状态初始为pending
  157. * 状态描述:
  158. * pending - 等待扫码
  159. * consumed - 使用二维码登录完成
  160. * scand:token - Jwt登录凭证
  161. */
  162. async createQrcode() {
  163. const qrcode = uuid();
  164. const key = `visit:qrcode:group:${qrcode}`;
  165. await this.app.redis.set(key, 'pending', 'EX', 600);
  166. return qrcode;
  167. }
  168. /**
  169. * 创建二维码
  170. * 生成群二维码
  171. * 状态描述:
  172. * pending - 等待扫码
  173. * consumed - 使用二维码登录完成
  174. * scand:token - Jwt登录凭证
  175. */
  176. async createQrcodeGroup({ groupid }) {
  177. const { authUrl = this.ctx.path } = this.app.config;
  178. let backUrl;
  179. if (authUrl.startsWith('http')) {
  180. backUrl = encodeURI(`${authUrl}?state=${groupid}`);
  181. } else {
  182. backUrl = encodeURI(`${this.ctx.protocol}://${this.ctx.host}${authUrl}?state=${groupid}`);
  183. }
  184. console.log(backUrl);
  185. return backUrl;
  186. }
  187. /**
  188. * 扫码登录确认
  189. */
  190. async scanQrcode({ qrcode, token }) {
  191. assert(qrcode, 'qrcode不能为空');
  192. assert(token, 'token不能为空');
  193. const key = `smart:qrcode:login:${qrcode}`;
  194. const status = await this.app.redis.get(key);
  195. if (!status) {
  196. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
  197. }
  198. if (status !== 'pending') {
  199. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码状态无效');
  200. }
  201. // 验证Token
  202. const { secret } = this.config.jwt;
  203. const decoded = jwt.verify(token, secret, { issuer: 'weixin' });
  204. this.ctx.logger.debug(`[weixin] qrcode login - ${decoded}`);
  205. // TODO: 修改二维码状态,登录凭证保存到redis
  206. await this.app.redis.set(key, `scaned:${token}`, 'EX', 600);
  207. // TODO: 发布扫码成功消息
  208. const { mq } = this.ctx;
  209. const ex = 'qrcode.login';
  210. if (mq) {
  211. await mq.topic(ex, qrcode, 'scaned', { durable: true });
  212. } else {
  213. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  214. }
  215. }
  216. // 使用二维码换取登录凭证
  217. async qrcodeLogin(qrcode) {
  218. assert(qrcode, 'qrcode不能为空');
  219. const key = `smart:qrcode:login:${qrcode}`;
  220. const val = await this.app.redis.get(key);
  221. if (!val) {
  222. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
  223. }
  224. const [status, token] = val.split(':', 2);
  225. if (status !== 'scaned' || !token) {
  226. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码状态无效');
  227. }
  228. // TODO: 修改二维码状态
  229. await this.app.redis.set(key, 'consumed', 'EX', 600);
  230. return { token };
  231. }
  232. // 检查二维码状态
  233. async checkQrcode(qrcode) {
  234. assert(qrcode, 'qrcode不能为空');
  235. const key = `smart:qrcode:login:${qrcode}`;
  236. const val = await this.app.redis.get(key);
  237. if (!val) {
  238. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
  239. }
  240. const [status] = val.split(':', 2);
  241. return { status };
  242. }
  243. }
  244. module.exports = WeixinAuthService;