bfd2af4f3655777ca45f677701bf8872b9142840.svn-base 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package cn.com.goldenwater.dcproj.controller.homepage;
  2. import cn.com.goldenwater.core.web.BaseController;
  3. import cn.com.goldenwater.core.web.BaseResponse;
  4. import cn.com.goldenwater.dcproj.param.BisInspMonitoringParam;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import com.github.pagehelper.PageInfo;
  7. import io.swagger.annotations.Api;
  8. import io.swagger.annotations.ApiOperation;
  9. import net.sf.json.JSONArray;
  10. import net.sf.json.JSONObject;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.apache.http.client.methods.CloseableHttpResponse;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.entity.ContentType;
  16. import org.apache.http.entity.StringEntity;
  17. import org.apache.http.impl.client.CloseableHttpClient;
  18. import org.apache.http.impl.client.HttpClients;
  19. import org.apache.http.protocol.HTTP;
  20. import org.apache.http.util.EntityUtils;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.bind.annotation.RequestMethod;
  24. import org.springframework.web.bind.annotation.RestController;
  25. import java.io.IOException;
  26. /**
  27. * Created by jinshui on 2019/7/17.
  28. */
  29. @Api(value = "", tags = "舆情接口")
  30. @RestController
  31. @RequestMapping("/dc/insp/monitror")
  32. public class BisInspMonitoringController extends BaseController {
  33. @Value("${bis-insp-monitor-ip}")
  34. public String ip;
  35. @ApiOperation(value = "获取舆情信息")
  36. @RequestMapping(value = "/getMonitoring", method = RequestMethod.GET)
  37. public BaseResponse getMonitoring(BisInspMonitoringParam param) {
  38. CloseableHttpClient client = null;
  39. CloseableHttpResponse response = null;
  40. String uri = "http://" + ip + ":8080/om/cus?id=9069e11e&key=2515505d" + "&sid=" + param.getSid() + "&pageNo=" + param.getPageNum() + "&pageSize=" + param.getPageSize();
  41. if (StringUtils.isNotBlank(param.getStartDate())) {
  42. uri += uri + "&startDate=" + param.getStartDate();
  43. }
  44. if (StringUtils.isNotBlank(param.getEndDate())) {
  45. uri += uri + "&endDate=" + param.getEndDate();
  46. }
  47. if (StringUtils.isNotBlank(param.getTimef())) {
  48. uri += uri + "&timef=" + param.getTimef();
  49. }
  50. if (StringUtils.isNotBlank(param.getOrder())) {
  51. uri += uri + "&order=" + param.getOrder();
  52. }
  53. if (StringUtils.isNotBlank(param.getTid())) {
  54. uri += uri + "&tid=" + param.getTid();
  55. }
  56. if (StringUtils.isNotBlank(param.getKw())) {
  57. uri += uri + "&kw=" + param.getKw();
  58. }
  59. if (StringUtils.isNotBlank(param.getKwt())) {
  60. uri += uri + "&kwt=" + param.getKwt();
  61. }
  62. try {
  63. HttpGet get = new HttpGet(uri);
  64. client = HttpClients.createDefault();
  65. response = client.execute(get);
  66. String result = EntityUtils.toString(response.getEntity());
  67. JSONObject json = JSONObject.fromObject(result);
  68. if (getResultState(json.getString("resultState"))) {
  69. return buildSuccessResponse(getResult(true, json));
  70. } else {
  71. return buildFailResponse(json.getInt("resultCode"), getReusltMessage(json.getString(json.getString("resultCode"))));
  72. }
  73. } catch (IOException e) {
  74. return buildFailResponse(e);
  75. } finally {
  76. if (client != null) {
  77. try {
  78. client.close();
  79. } catch (IOException e) {
  80. }
  81. }
  82. if (response != null) {
  83. try {
  84. response.close();
  85. } catch (IOException e) {
  86. }
  87. }
  88. }
  89. }
  90. @ApiOperation(value = "获取舆情信息")
  91. @RequestMapping(value = "/getMonitoring", method = RequestMethod.POST)
  92. public BaseResponse getMonitoringPos(BisInspMonitoringParam param) {
  93. CloseableHttpClient client = null;
  94. CloseableHttpResponse response = null;
  95. String uri = "http://" + ip + ":8080/om/cus?id=9069e11e&key=2515505d" + "&sid=" + param.getSid() + "&pageNo=" + param.getPageNum() + "&pageSize=" + param.getPageSize();
  96. HttpPost post = new HttpPost(uri);
  97. try {
  98. ObjectMapper objectMapper = new ObjectMapper();
  99. post.setHeader(HTTP.CONTENT_TYPE, "application/json");
  100. post.setEntity(new StringEntity(objectMapper.writeValueAsString(param),
  101. ContentType.create("text/json", "UTF-8")));
  102. client = HttpClients.createDefault();
  103. response = client.execute(post);
  104. String result = EntityUtils.toString(response.getEntity());
  105. JSONObject json = JSONObject.fromObject(result);
  106. if (getResultState(json.getString("resultState"))) {
  107. return buildSuccessResponse(getResult(true, json));
  108. } else {
  109. return buildFailResponse(Integer.valueOf(json.getString("resultCode")), getReusltMessage(json.getString("resultCode")));
  110. }
  111. } catch (IOException e) {
  112. return buildFailResponse(e);
  113. } finally {
  114. if (client != null) {
  115. try {
  116. client.close();
  117. } catch (IOException e) {
  118. }
  119. }
  120. if (response != null) {
  121. try {
  122. response.close();
  123. } catch (IOException e) {
  124. }
  125. }
  126. }
  127. }
  128. private boolean getResultState(String resultState) {
  129. if ("success".equalsIgnoreCase(resultState)) {
  130. return true;
  131. }
  132. if ("error".equalsIgnoreCase(resultState)) {
  133. return false;
  134. }
  135. return false;
  136. }
  137. private String getReusltMessage(String resultCode) {
  138. if ("301".equals(resultCode)) {
  139. return "请求的必要参数为空";
  140. }
  141. if ("302".equals(resultCode)) {
  142. return "请求数据接口的id或密钥有误";
  143. }
  144. if ("303".equals(resultCode)) {
  145. return "IP无访问权限";
  146. }
  147. if ("304".equals(resultCode)) {
  148. return "请求次数过于频繁";
  149. }
  150. if ("305".equals(resultCode)) {
  151. return "无请求数据类型权限";
  152. }
  153. if ("306".equals(resultCode)) {
  154. return "请求参数开始时间格式不符合约定规范";
  155. }
  156. if ("307".equals(resultCode)) {
  157. return "请求参数结束时间格式不符合约定规范";
  158. }
  159. if ("308".equals(resultCode)) {
  160. return "开始时间大于结束时间";
  161. }
  162. if ("309".equals(resultCode)) {
  163. return "查询异常";
  164. }
  165. if ("310".equals(resultCode)) {
  166. return "账号锁定";
  167. }
  168. if ("311".equals(resultCode)) {
  169. return "无访问接口类型权限";
  170. }
  171. if ("312".equals(resultCode)) {
  172. return "通过URL获取同源稿件时,当前URL不存在";
  173. }
  174. if ("313".equals(resultCode)) {
  175. return "无POST或GET类型请求权限";
  176. }
  177. if ("314".equals(resultCode)) {
  178. return "栏目任务修改失败";
  179. }
  180. return "";
  181. }
  182. private Object getResult(Boolean isPage, JSONObject json) {
  183. if (isPage) {
  184. JSONObject data = json.getJSONObject("resultData");
  185. JSONObject jsonObject = data.getJSONObject("databasic");
  186. JSONArray array = data.getJSONArray("dataList");
  187. PageInfo page = new PageInfo(array);
  188. page.setPageNum(jsonObject.getInt("pageno"));
  189. page.setPageSize(jsonObject.getInt("pagesize"));
  190. page.setPages(jsonObject.getInt("allpages"));
  191. page.setTotal(jsonObject.getLong("allcount"));
  192. return page;
  193. }
  194. return json.getJSONObject("resultData");
  195. }
  196. }