润华项目企业协作即时通讯设计:
根据业务即当前人员在需要进行流程审核时调用远程api达到即时通讯发信息的效果。
调用示例:
1 2 3 4 5 6
| SendClientMessageForm messageForm = SendClientMessageForm.builder() .userId(transferOrderUser) .url(PathConstants.TRANSFER_NOTICE) .content(String.format("%s给您发送了一条产品调拨申请,请及时审批!", user.getNickname())) .build(); instantMessageUtil.sendMessage(messageForm);
|
接口调用demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| @Slf4j @Component public class InstantMessageUtil { @Value("${instant.apiUrl}") private String apiUrl; @Value("${copper.url}") public String copperUrl; @Value("${spring.application.name}") private String clientId;
public boolean sendMessage(SendClientMessageForm messageForm) { try { messageForm.setClientId(clientId); messageForm.setUrl(copperUrl + messageForm.getUrl()); HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<List<SendClientMessageForm>> formEntity = new HttpEntity<>(Collections.singletonList(messageForm), headers); RestTemplate restTemplate = new RestTemplate(); MapMessage mapMessage = restTemplate.postForObject(apiUrl, formEntity, MapMessage.class); return (boolean) mapMessage.get("success"); return true; } catch (Exception e) { log.error("推送消息失败! message:{}", e.getMessage()); return false; }
}
public boolean sendMessageBatch(List<SendClientMessageForm> messageForms) { try { messageForms.forEach(messageForm -> { messageForm.setClientId(clientId); messageForm.setUrl(copperUrl + messageForm.getUrl()); }); HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<List<SendClientMessageForm>> formEntity = new HttpEntity<>(messageForms, headers); RestTemplate restTemplate = new RestTemplate(); MapMessage mapMessage = restTemplate.postForObject(apiUrl, formEntity, MapMessage.class); return (boolean) mapMessage.get("success"); return true; } catch (Exception e) { log.error("推送消息失败! message:{}", e.getMessage()); return false; } } }
|