WechatController.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.ruoyi.web.controller.wechat;
  2. import com.ruoyi.common.annotation.RepeatSubmit;
  3. import com.ruoyi.common.core.controller.BaseController;
  4. import com.ruoyi.common.core.domain.AjaxResult;
  5. import com.ruoyi.common.core.domain.model.LoginBody;
  6. import com.ruoyi.common.utils.wechat.MessageUtil;
  7. import com.ruoyi.common.utils.wechat.xml.XMLUtil;
  8. import com.ruoyi.framework.web.service.WechatService;
  9. import com.ruoyi.system.wechatUtils.UserQueue;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.web.bind.annotation.*;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import javax.servlet.http.HttpServletRequest;
  14. import java.io.IOException;
  15. import java.util.Map;
  16. /**
  17. * <p>
  18. * 与微信产生交换的控制器
  19. * </p>
  20. *
  21. * @author ruoyi
  22. * @date 2020-12-11
  23. */
  24. @RestController
  25. @RequestMapping("/wechat")
  26. public class WechatController extends BaseController {
  27. @Autowired
  28. private WechatService wechatService;
  29. /**
  30. * 公众号与微信服务器进行认证的API接口
  31. *
  32. * @param signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
  33. * @param timestamp 时间戳
  34. * @param nonce 随机数
  35. * @param echostr 随机字符串
  36. * @return 结果
  37. */
  38. @GetMapping(value = "/wechat")
  39. public Object check(String signature,
  40. String timestamp,
  41. String nonce,
  42. String echostr) {
  43. return echostr;
  44. }
  45. /**
  46. * 微信授权信息时返回token
  47. *
  48. * @param loginBody code
  49. * @return 结果
  50. */
  51. @RepeatSubmit
  52. @PostMapping(value = "/programLogin")
  53. public AjaxResult weChatProgramLogin(@RequestBody LoginBody loginBody) {
  54. // 生成令牌
  55. AjaxResult result = wechatService.login(loginBody);
  56. System.out.println("生成的令牌:" + result.get("data"));
  57. return result;
  58. }
  59. /**
  60. * 公众号接收请求
  61. *
  62. * @param request 请求
  63. * @return 接口
  64. */
  65. @PostMapping(value = "/wechat", produces = {"application/xml;charset=utf-8"})
  66. public Object toRequest(HttpServletRequest request) throws IOException {
  67. Map<String, String> map = XMLUtil.getMap(request.getInputStream());
  68. String msgType = map.get("MsgType");
  69. String toUser = map.get("ToUserName");
  70. String fromUser = map.get("FromUserName");
  71. String createTime = map.get("CreateTime");
  72. String reply = "";
  73. if ("event".equals(msgType)) {
  74. String event = map.get("Event");
  75. if ("subscribe".equals(event)) {
  76. reply = "欢迎关注";
  77. UserQueue.ATTENTIONQUEUE.push(fromUser);
  78. } else if ("unsubscribe".equals(event)) {
  79. UserQueue.CHECKOFFQUEUE.push(fromUser);
  80. }
  81. }
  82. return MessageUtil.setMessage(fromUser, toUser, 45860, "text", reply);
  83. }
  84. }