package cn.com.goldenwater.dcproj.controller.homepage; import cn.com.goldenwater.core.web.BaseController; import cn.com.goldenwater.core.web.BaseResponse; import cn.com.goldenwater.dcproj.param.BisInspMonitoringParam; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.pagehelper.PageInfo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang3.StringUtils; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; /** * Created by jinshui on 2019/7/17. */ @Api(value = "", tags = "舆情接口") @RestController @RequestMapping("/dc/insp/monitror") public class BisInspMonitoringController extends BaseController { @Value("${bis-insp-monitor-ip}") public String ip; @ApiOperation(value = "获取舆情信息") @RequestMapping(value = "/getMonitoring", method = RequestMethod.GET) public BaseResponse getMonitoring(BisInspMonitoringParam param) { CloseableHttpClient client = null; CloseableHttpResponse response = null; String uri = "http://" + ip + ":8080/om/cus?id=9069e11e&key=2515505d" + "&sid=" + param.getSid() + "&pageNo=" + param.getPageNum() + "&pageSize=" + param.getPageSize(); if (StringUtils.isNotBlank(param.getStartDate())) { uri += uri + "&startDate=" + param.getStartDate(); } if (StringUtils.isNotBlank(param.getEndDate())) { uri += uri + "&endDate=" + param.getEndDate(); } if (StringUtils.isNotBlank(param.getTimef())) { uri += uri + "&timef=" + param.getTimef(); } if (StringUtils.isNotBlank(param.getOrder())) { uri += uri + "&order=" + param.getOrder(); } if (StringUtils.isNotBlank(param.getTid())) { uri += uri + "&tid=" + param.getTid(); } if (StringUtils.isNotBlank(param.getKw())) { uri += uri + "&kw=" + param.getKw(); } if (StringUtils.isNotBlank(param.getKwt())) { uri += uri + "&kwt=" + param.getKwt(); } try { HttpGet get = new HttpGet(uri); client = HttpClients.createDefault(); response = client.execute(get); String result = EntityUtils.toString(response.getEntity()); JSONObject json = JSONObject.fromObject(result); if (getResultState(json.getString("resultState"))) { return buildSuccessResponse(getResult(true, json)); } else { return buildFailResponse(json.getInt("resultCode"), getReusltMessage(json.getString(json.getString("resultCode")))); } } catch (IOException e) { return buildFailResponse(e); } finally { if (client != null) { try { client.close(); } catch (IOException e) { } } if (response != null) { try { response.close(); } catch (IOException e) { } } } } @ApiOperation(value = "获取舆情信息") @RequestMapping(value = "/getMonitoring", method = RequestMethod.POST) public BaseResponse getMonitoringPos(BisInspMonitoringParam param) { CloseableHttpClient client = null; CloseableHttpResponse response = null; String uri = "http://" + ip + ":8080/om/cus?id=9069e11e&key=2515505d" + "&sid=" + param.getSid() + "&pageNo=" + param.getPageNum() + "&pageSize=" + param.getPageSize(); HttpPost post = new HttpPost(uri); try { ObjectMapper objectMapper = new ObjectMapper(); post.setHeader(HTTP.CONTENT_TYPE, "application/json"); post.setEntity(new StringEntity(objectMapper.writeValueAsString(param), ContentType.create("text/json", "UTF-8"))); client = HttpClients.createDefault(); response = client.execute(post); String result = EntityUtils.toString(response.getEntity()); JSONObject json = JSONObject.fromObject(result); if (getResultState(json.getString("resultState"))) { return buildSuccessResponse(getResult(true, json)); } else { return buildFailResponse(Integer.valueOf(json.getString("resultCode")), getReusltMessage(json.getString("resultCode"))); } } catch (IOException e) { return buildFailResponse(e); } finally { if (client != null) { try { client.close(); } catch (IOException e) { } } if (response != null) { try { response.close(); } catch (IOException e) { } } } } private boolean getResultState(String resultState) { if ("success".equalsIgnoreCase(resultState)) { return true; } if ("error".equalsIgnoreCase(resultState)) { return false; } return false; } private String getReusltMessage(String resultCode) { if ("301".equals(resultCode)) { return "请求的必要参数为空"; } if ("302".equals(resultCode)) { return "请求数据接口的id或密钥有误"; } if ("303".equals(resultCode)) { return "IP无访问权限"; } if ("304".equals(resultCode)) { return "请求次数过于频繁"; } if ("305".equals(resultCode)) { return "无请求数据类型权限"; } if ("306".equals(resultCode)) { return "请求参数开始时间格式不符合约定规范"; } if ("307".equals(resultCode)) { return "请求参数结束时间格式不符合约定规范"; } if ("308".equals(resultCode)) { return "开始时间大于结束时间"; } if ("309".equals(resultCode)) { return "查询异常"; } if ("310".equals(resultCode)) { return "账号锁定"; } if ("311".equals(resultCode)) { return "无访问接口类型权限"; } if ("312".equals(resultCode)) { return "通过URL获取同源稿件时,当前URL不存在"; } if ("313".equals(resultCode)) { return "无POST或GET类型请求权限"; } if ("314".equals(resultCode)) { return "栏目任务修改失败"; } return ""; } private Object getResult(Boolean isPage, JSONObject json) { if (isPage) { JSONObject data = json.getJSONObject("resultData"); JSONObject jsonObject = data.getJSONObject("databasic"); JSONArray array = data.getJSONArray("dataList"); PageInfo page = new PageInfo(array); page.setPageNum(jsonObject.getInt("pageno")); page.setPageSize(jsonObject.getInt("pagesize")); page.setPages(jsonObject.getInt("allpages")); page.setTotal(jsonObject.getLong("allcount")); return page; } return json.getJSONObject("resultData"); } }