| @@ -0,0 +1,117 @@ | |||||
| package com.simple.schedule; | |||||
| import com.alibaba.fastjson.JSONObject; | |||||
| import com.simple.common.IdWorker; | |||||
| import com.simple.common.Result; | |||||
| import com.simple.common.ResultData; | |||||
| import com.simple.domain.po.WxMsg; | |||||
| import com.simple.domain.po.WxMsgConfig; | |||||
| import com.simple.mapper.WxMsgConfigMapper; | |||||
| import com.simple.mapper.WxMsgMapper; | |||||
| import com.simple.utils.*; | |||||
| import org.apache.log4j.Logger; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.context.annotation.Configuration; | |||||
| import org.springframework.scheduling.annotation.EnableScheduling; | |||||
| import org.springframework.scheduling.annotation.Scheduled; | |||||
| import java.util.*; | |||||
| @Configuration | |||||
| @EnableScheduling | |||||
| public class SchedulingConfig { | |||||
| private final Logger logger = Logger.getLogger(SchedulingConfig.class); | |||||
| @Autowired | |||||
| private WxMsgMapper wxMsgMapper; | |||||
| @Autowired | |||||
| private WxMsgConfigMapper wxMsgConfigMapper; | |||||
| @Scheduled(cron = "0 1 * * * ?") // 每小时第一分钟执行 | |||||
| public void sendmsgschedule() { | |||||
| logger.info("sendmsg定时任务启动"); | |||||
| String systemTime = DateUtils.getSystemTime("yyyy-MM-dd HH:00:00"); | |||||
| WxMsg wxMsg = new WxMsg(); | |||||
| wxMsg.setIsright(0); | |||||
| wxMsg.setSendtime(systemTime); | |||||
| List<WxMsg> list = wxMsgMapper.findList(wxMsg); | |||||
| for(WxMsg msg:list){ | |||||
| sendmsg(msg); | |||||
| } | |||||
| logger.info("sendmsg定时任务结束"); | |||||
| } | |||||
| public void sendmsg(WxMsg wxMsg){ | |||||
| //从短信配置中查询密钥 bid 等信息 | |||||
| WxMsgConfig wxMsgConfig = new WxMsgConfig(); | |||||
| wxMsgConfig.setTenantId("1"); | |||||
| List<WxMsgConfig> wxMsgConfigs = wxMsgConfigMapper.findList(wxMsgConfig); | |||||
| if (wxMsgConfigs.size() == 0) return; | |||||
| wxMsgConfig = wxMsgConfigs.get(0); | |||||
| String secret = wxMsgConfig.getSecret(); | |||||
| String bid = wxMsgConfig.getBid(); | |||||
| String publickey = wxMsgConfig.getPublickey(); | |||||
| String phone = wxMsg.getPhones(); | |||||
| String signature = wxMsg.getSignature(); | |||||
| String msg = wxMsg.getMsg(); | |||||
| String notifyUrl = wxMsgConfig.getNotifyurl(); | |||||
| TreeMap<String, String> message = new TreeMap<>(); | |||||
| message.put("bid", bid); | |||||
| message.put("phone", phone); | |||||
| message.put("signature", signature); | |||||
| message.put("msg", msg); | |||||
| message.put("notify_url", notifyUrl); | |||||
| StringBuilder sb = new StringBuilder(); | |||||
| Set<Map.Entry<String, String>> entries = message.entrySet(); | |||||
| for (Map.Entry<String, String> entry : entries) { | |||||
| sb.append(entry.getKey()).append("=").append(entry.getValue()); | |||||
| } | |||||
| sb.append("&secret=").append(secret); | |||||
| String sign = HMACSHA256.sha256_HMAC(sb.toString(), secret); | |||||
| message.put("sign", sign.toUpperCase()); | |||||
| String str32 = "198b02e8fd704e96198b02e8fd704e96"; | |||||
| String iv = "198b02e8fd704e96"; | |||||
| Map<String, String> params = new HashMap<>(); | |||||
| params.put("iv", iv); | |||||
| params.put("bid", bid); | |||||
| try { | |||||
| String data = AesUtil.AESEncode(str32, JSONObject.toJSONString(message), iv); | |||||
| String sc = RsaUtil.RSAEncode(str32.getBytes(), publickey); | |||||
| params.put("data", data); | |||||
| params.put("sc", sc); | |||||
| } catch (Exception e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| String requestUrl = "https://webapp.wiwide.com/apisms/send"; | |||||
| String result = HttpUtil.doPost(requestUrl, params); | |||||
| JSONObject jsonObjectResult = JSONObject.parseObject(result); | |||||
| String ret = jsonObjectResult.get("ret").toString(); | |||||
| if (ret.equals("1")) { | |||||
| wxMsg.setSendstatus(1); | |||||
| } else { | |||||
| wxMsg.setSendstatus(0); | |||||
| } | |||||
| wxMsg.setStatus(1); | |||||
| wxMsgMapper.updateByPrimaryKeySelective(wxMsg); | |||||
| } | |||||
| } | |||||