Commit 01e11ff7 authored by zhangjianqian's avatar zhangjianqian

省接口对接

parent 6e94daed
...@@ -269,6 +269,80 @@ public class HttpClient { ...@@ -269,6 +269,80 @@ public class HttpClient {
return result; return result;
} }
/**
* 发送POST请求
* @param url
* @return JSON或者字符串
* @throws Exception
*/
public static <JSONObject> Object sendPostJson(String url, com.alibaba.fastjson.JSONObject json,String token) throws Exception{
JSONObject jsonObject = null;
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try{
/**
* 创建一个httpclient对象
*/
client = HttpClients.createDefault();
/**
* 创建一个post对象
*/
HttpPost post = new HttpPost(url);
/**
* 包装成一个Entity对象
*/
StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json; charset=utf-8");
/**
* 设置请求的内容
*/
post.setEntity(entity);
/**
* 设置请求的报文头部的编码
*/
post.setHeader(new BasicHeader("Content-Type", "application/json; charset=utf-8"));
/**
* 设置请求的报文头部的编码
*/
post.setHeader(new BasicHeader("Accept", "application/json;charset=utf-8"));
//需要时传token
if(token!=null){
post.setHeader("authorization",token);
}
/**
* 执行post请求
*/
response = client.execute(post);
/**
* 获取响应码
*/
int statusCode = response.getStatusLine().getStatusCode();
if (SUCCESS_CODE == statusCode){
/**
* 通过EntityUitls获取返回内容
*/
String result = EntityUtils.toString(response.getEntity(),"UTF-8");
/**
* 转换成json,根据合法性返回json或者字符串
*/
try{
jsonObject = (JSONObject) com.alibaba.fastjson.JSONObject.parseObject(result);
return jsonObject;
}catch (Exception e){
return result;
}
}else{
LOGGER.error("HttpClientService-line: {}, errorMsg:{}", 146, "POST请求失败!");
}
}catch (Exception e){
LOGGER.error("HttpClientService-line: {}, Exception:{}", 149, e);
}finally {
response.close();
client.close();
}
return null;
}
} }
package com.zehong.web.controller.api;
import com.alibaba.fastjson.JSONObject;
import com.zehong.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class OutUtil {
@Autowired
private RedisCache redisCache;
@Value("${zhengfu.appId}")
private String appId;
@Value("${zhengfu.appSecret}")
private String appSecret;
@Value("${zhengfu.tokenUrl}")
private String tokenUrl;
@Value("${zhengfu.apiUrl}")
private String apiUrl;
/**
* 获取token
* @return
* @throws Exception
*/
public String getToken()throws Exception
{
String token = (String)redisCache.getCacheObject("apiToken");
if(token!=null){
token = getNewToken();
}
return token;
}
/**
* 获取接口参数
* @return
* @throws Exception
*/
public JSONObject getInfo(String domain, String methodType, Map<String,Object> map)throws Exception
{
String token = (String)redisCache.getCacheObject("apiToken");
if(token==null){
token = getNewToken();
}
JSONObject json = new JSONObject();
json.put("domain", domain);
json.put("method", methodType);
json.put("condition", map);
JSONObject jobct = (JSONObject) HttpClient.sendPostJson(apiUrl,json,token);
return jobct;
}
/**
* 上传数据
* @param methodType
* @param list
* @return
* @throws Exception
*/
public JSONObject setInfo(String domain, String methodType, List<Map<String,Object>> list)throws Exception
{
String token = (String)redisCache.getCacheObject("apiToken");
if(token==null){
token = getNewToken();
}
JSONObject json = new JSONObject();
json.put("domain", domain);
json.put("method", methodType);
json.put("data", list);
JSONObject jobct = (JSONObject) HttpClient.sendPostJson(apiUrl,json,token);
return jobct;
}
public String getNewToken()throws Exception{
JSONObject json = new JSONObject();
json.put("appId", appId);
json.put("appSecret", appSecret);
JSONObject jobct = (JSONObject) HttpClient.sendPostJson(tokenUrl,json,null);
redisCache.setCacheObject("apiToken",(String)((Map<String,Object>)jobct.get("data")).get("accessToken"),7000, TimeUnit.SECONDS);
return (String)((Map<String,Object>)jobct.get("data")).get("accessToken");
}
}
package com.zehong.web.controller.api;
import com.alibaba.fastjson.JSONObject;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/token")
public class TokenController {
@Autowired
private OutUtil outUtil;
/**
* 获取营业执照
* @return
* @throws Exception
*/
@GetMapping(value = "/getInfo")
public AjaxResult getInfo()throws Exception
{
Map<String,Object> map = new HashMap<>();
map.put("updateTime","2022-11-28 14:01:00");
map.put("pageSize",100);
map.put("pageIndex",1);
map.put("cityId",10000010);
String domain = "region/county/information";
JSONObject json = outUtil.getInfo(domain,"READ",map);
return AjaxResult.success(json);
}
@GetMapping(value = "/setInfo")
public AjaxResult setInfo()throws Exception
{
Map<String,Object> map = new HashMap<>();
map.put("id","111");
map.put("idNo","321323198802116876");
map.put("expertEval","的好的");
map.put("selectionEvent","没问题的");
map.put("selectionStartTime","2024-04-05");
map.put("scorDesc","巴适哦");
map.put("updateTime","2024-04-05");
List<Map<String,Object>> list = new ArrayList<>();
list.add(map);
String domain = "professor/application/information";
JSONObject json = outUtil.setInfo(domain,"WRITE",list);
return AjaxResult.success(json);
}
}
...@@ -102,3 +102,12 @@ zehong: ...@@ -102,3 +102,12 @@ zehong:
addressEnabled: false addressEnabled: false
# 验证码类型 math 数组计算 char 字符验证 # 验证码类型 math 数组计算 char 字符验证
captchaType: math captchaType: math
zhengfu:
#appId
appId: 6e2c7b5a4g1h0fd9
#appSecret
appSecret: b5baee3c8b9bfea4efce1cb0cd3fe5ab
#tokenurl
tokenUrl: http://test.zfcxjst.hebei.gov.cn:8082/openApi/authorization/api-entry/v1/getAccessToken
#链接访问地址
apiUrl: http://test.zfcxjst.hebei.gov.cn:8082/openApi/index/sync/v1/entrypoint
...@@ -98,7 +98,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter ...@@ -98,7 +98,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// 过滤请求 // 过滤请求
.authorizeRequests() .authorizeRequests()
// 对于登录login 验证码captchaImage 允许匿名访问 // 对于登录login 验证码captchaImage 允许匿名访问
.antMatchers("/login", "/captchaImage","/websocket/**","/websocketServer", "/detector/detectorReport/**","/getout/**").anonymous() .antMatchers("/login", "/captchaImage","/websocket/**","/websocketServer", "/detector/detectorReport/**","/getout/**","/token/**").anonymous()
.antMatchers( .antMatchers(
HttpMethod.GET, HttpMethod.GET,
"/*.html", "/*.html",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment