wxController.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. class WXController extends Controller {
  4. async getAppidByCid() {
  5. const { ctx, service } = this;
  6. const query = ctx.query;
  7. const { cid } = query;
  8. const result = await service.wxConfigService.list({ cid });
  9. if (result.length === 1) {
  10. ctx.success(result[0]);
  11. } else {
  12. ctx.error('appid(' + cid + ') is error ');
  13. }
  14. }
  15. async oauth() {
  16. const { ctx, service } = this;
  17. const query = ctx.query;
  18. this.ctx.body = await service.wxService.oauth(query);
  19. }
  20. async getAppletOpenId() {
  21. const { ctx, service } = this;
  22. const query = ctx.query;
  23. this.ctx.body = await service.wxService.getAppletOpenId(query);
  24. }
  25. async toWechat() {
  26. const message = this.ctx.req.body;
  27. if (message) {
  28. const MsgType = message.MsgType;
  29. let reply;
  30. if (MsgType === 'event') {
  31. reply = await this.service.wxService.handleEvent(message);
  32. } else {
  33. reply = await this.service.wxService.handleMsg(message);
  34. }
  35. if (reply) {
  36. const result = await this.service.wxService.replyMsg(message, reply);
  37. this.ctx.body = result;
  38. return true;
  39. }
  40. }
  41. this.ctx.body = 'success';
  42. }
  43. async createMenu() {
  44. const config = this.ctx.app.config.wechat_config;
  45. this.ctx.body = await this.service.wxService.createMenu(config.appId);
  46. }
  47. async getUserInfo() {
  48. const { ctx, service } = this;
  49. const query = ctx.query;
  50. const { openid } = query;
  51. this.ctx.body = await service.wxService.getUserInfo(openid);
  52. }
  53. async getSign() {
  54. const { ctx, service } = this;
  55. const query = ctx.query;
  56. this.ctx.body = await service.wxService.getSign(query);
  57. }
  58. }
  59. module.exports = WXController;