wxapi.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const { v4: uuidv4 } = require('uuid');
  4. class WxapiController extends Controller {
  5. async get_openid() {
  6. const { ctx } = this;
  7. const { code, redirect_uri, userId, state } = ctx.query.code;
  8. const { appid } = this.app.config.wxconfig;
  9. const uuid = uuidv4();
  10. if (redirect_uri) {
  11. // redirect_uri = 获取openid后跳转地址(可以是用户正常回调地址,也可以是绑定页面地址)
  12. await this.app.redis.set(`redirect_uri${uuid}`, redirect_uri, 'EX', 600);
  13. }
  14. if (userId) {
  15. await this.app.redis.set(`userId${uuid}`, userId, 'EX', 600);
  16. }
  17. if (code) {
  18. return await this.get_openid_back({ code, state });
  19. }
  20. // const host = this.ctx.header.referer.split('/')[2];
  21. const host = this.ctx.header.host;
  22. const backUrl = `${this.ctx.protocol}://${host}${ctx.path}`;
  23. const to_url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${backUrl}&response_type=code&state=${uuid}&scope=snsapi_base#wechat_redirect`;
  24. ctx.redirect(to_url);
  25. }
  26. // GET 获取openid认证回调
  27. async get_openid_back({ code, state }) {
  28. const redirect_uri = await this.app.redis.get(`redirect_uri${state}`);
  29. const userId = await this.app.redis.get(`userId${state}`);
  30. if (!redirect_uri) return { errcode: -1001, errmsg: '回调地址不存在', data: '' };
  31. const { appid, appsecret } = this.app.config.wxconfig;
  32. const url = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${appsecret}&code=${code}&grant_type=authorization_code`;
  33. const { openid } = await this.ctx.curl(url);
  34. // TODO: 重定性到跳转页面
  35. await this.ctx.render('redirect.njk', { openid, redirect_uri, userId });
  36. }
  37. async get_userInfo() {
  38. const { ctx } = this;
  39. const { code, redirect_uri, state, userId } = ctx.query;
  40. const { appid } = this.app.config.wxconfig;
  41. const uuid = uuidv4();
  42. if (redirect_uri) {
  43. await this.app.redis.set(`redirect_uri${uuid}`, redirect_uri, 'EX', 600);
  44. }
  45. if (userId) {
  46. await this.app.redis.set(`userId${uuid}`, userId, 'EX', 600);
  47. }
  48. if (code) {
  49. return await this.get_userInfo_back({ code, state });
  50. }
  51. // const host = this.ctx.header.referer.split('/')[2];
  52. const host = this.ctx.header.host;
  53. const backUrl = `${this.ctx.protocol}://${host}${ctx.path}`;
  54. const to_url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${backUrl}&response_type=code&state=${uuid}&scope=snsapi_userinfo#wechat_redirect`;
  55. ctx.redirect(to_url);
  56. }
  57. // GET 获取用户信息认证回调
  58. async get_userInfo_back({ code, state }) {
  59. const redirect_uri = await this.app.redis.get(`redirect_uri${state}`);
  60. if (!redirect_uri) return { errcode: -1001, errmsg: '回调地址不存在', data: '' };
  61. const userId = await this.app.redis.get(`userId${state}`);
  62. const { appid, appsecret } = this.app.config.wxconfig;
  63. const url = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${appsecret}&code=${code}&grant_type=authorization_code`;
  64. const { openid, access_token } = await this.ctx.curl(url);
  65. const wxuserurl = `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}&lang=zh_CN`;
  66. const wxuserinfo = await this.ctx.curl(wxuserurl);
  67. // TODO: 重定性到跳转页面
  68. await this.ctx.render('redirect.njk', { openid, redirect_uri, wxuserinfo, userId });
  69. }
  70. // 获取access_token
  71. async get_accessToken() {
  72. const { appid, appsecret } = this.app.config.wxconfig;
  73. const to_uri = `https://api.weixin.qq.com/cgi-bin/token?appid=${appid}&secret=${appsecret}&grant_type=client_credential`;
  74. const { access_token } = await this.ctx.curl(to_uri);
  75. return access_token;
  76. }
  77. // 下发模板消息
  78. async pushMould() {
  79. const { adminTemplateId } = this.app.config.wxconfig;
  80. const access_token = await this.gettoken();
  81. const to_uri = `ttps://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${access_token}`;
  82. const { openid, data } = this.ctx.request.body;
  83. await this.ctx.curl(to_uri, {
  84. method: 'POST',
  85. dataType: 'json',
  86. data: JSON.stringify({
  87. touser: openid,
  88. template_id: adminTemplateId,
  89. data,
  90. }),
  91. });
  92. }
  93. }
  94. module.exports = WxapiController;