weixin.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const uuid = require('uuid');
  4. const jwt = require('jsonwebtoken');
  5. class LoginController extends Controller {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.service = this.ctx.service.login;
  9. this.model = this.ctx.model.Admin;
  10. }
  11. // 获取openid
  12. async auth() {
  13. try {
  14. const { redirect_uri, code } = this.ctx.query;
  15. if (redirect_uri) {
  16. await this.app.redis.set('redirect_uri', redirect_uri, 'EX', 600);
  17. }
  18. if (code) {
  19. return await this.authBack({ code });
  20. }
  21. // TODO: 生成回调地址
  22. const { wxapi, authUrl = this.ctx.path } = this.app.config;
  23. const referer = this.ctx.header.referer;
  24. if (referer) {
  25. const host = referer.split('/')[2];
  26. const backUrl = encodeURI(`${this.ctx.protocol}://${host}${authUrl}`);
  27. const to_uri = `${wxapi.baseUrl}/api/auth?appid=${wxapi.appid}&response_type=code&redirect_uri=${backUrl}#wechat`;
  28. this.ctx.redirect(to_uri);
  29. }
  30. } catch (error) {
  31. console.log(error);
  32. }
  33. }
  34. // GET 获取openid认证回调
  35. async authBack({ code }) {
  36. const { weixin } = this.ctx.service;
  37. const val = await this.app.redis.get('redirect_uri');
  38. const res = await weixin.fetch(code);
  39. const openid = res.openid;
  40. const { secret, expiresIn = '1h' } = this.config.jwt;
  41. const subject = openid;
  42. const token = await jwt.sign({ openid }, secret, { expiresIn, issuer: 'user', subject });
  43. // TODO: 重定性到跳转页面
  44. await this.ctx.render('redirect.njk', { openid, redirect_uri: val, token });
  45. }
  46. // POST 绑定用户微信号
  47. async bind() {
  48. try {
  49. const { code } = this.ctx.query;
  50. if (code) {
  51. return await this.Back({ code });
  52. }
  53. // TODO: 生成回调地址
  54. const { wxapi, authUrl = this.ctx.path } = this.app.config;
  55. const host = this.ctx.header['x-forwarded-host'];
  56. const backUrl = encodeURI(`${this.ctx.protocol}://${host}${authUrl}`);
  57. const to_uri = `${wxapi.baseUrl}/api/auth?appid=${wxapi.appid}&response_type=code&redirect_uri=${backUrl}#wechat`;
  58. this.ctx.redirect(to_uri);
  59. } catch (error) {
  60. console.log(error);
  61. }
  62. }
  63. // GET 绑定认证回调
  64. async Back({ code }) {
  65. const { weixin } = this.ctx.service;
  66. const val = await this.app.redis.get('key');
  67. const res = await weixin.fetch(code);
  68. const openid = res.openid;
  69. // TODO: 重定性到跳转页面
  70. await this.ctx.render('weixinbind.njk', { openid, id: val });
  71. }
  72. // mp
  73. async mqtt() {
  74. const key = this.ctx.params.key;
  75. let msg = 'success';
  76. if (key === 'bind') msg = JSON.stringify({ openid: this.ctx.query.openid });
  77. if (key === 'pay') msg = JSON.stringify(this.ctx.query);
  78. // TODO: 发布扫码成功消息
  79. const { mq } = this.ctx;
  80. if (mq) {
  81. await mq.topic('qrcode.topic', key, msg, { durable: true });
  82. } else {
  83. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  84. }
  85. this.ctx.ok();
  86. }
  87. // POST 检查二维码
  88. async check() {
  89. const uuid = this.ctx.query;
  90. const res = await this.service.checkQrcode(uuid);
  91. this.ctx.ok(res);
  92. }
  93. // 获取uuid创建二维码
  94. async getuuid() {
  95. const out_trade_no = uuid.v1();
  96. await this.app.redis.set(out_trade_no, 'ready', 'EX', 600);
  97. this.ctx.ok({ uuid: out_trade_no });
  98. }
  99. // 二维码登录
  100. async qrcodelogin() {
  101. const { ctx } = this;
  102. const { uuid } = ctx.params;
  103. await this.service.qrcodelogin({ uuid });
  104. // this.ctx.ok(res);
  105. }
  106. // get uuid换取token
  107. async qrcodeToken() {
  108. const { ctx } = this;
  109. const { uuid } = ctx.query;
  110. const res = await this.service.qrcodeToken(uuid);
  111. this.ctx.ok({ data: JSON.parse(res) });
  112. }
  113. // 获取微信token
  114. async gettoken() {
  115. const { weixin } = this.ctx.service;
  116. const res = await weixin.gettoken();
  117. this.ctx.ok({ data: res });
  118. }
  119. // 预支付订单
  120. async orderPay() {
  121. const { openid, money, out_trade_no, hospitalName, subjectName, specialistName, _id } = this.ctx.request.body;
  122. await this.ctx.service.weixin.orderPay({ openid, money, out_trade_no, description: `${hospitalName}-${subjectName}-${specialistName}`, _id });
  123. this.ctx.ok();
  124. }
  125. // 支付接口
  126. async pay() {
  127. await this.ctx.service.weixin.pay();
  128. }
  129. // 关闭订单
  130. async orderClose() {
  131. const { out_trade_no } = this.ctx.request.body;
  132. const res = await this.ctx.service.weixin.orderClose({ out_trade_no });
  133. this.ctx.ok(res);
  134. }
  135. // 消息模板下发
  136. async pushMould() {
  137. const { out_trade_no, openid } = this.ctx.request.body;
  138. await this.ctx.service.weixin.pushMould({ out_trade_no, openid });
  139. this.ctx.ok('ok');
  140. }
  141. }
  142. module.exports = LoginController;