RJKController.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.malk.rjk.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.malk.rjk.dto.CallbackBody;
  5. import com.malk.rjk.server.RjkServer;
  6. import com.malk.rjk.util.CryptUtil;
  7. import com.malk.rjk.util.SHA1;
  8. import com.malk.server.common.McR;
  9. import lombok.SneakyThrows;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.*;
  13. import java.util.Map;
  14. import java.util.concurrent.CompletableFuture;
  15. @Slf4j
  16. @RestController
  17. @RequestMapping("/weiling")
  18. public class RJKController {
  19. private final static String TOKEN = "Re7szzfwkolU2TPZs59nkGEcGqehjF0F";
  20. private final static String ENCODING_AES_KEY ="Re7szzfwkolU2TPZs59nkGEcGqehjF0FuSCvxldv6dr";
  21. @Autowired
  22. private RjkServer rjkServer;
  23. /*如果该项目存在子项目继续查*/
  24. @SneakyThrows
  25. @PostMapping("/CustomerProfileSubmission")
  26. McR CustomerProfileSubmission(@RequestBody Map<String,String> data){
  27. // 先返回成功响应
  28. CompletableFuture.runAsync(() -> {
  29. try {
  30. Thread.sleep(5000);
  31. log.info("响应客户资料等待完成");
  32. rjkServer.H3UserToweiling(data);
  33. } catch (Exception e) {
  34. log.error("客户资料同步失败", e);
  35. }
  36. });
  37. return McR.success();
  38. }
  39. @SneakyThrows
  40. @PostMapping("/StoreOrderSubmission")
  41. McR StoreOrderSubmission(@RequestBody Map<String,String> data){
  42. // 先返回成功响应
  43. CompletableFuture.runAsync(() -> {
  44. try {
  45. Thread.sleep(5000);
  46. log.info("响应商机资料等待完成");
  47. rjkServer.H3BusinessToWeiling(data);
  48. } catch (Exception e) {
  49. log.error("商机资料同步失败", e);
  50. }
  51. });
  52. return McR.success();
  53. }
  54. /**
  55. * 回调服务 测试请求地址的合法性
  56. *
  57. * @param msgSignature 消息签名,msg_signature计算结合了企业填写的token、请求中的timestamp、nonce、echostr
  58. * @param timeStamp 时间戳
  59. * @param nonce 随机数
  60. * @param echoStr 加密字符串
  61. * @return
  62. */
  63. @GetMapping(value = "/callback")
  64. public String listen(@RequestParam("msg_signature") String msgSignature,
  65. @RequestParam("timestamp") String timeStamp,
  66. @RequestParam("nonce") String nonce,
  67. @RequestParam("echostr") String echoStr) {
  68. //通过token, timestamp. nonce, echostr四个参数生成签名
  69. String signature = SHA1.gen(TOKEN, timeStamp, nonce, echoStr);
  70. //如果签名相同,则解密echostr并返回
  71. if (signature.equals(msgSignature)) {
  72. CryptUtil util = new CryptUtil(ENCODING_AES_KEY);
  73. // System.out.println("解密成功"+util.decrypt(echoStr));
  74. return util.decrypt(echoStr);
  75. }
  76. return null;
  77. }
  78. /**
  79. * 回调服务示例 解密后包含回调信息
  80. *
  81. * @param msgSignature 消息签名,msg_signature计算结合了企业填写的token、
  82. * 请求中的timestamp、nonce、请求体中的encrypt_msg
  83. * @param timeStamp 时间戳
  84. * @param nonce 随机数
  85. * @param body 请求体(回调应用的appid、 加密信息encrypt_msg)
  86. * @return
  87. */
  88. @PostMapping(value = "/callback")
  89. public String listen(@RequestParam("msg_signature") String msgSignature,
  90. @RequestParam("timestamp") String timeStamp,
  91. @RequestParam("nonce") String nonce,
  92. @RequestBody CallbackBody body) {
  93. String encrypted = body.getEncrypt_msg();
  94. String signature = SHA1.gen(TOKEN, timeStamp, nonce, encrypted);
  95. if (signature.equals(msgSignature)) {
  96. CryptUtil util = new CryptUtil(ENCODING_AES_KEY);
  97. // msg中包含回调信息
  98. String msg = util.decrypt(encrypted);
  99. log.info("回调信息:msg={}", msg);
  100. // 解析JSON字符串为JSONObject
  101. JSONObject callbackJson = JSON.parseObject(msg);
  102. rjkServer.syncWeilingToH3yun(callbackJson);
  103. }
  104. return "success";
  105. }
  106. }