You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

117 lines
3.7 KiB

  1. package com.simple.schedule;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.simple.common.IdWorker;
  4. import com.simple.common.Result;
  5. import com.simple.common.ResultData;
  6. import com.simple.domain.po.WxMsg;
  7. import com.simple.domain.po.WxMsgConfig;
  8. import com.simple.mapper.WxMsgConfigMapper;
  9. import com.simple.mapper.WxMsgMapper;
  10. import com.simple.utils.*;
  11. import org.apache.log4j.Logger;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.context.annotation.Configuration;
  14. import org.springframework.scheduling.annotation.EnableScheduling;
  15. import org.springframework.scheduling.annotation.Scheduled;
  16. import java.util.*;
  17. @Configuration
  18. @EnableScheduling
  19. public class SchedulingConfig {
  20. private final Logger logger = Logger.getLogger(SchedulingConfig.class);
  21. @Autowired
  22. private WxMsgMapper wxMsgMapper;
  23. @Autowired
  24. private WxMsgConfigMapper wxMsgConfigMapper;
  25. @Scheduled(cron = "0 1 * * * ?") // 每小时第一分钟执行
  26. public void sendmsgschedule() {
  27. logger.info("sendmsg定时任务启动");
  28. String systemTime = DateUtils.getSystemTime("yyyy-MM-dd HH:00:00");
  29. WxMsg wxMsg = new WxMsg();
  30. wxMsg.setIsright(0);
  31. wxMsg.setSendtime(systemTime);
  32. List<WxMsg> list = wxMsgMapper.findList(wxMsg);
  33. for(WxMsg msg:list){
  34. sendmsg(msg);
  35. }
  36. logger.info("sendmsg定时任务结束");
  37. }
  38. public void sendmsg(WxMsg wxMsg){
  39. //从短信配置中查询密钥 bid 等信息
  40. WxMsgConfig wxMsgConfig = new WxMsgConfig();
  41. wxMsgConfig.setTenantId("1");
  42. List<WxMsgConfig> wxMsgConfigs = wxMsgConfigMapper.findList(wxMsgConfig);
  43. if (wxMsgConfigs.size() == 0) return;
  44. wxMsgConfig = wxMsgConfigs.get(0);
  45. String secret = wxMsgConfig.getSecret();
  46. String bid = wxMsgConfig.getBid();
  47. String publickey = wxMsgConfig.getPublickey();
  48. String phone = wxMsg.getPhones();
  49. String signature = wxMsg.getSignature();
  50. String msg = wxMsg.getMsg();
  51. String notifyUrl = wxMsgConfig.getNotifyurl();
  52. TreeMap<String, String> message = new TreeMap<>();
  53. message.put("bid", bid);
  54. message.put("phone", phone);
  55. message.put("signature", signature);
  56. message.put("msg", msg);
  57. message.put("notify_url", notifyUrl);
  58. StringBuilder sb = new StringBuilder();
  59. Set<Map.Entry<String, String>> entries = message.entrySet();
  60. for (Map.Entry<String, String> entry : entries) {
  61. sb.append(entry.getKey()).append("=").append(entry.getValue());
  62. }
  63. sb.append("&secret=").append(secret);
  64. String sign = HMACSHA256.sha256_HMAC(sb.toString(), secret);
  65. message.put("sign", sign.toUpperCase());
  66. String str32 = "198b02e8fd704e96198b02e8fd704e96";
  67. String iv = "198b02e8fd704e96";
  68. Map<String, String> params = new HashMap<>();
  69. params.put("iv", iv);
  70. params.put("bid", bid);
  71. try {
  72. String data = AesUtil.AESEncode(str32, JSONObject.toJSONString(message), iv);
  73. String sc = RsaUtil.RSAEncode(str32.getBytes(), publickey);
  74. params.put("data", data);
  75. params.put("sc", sc);
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. String requestUrl = "https://webapp.wiwide.com/apisms/send";
  80. String result = HttpUtil.doPost(requestUrl, params);
  81. JSONObject jsonObjectResult = JSONObject.parseObject(result);
  82. String ret = jsonObjectResult.get("ret").toString();
  83. if (ret.equals("1")) {
  84. wxMsg.setSendstatus(1);
  85. } else {
  86. wxMsg.setSendstatus(0);
  87. }
  88. wxMsg.setStatus(1);
  89. wxMsgMapper.updateByPrimaryKeySelective(wxMsg);
  90. }
  91. }