weixin.js 9.4 KB

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