package com.jjt.biz.service; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiRobotSendRequest; import com.dingtalk.api.response.OapiRobotSendResponse; import com.jjt.biz.domain.RealTimeInfoVO; import com.jjt.biz.domain.WifiInfo; import com.jjt.biz.mapper.WifiInfoMapper; import com.jjt.common.core.redis.RedisCache; import com.jjt.framework.web.domain.server.Sys; import com.jjt.system.service.ISysConfigService; import com.taobao.api.ApiException; import org.apache.commons.codec.binary.Base64; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; /** * WIFI信息Service业务层处理 * * @author ruoyi * @date 2023-07-06 */ @Service @EnableScheduling public class DingTalkService { @Resource private ISysConfigService sysConfigService; @Resource private RedisTemplate redisTemplate; @Scheduled(cron = "0/5 * * * * ?") public void sendMsg() { List> list = new ArrayList<>(); long s = System.currentTimeMillis(); while (true) { //超过4秒,或者队列取空,则本次任务结束 if (System.currentTimeMillis() - s > 4000) { break; } Object o = redisTemplate.opsForList().rightPop("nls:need_handler"); if (o == null) { break; } else { Map map = (Map) o; list.add(map); } } try { if (list.size() == 0) { //如果队列中没有取到数据 return; } RedisCache cache = new RedisCache(); // cache.set Long timestamp = System.currentTimeMillis(); String secret = sysConfigService.selectConfigByKey("dingtalk.scret"); String webhook = sysConfigService.selectConfigByKey("dingtalk.webhook"); String accessURL = sysConfigService.selectConfigByKey("web.access.url"); String stringToSign = timestamp + "\n" + secret; Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256")); byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8"); // System.out.println(sign); String uri = webhook + "×tamp=" + timestamp + "&sign=" + sign; DingTalkClient client = new DefaultDingTalkClient(uri); OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype("markdown"); OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); markdown.setTitle("恶意动态预警"); String text = "###恶意动态预警\n"; for (Map wsMap : list) { String msgUrl = "http://api.map.baidu.com/marker?location=" + wsMap.get("lat") + "," + wsMap.get("lng") + "&title=恶意" + wsMap.get("type") + "&content=信息地址&output=html"; text += "> 发现类型为”" + wsMap.get("keyword") + "“的恶意" + wsMap.get("type") + "\t[位置](" + msgUrl + ") \n\n"; } System.err.println(text); markdown.setText(text); request.setMarkdown(markdown); OapiRobotSendResponse response = client.execute(request); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } catch (ApiException e) { throw new RuntimeException(e); } } }