Commit 7116aac0 by zhouhaibao

feat(sop): 新增ROP HTTP工具类并重构枚举数据源服务

- 添加RopHttpUtil和RopUtils工具类用于HTTP调用ROP端服务
- 集成签名算法和参数处理功能
- 重构SopEnumDataSourceService使用新的HTTP工具类替换原有ROP客户端
- 配置可配置的基础URL地址
- 添加触发时间类型元数据项支持
- 更新业务类型映射将金融改为轻改
- 移除对SopRopClient的依赖注入方式
1 parent 25098692
...@@ -24,6 +24,9 @@ public class SopStrategyMetaDTO implements Serializable { ...@@ -24,6 +24,9 @@ public class SopStrategyMetaDTO implements Serializable {
@ApiModelProperty(notes = "触发类型列表") @ApiModelProperty(notes = "触发类型列表")
private List<SopMetaItemDTO> triggers; private List<SopMetaItemDTO> triggers;
@ApiModelProperty(notes = "触发时间类型列表")
private List<SopMetaItemDTO> triggerMetaDates;
@ApiModelProperty(notes = "消息变量列表") @ApiModelProperty(notes = "消息变量列表")
private List<SopMetaItemDTO> messageVariables; private List<SopMetaItemDTO> messageVariables;
......
package cn.com.glsx.dj.strategy.service; package cn.com.glsx.dj.strategy.service;
import cn.com.glsx.dj.strategy.dto.sop.SopEnumValueDTO; import cn.com.glsx.dj.strategy.dto.sop.SopEnumValueDTO;
import cn.hutool.core.collection.CollUtil; import cn.com.glsx.dj.strategy.util.RopHttpUtil;
import cn.com.glsx.scrm.proxy.dto.RopResponse;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -23,8 +27,8 @@ public class SopEnumDataSourceService { ...@@ -23,8 +27,8 @@ public class SopEnumDataSourceService {
private static final String FIXED_DATA_SOURCE = "固定选项"; private static final String FIXED_DATA_SOURCE = "固定选项";
@Autowired(required = false) @Value("${sop.enum.datasource.baseurl:https://dj.dev.glsx.com.cn/rop-dj-strategy/router}")
private SopRopClient sopRopClient; private String baseurl;
/** /**
* 获取枚举值列表 * 获取枚举值列表
...@@ -54,54 +58,40 @@ public class SopEnumDataSourceService { ...@@ -54,54 +58,40 @@ public class SopEnumDataSourceService {
/** /**
* 从ROP接口获取枚举值 * 从ROP接口获取枚举值
* 若ROP客户端未注入或调用失败,使用模拟数据作为降级方案 * 若调用失败,使用模拟数据作为降级方案
* *
* @param ropApiMethod ROP接口方法名 * @param ropApiMethod ROP接口方法名
* @return 枚举值DTO列表 * @return 枚举值DTO列表
*/ */
private List<SopEnumValueDTO> fetchEnumValuesFromRop(String ropApiMethod) { private List<SopEnumValueDTO> fetchEnumValuesFromRop(String ropApiMethod) {
List<SopEnumValueDTO> enumValues = new ArrayList<>();
try { try {
if (sopRopClient == null) { MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
log.warn("SopRopClient未注入,使用模拟数据"); RopResponse ropResponse = RopHttpUtil.post(paramsMap, RopResponse.class, baseurl, ropApiMethod);
return buildMockEnumValues(ropApiMethod); log.info("调用ROP接口获取枚举值, param:{}, ropApiMethod:{}, result:{}", paramsMap, ropApiMethod, ropResponse);
}
Object result = sopRopClient.invoke(ropApiMethod, null); if (ropResponse != null && "0".equals(ropResponse.getCode())) {
return parseRopResponse(result); String data = JSONObject.toJSONString(ropResponse.getData(), SerializerFeature.WriteMapNullValue);
List<Map> dataList = JSONObject.parseArray(data, Map.class);
enumValues = parseRopResponseData(dataList);
}
} catch (Exception e) { } catch (Exception e) {
log.error("调用ROP接口获取枚举值失败, ropApiMethod:{}, error:{}", ropApiMethod, e.getMessage(), e); log.error("调用ROP接口获取枚举值失败, ropApiMethod:{}, error:{}", ropApiMethod, e.getMessage(), e);
return buildMockEnumValues(ropApiMethod); enumValues = buildMockEnumValues(ropApiMethod);
} }
return enumValues;
} }
/** /**
* 解析ROP接口返回的数据 * 解析ROP接口返回的数据列表
* 支持两种返回格式:直接返回列表或返回包含data字段的Map
* *
* @param response ROP接口返回的原始数据 * @param dataList ROP接口返回的数据列表
* @return 枚举值DTO列表 * @return 枚举值DTO列表
*/ */
private List<SopEnumValueDTO> parseRopResponse(Object response) { private List<SopEnumValueDTO> parseRopResponseData(List<Map> dataList) {
List<SopEnumValueDTO> enumValues = new ArrayList<>(); List<SopEnumValueDTO> enumValues = new ArrayList<>();
if (response == null) { if (dataList == null || dataList.isEmpty()) {
return enumValues;
}
List<Map<String, Object>> dataList;
if (response instanceof List) {
dataList = (List<Map<String, Object>>) response;
} else if (response instanceof Map) {
Map<String, Object> responseMap = (Map<String, Object>) response;
Object data = responseMap.get("data");
if (data instanceof List) {
dataList = (List<Map<String, Object>>) data;
} else {
log.warn("ROP接口返回数据格式不支持: {}", JSONUtil.toJsonStr(response));
return enumValues;
}
} else {
log.warn("ROP接口返回数据格式不支持: {}", JSONUtil.toJsonStr(response));
return enumValues; return enumValues;
} }
......
...@@ -12,6 +12,7 @@ import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyCustomerSnapshotMapper; ...@@ -12,6 +12,7 @@ import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyCustomerSnapshotMapper;
import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyExecutionRecordMapper; import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyExecutionRecordMapper;
import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMapper; import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMapper;
import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMetaConditionVariableMapper; import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMetaConditionVariableMapper;
import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMetaDateVariableMapper;
import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMetaEnumValueMapper; import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMetaEnumValueMapper;
import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMetaMarketingStageMapper; import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMetaMarketingStageMapper;
import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMetaMessageVariableMapper; import cn.com.glsx.dj.strategy.mapper.sop.SopStrategyMetaMessageVariableMapper;
...@@ -62,6 +63,7 @@ public class StrategySopQueryService { ...@@ -62,6 +63,7 @@ public class StrategySopQueryService {
private final SopStrategyMetaTriggerMapper metaTriggerMapper; private final SopStrategyMetaTriggerMapper metaTriggerMapper;
private final SopStrategyMetaMessageVariableMapper metaMessageVariableMapper; private final SopStrategyMetaMessageVariableMapper metaMessageVariableMapper;
private final SopEnumDataSourceService sopEnumDataSourceService; private final SopEnumDataSourceService sopEnumDataSourceService;
private final SopStrategyMetaDateVariableMapper sopStrategyMetaDateVariableMapper;
@Autowired @Autowired
private SopStrategyTaskMapper sopStrategyTaskMapper; private SopStrategyTaskMapper sopStrategyTaskMapper;
...@@ -221,6 +223,8 @@ public class StrategySopQueryService { ...@@ -221,6 +223,8 @@ public class StrategySopQueryService {
meta.setConditionVariables(allConditionVariables); meta.setConditionVariables(allConditionVariables);
meta.setStep2ConditionVariables(filterStep2ConditionVariables(allConditionVariables)); meta.setStep2ConditionVariables(filterStep2ConditionVariables(allConditionVariables));
meta.setTriggers(buildTriggers()); meta.setTriggers(buildTriggers());
meta.setTriggerMetaDates(buildMetaDates());
meta.setMessageVariables(buildMessageVariables()); meta.setMessageVariables(buildMessageVariables());
return meta; return meta;
...@@ -413,6 +417,17 @@ public class StrategySopQueryService { ...@@ -413,6 +417,17 @@ public class StrategySopQueryService {
* *
* @return 触发类型元数据项列表 * @return 触发类型元数据项列表
*/ */
private List<SopStrategyMetaDTO.SopMetaItemDTO> buildMetaDates() {
List<SopStrategyMetaDateVariable> sopStrategyMetaDateVariables = sopStrategyMetaDateVariableMapper.selectByStatus(1);
return sopStrategyMetaDateVariables.stream()
.map(trigger -> createMetaItem(trigger.getDateCode(), trigger.getDateName(), 4, trigger.getDateDesc()))
.collect(Collectors.toList());
}
/**
* 构建触发类型列表
*
* @return 触发类型元数据项列表
*/
private List<SopStrategyMetaDTO.SopMetaItemDTO> buildTriggers() { private List<SopStrategyMetaDTO.SopMetaItemDTO> buildTriggers() {
List<SopStrategyMetaTrigger> triggers = metaTriggerMapper.selectByStatus(1); List<SopStrategyMetaTrigger> triggers = metaTriggerMapper.selectByStatus(1);
return triggers.stream() return triggers.stream()
...@@ -732,10 +747,7 @@ public class StrategySopQueryService { ...@@ -732,10 +747,7 @@ public class StrategySopQueryService {
private String getBizTypeName(Integer bizType) { private String getBizTypeName(Integer bizType) {
if (bizType == null) return ""; if (bizType == null) return "";
switch (bizType) { switch (bizType) {
case 1: return "试驾"; case 1: return "轻改";
case 2: return "售后";
case 3: return "续保";
case 4: return "金融";
default: return ""; default: return "";
} }
} }
......
package cn.com.glsx.dj.strategy.util;
import java.net.URI;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilder;
/**
* @author zhouhaibao
* @version 1.0
* @description: http调用rop端服务
* @date 2021/7/30 11:26
*/
public class RopHttpUtil {
private final static String appKey = "48e5e13229b82c1b4e6e8c96151f0637";
private final static String secret = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b";
private static void addBaseParam(MultiValueMap<String, String> paramsMap, String method) {
paramsMap.add("v", "1.0.0");
paramsMap.add("method", method);
paramsMap.add("name", "zhouhb");
}
private static void addBaseParamE(MultiValueMap<String, String> paramsMap, String method) {
paramsMap.add("v", "1.0.0");
paramsMap.add("method", method);
}
public static <T> T get(MultiValueMap<String, String> paramsMap, Class<T> responseType, String baseurl, String method) throws Exception {
addBaseParam(paramsMap, method);
MultiValueMap<String, String> params = getSysParams();
params.putAll(paramsMap);
for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
params.remove(entry.getKey());
}
}
String sign = RopUtils.sign(params.toSingleValueMap(), secret);
params.add("sign", sign);
RestTemplate rest = new RestTemplate();
RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler(baseurl);
uriTemplateHandler.setDefaultUriVariables(params.toSingleValueMap());
rest.setUriTemplateHandler(uriTemplateHandler);
T forObject = rest.getForObject(baseurl, responseType, params.toSingleValueMap());
return forObject;
}
public static <T> T post(MultiValueMap<String, String> paramsMap, Class<T> responseType, String baseurl, String method) throws Exception {
addBaseParam(paramsMap, method);
MultiValueMap<String, String> params = getSysParams();
params.putAll(paramsMap);
for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
params.remove(entry.getKey());
}
}
String sign = RopUtils.sign(params.toSingleValueMap(), secret);
params.add("sign", sign);
return new RestTemplate().postForObject(baseurl, params, responseType);
}
public static MultiValueMap<String, String> getSysParams() {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(16);
params.add("appKey", appKey);
params.add("format", "json");
return params;
}
public static MultiValueMap<String, String> getSysParamsE() {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(16);
params.add("app_key", appKey);
params.add("format", "json");
params.add("timestamp", System.currentTimeMillis() + "");
return params;
}
static class RopUriTemplateHandler extends DefaultUriBuilderFactory {
public RopUriTemplateHandler(String baseUriTemplate) {
super(baseUriTemplate);
}
@Override
public URI expand(String uriTemplate, Map<String, ?> uriVars) {
UriBuilder builder = uriString(uriTemplate);
if (uriVars != null && !uriVars.isEmpty()) {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
for (Entry<String, ?> entry : uriVars.entrySet()) {
params.add(entry.getKey(), entry.getValue().toString());
}
builder.queryParams(params);
}
return builder.build(uriVars);
}
}
}
package cn.com.glsx.dj.strategy.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RopUtils {
private static final Logger logger = LoggerFactory.getLogger(RopUtils.class);
public RopUtils() {
}
public static String sign(Map<String, String> paramValues, String secret) {
return sign(paramValues, (List) null, secret);
}
public static String sign(Map<String, String> paramValues, List<String> ignoreParamNames, String secret) {
try {
StringBuilder sb = new StringBuilder();
List<String> paramNames = new ArrayList(paramValues.size());
paramNames.addAll(paramValues.keySet());
Iterator var5;
String paramName;
if (ignoreParamNames != null && ignoreParamNames.size() > 0) {
var5 = ignoreParamNames.iterator();
while (var5.hasNext()) {
paramName = (String) var5.next();
paramNames.remove(paramName);
}
}
Collections.sort(paramNames);
sb.append(secret);
var5 = paramNames.iterator();
while (var5.hasNext()) {
paramName = (String) var5.next();
sb.append(paramName).append((String) paramValues.get(paramName));
}
sb.append(secret);
byte[] sha1Digest = getSHA1Digest(sb.toString());
return byte2hex(sha1Digest);
} catch (IOException var7) {
logger.info("异常信息", var7);
return null;
}
}
public static String utf8Encoding(String value, String sourceCharsetName) {
try {
return new String(value.getBytes(sourceCharsetName), "UTF-8");
} catch (UnsupportedEncodingException var3) {
throw new IllegalArgumentException(var3);
}
}
private static byte[] getSHA1Digest(String data) throws IOException {
Object var1 = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] bytes = md.digest(data.getBytes("UTF-8"));
return bytes;
} catch (GeneralSecurityException var3) {
throw new IOException(var3);
}
}
private static byte[] getMD5Digest(String data) throws IOException {
Object var1 = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(data.getBytes("UTF-8"));
return bytes;
} catch (GeneralSecurityException var3) {
throw new IOException(var3);
}
}
private static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; ++i) {
String hex = Integer.toHexString(bytes[i] & 255);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
return sign.toString();
}
public static String getUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString().toUpperCase();
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!