This commit is contained in:
2025-11-06 13:57:42 +08:00
parent f79cdcaec7
commit 59b6f22177
3 changed files with 50 additions and 81 deletions
@@ -295,12 +295,11 @@ public class SysUnitController {
nodeList.add(new TreeNode<>(list.get(i).getId(), list.get(i).getParentId(), list.get(i).getName(), i) nodeList.add(new TreeNode<>(list.get(i).getId(), list.get(i).getParentId(), list.get(i).getName(), i)
.setExtra( .setExtra(
Map.of( Map.of(
"unitType", list.get(i).getUnitType(),
"unitTypeCode", list.get(i).getUnitTypeCode() "unitTypeCode", list.get(i).getUnitTypeCode()
) )
)); ));
} }
List<Tree<String>> treeList = TreeUtil.build(nodeList, StrUtil.blankToDefault(pid, "0000")); List<Tree<String>> treeList = TreeUtil.build(nodeList, StrUtil.blankToDefault(pid, "10"));
// NutMap menuMap = NutMap.NEW(); // NutMap menuMap = NutMap.NEW();
// for (Sys_unit unit : list) { // for (Sys_unit unit : list) {
@@ -1,5 +1,6 @@
package com.budwk.app.sys.services.impl; package com.budwk.app.sys.services.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
@@ -74,46 +75,22 @@ public class SysDataUnitPullServiceImpl extends BaseServiceImpl<Sys_unit> implem
@Override @Override
@Aop(TransAop.READ_COMMITTED) @Aop(TransAop.READ_COMMITTED)
public void updateUnits(List<String> unitIds) { public void updateUnits(List<String> unitIds) {
String url = "https://dcs.hgu.edu.cn:8888/openApi/API/jzgjbxx"; // 确保无空格!
try (CloseableHttpClient httpClient = HttpClients.custom()
.build()) {
// 构建请求体 JSON
JSONObject body = new JSONObject();
body.put("page", 1);
body.put("pagesize", 1000); // ⚠️ 建议 ≤1000,此处设为 100 更安全
// 创建 POST 请求 // 请求数据
HttpPost post = new HttpPost(url); HttpRequest httpRequest = HttpRequest.post("https://dcs.hgu.edu.cn:8888/openApi/API/zzjg");
post.setHeader("Content-Type", "application/json"); httpRequest.header("Content-Type", "application/json");
post.setHeader("applyId", "39923603855883264"); // 替换为你的实际 applyId httpRequest.header("applyId", "39923603855883264");
post.setHeader("secretKey", "169a960048124f4483e9637807afc9f6"); // 替换为你的实际 secretKey httpRequest.header("secretKey", "169a960048124f4483e9637807afc9f6");
post.setHeader("User-Agent", "ApiClient/1.0 (Java)"); httpRequest.body(JSONUtil.toJsonStr(Map.of("page", 1, "pagesize", 1000)));
post.setHeader("Accept", "*/*");
post.setHeader("Connection", "close");
// 设置请求体 log.info("请求单位数据: {}", httpRequest);
StringEntity entity = new StringEntity( String resBody = httpRequest.execute().body();
JSONUtil.toJsonStr(body), log.info("请求单位数据结果: {}", resBody);
StandardCharsets.UTF_8 JSONObject jsonBody = JSONUtil.parseObj(resBody);
);
post.setEntity(entity);
// 执行请求 if (jsonBody.getInt("status") != 200) {
HttpResponse response = httpClient.execute(post); throw new BaseException("获取单位数据失败,错误码: " + jsonBody.getInt("status") + ";错误原因:" + jsonBody.getStr("msg"));
HttpEntity responseEntity = response.getEntity();
String responseBody = responseEntity != null
? EntityUtils.toString(responseEntity, StandardCharsets.UTF_8)
: "";
System.out.println("响应状态码: " + response.getStatusLine().getStatusCode());
System.out.println("响应内容: " + responseBody);
JSONObject jsonBody = JSONUtil.parseObj(responseBody);
if (jsonBody.getInt("code") != 200) {
throw new BaseException("获取单位数据失败,错误码: " + jsonBody.getInt("code") + ";错误原因:" + jsonBody.getStr("msg"));
} }
// 需要新增的单位 // 需要新增的单位
@@ -131,7 +108,11 @@ public class SysDataUnitPullServiceImpl extends BaseServiceImpl<Sys_unit> implem
for (JSONObject row : data) { for (JSONObject row : data) {
Sys_unit unit = DataCenterUtil.mapJsonToBean(row, Sys_unit.class, api2db); Sys_unit unit = DataCenterUtil.mapJsonToBean(row, Sys_unit.class, api2db);
unit.setUnitcode(unit.getId()); unit.setUnitcode(unit.getId());
if (ObjectUtil.isNotEmpty(unit.getParentId())) {
unit.setUnitTypeCode(unit.getParentId().length() == 2 ? 0 : 1); unit.setUnitTypeCode(unit.getParentId().length() == 2 ? 0 : 1);
} else {
unit.setUnitTypeCode(0);
}
if (unitIds.contains(unit.getId())) { if (unitIds.contains(unit.getId())) {
// 存在则更新 // 存在则更新
@@ -145,22 +126,6 @@ public class SysDataUnitPullServiceImpl extends BaseServiceImpl<Sys_unit> implem
dao().insert(insertList); dao().insert(insertList);
dao().update(updateList); dao().update(updateList);
} catch (Exception e) {
e.printStackTrace();
// 实际项目中:log.error("拉取人员数据失败", e);
}
// 请求数据
/* HttpRequest httpRequest = HttpRequest.post("https://dcs.hgu.edu.cn:8888/openApi/API/zzjg");
httpRequest.header("Content-Type", "application/json");
httpRequest.header("applyId", "39923603855883264");
httpRequest.header("secretKey", "169a960048124f4483e9637807afc9f6");
httpRequest.body(JSONUtil.toJsonStr(Map.of("page", 1, "pagesize", 1000)));
log.info("请求人员数据: {}", httpRequest);
String resBody = httpRequest.execute().body();
log.info("请求人员数据结果: {}", resBody);
JSONObject jsonBody = JSONUtil.parseObj(resBody);*/
} }
} }
@@ -13,6 +13,7 @@ import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray; import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.budwk.app.base.config.DataCenterProperties; import com.budwk.app.base.config.DataCenterProperties;
import com.budwk.app.base.exception.BaseException; import com.budwk.app.base.exception.BaseException;
import com.budwk.app.base.service.impl.BaseServiceImpl; import com.budwk.app.base.service.impl.BaseServiceImpl;
@@ -36,6 +37,7 @@ import org.nutz.dao.sql.Sql;
import org.nutz.ioc.aop.Aop; import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.util.NutMap; import org.nutz.lang.util.NutMap;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@@ -118,15 +120,17 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
httpRequest.header("Content-Type", "application/json"); httpRequest.header("Content-Type", "application/json");
httpRequest.header("applyId", "39923603855883264"); httpRequest.header("applyId", "39923603855883264");
httpRequest.header("secretKey", "169a960048124f4483e9637807afc9f6"); httpRequest.header("secretKey", "169a960048124f4483e9637807afc9f6");
httpRequest.body(JSONUtil.toJsonStr(Map.of("page", 1, "pagesize", 900))); httpRequest.body(JSONUtil.toJsonStr(Map.of("page", 1, "pagesize", 5000)));
log.info("请求人员数据: {}", httpRequest); log.info("请求人员数据: {}", httpRequest);
String resBody = httpRequest.execute().body(); String resBody = httpRequest.execute().body();
log.info("请求人员数据结果: {}", resBody); log.info("请求人员数据结果: {}", resBody);
JSONObject jsonBody = JSONUtil.parseObj(resBody); JSONObject jsonBody = JSONUtil.parseObj(resBody);
System.out.println(jsonBody);
if (jsonBody.getInt("status") != 200) { if (jsonBody.getInt("status") != 200) {
throw new BaseException("获取人员数据失败,错误码: " + jsonBody.getInt("code") + ";错误原因:" + jsonBody.getStr("msg")); throw new BaseException("获取人员数据失败,错误码: " + jsonBody.getInt("status") + ";错误原因:" + jsonBody.getStr("msg"));
} }
List<JSONObject> rawDataList = new ArrayList<>(); List<JSONObject> rawDataList = new ArrayList<>();
@@ -194,6 +198,7 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
} }
return nowDate; return nowDate;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
throw new BaseException("数据拉取失败,{}", e.getMessage()); throw new BaseException("数据拉取失败,{}", e.getMessage());
} }
} }