Commit ef75b2f3 by HlQ

[add] 使用 RestClient 替换 Hutool 的 http 请求工具类

[fix] 修复项目合并时,集团 id 错误的问题
1 parent f18e25ef
package vion.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
/**
* @author HlQ
* @date 2024/7/19
*/
@Configuration
@Slf4j
public class RestClientConfig {
@Bean(name = "bongRestClient")
public RestClient getBongRestClient() {
return RestClient.builder()
.baseUrl("https://proapi.xbongbong.com")
.build();
}
@Bean(name = "oDingRestClient")
public RestClient getODingRestClient() {
return RestClient.builder()
.baseUrl("https://oapi.dingtalk.com")
.build();
}
@Bean(name = "dingRestClient")
public RestClient getdingRestClient() {
return RestClient.builder()
.baseUrl("https://api.dingtalk.com")
.build();
}
}
......@@ -10,15 +10,15 @@ import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.crypto.SecureUtil;
import org.dromara.hutool.http.client.Request;
import org.dromara.hutool.http.meta.Method;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONUtil;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestClient;
import vion.constant.RedisKeyEnum;
import vion.model.Dictionary;
import vion.model.*;
......@@ -46,6 +46,7 @@ public class ContractRunner {
private final IPaymentService paymentService;
private final IRContractProductService contractProductService;
private final RedissonClient redissonClient;
private final RestClient bongRestClient;
@Value("${xbongbong.token}")
private String token;
......@@ -75,12 +76,13 @@ public class ContractRunner {
.set("corpid", corpId)
.set("formId", 8429903)
.set("pageSize", 100);
var res = Request.of("https://proapi.xbongbong.com/pro/v2/api/contract/list")
var jobO = bongRestClient.post()
.uri("/pro/v2/api/contract/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(json.toString() + token))
.body(json.toString())
.method(Method.POST)
.send();
var jobO = JSONUtil.parseObj(res.bodyStr());
.retrieve()
.body(JSONObject.class);
if (jobO.getInt("code") != 1) {
log.error("获取合同列表失败,错误原因:{}", jobO);
return;
......@@ -115,12 +117,13 @@ public class ContractRunner {
.set("formId", 8429903)
.set("page", i)
.set("pageSize", 100);
var resR = Request.of("https://proapi.xbongbong.com/pro/v2/api/contract/list")
var jobR = bongRestClient.post()
.uri("/pro/v2/api/contract/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(jsonR.toString() + token))
.body(jsonR.toString())
.method(Method.POST)
.send();
var jobR = JSONUtil.parseObj(resR.bodyStr());
.body(json.toString())
.retrieve()
.body(JSONObject.class);
var jsonArrR = jobR.getJSONObject("result").getJSONArray("list");
var dataIdSetR = jsonArrR.stream().map(v -> JSONUtil.parseObj(v).getInt("dataId")).collect(Collectors.toSet());
dataIdSet.addAll(dataIdSetR);
......@@ -139,12 +142,13 @@ public class ContractRunner {
var json1 = JSONUtil.ofObj()
.set("corpid", corpId)
.set("dataId", dataId);
var res1 = Request.of("https://proapi.xbongbong.com/pro/v2/api/contract/detail")
var jobj1 = bongRestClient.post()
.uri("/pro/v2/api/contract/detail")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(json1.toString() + token))
.body(json1.toString())
.method(Method.POST)
.send();
var jobj1 = JSONUtil.parseObj(res1.bodyStr());
.retrieve()
.body(JSONObject.class);
if (jobj1.getInt("code") != 1) {
log.error("获取合同详情失败,错误原因:{}", jobj1);
return;
......@@ -159,6 +163,7 @@ public class ContractRunner {
contract.setMaintainSdate(Opt.ofNullable(jsonObj1.getLong("date_4")).map(sec -> DateUtil.date(sec * 1000)).orElse(null));
contract.setMaintainEdate(Opt.ofNullable(jsonObj1.getLong("date_5")).map(sec -> DateUtil.date(sec * 1000)).orElse(null));
String warrantyPeriod = Opt.ofNullable(jsonObj1.getJSONObject("text_23")).map(wp -> wp.getStr("text")).orElse("");
// 23 质保 没有维保开始时间和结束时间 25 维保
if (StrUtil.isNotBlank(warrantyPeriod) && warrantyPeriod.contains("个月")) {
String substring = warrantyPeriod.substring(0, (warrantyPeriod.length() - 2));
contract.setWarrantyPeriod(Integer.parseInt(substring));
......@@ -400,13 +405,13 @@ public class ContractRunner {
.set("page", 1)
.set("pageSize", 100);
var res = Request.of("https://proapi.xbongbong.com/pro/v2/api/product/list")
var jobj = bongRestClient.post()
.uri("/pro/v2/api/product/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(json.toString() + token))
.body(json.toString())
.method(Method.POST)
.send();
var body = res.bodyStr();
var jobj = JSONUtil.parseObj(body);
.retrieve()
.body(JSONObject.class);
if (jobj.getInt("code") != 1) {
log.error("获取CRM产品列表失败:{}", jobj);
return List.of();
......@@ -420,13 +425,13 @@ public class ContractRunner {
.set("corpid", corpId)
.set("page", i)
.set("pageSize", 100);
var res1 = Request.of("https://proapi.xbongbong.com/pro/v2/api/product/list")
.header("sign", SecureUtil.sha256(json1 + token))
.body(json1.toString())
.method(Method.POST)
.send();
var body1 = res1.bodyStr();
var jobj1 = JSONUtil.parseObj(body1);
var jobj1 = bongRestClient.post()
.uri("/pro/v2/api/product/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(json1.toString() + token))
.body(json.toString())
.retrieve()
.body(JSONObject.class);
if (jobj1.getInt("code") != 1) {
log.error("获取CRM产品列表失败:{}", jobj1);
return List.of();
......
......@@ -29,14 +29,14 @@ import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.crypto.SecureUtil;
import org.dromara.hutool.http.client.HttpDownloader;
import org.dromara.hutool.http.client.Request;
import org.dromara.hutool.http.meta.Method;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestClient;
import vion.dto.ContractDTO;
import vion.dto.RContractTeamDTO;
import vion.mapper.ContractMapper;
......@@ -75,6 +75,7 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
private final SettlementDiffMapper settlementDiffMapper;
private final DingMod dingMod;
private final Converter converter;
private final RestClient bongRestClient;
@Value("${fileUrl:}")
private String fileUrl;
......@@ -527,15 +528,15 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
@Override
public String createContract(String contractJson) {
var sha256 = SecureUtil.sha256(contractJson + token);
var res = Request.of("https://proapi.xbongbong.com/pro/v2/api/contract/list")
var jobj = bongRestClient.post()
.uri("/pro/v2/api/contract/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", sha256)
.body(contractJson)
.method(Method.POST)
.send();
var body = res.bodyStr();
var jobj = JSONUtil.parseObj(body);
.retrieve()
.body(JSONObject.class);
if (jobj.getInt("code") != 1) {
log.error("调用CRM接口创建合同失败,错误原因:{}", body);
log.error("调用CRM接口创建合同失败,错误原因:{}", jobj);
return "创建合同失败";
}
return "合同创建成功";
......@@ -559,15 +560,15 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
.set("pageSize", pageSize)
.set("conditions", conditions);
var res = Request.of("https://proapi.xbongbong.com/pro/v2/api/product/list")
var jobj = bongRestClient.post()
.uri("/pro/v2/api/product/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(json.toString() + token))
.body(json.toString())
.method(Method.POST)
.send();
var body = res.bodyStr();
var jobj = JSONUtil.parseObj(body);
.retrieve()
.body(JSONObject.class);
if (jobj.getInt("code") != 1) {
log.error("调用CRM接口获取产品列表失败,错误原因:{}", body);
log.error("调用CRM接口获取产品列表失败,错误原因:{}", jobj);
return JSONUtil.ofObj();
}
return JSONUtil.ofObj().set("records", jobj.getJSONObject("result").getJSONArray("list"))
......@@ -597,12 +598,13 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
.set("corpid", corpId)
.set("formId", 8429903)
.set("pageSize", 100);
var res = Request.of("https://proapi.xbongbong.com/pro/v2/api/contract/list")
var jobO = bongRestClient.post()
.uri("/pro/v2/api/contract/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(json.toString() + token))
.body(json.toString())
.method(Method.POST)
.send();
var jobO = JSONUtil.parseObj(res.bodyStr());
.retrieve()
.body(JSONObject.class);
if (jobO.getInt("code") != 1) {
var errorInfo = StrUtil.format("获取合同列表失败,错误原因:{}", jobO);
log.error(errorInfo);
......@@ -627,12 +629,14 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
.set("formId", 8429903)
.set("page", i)
.set("pageSize", 100);
var resR = Request.of("https://proapi.xbongbong.com/pro/v2/api/contract/list")
var jobR = bongRestClient.post()
.uri("/pro/v2/api/contract/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(jsonR.toString() + token))
.body(jsonR.toString())
.method(Method.POST)
.send();
var jobR = JSONUtil.parseObj(resR.bodyStr());
.retrieve()
.body(JSONObject.class);
var jsonArrR = jobR.getJSONObject("result").getJSONArray("list");
var fileListR = jsonArrR.stream().map(v -> {
var fileArr = JSONUtil.parseObj(v).getJSONObject("data").getJSONArray("file_1");
......@@ -649,7 +653,6 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
var contractId2Sha256Map = fileInfoList.stream().collect(Collectors.groupingBy(FileInfo::getContractId,
Collectors.mapping(FileInfo::getSha256, Collectors.toList())));
List<FileInfo> saveFileInfoList = new ArrayList<>();
for (JSONObject entries : fileList) {
var contractNo = entries.getStr("contractNo");
var fileArr = entries.getJSONArray("fileArr");
......@@ -663,6 +666,7 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
continue;
}
List<FileInfo> saveFileInfoList = new ArrayList<>();
for (Object o : fileArr) {
var fileObj = JSONUtil.parseObj(o);
var filename = fileObj.getStr("filename");
......@@ -688,8 +692,8 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
fileInfo.setUploader("销帮帮同步");
saveFileInfoList.add(fileInfo);
}
}
fileService.saveBatch(saveFileInfoList);
}
log.info("同步销帮帮合同文件[完成]");
return "销帮帮合同文件同步完成,请核对已同步文件!";
}
......@@ -869,12 +873,13 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
.set("corpid", corpId)
.set("formId", 8429903)
.set("pageSize", 100);
var res = Request.of("https://proapi.xbongbong.com/pro/v2/api/contract/list")
var jobO = bongRestClient.post()
.uri("/pro/v2/api/contract/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(json.toString() + token))
.body(json.toString())
.method(Method.POST)
.send();
var jobO = JSONUtil.parseObj(res.bodyStr());
.retrieve()
.body(JSONObject.class);
if (jobO.getInt("code") != 1) {
log.error("获取合同列表失败,错误原因:{}", jobO);
}
......@@ -897,12 +902,13 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
.set("formId", 8429903)
.set("page", i)
.set("pageSize", 100);
var resR = Request.of("https://proapi.xbongbong.com/pro/v2/api/contract/list")
var jobR = bongRestClient.post()
.uri("/pro/v2/api/contract/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(jsonR.toString() + token))
.body(jsonR.toString())
.method(Method.POST)
.send();
var jobR = JSONUtil.parseObj(resR.bodyStr());
.retrieve()
.body(JSONObject.class);
var jsonArrR = jobR.getJSONObject("result").getJSONArray("list");
var dataIdSetR = jsonArrR.stream().map(v -> JSONUtil.parseObj(v).getJSONObject("data").getStr("serialNo")).collect(Collectors.toSet());
dataIdSet.addAll(dataIdSetR);
......@@ -988,12 +994,13 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
.set("corpid", corpId)
.set("formId", 8429903)
.set("pageSize", 100);
var res = Request.of("https://proapi.xbongbong.com/pro/v2/api/contract/list")
var jobO = bongRestClient.post()
.uri("/pro/v2/api/contract/list")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(json.toString() + token))
.body(json.toString())
.method(Method.POST)
.send();
var jobO = JSONUtil.parseObj(res.bodyStr());
.retrieve()
.body(JSONObject.class);
if (jobO.getInt("code") != 1) {
log.error("获取合同列表失败,错误原因:{}", jobO);
}
......@@ -1004,12 +1011,13 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
var json1 = JSONUtil.ofObj()
.set("corpid", corpId)
.set("dataId", CollUtil.getFirst(dataIdSet));
var res1 = Request.of("https://proapi.xbongbong.com/pro/v2/api/contract/detail")
var jobj1 = bongRestClient.post()
.uri("/pro/v2/api/contract/detail")
.contentType(MediaType.APPLICATION_JSON)
.header("sign", SecureUtil.sha256(json1.toString() + token))
.body(json1.toString())
.method(Method.POST)
.send();
var jobj1 = JSONUtil.parseObj(res1.bodyStr());
.retrieve()
.body(JSONObject.class);
if (jobj1.getInt("code") != 1) {
log.error("获取合同详情失败,错误原因:{}", jobj1);
}
......@@ -1089,7 +1097,6 @@ public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Cont
var bongMap = BeanUtil.beanToMap(xbongContract, new HashMap<>(), copyOptions);
if (!mapEqual(platMap, bongMap)) {
log.info("不一致的合同:{}", contractNo);
// SC 合同全部对比, CS 合同只对比金额
}
}
}
......
......@@ -367,8 +367,8 @@ public class StoreServiceImpl extends MPJBaseServiceImpl<StoreMapper, Store> imp
public String mergeStore(StoreDTO dto) {
// targetId 合并到 sourceId
Long sourceId = dto.getSourceId();
Store store = this.getById(sourceId);
Long targetId = dto.getTargetId();
var targetStore = this.getById(targetId);
// 项目关联合同合并
Opt.ofEmptyAble(contractStoreService.lambdaQuery().select(RContractStore::getContractId).eq(RContractStore::getStoreId, targetId).list())
.ifPresent(l -> {
......@@ -385,13 +385,13 @@ public class StoreServiceImpl extends MPJBaseServiceImpl<StoreMapper, Store> imp
});
// 工单相关合并
taskMapper.update(new Task(), Wrappers.lambdaUpdate(Task.class).set(Task::getStoreId, sourceId).set(Task::getAccountId, store.getAccountId()).eq(Task::getStoreId, targetId));
taskMapper.update(new Task(), Wrappers.lambdaUpdate(Task.class).set(Task::getStoreId, sourceId).set(Task::getAccountId, targetStore.getAccountId()).eq(Task::getStoreId, targetId));
taskTempService.lambdaUpdate().set(TaskTemp::getStoreId, sourceId).eq(TaskTemp::getStoreId, targetId).update();
// 所有 targetId 的文件 转移到 sourceId
fileService.lambdaUpdate().set(FileInfo::getStoreId, sourceId).eq(FileInfo::getStoreId, targetId).update();
faultLogService.lambdaUpdate().set(FaultLog::getStoreId, sourceId).eq(FaultLog::getStoreId, targetId).update();
formService.lambdaUpdate().set(Form::getStoreId, sourceId).set(Form::getAccountId, store.getAccountId()).eq(Form::getStoreId, targetId).update(new Form());
inspectService.lambdaUpdate().set(Inspect::getStoreId, sourceId).set(Inspect::getAccountId, store.getAccountId()).eq(Inspect::getStoreId, targetId).update(new Inspect());
formService.lambdaUpdate().set(Form::getStoreId, sourceId).set(Form::getAccountId, targetStore.getAccountId()).eq(Form::getStoreId, targetId).update(new Form());
inspectService.lambdaUpdate().set(Inspect::getStoreId, sourceId).set(Inspect::getAccountId, targetStore.getAccountId()).eq(Inspect::getStoreId, targetId).update(new Inspect());
// 删除 targetId 对应的项目
this.removeById(targetId);
return "合并成功";
......
......@@ -14,15 +14,14 @@ import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.crypto.SecureUtil;
import org.dromara.hutool.crypto.digest.mac.HMac;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.client.Request;
import org.dromara.hutool.http.client.Response;
import org.dromara.hutool.json.JSONArray;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONUtil;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;
import vion.constant.RedisKeyEnum;
import vion.dto.DingDTO;
import vion.model.*;
......@@ -60,6 +59,8 @@ public class DingMod {
private final IDeptService deptService;
private final Converter converter;
private final RedissonClient redissonClient;
private final RestClient oDingRestClient;
private final RestClient dingRestClient;
/**
* 获取钉钉token
......@@ -69,7 +70,10 @@ public class DingMod {
public String getToken() {
return (String) Opt.ofNullable(redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.ACCESS_TOKEN.getVal()).get())
.orElseGet(() -> {
String res = HttpUtil.get("https://oapi.dingtalk.com/gettoken?appkey=" + appKey + "&appsecret=" + appSecret);
String res = oDingRestClient.get()
.uri("/gettoken?appkey=" + appKey + "&appsecret=" + appSecret)
.retrieve()
.body(String.class);
JSONObject jsonObj = JSONUtil.parseObj(res);
if (jsonObj.containsKey("access_token")) {
String accessToken = jsonObj.getStr("access_token");
......@@ -95,8 +99,12 @@ public class DingMod {
.set("status_list", "2,3,5,-1")
.set("size", 50)
.set("offset", offset);
String res = HttpUtil.post("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob?access_token=" + accessToken, paramJson.toString());
JSONObject jsonObject = JSONUtil.parseObj(res);
var jsonObject = oDingRestClient.post()
.uri("/topapi/smartwork/hrm/employee/queryonjob?access_token=" + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.body(paramJson.toString())
.retrieve()
.body(JSONObject.class);
if (jsonObject.getBool("success", false)) {
JSONArray jsonArray = jsonObject.getJSONObject("result").getJSONArray("data_list");
List<String> list = jsonArray.toList(String.class);
......@@ -123,8 +131,12 @@ public class DingMod {
.set("userid_list", String.join(",", tmpList))
.set("field_filter_list", "sys00-name,sys00-mobile,sys00-mainDeptId,sys00-mainDept,sys01-employeeStatus");
String res = HttpUtil.post("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/v2/list?access_token=" + accessToken, paramJson.toString());
JSONObject jsonObject = JSONUtil.parseObj(res);
var jsonObject = oDingRestClient.post()
.uri("/topapi/smartwork/hrm/employee/v2/list?access_token=" + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.body(paramJson.toString())
.retrieve()
.body(JSONObject.class);
if (jsonObject.getBool("success", false)) {
JSONArray resArr = jsonObject.getJSONArray("result");
for (Object o : resArr) {
......@@ -172,8 +184,12 @@ public class DingMod {
while (idx < deptIdList.size()) {
// 2:试用期 3:正式 5:待离职 -1:无状态
Long value = deptIdList.get(idx++);
String res = HttpUtil.post("https://oapi.dingtalk.com/topapi/v2/department/listsub?access_token=" + accessToken, JSONUtil.ofObj().set("dept_id", value).toString());
JSONObject jsonObject = JSONUtil.parseObj(res);
var jsonObject = oDingRestClient.post()
.uri("/topapi/v2/department/listsub?access_token=" + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.body(JSONUtil.ofObj().set("dept_id", value).toString())
.retrieve()
.body(JSONObject.class);
if (StrUtil.equals("ok", jsonObject.getStr("errmsg"))) {
JSONArray resArr = jsonObject.getJSONArray("result");
if (CollUtil.isNotEmpty(resArr)) {
......@@ -203,7 +219,12 @@ public class DingMod {
*/
public String workMsg(JSONObject msg) {
String token = getToken();
String res = HttpUtil.post("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=" + token, msg.toString());
var res = oDingRestClient.post()
.uri("/topapi/message/corpconversation/asyncsend_v2?access_token=" + token)
.contentType(MediaType.APPLICATION_JSON)
.body(msg.toString())
.retrieve()
.body(String.class);
log.info("钉钉工作通知消息推送:{}", res);
return res;
}
......@@ -223,24 +244,31 @@ public class DingMod {
.set("clientSecret", appSecret)
.set("code", dto.getAuthCode())
.set("grantType", "authorization_code");
String tokenRes = HttpUtil.post("https://api.dingtalk.com/v1.0/oauth2/userAccessToken", jsonObject.toString());
JSONObject tokenObj = JSONUtil.parseObj(tokenRes);
var tokenObj = dingRestClient.post()
.uri("/v1.0/oauth2/userAccessToken")
.contentType(MediaType.APPLICATION_JSON)
.body(jsonObject.toString())
.retrieve()
.body(JSONObject.class);
if (!tokenObj.containsKey("accessToken")) {
log.error("钉钉回调接口获取accessToken失败:{}", tokenObj);
return tokenObj;
}
String accessToken = tokenObj.getStr("accessToken");
Response response = Request.of("https://api.dingtalk.com/v1.0/contact/users/me")
var userInfoObj = dingRestClient.get()
.uri("/v1.0/contact/users/me")
.header("x-acs-dingtalk-access-token", accessToken)
.send();
if (!response.isOk()) {
log.error("钉钉回调接口获取unionid失败:{}", response.bodyStr());
return response.bodyStr();
}
JSONObject userInfoObj = JSONUtil.parseObj(response.bodyStr());
.retrieve()
.onStatus(httpStatusCode -> !httpStatusCode.is2xxSuccessful(), (request, response) -> {
throw new RuntimeException("钉钉回调接口获取unionid失败:" + response.getBody());
})
.body(JSONObject.class);
String unionId = userInfoObj.getStr("unionId");
String response1 = HttpUtil.post("https://oapi.dingtalk.com/topapi/user/getbyunionid?access_token=" + getToken(), "{\"unionid\":\"" + unionId + "\"}");
JSONObject useridObj = JSONUtil.parseObj(response1);
var useridObj = oDingRestClient.post()
.uri("/topapi/user/getbyunionid?access_token=" + getToken())
.body(JSONUtil.ofObj().set("unionid", unionId).toString())
.retrieve()
.body(JSONObject.class);
if (!useridObj.containsKey("result")) {
log.error("钉钉回调接口获取userid失败:{}", useridObj);
return useridObj;
......@@ -267,16 +295,24 @@ public class DingMod {
StpUtil.getTokenSession().set("curLoginUser", userVO);
return userVO;
} else if (StrUtil.equals(target, "inside")) {
String response = HttpUtil.post(buildSignUrl(), "{\"tmp_auth_code\":\"" + dto.getCode() + "\"}");
JSONObject userInfoObj = JSONUtil.parseObj(response);
var userInfoObj = oDingRestClient.post()
.uri(buildSignUrl())
.contentType(MediaType.APPLICATION_JSON)
.body(JSONUtil.ofObj().set("tmp_auth_code", dto.getCode()).toString())
.retrieve()
.body(JSONObject.class);
if (!userInfoObj.containsKey("user_info")) {
log.error("钉钉回调接口获取unionid失败:{}", userInfoObj);
return userInfoObj;
}
String unionId = userInfoObj.getJSONObject("user_info").getStr("unionid");
String response1 = HttpUtil.post("https://oapi.dingtalk.com/topapi/user/getbyunionid?access_token=" + getToken(), "{\"unionid\":\"" + unionId + "\"}");
JSONObject useridObj = JSONUtil.parseObj(response1);
JSONObject useridObj = oDingRestClient.post()
.uri("/topapi/user/getbyunionid?access_token=" + getToken())
.contentType(MediaType.APPLICATION_JSON)
.body(JSONUtil.ofObj().set("unionid", unionId).toString())
.retrieve()
.body(JSONObject.class);
if (!useridObj.containsKey("result")) {
log.error("钉钉回调接口获取userid失败:{}", useridObj);
return useridObj;
......@@ -325,16 +361,22 @@ public class DingMod {
* @return java.lang.Object
*/
public Object getPlatformToken(String authCode) {
var accessTokenStr = HttpUtil.get(StrUtil.format("https://oapi.dingtalk.com/gettoken?appkey={}&appsecret={}",
appKey, appSecret));
var accessTokenStr = oDingRestClient.get()
.uri(StrUtil.format("/gettoken?appkey={}&appsecret={}", appKey, appSecret))
.retrieve()
.body(String.class);
if (!StrUtil.contains(accessTokenStr, "access_token")) {
log.error("获取钉钉access_token失败:{}", accessTokenStr);
return "获取钉钉access_token失败";
}
var accessToken = JSONUtil.parseObj(accessTokenStr).getStr("access_token");
var userInfoStr = HttpUtil.post("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo?access_token=" + accessToken,
JSONUtil.ofObj().set("code", authCode));
var userInfoStr = oDingRestClient.post()
.uri("/topapi/v2/user/getuserinfo?access_token=" + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.body(JSONUtil.ofObj().set("code", authCode).toString())
.retrieve()
.body(String.class);
if (!StrUtil.contains(userInfoStr, "\"errmsg\":\"ok\"")) {
log.error("获取钉钉用户信息失败:{}", userInfoStr);
return "获取钉钉用户信息失败";
......@@ -397,11 +439,16 @@ public class DingMod {
String sign = Base64.encode(signBytes);
String encode = URLEncoder.encode(sign, StandardCharsets.UTF_8);
String encodeSign = encode.replace("+", "%20").replace("*", "%2A").replace("~", "%7E").replace("/", "%2F").replace("=", "%3D");
return StrUtil.format("https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey=dingkrzwks0jpi2di3uo&timestamp={}&signature={}", timestamp, encodeSign);
return StrUtil.format("/sns/getuserinfo_bycode?accessKey=dingkrzwks0jpi2di3uo&timestamp={}&signature={}", timestamp, encodeSign);
}
public String robotPush(String accessToken, String body) {
String res = HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token=" + accessToken, body);
var res = oDingRestClient.post()
.uri("/robot/send?access_token=" + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.body(body)
.retrieve()
.body(String.class);
log.info("钉钉机器人消息推送:{}", res);
return res;
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!