|
|
|
@ -0,0 +1,412 @@
|
|
|
|
|
package com.ruoyi.flyingbook.strategy.cjt;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.lark.oapi.service.bitable.v1.model.CreateAppTableRecordRespBody;
|
|
|
|
|
import com.ruoyi.common.constant.RedisConstants;
|
|
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
|
|
import com.ruoyi.common.enums.AppType;
|
|
|
|
|
import com.ruoyi.common.enums.CjtAccountEnum;
|
|
|
|
|
import com.ruoyi.common.enums.FlagStatus;
|
|
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
|
|
|
import com.ruoyi.flyingbook.LarkHelper.LarkRobotHelper;
|
|
|
|
|
import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper;
|
|
|
|
|
import com.ruoyi.flyingbook.domain.ErpLarkRelation;
|
|
|
|
|
import com.ruoyi.flyingbook.domain.ErpLarkTempRelation;
|
|
|
|
|
import com.ruoyi.flyingbook.domain.LarkCompanyRelation;
|
|
|
|
|
import com.ruoyi.flyingbook.domain.LarkTableRelation;
|
|
|
|
|
import com.ruoyi.flyingbook.domain.cjt.CJTRequest;
|
|
|
|
|
import com.ruoyi.flyingbook.domain.cjt.CJTRequestBody;
|
|
|
|
|
import com.ruoyi.flyingbook.domain.cjt.CJTResponseBody;
|
|
|
|
|
import com.ruoyi.flyingbook.domain.edi.CJTEdiCallbackRequestVo;
|
|
|
|
|
import com.ruoyi.flyingbook.domain.lark.LarkTableRequest;
|
|
|
|
|
import com.ruoyi.flyingbook.helper.OkHttpHelper;
|
|
|
|
|
import com.ruoyi.flyingbook.mapper.ErpLarkRelationMapper;
|
|
|
|
|
import com.ruoyi.flyingbook.mapper.ErpLarkTempRelationMapper;
|
|
|
|
|
import com.ruoyi.flyingbook.mapper.LarkCompanyRelationMapper;
|
|
|
|
|
import com.ruoyi.flyingbook.mapper.LarkTableRelationMapper;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 畅捷通回调接口操作
|
|
|
|
|
* @author yuxiangyong
|
|
|
|
|
* @create 2023-10-11 19:54
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
public abstract class CJTOperateCallBackAbstract {
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisCache redisCache;
|
|
|
|
|
@Resource
|
|
|
|
|
private LarkCompanyRelationMapper larkCompanyRelationMapper;
|
|
|
|
|
@Resource
|
|
|
|
|
private LarkTableRelationMapper larkTableRelationMapper;
|
|
|
|
|
@Autowired
|
|
|
|
|
private LarkRobotHelper larkRobotHelper;
|
|
|
|
|
@Autowired
|
|
|
|
|
private LarkTableHelper larkTableHelper;
|
|
|
|
|
@Autowired
|
|
|
|
|
protected ErpLarkRelationMapper erpLarkRelationMapper;
|
|
|
|
|
@Autowired
|
|
|
|
|
protected ErpLarkTempRelationMapper erpLarkTempRelationMapper;
|
|
|
|
|
@Value("${lark.robot.group}")
|
|
|
|
|
private String ROBOT_GROUP;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 畅捷通域名
|
|
|
|
|
*/
|
|
|
|
|
protected static final String REQUEST_ROOT_PATH = "https://openapi.chanjet.com";
|
|
|
|
|
/**
|
|
|
|
|
* 重置畅捷通ticket的接口
|
|
|
|
|
*/
|
|
|
|
|
private static final String REQUEST_RESET_TICKET_PATH = REQUEST_ROOT_PATH + "/auth/appTicket/resend";
|
|
|
|
|
/**
|
|
|
|
|
* 生成调用畅捷通接口的token信息
|
|
|
|
|
*/
|
|
|
|
|
private static final String REQUEST_GENERATE_TOKEN_PATH = REQUEST_ROOT_PATH + "/v1/common/auth/selfBuiltApp/generateToken";
|
|
|
|
|
|
|
|
|
|
public void execute(CJTEdiCallbackRequestVo req){
|
|
|
|
|
try {
|
|
|
|
|
preHandle(req);
|
|
|
|
|
initContext(req);
|
|
|
|
|
resetTicket(req);
|
|
|
|
|
sync(req);
|
|
|
|
|
endHandle(req);
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
errorHandle(req,e);
|
|
|
|
|
log.error("{} execute error",this.getClass().getSimpleName(),e);
|
|
|
|
|
larkRobotHelper.sendMessageByBot(ROBOT_GROUP, buildRobotErrorCountMessage(req,e));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void preHandle(CJTEdiCallbackRequestVo req){
|
|
|
|
|
Date date = new Date();
|
|
|
|
|
ErpLarkTempRelation erpLarkTempRelation = new ErpLarkTempRelation();
|
|
|
|
|
erpLarkTempRelation.setKey(req.getBody().getString("voucherID"));
|
|
|
|
|
erpLarkTempRelation.setMethod(req.getMsgType());
|
|
|
|
|
erpLarkTempRelation.setLarkKey(req.getAppId());
|
|
|
|
|
erpLarkTempRelation.setCreateBy("System");
|
|
|
|
|
erpLarkTempRelation.setCreateTime(date);
|
|
|
|
|
erpLarkTempRelation.setUpdateTime(date);
|
|
|
|
|
erpLarkTempRelation.setFlag(FlagStatus.OK.getCode());
|
|
|
|
|
erpLarkTempRelationMapper.insertErpLarkTempRelation(erpLarkTempRelation);
|
|
|
|
|
req.setTempId(erpLarkTempRelation.getId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void endHandle(CJTEdiCallbackRequestVo req){
|
|
|
|
|
if (req.getTempId() != null){
|
|
|
|
|
erpLarkTempRelationMapper.updateForCjt(FlagStatus.DELETED.getCode(), req.getTempId(),"");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void errorHandle(CJTEdiCallbackRequestVo req,Exception e){
|
|
|
|
|
if (req.getTempId() != null){
|
|
|
|
|
erpLarkTempRelationMapper.updateForCjt(FlagStatus.OK.getCode(), req.getTempId(),e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sync(CJTEdiCallbackRequestVo req){
|
|
|
|
|
JSONObject body = req.getBody();
|
|
|
|
|
CjtAccountEnum cjtAccountEnum = req.getCjtAccountEnum();
|
|
|
|
|
String voucherID = body.getString("voucherID");
|
|
|
|
|
req.setVoucherId(voucherID);
|
|
|
|
|
CJTRequestBody cjtRequestBody = new CJTRequestBody(req.getVoucherId());
|
|
|
|
|
CJTRequest cjtRequest = new CJTRequest(getRequestUrl(), cjtAccountEnum.getAppKey(), cjtAccountEnum.getAppSecret(), generateToken(req),cjtRequestBody);
|
|
|
|
|
OkHttpHelper.post(cjtRequest);
|
|
|
|
|
JSONObject jsonObject = requestCjt(cjtRequest);
|
|
|
|
|
syncRecord(req,jsonObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 解析实体,并构建
|
|
|
|
|
* @param req
|
|
|
|
|
*/
|
|
|
|
|
protected abstract void syncRecord(CJTEdiCallbackRequestVo req,JSONObject returnBody);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取请求明细的路径
|
|
|
|
|
*/
|
|
|
|
|
protected abstract String getRequestUrl();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取请求明细的路径
|
|
|
|
|
*/
|
|
|
|
|
protected abstract String getMethodUrl();
|
|
|
|
|
|
|
|
|
|
private void initContext(CJTEdiCallbackRequestVo req){
|
|
|
|
|
LarkCompanyRelation queryCompany = new LarkCompanyRelation();
|
|
|
|
|
String larkCompanyCode = req.getMsgTypeEnum().getLarkCompanyCode();
|
|
|
|
|
queryCompany.setAppType(larkCompanyCode);
|
|
|
|
|
queryCompany.setFlag(FlagStatus.OK.getCode());
|
|
|
|
|
queryCompany.setRemark(req.getAppId());
|
|
|
|
|
List<LarkCompanyRelation> larkCompanyRelations = larkCompanyRelationMapper.selectLarkCompanyRelationList(queryCompany);
|
|
|
|
|
if (CollectionUtils.isEmpty(larkCompanyRelations)){
|
|
|
|
|
throw new RuntimeException(String.format("%s 无larkCompany配置信息 appId:%s appType:%s",req.getMsgType(),req.getAppId(),larkCompanyCode));
|
|
|
|
|
}
|
|
|
|
|
LarkCompanyRelation companyRelation = larkCompanyRelations.get(0);
|
|
|
|
|
req.setCompanyRelation(companyRelation);
|
|
|
|
|
LarkTableRelation queryTable = new LarkTableRelation();
|
|
|
|
|
queryTable.setLarkCompanyRelationId(companyRelation.getId());
|
|
|
|
|
queryTable.setFlag(FlagStatus.OK.getCode());
|
|
|
|
|
List<LarkTableRelation> larkTableRelations = larkTableRelationMapper.selectLarkTableRelationList(queryTable);
|
|
|
|
|
if (CollectionUtils.isEmpty(larkTableRelations)){
|
|
|
|
|
throw new RuntimeException(String.format("%s 无larkTableRelation配置信息 appId:%s appType:%s larkCompanyId:%s",req.getMsgType(),req.getAppId(),larkCompanyCode,companyRelation.getId()));
|
|
|
|
|
}
|
|
|
|
|
LarkTableRelation tableRelation = larkTableRelations.get(0);
|
|
|
|
|
req.setTableRelation(tableRelation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String buildRobotErrorCountMessage(CJTEdiCallbackRequestVo req,Exception e){
|
|
|
|
|
JSONObject body = new JSONObject();
|
|
|
|
|
body.put("cjtId",req.getAppId());
|
|
|
|
|
body.put("msgType",req.getMsgType());
|
|
|
|
|
LarkCompanyRelation companyRelation = req.getCompanyRelation();
|
|
|
|
|
if (companyRelation != null){
|
|
|
|
|
body.put("companyRelationId",companyRelation.getId());
|
|
|
|
|
}
|
|
|
|
|
LarkTableRelation tableRelation = req.getTableRelation();
|
|
|
|
|
if (tableRelation != null){
|
|
|
|
|
body.put("tableRelationId",tableRelation.getId());
|
|
|
|
|
}
|
|
|
|
|
body.put("errorInfo",e.getMessage());
|
|
|
|
|
return body.toJSONString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected LarkTableRequest buildLarkTableRequest(LarkCompanyRelation companyRelation,LarkTableRelation tableRelation){
|
|
|
|
|
LarkTableRequest request = new LarkTableRequest();
|
|
|
|
|
request.setAppId(companyRelation.getAppId());
|
|
|
|
|
request.setAppSecret(companyRelation.getSecret());
|
|
|
|
|
request.setAppToken(tableRelation.getToAppToken());
|
|
|
|
|
request.setAppTable(tableRelation.getToTableId());
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void addRecord(CJTEdiCallbackRequestVo req, JSONObject returnBody) {
|
|
|
|
|
LarkTableRelation tableRelation = req.getTableRelation();
|
|
|
|
|
//多维表格id
|
|
|
|
|
String toTableId = tableRelation.getToTableId();
|
|
|
|
|
//头部信息
|
|
|
|
|
JSONObject header = returnBody.getJSONObject("data");
|
|
|
|
|
//明细信息
|
|
|
|
|
JSONArray saleOrderDetails = header.getJSONArray(getDetailName());
|
|
|
|
|
//待创建关联关系
|
|
|
|
|
List<ErpLarkRelation> relationList = new ArrayList<>();
|
|
|
|
|
LarkTableRequest request = this.buildLarkTableRequest(req.getCompanyRelation(), req.getTableRelation());
|
|
|
|
|
CjtAccountEnum cjtAccountEnum = req.getCjtAccountEnum();
|
|
|
|
|
for (int i = 0; i < saleOrderDetails.size(); i++) {
|
|
|
|
|
JSONObject detail = saleOrderDetails.getJSONObject(i);
|
|
|
|
|
String uniqueKey = buildUniqueKey(toTableId, header, detail,cjtAccountEnum);
|
|
|
|
|
Map<String, Object> larkBody = buildLarkBody(header, detail,cjtAccountEnum);
|
|
|
|
|
request.setBody(larkBody);
|
|
|
|
|
CreateAppTableRecordRespBody respBody = larkTableHelper.addTableRecord(request);
|
|
|
|
|
String larkKey = respBody.getRecord().getRecordId();
|
|
|
|
|
relationList.add(buildErpLarkRelation(uniqueKey, larkKey, cjtAccountEnum.getCode()));
|
|
|
|
|
}
|
|
|
|
|
if (!org.springframework.util.CollectionUtils.isEmpty(relationList)) {
|
|
|
|
|
erpLarkRelationMapper.batchInsert(relationList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void updateRecord(CJTEdiCallbackRequestVo req, JSONObject returnBody) {
|
|
|
|
|
LarkTableRelation tableRelation = req.getTableRelation();
|
|
|
|
|
//多维表格id
|
|
|
|
|
String toTableId = tableRelation.getToTableId();
|
|
|
|
|
//头部信息
|
|
|
|
|
JSONObject header = returnBody.getJSONObject("data");
|
|
|
|
|
//明细信息
|
|
|
|
|
JSONArray saleOrderDetails = header.getJSONArray(getDetailName());
|
|
|
|
|
//待创建关联关系
|
|
|
|
|
List<ErpLarkRelation> waitCreateRelationList = new ArrayList<>();
|
|
|
|
|
LarkTableRequest request = this.buildLarkTableRequest(req.getCompanyRelation(), req.getTableRelation());
|
|
|
|
|
CjtAccountEnum cjtAccountEnum = req.getCjtAccountEnum();
|
|
|
|
|
//获取已经存在的明细
|
|
|
|
|
Map<String, ErpLarkRelation> existMap = getExistRecords(req).stream().collect(Collectors.toMap(ErpLarkRelation::getKey, Function.identity(), (K1, k2) -> K1));
|
|
|
|
|
for (int i = 0; i < saleOrderDetails.size(); i++) {
|
|
|
|
|
JSONObject detail = saleOrderDetails.getJSONObject(i);
|
|
|
|
|
String uniqueKey = buildUniqueKey(toTableId, header, detail,cjtAccountEnum);
|
|
|
|
|
Map<String, Object> larkBody = buildLarkBody(header, detail,cjtAccountEnum);
|
|
|
|
|
request.setBody(larkBody);
|
|
|
|
|
if (existMap.containsKey(uniqueKey)){
|
|
|
|
|
ErpLarkRelation erpLarkRelation = existMap.get(uniqueKey);
|
|
|
|
|
request.setRecord(erpLarkRelation.getLarkKey());
|
|
|
|
|
request.setAppTable(erpLarkRelation.getLarkTable());
|
|
|
|
|
larkTableHelper.updateTableRecord(request);
|
|
|
|
|
}else {
|
|
|
|
|
request.setRecord(null);
|
|
|
|
|
request.setAppTable(toTableId);
|
|
|
|
|
CreateAppTableRecordRespBody respBody = larkTableHelper.addTableRecord(request);
|
|
|
|
|
String larkKey = respBody.getRecord().getRecordId();
|
|
|
|
|
waitCreateRelationList.add(buildErpLarkRelation(uniqueKey, larkKey, cjtAccountEnum.getCode()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!org.springframework.util.CollectionUtils.isEmpty(waitCreateRelationList)) {
|
|
|
|
|
erpLarkRelationMapper.batchInsert(waitCreateRelationList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract String getDetailName();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构建唯一key
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
protected abstract String buildUniqueKey(String toTableId, JSONObject header, JSONObject detail,CjtAccountEnum cjtAccountEnum);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构建多维表格请求体的body
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
protected abstract Map<String, Object> buildLarkBody(JSONObject header, JSONObject detail,CjtAccountEnum cjtAccountEnum);
|
|
|
|
|
|
|
|
|
|
//构建飞书行与cjt唯一键的对应关系对象
|
|
|
|
|
private ErpLarkRelation buildErpLarkRelation(String key, String larkKey, String cjt) {
|
|
|
|
|
ErpLarkRelation erpLarkRelation = new ErpLarkRelation();
|
|
|
|
|
erpLarkRelation.setKey(key);
|
|
|
|
|
erpLarkRelation.setLarkKey(larkKey);
|
|
|
|
|
erpLarkRelation.setMethod(getRequestUrl());
|
|
|
|
|
erpLarkRelation.setFlag(FlagStatus.OK.getCode());
|
|
|
|
|
erpLarkRelation.setRemark(cjt);
|
|
|
|
|
erpLarkRelation.setCreateBy("SYSTEM");
|
|
|
|
|
erpLarkRelation.setCreateTime(new Date());
|
|
|
|
|
erpLarkRelation.setUpdateTime(new Date());
|
|
|
|
|
return erpLarkRelation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Object getSpecialKey(JSONObject detail,String key){
|
|
|
|
|
JSONArray dynamicPropertyKeys = detail.getJSONArray("DynamicPropertyKeys");
|
|
|
|
|
for (int i = 0; i < dynamicPropertyKeys.size(); i++) {
|
|
|
|
|
if (key.equals(dynamicPropertyKeys.get(i))) {
|
|
|
|
|
JSONArray DynamicPropertyValues = detail.getJSONArray("DynamicPropertyValues");
|
|
|
|
|
return DynamicPropertyValues.get(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除无效记录
|
|
|
|
|
* @param req
|
|
|
|
|
*/
|
|
|
|
|
protected void deleteRecord(CJTEdiCallbackRequestVo req){
|
|
|
|
|
LarkCompanyRelation companyRelation = req.getCompanyRelation();
|
|
|
|
|
LarkTableRelation tableRelation = req.getTableRelation();
|
|
|
|
|
LarkTableRequest request = new LarkTableRequest(companyRelation.getAppId(), companyRelation.getSecret(), tableRelation.getToAppToken());
|
|
|
|
|
Map<String,List<String>> waitDeleteLarkMap = new HashMap<>();
|
|
|
|
|
List<ErpLarkRelation> relationList = getExistRecords(req);
|
|
|
|
|
List<Long> waitDeleteIdList = new ArrayList<>();
|
|
|
|
|
for (ErpLarkRelation record : relationList) {
|
|
|
|
|
String[] s = record.getKey().split("_");
|
|
|
|
|
String appTable = s[s.length-1];
|
|
|
|
|
List<String> waitDeleteLarkList = waitDeleteLarkMap.getOrDefault(appTable, new ArrayList<>());
|
|
|
|
|
waitDeleteLarkList.add(record.getLarkKey());
|
|
|
|
|
waitDeleteLarkMap.put(appTable,waitDeleteLarkList);
|
|
|
|
|
waitDeleteIdList.add(record.getId());
|
|
|
|
|
}
|
|
|
|
|
if (!waitDeleteLarkMap.isEmpty()) {
|
|
|
|
|
for (Map.Entry<String, List<String>> entry : waitDeleteLarkMap.entrySet()) {
|
|
|
|
|
request.setAppTable(entry.getKey());
|
|
|
|
|
request.setRecords(entry.getValue().toArray(new String[0]));
|
|
|
|
|
larkTableHelper.deleteTableRecordBatch(request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (CollectionUtils.isNotEmpty(waitDeleteIdList)) {
|
|
|
|
|
erpLarkRelationMapper.deleteErpLarkRelationByIds(waitDeleteIdList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected List<ErpLarkRelation> getExistRecords(CJTEdiCallbackRequestVo req){
|
|
|
|
|
CjtAccountEnum cjtAccountEnum = req.getCjtAccountEnum();
|
|
|
|
|
ErpLarkRelation erpLarkRelation = new ErpLarkRelation();
|
|
|
|
|
erpLarkRelation.setRemark(cjtAccountEnum.getCode());
|
|
|
|
|
erpLarkRelation.setCjtId(req.getVoucherId());
|
|
|
|
|
erpLarkRelation.setMethod(getMethodUrl());
|
|
|
|
|
return erpLarkRelationMapper.selectErpLarkRelationList(erpLarkRelation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取ticket,失效前重置
|
|
|
|
|
*/
|
|
|
|
|
private void resetTicket(CJTEdiCallbackRequestVo req) {
|
|
|
|
|
CjtAccountEnum cjtAccountEnum = req.getCjtAccountEnum();
|
|
|
|
|
String ticketCacheKey = buildCacheUniqueKey(RedisConstants.CJT_TICKET_CACHE_KEY, cjtAccountEnum.getAppKey());
|
|
|
|
|
Long expireTime = redisCache.getExpireTime(ticketCacheKey);
|
|
|
|
|
//重置ticket存在响应时间,提前十秒重置
|
|
|
|
|
if (expireTime == null || expireTime < 10) {
|
|
|
|
|
log.info("重置ticket");
|
|
|
|
|
CJTRequest cjtRequest = new CJTRequest(REQUEST_RESET_TICKET_PATH
|
|
|
|
|
, cjtAccountEnum.getAppKey()
|
|
|
|
|
, cjtAccountEnum.getAppSecret());
|
|
|
|
|
OkHttpHelper.post(cjtRequest);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
String ticket = (String) redisCache.getCacheObject(ticketCacheKey);
|
|
|
|
|
req.setTicket(ticket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String buildCacheUniqueKey(String key, String appKey) {
|
|
|
|
|
return String.format("%s:%s", key, appKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成调用接口的token信息
|
|
|
|
|
*/
|
|
|
|
|
private String generateToken(CJTEdiCallbackRequestVo req) {
|
|
|
|
|
CjtAccountEnum cjtAccountEnum = req.getCjtAccountEnum();
|
|
|
|
|
String tokenCacheKey = buildCacheUniqueKey(RedisConstants.CJT_TOKEN_CACHE_KEY, cjtAccountEnum.getAppKey());
|
|
|
|
|
String openToken = (String) redisCache.getCacheObject(tokenCacheKey);
|
|
|
|
|
if (StringUtils.isEmpty(openToken)) {
|
|
|
|
|
CJTRequest cjtRequest = new CJTRequest(REQUEST_GENERATE_TOKEN_PATH
|
|
|
|
|
, cjtAccountEnum.getAppKey()
|
|
|
|
|
, cjtAccountEnum.getAppSecret()
|
|
|
|
|
, req.getTicket()
|
|
|
|
|
, cjtAccountEnum.getCertificate());
|
|
|
|
|
cjtRequest.buildGenerateBody();
|
|
|
|
|
JSONObject body = JSONObject.parseObject(OkHttpHelper.post(cjtRequest));
|
|
|
|
|
JSONObject value = body.getJSONObject("value");
|
|
|
|
|
String token = value.getString("accessToken");
|
|
|
|
|
req.setOpenToken(token);
|
|
|
|
|
redisCache.setCacheObject(tokenCacheKey, token, 5, TimeUnit.MINUTES);
|
|
|
|
|
return token;
|
|
|
|
|
} else {
|
|
|
|
|
req.setOpenToken(openToken);
|
|
|
|
|
return openToken;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 请求接口并序列化返回对象
|
|
|
|
|
*/
|
|
|
|
|
protected JSONObject requestCjt(CJTRequest req) {
|
|
|
|
|
String post = OkHttpHelper.post(req);
|
|
|
|
|
CJTResponseBody responseBody = JSONObject.parseObject(post, CJTResponseBody.class);
|
|
|
|
|
if (!"0".equals(responseBody.getCode())) {
|
|
|
|
|
log.error("{} exception", this.getClass().getSimpleName(), responseBody.getMessage());
|
|
|
|
|
throw new RuntimeException(buildErrorBody(req, responseBody.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
return responseBody.getData();
|
|
|
|
|
}
|
|
|
|
|
protected String buildErrorBody(CJTRequest req, String errorMessage) {
|
|
|
|
|
JSONObject errorInfo = new JSONObject();
|
|
|
|
|
errorInfo.put("url", req.getUrl());
|
|
|
|
|
errorInfo.put("body", JSONObject.toJSONString(req.getBody()));
|
|
|
|
|
errorInfo.put("errorMessage", errorMessage);
|
|
|
|
|
return errorInfo.toJSONString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|