weixin.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 _ = require('lodash');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. const jwt = require('jsonwebtoken');
  9. const { AxiosService } = require('naf-framework-mongoose/lib/service');
  10. class WeixinAuthService extends AxiosService {
  11. constructor(ctx) {
  12. super(ctx, {}, _.get(ctx.app.config, 'wxapi'));
  13. this.prefix = 'visit-auth:';
  14. this.jsapiKey = 'visit-access_token';
  15. this.wxInfo = ctx.app.config.wxapi;
  16. }
  17. /**
  18. * 网页授权
  19. * @param {Object} query 参数
  20. */
  21. async auth(query) {
  22. let { redirect_uri, ...others } = query;
  23. if (!redirect_uri) {
  24. redirect_uri = `${this.ctx.app.config.baseUrl}/api/visit/authBack`;
  25. }
  26. const { appid } = this.wxInfo;
  27. if (!appid) {
  28. throw new BusinessError(ErrorCode.SERVICE_FAULT, '缺少公众号设置');
  29. }
  30. // 用于redis
  31. const state = uuid.v4();
  32. const key = `${this.prefix}${state}`;
  33. const val = JSON.stringify({ ...others, redirect_uri });
  34. await this.app.redis.set(key, val, 'EX', 600);
  35. const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;
  36. this.ctx.redirect(url);
  37. }
  38. /**
  39. * 网页授权回调,获取openid
  40. * @param {Object} query 参数
  41. */
  42. async authBack(query) {
  43. const { code, state } = query;
  44. if (!code) throw new BusinessError(ErrorCode.SERVICE_FAULT, '授权未成功');
  45. const { appid, appSecret } = this.wxInfo;
  46. const url = ` https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${appSecret}&code=${code}&grant_type=authorization_code`;
  47. const req = this.ctx.curl(url, { method: 'GET' });
  48. if (req.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'openid获取失败');
  49. const openid = _.get(req, 'data.openid');
  50. if (!openid) {
  51. this.ctx.logger.error(JSON.stringify(req.data));
  52. throw new BusinessError(ErrorCode.SERVICE_FAULT, '未获取到openid');
  53. }
  54. // 验证获取openid结束,接下来应该返回前端
  55. const key = `${this.prefix}${state}`;
  56. let fqueries = await this.app.redis.get(key);
  57. if (fqueries)fqueries = JSON.parse(fqueries);
  58. const { redirect_uri } = fqueries;
  59. this.ctx.redirect(redirect_uri);
  60. }
  61. /**
  62. * JsApi验证
  63. * @param {Object} query 参数
  64. */
  65. async jsapiAuth(query) {
  66. const { url } = query;
  67. let jsapi_ticket = await this.app.redis.get(this.jsapiKey);
  68. const { appid, appSecret } = this.wxInfo;
  69. if (!jsapi_ticket) {
  70. // 1,重新获取access_token
  71. const atUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appSecret}`;
  72. const req = await this.ctx.curl(atUrl, { method: 'GET' });
  73. if (req.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'access_token获取失败');
  74. const access_token = _.get(req, 'data.access_token');
  75. // 2,获取jsapi_token
  76. const jtUrl = `https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi`;
  77. const jtReq = await this.ctx.curl(jtUrl, { method: 'GET' });
  78. if (jtReq.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'jsapi_ticket获取失败');
  79. jsapi_ticket = _.get(jtReq, 'data.ticket');
  80. // 实际过期时间是7200s(2h),系统默认设置6000s
  81. const expiresIn = _.get(jtReq, 'data.expires_in', 6000);
  82. // 缓存jsapi_ticket,重复使用
  83. await this.app.redis.set(this.jsapiKey, jsapi_ticket, 'EX', expiresIn);
  84. }
  85. const noncestr = random(16);
  86. const timestamp = new Date().getTime();
  87. const signStr = `jsapi_ticket=${jsapi_ticket}&noncestr=${noncestr}&timestamp=${timestamp}&url=${url}`;
  88. const sign = crypto.createHash('sha1').update(signStr).digest('hex');
  89. return { jsapi_ticket, noncestr, timestamp, signStr, sign, appid };
  90. }
  91. async createJwt({ openid, nickname, subscribe }) {
  92. const { secret, expiresIn = '1d', issuer = 'weixin' } = this.config.jwt;
  93. const subject = openid;
  94. const userinfo = { nickname, subscribe };
  95. const token = await jwt.sign(userinfo, secret, { expiresIn, issuer, subject });
  96. return token;
  97. }
  98. /**
  99. * 创建二维码
  100. * 随机生成二维码,并保存在Redis中,状态初始为pending
  101. * 状态描述:
  102. * pending - 等待扫码
  103. * consumed - 使用二维码登录完成
  104. * scand:token - Jwt登录凭证
  105. */
  106. async createQrcode() {
  107. const qrcode = uuid();
  108. const key = `visit:qrcode:group:${qrcode}`;
  109. await this.app.redis.set(key, 'pending', 'EX', 600);
  110. return qrcode;
  111. }
  112. /**
  113. * 创建二维码
  114. * 生成群二维码
  115. * 状态描述:
  116. * pending - 等待扫码
  117. * consumed - 使用二维码登录完成
  118. * scand:token - Jwt登录凭证
  119. */
  120. async createQrcodeGroup({ groupid }) {
  121. const { authUrl = this.ctx.path } = this.app.config;
  122. let backUrl;
  123. if (authUrl.startsWith('http')) {
  124. backUrl = encodeURI(`${authUrl}?state=${groupid}`);
  125. } else {
  126. backUrl = encodeURI(`${this.ctx.protocol}://${this.ctx.host}${authUrl}?state=${groupid}`);
  127. }
  128. console.log(backUrl);
  129. return backUrl;
  130. }
  131. /**
  132. * 扫码登录确认
  133. */
  134. async scanQrcode({ qrcode, token }) {
  135. assert(qrcode, 'qrcode不能为空');
  136. assert(token, 'token不能为空');
  137. const key = `smart:qrcode:login:${qrcode}`;
  138. const status = await this.app.redis.get(key);
  139. if (!status) {
  140. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
  141. }
  142. if (status !== 'pending') {
  143. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码状态无效');
  144. }
  145. // 验证Token
  146. const { secret } = this.config.jwt;
  147. const decoded = jwt.verify(token, secret, { issuer: 'weixin' });
  148. this.ctx.logger.debug(`[weixin] qrcode login - ${decoded}`);
  149. // TODO: 修改二维码状态,登录凭证保存到redis
  150. await this.app.redis.set(key, `scaned:${token}`, 'EX', 600);
  151. // TODO: 发布扫码成功消息
  152. const { mq } = this.ctx;
  153. const ex = 'qrcode.login';
  154. if (mq) {
  155. await mq.topic(ex, qrcode, 'scaned', { durable: true });
  156. } else {
  157. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  158. }
  159. }
  160. // 使用二维码换取登录凭证
  161. async qrcodeLogin(qrcode) {
  162. assert(qrcode, 'qrcode不能为空');
  163. const key = `smart:qrcode:login:${qrcode}`;
  164. const val = await this.app.redis.get(key);
  165. if (!val) {
  166. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
  167. }
  168. const [ status, token ] = val.split(':', 2);
  169. if (status !== 'scaned' || !token) {
  170. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码状态无效');
  171. }
  172. // TODO: 修改二维码状态
  173. await this.app.redis.set(key, 'consumed', 'EX', 600);
  174. return { token };
  175. }
  176. // 检查二维码状态
  177. async checkQrcode(qrcode) {
  178. assert(qrcode, 'qrcode不能为空');
  179. const key = `smart:qrcode:login:${qrcode}`;
  180. const val = await this.app.redis.get(key);
  181. if (!val) {
  182. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
  183. }
  184. const [ status ] = val.split(':', 2);
  185. return { status };
  186. }
  187. }
  188. module.exports = WeixinAuthService;