PushMsgController.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.boman.wechat.controller;
  2. import com.boman.domain.dto.WxMsgDto;
  3. import com.boman.wechat.service.WxPushService;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.*;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.PrintWriter;
  11. import java.util.Objects;
  12. import static com.boman.wechat.utils.CheckAuthUtils.*;
  13. /**
  14. * 微信登录验证
  15. *
  16. * @author zhong.h
  17. */
  18. @RestController
  19. @RequestMapping("/push/msg")
  20. public class PushMsgController {
  21. private static final Logger LOGGER = LoggerFactory.getLogger(PushMsgController.class);
  22. @Autowired
  23. private WxPushService wxPushService;
  24. @GetMapping
  25. public void pushMsg(HttpServletRequest request, HttpServletResponse response) {
  26. // 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
  27. String signature = request.getParameter(SIGNATURE);
  28. // 时间戳
  29. String timestamp = request.getParameter(TIMESTAMP);
  30. // 随机数
  31. String nonce = request.getParameter(NONCE);
  32. // 随机字符串
  33. String echostr = request.getParameter(ECHOSTR);
  34. LOGGER.info("method: pushMsg, signature: {}, timestamp: {}, nonce: {}, echostr: {}", signature, timestamp, nonce, echostr);
  35. PrintWriter out = null;
  36. try {
  37. out = response.getWriter();
  38. if (checkSignature(signature, timestamp, nonce)) {
  39. out.print(echostr);
  40. out.flush();
  41. }
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. } finally {
  45. if (Objects.nonNull(out)) {
  46. out.close();
  47. }
  48. }
  49. }
  50. @PostMapping("/pushMsg")
  51. public String pushMsg(@RequestBody WxMsgDto dto) {
  52. return wxPushService.pushToUser(dto.getIds(), dto.getParams());
  53. }
  54. }