wechat_controller.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. module.exports = app => {
  13. class WechatController extends Controller {
  14. /**
  15. * 接入微信
  16. *
  17. * @param {Object} ctx - egg全局页面
  18. * @return {void}
  19. */
  20. async index(ctx) {
  21. try {
  22. const signature = ctx.query.signature;
  23. const timestamp = ctx.query.timestamp;
  24. const nonce = ctx.query.nonce;
  25. const echostr = ctx.query.echostr;
  26. const array = [ctx.app.config.wechatAll.token, timestamp, nonce];
  27. array.sort();
  28. const tempStr = array.join('');
  29. const hashCode = crypto.createHash('sha1');
  30. const resultCode = hashCode.update(tempStr, 'utf8').digest('hex');
  31. if (resultCode === signature) {
  32. ctx.body = echostr;
  33. // res.send(echostr);
  34. } else {
  35. throw '验证失败';
  36. }
  37. } catch (e) {
  38. console.log(e);
  39. }
  40. }
  41. /**
  42. * 登录验证
  43. *
  44. * @param {Object} ctx - egg全局页面
  45. * @return {void}
  46. */
  47. async oauth(ctx) {
  48. console.log('hello');
  49. const url = await app.wechat.oauth.getAuthorizeURL('http://jluat.smartcost.com.cn/wx/hello', 'hello world', 'snsapi_userinfo');
  50. console.log(url);
  51. // const token = await app.wechat.oauth.getAccessToken(ctx.query.code);
  52. // const accessToken = token.data.access_token;
  53. // const openid = token.data.openid;
  54. // console.log(token);
  55. }
  56. async hello(ctx) {
  57. console.log('hello world');
  58. }
  59. }
  60. return WechatController;
  61. };