pipIrr-platform/pipIrr-global/pom.xml
@@ -140,6 +140,19 @@ <artifactId>okhttp</artifactId> <version>4.9.2</version> </dependency> <!--钉钉消息推送--> <dependency> <groupId>com.aliyun</groupId> <artifactId>alibaba-dingtalk-service-sdk</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.15</version> </dependency> </dependencies> <build> pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/config/DingTalk.java
New file @@ -0,0 +1,86 @@ package com.dy.pipIrrGlobal.config; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiRobotSendRequest; import com.dingtalk.api.response.OapiRobotSendResponse; import com.taobao.api.ApiException; import org.apache.commons.codec.binary.Base64; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; 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.Collections; /** * @author ZhuBaoMin * @date 2024-07-31 9:20 * @LastEditTime 2024-07-31 9:20 * @Description 钉钉客户端工具类 */ @Component public class DingTalk { @Value("${dingtalk.robot.url}") private String URL; @Value("${dingtalk.robot.access-token}") private String CUSTOM_ROBOT_TOKEN; @Value("${dingtalk.robot.secret}") private String SECRET; @Value("${dingtalk.at-all}") private Boolean AT_ALL; @Value("${dingtalk.mobile}") private String MOBILE; public void sendMessage(String message) { try { Long timestamp = System.currentTimeMillis(); String secret = SECRET; 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"); //sign字段和timestamp字段必须拼接到请求URL上,否则会出现 310000 的错误信息 DingTalkClient client = new DefaultDingTalkClient( URL + "?sign=" + sign + "×tamp=" + timestamp); OapiRobotSendRequest req = new OapiRobotSendRequest(); /** * 发送文本消息 */ //定义文本内容 OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text(); text.setContent(message); //定义 @对象 OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); if(AT_ALL) { at.setIsAtAll(true); }else { at.setAtMobiles(Collections.singletonList(MOBILE)); } //设置消息类型 req.setMsgtype("text"); req.setText(text); req.setAt(at); OapiRobotSendResponse rsp = client.execute(req, CUSTOM_ROBOT_TOKEN); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } } } pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
@@ -167,3 +167,12 @@ auto-statistics: startHour: 17 #开始小时 0 startMinute: 19 #开始分钟 5 #钉钉消息推送 dingtalk: robot: url: https://oapi.dingtalk.com/robot/send access-token: fecef8e7725998f8912af05419580861aafc73413c4920036c07c050fa33055f secret: SEC6042bc964d08899a5853eb321eb5a4d842a395982777f815bd07451c879228b7 at-all: true mobile: 18602657034 pipIrr-platform/pipIrr-web/pipIrr-web-project/src/main/java/com/dy/pipIrrProject/intake/IntakeCtrl.java
@@ -13,6 +13,7 @@ import com.dy.pipIrrGlobal.voSe.VoActiveCard; import com.dy.pipIrrProject.intake.qo.OnLineIntakesQO; import com.dy.pipIrrProject.result.ProjectResultCode; import com.taobao.api.ApiException; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Content; @@ -51,6 +52,11 @@ @RequiredArgsConstructor public class IntakeCtrl { private final IntakeSv intakeSv; @GetMapping("/test") public String sendMessage(String message) throws ApiException { return intakeSv.sendMessage(message); } /** * 客户端请求得到所有取水口名字 @@ -142,7 +148,7 @@ @PostMapping(path = "add", consumes = MediaType.APPLICATION_JSON_VALUE) @Transactional(rollbackFor = Exception.class) @SsoAop() public BaseResponse<Boolean> add(@RequestBody @Valid PrIntake po, @Parameter(hidden = true) BindingResult bindingResult) { public BaseResponse<Boolean> add(@RequestBody @Valid PrIntake po, @Parameter(hidden = true) BindingResult bindingResult) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); if (bindingResult != null && bindingResult.hasErrors()) { return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); @@ -152,7 +158,7 @@ return BaseResponseUtils.buildFail(ProjectResultCode.INTAKE_NAME_EXIST.getMessage()); } po.setOperateDt(new Date()); po.setDeleted((byte)0); po.setDeleted((byte) 0); Integer rec = Optional.ofNullable(intakeSv.addIntake(po)).orElse(0); if (rec == 0) { return BaseResponseUtils.buildFail(ProjectResultCode.ADD_INTAKE_FAIL.getMessage()); @@ -182,11 +188,11 @@ return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); } List<Long> ids = intakeSv.getIntakeIdByNameExcludeId(po.getId(), po.getName()); if (ids.size() > 0){ if (ids.size() > 0) { return BaseResponseUtils.buildFail(ProjectResultCode.INTAKE_NAME_EXIST.getMessage()); } int count; po.setOperateDt( new Date()); po.setOperateDt(new Date()); try { count = this.intakeSv.update(po); } catch (Exception e) { @@ -286,6 +292,7 @@ /** * 获取取水口列表(在线和不在线) * * @param qo * @return */ @@ -303,6 +310,7 @@ /** * 根据操作员获取常用取水口(在线和不在线) * * @param operator * @return */ pipIrr-platform/pipIrr-web/pipIrr-web-project/src/main/java/com/dy/pipIrrProject/intake/IntakeSv.java
@@ -3,9 +3,11 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.dy.common.multiDataSource.DataSourceContext; import com.dy.common.mw.protocol.Command; import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.QueryResultVo; import com.dy.pipIrrGlobal.config.DingTalk; import com.dy.pipIrrGlobal.daoPr.PrDivideMapper; import com.dy.pipIrrGlobal.daoPr.PrIntakeMapper; import com.dy.pipIrrGlobal.pojoPr.PrIntake; @@ -15,6 +17,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.common.utils.PojoUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -45,8 +48,32 @@ @Autowired private RestTemplate restTemplate; protected static String mwUrlSendCom = "http://127.0.0.1:8070/rtuMw/com/send" ; protected String comSendUrl; /** * pro_mw:属性 * tag从控制器中获取 * key_mw:url的key */ private Environment env = null; private String pro_mw = "mw"; private String key_mw = "comSendUrl"; @Autowired public IntakeSv(Environment env) { this.env = env; } @Autowired private DingTalk dingTalk; public String sendMessage(String message) { try { dingTalk.sendMessage(message); } catch (Exception e) { return "发送失败"; } return "发送成功"; } /** * 添加取水口 @@ -78,7 +105,6 @@ public Long getSupperByVillageId(long vaId) { return prIntakeMapper.getSupperByVillageId(vaId); } /** * 保存修改实体 @@ -207,9 +233,9 @@ com.id = Command.defaultId; com.code = "LCD0001"; com.type = "innerCommand"; comSendUrl = env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + key_mw); JSONObject response = (JSONObject) JSON.toJSON(sendCom2Mw(com)); if(response != null && response.getString("code").equals("0001")) { JSONObject attachment = response.getJSONObject("content").getJSONObject("attachment").getJSONObject("onLineMap"); HashMap<String, Boolean> onLineMap = JSON.parseObject(attachment.toJSONString(), HashMap.class); @@ -272,7 +298,7 @@ * @return */ protected BaseResponse sendCom2Mw(Command com){ String url = UriComponentsBuilder.fromUriString(mwUrlSendCom) String url = UriComponentsBuilder.fromUriString(comSendUrl) .build() .toUriString(); HttpHeaders headers = new HttpHeaders(); pipIrr-platform/pipIrr-web/pipIrr-web-project/src/main/resources/application.yml
@@ -15,4 +15,4 @@ #GenerateIdSetSuffixListener中应用,取值范围是0-99 idSuffix: ${pipIrr.project.idSuffix} #ConfigListener中应用 #configFileNames: config-global.xml,config-demo.xml #configFileNames: config-global.xml,config-demo.xml pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/resources/application.yml
@@ -15,4 +15,4 @@ #GenerateIdSetSuffixListener中应用,取值范围是0-99 idSuffix: ${pipIrr.remote.idSuffix} #ConfigListener中应用 #configFileNames: config-global.xml,config-demo.xml #configFileNames: config-global.xml,config-demo.xml