package com.highdatas.mdm.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.highdatas.mdm.entity.SysMenu; import com.highdatas.mdm.pojo.Operate; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.http.*; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; import java.text.MessageFormat; import java.util.HashMap; import java.util.LinkedHashMap; @ConfigurationProperties(prefix = "notice") @Component public class NoticeClient { @Value("${notice.url}") String url; String prefix ; private NoticeClient() { this.prefix = "/api/datacvg/msg/"; } public boolean EditMenuMapping(SysMenu menuByTableName, SysMenu menu, String userId) { if (menu == null) { String content = MessageFormat.format("编辑{0}主数据信息", menuByTableName.getName()); String theme = MessageFormat.format("{0}主数据", menuByTableName.getName()); return open(content, theme, userId, "MAIN_001_001_007"); } String content = MessageFormat.format("编辑{0}主题下的{1}主数据信息", menu.getName(), menuByTableName.getName()); String theme = MessageFormat.format("{0}主数据", menuByTableName.getName()); return open(content, theme, userId,"MAIN_001_001_007"); } public boolean EditMasterData(SysMenu menuByTableName, SysMenu menu, String userId, Operate operate) { if (menu == null) { String content = MessageFormat.format("{1}主数据" + operate.getLogMessage(),1, menuByTableName.getName()); String theme = MessageFormat.format("{0}主数据", menuByTableName.getName()); return open(content, theme, userId, "MAIN_001_001_002_002"); } String content = MessageFormat.format("{1}主题下的{2}主数据" + operate.getLogMessage(),1, menu.getName(), menuByTableName.getName()); String theme = MessageFormat.format("{0}主数据", menuByTableName.getName()); return open(content, theme, userId,"MAIN_001_001_002_002"); } public boolean openByAddMenu(SysMenu menuByTableName, SysMenu menu, String userId) { if (menu == null) { String content = MessageFormat.format("{0}主数据发布", menuByTableName.getName()); String theme = MessageFormat.format("{0}主数据", menuByTableName.getName()); return open(content, theme, userId, "MAIN_001_001_001"); } String content = MessageFormat.format("{0}主题下的{1}主数据发布", menu.getName(), menuByTableName.getName()); String theme = MessageFormat.format("{0}主数据", menuByTableName.getName()); return open(content, theme, userId, "MAIN_001_001_001"); } public boolean open(String msg, String theme, String userId, String code) { String url = this.url + prefix + "sendMsg"; HashMap params = new LinkedHashMap<>(); params.put("module","主数据管理"); params.put("userId",userId); params.put("msgContent", msg); params.put("msgTheme", theme); params.put("detailCode", code); String s = HttpRestClient(url, HttpMethod.POST, params, null, MediaType.APPLICATION_JSON); JSONObject result = (JSONObject) JSON.parse(s); String sucess = result.getString(Constant.Success); if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) { return false; }else { return true; } } public String HttpRestClient(String url, HttpMethod method, HashMap formParams, String getParams, MediaType mediaType) { if (!StringUtils.isEmpty(getParams)) { url = url + Constant.QUESTION + getParams; } SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(10*1000); requestFactory.setReadTimeout(10*1000); RestTemplate client = new RestTemplate(requestFactory); client.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8)); HttpHeaders headers = new HttpHeaders(); if (mediaType == null){ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); }else { headers.setContentType(mediaType); } HttpEntity> requestEntity = new HttpEntity>(formParams, headers); // 执行HTTP请求 ResponseEntity response = client.exchange(url, method, requestEntity, String.class); return response.getBody(); } }