wechat_controller.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/7/2
  7. * @version
  8. */
  9. const moment = require('moment');
  10. // const Controller = require('egg').Controller;
  11. const crypto = require('crypto');
  12. const maintainConst = require('../const/maintain');
  13. module.exports = app => {
  14. class WechatController extends app.BaseController {
  15. /**
  16. * 接入微信
  17. *
  18. * @param {Object} ctx - egg全局页面
  19. * @return {void}
  20. */
  21. async index(ctx) {
  22. try {
  23. const signature = ctx.query.signature;
  24. const timestamp = ctx.query.timestamp;
  25. const nonce = ctx.query.nonce;
  26. const echostr = ctx.query.echostr;
  27. const array = [ctx.app.config.wechatAll.token, timestamp, nonce];
  28. array.sort();
  29. const tempStr = array.join('');
  30. const hashCode = crypto.createHash('sha1');
  31. const resultCode = hashCode.update(tempStr, 'utf8').digest('hex');
  32. if (resultCode === signature) {
  33. ctx.body = echostr;
  34. // res.send(echostr);
  35. } else {
  36. throw '验证失败';
  37. }
  38. } catch (e) {
  39. console.log(e);
  40. }
  41. }
  42. /**
  43. * 微信登录验证
  44. *
  45. * @param {Object} ctx - egg全局页面
  46. * @return {void}
  47. */
  48. async oauth(ctx) {
  49. const redirect_uri = ctx.query.redirect_uri;
  50. const url = await app.wechat.oauth.getAuthorizeURL(redirect_uri, '', 'snsapi_userinfo');
  51. ctx.redirect(url);
  52. }
  53. /**
  54. * 绑定页面
  55. *
  56. * @param {Object} ctx - egg全局页面
  57. * @return {void}
  58. */
  59. async bind(ctx) {
  60. try {
  61. const user = await app.wechat.oauth.getUser(ctx.session.wechatToken.openid);
  62. const errorMessage = ctx.session.loginError;
  63. // 显示完删除
  64. ctx.session.loginError = null;
  65. // 获取系统维护信息
  66. const maintainData = await ctx.service.maintain.getDataById(1);
  67. const renderData = {
  68. maintainData,
  69. maintainConst,
  70. errorMessage,
  71. user,
  72. };
  73. // ctx.body = renderData;
  74. await ctx.render('wechat/bind.ejs', renderData);
  75. } catch (e) {
  76. console.log(e);
  77. ctx.body = e;
  78. }
  79. }
  80. async bindwx(ctx) {
  81. try {
  82. const result = await ctx.service.projectAccount.accountCheck(ctx.request.body);
  83. if (!result) {
  84. throw '用户名或密码错误';
  85. }
  86. if (result === 2) {
  87. throw '该账号已被停用,请联系销售人员';
  88. }
  89. const accountData = result;
  90. if (accountData.wx_openid || ctx.session.wechatToken.openid === accountData.wx_openid) {
  91. throw '该账号已经绑定过微信';
  92. }
  93. const wxAccountData = await ctx.service.projectAccount.getDataByCondition({ wx_openid: ctx.session.wechatToken.openid });
  94. if (wxAccountData) {
  95. throw '该微信号已绑定过本项目账号';
  96. }
  97. const user = await app.wechat.oauth.getUser(ctx.session.wechatToken.openid);
  98. const result2 = await ctx.service.projectAccount.bindWx(accountData.id, ctx.session.wechatToken.openid, user.nickname);
  99. if (!result2) {
  100. throw '绑定失败';
  101. }
  102. const projectData = await ctx.service.project.getDataById(accountData.project_id);
  103. // 绑定成功通知
  104. const templateId = 'ElT988uf7EV8ROPKSAX7z7tN9ZxZCDMaXK5ouc9N49E';
  105. const url = '';
  106. const msgData = {
  107. first: {
  108. value: '微信绑定成功',
  109. },
  110. keyword1: {
  111. value: accountData.account,
  112. },
  113. keyword2: {
  114. value: '项目编号' + projectData.code + ' 账号' + accountData.account + ' 绑定成功',
  115. },
  116. remark: {
  117. value: '欢迎使用纵横云计量,我们竭诚为您服务。',
  118. },
  119. };
  120. await app.wechat.api.sendTemplate(ctx.session.wechatToken.openid, templateId, url, '', msgData);
  121. ctx.body = '绑定成功';
  122. } catch (error) {
  123. this.log(error);
  124. ctx.session.loginError = error;
  125. ctx.redirect('/wx/bind');
  126. }
  127. }
  128. async oauthTxt(ctx) {
  129. ctx.body = 't3MkWAMqplVxPjmr';
  130. }
  131. async testwx(ctx) {
  132. try {
  133. // 绑定成功通知
  134. const templateId = 'ElT988uf7EV8ROPKSAX7z7tN9ZxZCDMaXK5ouc9N49E';
  135. const url = '';
  136. const topColor = '#FFFFFF';
  137. const msgData = {
  138. first: {
  139. value: '微信绑定成功',
  140. },
  141. keyword1: {
  142. value: 'hello world',
  143. },
  144. keyword2: {
  145. value: 'hello world 绑定成功',
  146. },
  147. remark: {
  148. value: '欢迎使用纵横云计量,我们竭诚为您服务。',
  149. },
  150. };
  151. await app.wechat.api.sendTemplate(ctx.session.wechatToken.openid, templateId, url, topColor, msgData);
  152. ctx.body = 'success';
  153. } catch (error) {
  154. console.log(error);
  155. ctx.body = error;
  156. }
  157. }
  158. }
  159. return WechatController;
  160. };