You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
370 lines
17 KiB
370 lines
17 KiB
package com.kms.web.utils;
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.http.HttpResponse;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.TypeReference;
|
|
import com.kms.web.utils.singleDomain.SingleMenu;
|
|
import com.shuili.common.exception.CustomException;
|
|
import lombok.Data;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
@Component
|
|
public class WaterRequestUtil {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(WaterRequestUtil.class);
|
|
|
|
//网关
|
|
@Value("${water.gateway.paasToken:}")
|
|
public String gatewayPaasToken;
|
|
@Value("${water.gateway.paasId:}")
|
|
public String gatewayPaasId;
|
|
|
|
//应用中台
|
|
@Value("${water.center.paasToken:}")
|
|
public String centerPaasToken;
|
|
@Value("${water.center.paasId:}")
|
|
public String centerPaasId;
|
|
|
|
//网关
|
|
@Value("${water.url:}")
|
|
public String url;
|
|
|
|
@Value("${water.singOnUrl:}")
|
|
public String singOnUrl;
|
|
@Value("${water.logoutUrl:}")
|
|
public String logoutUrl;
|
|
|
|
@Value("${water.accessTokenUrl:}")
|
|
public String accessTokenUrl;
|
|
|
|
|
|
//验证用户登录
|
|
@Value("${water.userUrl:}")
|
|
public String userUrl;
|
|
|
|
@Value("${water.systemCode:}")
|
|
public String systemCode;
|
|
@Value("${water.systemName:}")
|
|
public String systemName;
|
|
|
|
//获取部门信息
|
|
@Value("${water.orgUrl:}")
|
|
public String orgUrl;
|
|
|
|
//业务角色
|
|
@Value("${water.roleUrl:}")
|
|
public String roleUrl;
|
|
|
|
|
|
@Value("${water.systemUrl:}")
|
|
public String systemUrl;
|
|
|
|
|
|
@Value("${water.usersUrl:}")
|
|
public String usersUrl;
|
|
|
|
|
|
@Value("${water.resourceUrl:}")
|
|
public String resourceUrl;
|
|
|
|
/**
|
|
* 设置
|
|
*
|
|
* @param httpRequest
|
|
*/
|
|
private void setHead(HttpRequest httpRequest) {
|
|
String timestamp = String.valueOf(new Date().getTime());
|
|
String nonce = IdUtil.fastSimpleUUID();
|
|
String gatewaySignature = timestamp + gatewayPaasToken + nonce + timestamp;
|
|
String centerSignature = timestamp + centerPaasToken + nonce + timestamp;
|
|
try {
|
|
gatewaySignature = SHACoder.encodeSHA256Hex(gatewaySignature).toUpperCase();
|
|
centerSignature = SHACoder.encodeSHA256Hex(centerSignature).toUpperCase();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
httpRequest
|
|
.header("x-tif-paasid", gatewayPaasId)
|
|
.header("x-tif-signature", gatewaySignature)
|
|
.header("x-tif-timestamp", timestamp)
|
|
.header("x-tif-nonce", nonce)
|
|
.header("x-tsp-paasid", centerPaasId)
|
|
.header("x-tsp-signature", centerSignature)
|
|
.header("x-tsp-timestamp", timestamp)
|
|
.header("x-tsp-nonce", nonce);
|
|
|
|
}
|
|
|
|
|
|
public HttpRequest createGet(String url) {
|
|
HttpRequest httpRequest = HttpRequest.get(url);
|
|
setHead(httpRequest);
|
|
return httpRequest;
|
|
}
|
|
|
|
public HttpRequest createPost(String url) {
|
|
HttpRequest httpRequest = HttpRequest.post(url);
|
|
setHead(httpRequest);
|
|
return httpRequest;
|
|
}
|
|
|
|
/**
|
|
* 业务系统
|
|
*
|
|
* @return
|
|
*/
|
|
public WaterResult systemPage() {
|
|
log.info("查询业务系统-----------------");
|
|
HttpRequest post = createPost(systemUrl);
|
|
HashMap<String, String> query = new HashMap<>();
|
|
query.put("code", systemCode);
|
|
query.put("name", systemName);
|
|
String body;
|
|
try (HttpResponse response = post.body(JSONObject.toJSONString(query))
|
|
.execute()) {
|
|
body = response.body();
|
|
}
|
|
WaterResult waterResult = JSONObject.parseObject(body, WaterResult.class);
|
|
isSuccess(waterResult);
|
|
//{
|
|
// "msg": "操作成功",
|
|
// "code": 200,
|
|
// "data": {
|
|
// "code": 200,
|
|
// "message": "成功",
|
|
// "success": true,
|
|
// "data": "[{\"contractor\":\"数字广东网络建设有限公司\",\"code\":\"sgc-jg\",\"buildingLinkman\":\"张三\",\"contractorId\":\"1647768587123822593\",\"displayMobileFlag\":\"2\",\"manageUrl\":\"http://19.25.74.73:8001/\",\"type\":\"2\",\"building\":\"广东省水利厅\",\"developerId\":\"1732646766216482817\",\"developerPhone\":\"13333333333\",\"contractorPhone\":\"17324499124\",\"levelText\":\"省级\",\"buildingPhone\":\"13333333333\",\"id\":\"1732648267643097090\",\"manageImg\":\"{}\",\"area\":\"440000\",\"level\":\"1\",\"displayFlag\":\"1\",\"buildingId\":\"202206101718001\",\"createTime\":\"2023-12-07 06:28:33\",\"developerLinkman\":\"张三\",\"name\":\"水工程应用-水利工程建设管理模块\",\"publicUrl\":\"http://10.169.133.130:8001\",\"developer\":\"中通服建设有限公司\",\"contractorLinkman\":\"徐勇\",\"status\":\"1\"}]",
|
|
// "timestamp": "1710140017747"
|
|
// }
|
|
//}
|
|
return waterResult;
|
|
}
|
|
|
|
|
|
// public static void main(String[] args) {
|
|
// HttpRequest post = createPost("http://19.25.35.204:31190/data_center/gateway/api/usrc/open-api/system/list", "DGSP_1606199297580052481", "b26a1c7b95744369c00f65b7a0790b6");
|
|
// HashMap<String, String> query = new HashMap<>();
|
|
// query.put("code","sgc-jg");
|
|
// query.put("name","水工程应用-水利工程建设管理模块");
|
|
// HttpResponse response = post.body(JSONObject.toJSONString(query))
|
|
// .execute();
|
|
// String body = response.body();
|
|
// WaterResult waterResult = JSONObject.parseObject(body, WaterResult.class);
|
|
//
|
|
// }
|
|
|
|
|
|
/**
|
|
* 单点登录
|
|
*
|
|
* @return
|
|
*/
|
|
public WaterResult signOn(String userId) {
|
|
HttpRequest get = createGet(userUrl);
|
|
String body;
|
|
try (HttpResponse response = get.header("x-tsp-target", gatewayPaasId)
|
|
.header("x-tsp-uid-type", "id")
|
|
.header("x-tsp-uid", userId)
|
|
.execute()) {
|
|
body = response.body();
|
|
}
|
|
return JSONObject.parseObject(body, WaterResult.class);
|
|
}
|
|
|
|
public WaterResultV2<String> logout(String authorization) {
|
|
long startTime = System.currentTimeMillis();
|
|
log.info("--------请求统一门户用户注销登陆,请求地址为:{}", logoutUrl);
|
|
HttpRequest get = createGet(logoutUrl);
|
|
String body;
|
|
try (HttpResponse response = get.header("Authorization", authorization)
|
|
.execute()) {
|
|
body = response.body();
|
|
}
|
|
long endTime = System.currentTimeMillis();
|
|
log.info("--------请求统一门户用户注销登陆,耗时{}毫秒,返回结果为:{}", endTime - startTime, body);
|
|
WaterResultV2<String> waterResult = JSON.parseObject(body, new TypeReference<WaterResultV2<String>>() {
|
|
});
|
|
isSuccess(waterResult);
|
|
return waterResult;
|
|
}
|
|
|
|
/**
|
|
* 验证用户登录
|
|
*
|
|
* @param authorization
|
|
* @return
|
|
*/
|
|
public WaterResultV2<WaterUser> authUser(String authorization) {
|
|
long startTime = System.currentTimeMillis();
|
|
log.info("--------请求统一门户用户验证,请求地址为:{}", userUrl);
|
|
HttpRequest get = createGet(userUrl);
|
|
String body;
|
|
try (HttpResponse response = get.header("Authorization", authorization)
|
|
.execute()) {
|
|
body = response.body();
|
|
}
|
|
long endTime = System.currentTimeMillis();
|
|
log.info("--------请求统一门户用户验证,耗时{}毫秒", endTime - startTime);
|
|
WaterResultV2<WaterUser> waterResult = JSON.parseObject(body, new TypeReference<WaterResultV2<WaterUser>>() {
|
|
});
|
|
isSuccess(waterResult);
|
|
return waterResult;
|
|
}
|
|
|
|
public String getToken(String code) {
|
|
long startTime = System.currentTimeMillis();
|
|
log.info("--------请求统一门户获取token,请求地址为:{}", accessTokenUrl);
|
|
HttpRequest get = createGet(accessTokenUrl);
|
|
String body;
|
|
try (HttpResponse response = get.header("Authorization", code)
|
|
.execute()) {
|
|
body = response.body();
|
|
}
|
|
long endTime = System.currentTimeMillis();
|
|
log.info("-------请求统一门户获取token,耗时{}毫秒,token:[{}]", endTime - startTime, body);
|
|
WaterResultV2<WaterToken> waterResult = JSON.parseObject(body, new TypeReference<WaterResultV2<WaterToken>>() {
|
|
});
|
|
isSuccess(waterResult);
|
|
return waterResult.getData().getAccessToken();
|
|
}
|
|
|
|
@Data
|
|
public static class WaterToken {
|
|
private String accessToken;
|
|
private String tokenType;
|
|
private String refreshToken;
|
|
private String expiresIn;
|
|
}
|
|
|
|
@Data
|
|
public static class WaterUser {
|
|
private WaterUserInfo user;
|
|
private List<String> functionPerms;
|
|
|
|
}
|
|
|
|
@Data
|
|
public static class WaterUserInfo {
|
|
private String id;
|
|
}
|
|
|
|
|
|
public WaterResult getOrgPage() {
|
|
log.info("--------请求获取门户组织信息,请求地址为:{}", orgUrl);
|
|
HashMap<String, Object> hashMap = new HashMap<>();
|
|
hashMap.put("page", 1);
|
|
hashMap.put("size", 100);
|
|
hashMap.put("systemCode", systemCode);
|
|
HttpRequest get = createPost(orgUrl);
|
|
String body;
|
|
try (HttpResponse response = get.body(JSONObject.toJSONString(hashMap))
|
|
.execute()) {
|
|
body = response.body();
|
|
}
|
|
WaterResult waterResult = JSONObject.parseObject(body, WaterResult.class);
|
|
isSuccess(waterResult);
|
|
//{
|
|
// "msg": "操作成功",
|
|
// "code": 200,
|
|
// "data": {
|
|
// "code": 200,
|
|
// "message": "成功",
|
|
// "success": true,
|
|
// "data": "{\"total\":4,\"size\":100,\"records\":[{\"parent\":\"202206101718001\",\"source\":\"tif\",\"type\":\"GA\",\"levelText\":\"省级\",\"id\":\"1646339896680984578\",\"area\":\"440000\",\"level\":\"1\",\"sourceText\":\"统一身份认证\",\"categoryText\":\"单位\",\"name\":\"水文局\",\"treeCode\":\"00030-00016\",\"status\":\"1\",\"code\":\"SJ0013\",\"orgId\":\"1646339896680984578\",\"tifld\":\"1232236\",\"seq\":18,\"areaText\":\"广东省\",\"updateTime\":\"2024-02-28 02:45:24\",\"createTime\":\"2023-04-13 02:29:54\",\"statusText\":\"启用\",\"category\":\"1\",\"crossParent\":\"\"},{\"parent\":\"0\",\"type\":\"FIRM\",\"levelText\":\"省级\",\"id\":\"1732646766216482817\",\"area\":\"440000\",\"level\":\"1\",\"categoryText\":\"单位\",\"name\":\"中通服建设有限公司\",\"treeCode\":\"00028\",\"status\":\"1\",\"code\":\"914400001903268010\",\"orgId\":\"1732646766216482817\",\"seq\":84,\"areaText\":\"广东省\",\"updateTime\":\"2023-12-11 18:05:29\",\"createTime\":\"2023-12-07 06:22:35\",\"statusText\":\"启用\",\"category\":\"1\"},{\"parent\":\"0\",\"natureText\":\"\",\"source\":\"input\",\"type\":\"GA\",\"levelText\":\"省级\",\"id\":\"202206101718001\",\"area\":\"440000\",\"level\":\"1\",\"sourceText\":\"手动创建\",\"categoryText\":\"单位\",\"name\":\"广东省水利厅\",\"treeCode\":\"00030\",\"status\":\"1\",\"code\":\"006941135\",\"abbrName\":\"水利厅\",\"uscc\":\"114400000069411352\",\"orgId\":\"202206101718001\",\"tifld\":\"114400000069411352\",\"seq\":67,\"nature\":\"1\",\"areaText\":\"广东省\",\"updateTime\":\"2024-02-28 02:45:24\",\"createTime\":\"2022-12-14 10:02:37\",\"statusText\":\"启用\",\"category\":\"1\",\"remarks\":\"默认机构,不可删除\"},{\"parent\":\"202206101718001\",\"source\":\"tif\",\"type\":\"GA\",\"deptTags\":\"noEntity\",\"levelText\":\"省级\",\"id\":\"1646339701234806785\",\"area\":\"440000\",\"level\":\"1\",\"sourceText\":\"统一身份认证\",\"categoryText\":\"部门\",\"name\":\"建设处\",\"treeCode\":\"00030-00044\",\"status\":\"1\",\"code\":\"SJ0001-024\",\"orgId\":\"1646339701234806785\",\"seq\":114,\"areaText\":\"广东省\",\"updateTime\":\"2024-01-08 18:47:31\",\"deptTagsText\":\"\",\"createTime\":\"2023-04-13 02:29:07\",\"statusText\":\"启用\",\"category\":\"2\",\"crossParent\":\"\"}],\"page\":1}",
|
|
// "timestamp": "1710140677104"
|
|
// }
|
|
//}
|
|
return waterResult;
|
|
}
|
|
|
|
|
|
public WaterResultV2<List<SingleMenu>> getUserResource(String authorization) {
|
|
//计算请求消耗的时间
|
|
long startTime = System.currentTimeMillis();
|
|
log.info("--------请求获取门户用户资源信息,请求地址为:{}", resourceUrl);
|
|
HttpRequest get = createPost(resourceUrl + "?systemCode=" + systemCode);
|
|
get.header("Authorization", authorization);
|
|
String body;
|
|
try (HttpResponse response = get.execute()) {
|
|
body = response.body();
|
|
}
|
|
long endTime = System.currentTimeMillis();
|
|
log.info("--------请求获取门户用户资源信息,耗时{}毫秒", endTime - startTime);
|
|
log.debug("--------请求获取门户用户资源信息,返回结果:{}", body);
|
|
WaterResultV2<List<SingleMenu>> waterResult = JSON.parseObject(body, new TypeReference<WaterResultV2<List<SingleMenu>>>() {
|
|
});
|
|
isSuccess(waterResult);
|
|
//{\"total\":1,\"size\":100,\"records\":[{\"area\":\"440000\",\"systemList\":[{\"area\":\"440000\",\"code\":\"sgc-jg\",\"name\":\"水工程应用-水利工程建设管理模块\",\"id\":\"1732648267643097090\"}],\"code\":\"YW00013\",\"level\":\"1d\",\"dataConfig\":\"none\",\"type\":\"BUSINESS\",\"posts\":[],\"users\":[{\"id\":\"20231225000006\"},{\"id\":\"20240119000002\"}],\"name\":\"水利工程应用-水利工程建设管理模块角色\",\"id\":\"20231213000001\",\"category\":\"BUSINESS\",\"businesses\":[{\"parent\":\"1762458920822599682\",\"businessCode\":\"YWSLGCJSGL0008\",\"parentName\":\"水利工程建设管理\",\"businessName\":\"水利工程建设管理\",\"description\":\"\",\"id\":\"1705128209926942721\",\"delFlag\":0}],\"status\":\"1\"}],\"page\":1}
|
|
return waterResult;
|
|
}
|
|
|
|
|
|
public WaterResult getRolePage() {
|
|
log.info("--------请求获取门户角色信息,请求地址为:{}", roleUrl);
|
|
HashMap<String, Object> hashMap = new HashMap<>();
|
|
hashMap.put("page", 1);
|
|
hashMap.put("size", 1000);
|
|
hashMap.put("systemCode", systemCode);
|
|
HttpRequest get = createPost(roleUrl);
|
|
String body;
|
|
try (HttpResponse response = get.body(JSONObject.toJSONString(hashMap))
|
|
.execute()) {
|
|
body = response.body();
|
|
}
|
|
log.info("--------请求获取门户角色信息");
|
|
WaterResult waterResult = JSONObject.parseObject(body, WaterResult.class);
|
|
isSuccess(waterResult);
|
|
//{\"total\":1,\"size\":100,\"records\":[{\"area\":\"440000\",\"systemList\":[{\"area\":\"440000\",\"code\":\"sgc-jg\",\"name\":\"水工程应用-水利工程建设管理模块\",\"id\":\"1732648267643097090\"}],\"code\":\"YW00013\",\"level\":\"1d\",\"dataConfig\":\"none\",\"type\":\"BUSINESS\",\"posts\":[],\"users\":[{\"id\":\"20231225000006\"},{\"id\":\"20240119000002\"}],\"name\":\"水利工程应用-水利工程建设管理模块角色\",\"id\":\"20231213000001\",\"category\":\"BUSINESS\",\"businesses\":[{\"parent\":\"1762458920822599682\",\"businessCode\":\"YWSLGCJSGL0008\",\"parentName\":\"水利工程建设管理\",\"businessName\":\"水利工程建设管理\",\"description\":\"\",\"id\":\"1705128209926942721\",\"delFlag\":0}],\"status\":\"1\"}],\"page\":1}
|
|
return waterResult;
|
|
}
|
|
|
|
|
|
public WaterResult usersPage() {
|
|
log.info("--------请求获取门户用户信息,请求地址为:{}", usersUrl);
|
|
HashMap<String, Object> hashMap = new HashMap<>();
|
|
hashMap.put("page", 1);
|
|
hashMap.put("size", 100);
|
|
hashMap.put("systemCode", systemCode);
|
|
HttpRequest get = createPost(usersUrl);
|
|
String body;
|
|
try (HttpResponse response = get.body(JSONObject.toJSONString(hashMap))
|
|
.execute()) {
|
|
body = response.body();
|
|
}
|
|
log.info("--------请求获取门户用户信息");
|
|
WaterResult waterResult = JSONObject.parseObject(body, WaterResult.class);
|
|
isSuccess(waterResult);
|
|
return waterResult;
|
|
}
|
|
|
|
|
|
private void isSuccess(WaterResult waterResult) {
|
|
if (waterResult == null) {
|
|
throw new CustomException("请求异常");
|
|
}
|
|
if (waterResult.getCode() != 200 || !waterResult.getSuccess()) {
|
|
throw new CustomException("请求失败,原因:" + waterResult.getMessage());
|
|
}
|
|
}
|
|
|
|
private void isSuccess(WaterResultV2<?> waterResult) {
|
|
if (waterResult == null) {
|
|
throw new CustomException("请求异常");
|
|
}
|
|
if (waterResult.getCode() != 200 || !waterResult.getSuccess()) {
|
|
throw new CustomException("请求失败,原因:" + waterResult.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|