HangxinUtil.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package org.springblade.common.utils.hangxin;
  2. import cn.hutool.json.JSONUtil;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springblade.common.utils.hangxin.bean.HangxinResultBean;
  5. import org.springblade.common.utils.hangxin.conf.HangxinConfig;
  6. import org.springblade.common.utils.hangxin.contants.HangxinConstants;
  7. import org.springblade.core.tool.utils.StringUtil;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.core.ParameterizedTypeReference;
  10. import org.springframework.http.HttpEntity;
  11. import org.springframework.http.HttpHeaders;
  12. import org.springframework.http.HttpMethod;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.web.client.RestTemplate;
  16. import java.net.URI;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. @Slf4j
  21. @Component
  22. public class HangxinUtil {
  23. @Autowired
  24. private HangxinConfig hangxinConfig;
  25. public HangxinResultBean sendMsg (String userEhrs, String title, String content){
  26. RestTemplate restTemplate = new RestTemplate();
  27. String url = hangxinConfig.getUrl() + HangxinConstants.SEND_MSG;
  28. HttpHeaders headers = createHttpHeaders();
  29. Map<String, Object> params = new HashMap<>();
  30. params.put("touser", userEhrs);
  31. params.put("sysid", hangxinConfig.getSysid() + "|text");
  32. params.put("title", title);
  33. params.put("content", content);
  34. HangxinResultBean body = null;
  35. try{
  36. HttpEntity<String> requestEntity = new HttpEntity<>(JSONUtil.toJsonStr(params), headers);
  37. URI uri = URI.create(url);
  38. ParameterizedTypeReference<HangxinResultBean> reference = new ParameterizedTypeReference<HangxinResultBean>() {};
  39. ResponseEntity<HangxinResultBean> response = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
  40. body = response.getBody();
  41. }catch (Exception e){
  42. e.printStackTrace();
  43. }
  44. return body;
  45. }
  46. private HttpHeaders createHttpHeaders(){
  47. HttpHeaders httpHeaders = new HttpHeaders();
  48. httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json");
  49. // httpHeaders.add("appKey", elevatorConfig.getAppKey());
  50. // httpHeaders.add("appSecret", elevatorConfig.getAppSecret());
  51. return httpHeaders;
  52. }
  53. }