Commit 709a5f6c by 林冲

feat(stoassistant): add live code API for AI car purchase report

- Add LiveCodeController with ROP endpoint for retrieving group live codes
- Implement LiveCodeService to call gsc-vcp-web-facade via SpringCloudHttpUtil
- Add GSCServicePoolConfig for service pool configuration management
- Add GSCServiceConstants for centralized service name definitions
- Add LiveCodeRequest/Response DTOs for API contract
- Configure .gitignore for Maven and IntelliJ artifacts
1 parent f227fb5d
# IntelliJ / IDEA
*.iml
.idea/
out/
# Maven 构建物
target/
# Maven Flatten 插件的产物
.flattened-pom.xml
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.stoassistant.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* GSC 服务池配置
*
* @author system
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "gsc.common")
public class GSCServicePoolConfig {
/**
* GSC 服务池配置
* key: 服务名称(如 gsc-vcp-web-facade)
* value: 服务配置参数
*/
private Map<String, GSCServiceParam> gscPool = new HashMap<>();
/**
* 获取指定服务的配置
*
* @param serviceName 服务名称
* @return 服务配置参数
*/
public GSCServiceParam getServiceParam(String serviceName) {
GSCServiceParam param = gscPool.get(serviceName);
if (param == null) {
throw new IllegalArgumentException("未找到服务配置:" + serviceName);
}
return param;
}
/**
* GSC 服务配置参数
*/
@Data
public static class GSCServiceParam {
/**
* 服务基础URL
*/
private String baseurl;
/**
* 应用ID
*/
private String appId;
/**
* 随机字符串
*/
private String nonce;
/**
* 密钥
*/
private String secret;
}
}
package cn.com.glsx.stoassistant.constans;
/**
* GSC 服务名称常量
*
* @author system
*/
public class GSCServiceConstants {
/**
* GSC VCP Web Facade 服务
*/
public static final String GSC_VCP_WEB_FACADE = "gsc-vcp-web-facade";
/**
* GSC VCP Pad Facade 服务
*/
public static final String GSC_VCP_PAD_FACADE = "gsc-vcp-pad-facade";
private GSCServiceConstants() {
// 私有构造函数,防止实例化
}
}
package cn.com.glsx.stoassistant.controller;
import cn.com.glsx.framework.rop.response.RopResponse;
import cn.com.glsx.stoassistant.request.LiveCodeRequest;
import cn.com.glsx.stoassistant.response.LiveCodeResponse;
import cn.com.glsx.stoassistant.service.LiveCodeService;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 活码管理
*/
@Slf4j
@Api("活码管理")
@ServiceMethodBean(version = "1.0.0", needInSession = NeedInSessionType.YES)
public class LiveCodeController {
@Autowired
private LiveCodeService liveCodeService;
/**
* 获取群活码(车主扫码分配真实企微群)
*/
@ServiceMethod(method = "scrm.api.stoassistant.livecode.get", needInSession = NeedInSessionType.YES)
@ApiOperation("获取群活码")
public RopResponse<LiveCodeResponse> getLiveCode(LiveCodeRequest request) {
try {
if (request == null || request.getReportUuidId() == null || request.getReportUuidId().trim().isEmpty()) {
return RopResponse.error("AI收车报告唯一ID不能为空");
}
LiveCodeResponse response = liveCodeService.getLiveCode(request);
return RopResponse.success(response);
} catch (Exception e) {
log.error("获取群活码失败", e);
return RopResponse.error(e.getMessage());
}
}
}
package cn.com.glsx.stoassistant.request;
import com.rop.AbstractRopRequest;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* 获取群活码请求
*/
@Data
@ApiModel("获取群活码请求")
public class LiveCodeRequest extends AbstractRopRequest implements Serializable {
private static final long serialVersionUID = 1L;
/**
* AI收车报告唯一ID
*/
@ApiModelProperty(value = "AI收车报告唯一ID", required = true)
private String reportUuidId;
}
package cn.com.glsx.stoassistant.response;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* 获取群活码响应
*/
@Data
@ApiModel("获取群活码响应")
public class LiveCodeResponse implements Serializable {
private static final long serialVersionUID = 1L;
/**
* H5引流链接
*/
@ApiModelProperty("H5引流链接")
private String h5Url;
/**
* 二维码图片URL
*/
@ApiModelProperty("二维码图片URL")
private String qrCodeUrl;
}
package cn.com.glsx.stoassistant.service;
import cn.com.glsx.stoassistant.config.GSCServicePoolConfig;
import cn.com.glsx.stoassistant.constans.GSCServiceConstants;
import cn.com.glsx.stoassistant.request.LiveCodeRequest;
import cn.com.glsx.stoassistant.response.LiveCodeResponse;
import cn.com.glsx.stoassistant.utils.SpringCloudHttpUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* 活码服务
*/
@Slf4j
@Service
public class LiveCodeService {
@Autowired
private GSCServicePoolConfig gscServicePoolConfig;
/**
* 获取群活码
*/
public LiveCodeResponse getLiveCode(LiveCodeRequest request) {
// 默认调用 gsc-vcp-pad-facade 服务
return getLiveCode(request, GSCServiceConstants.GSC_VCP_PAD_FACADE);
}
/**
* 获取群活码(支持指定服务)
*
* @param request 请求参数
* @param serviceName 服务名称(如 gsc-vcp-pad-facade、gsc-vcp-web-facade)
* @return 活码响应
*/
public LiveCodeResponse getLiveCode(LiveCodeRequest request, String serviceName) {
try {
// 获取指定服务的配置
GSCServicePoolConfig.GSCServiceParam serviceParam = gscServicePoolConfig.getServiceParam(serviceName);
// 构建请求参数
Map<String, Object> params = new HashMap<>();
params.put("reportUuidId", request.getReportUuidId());
log.info("调用服务:{},请求参数:{}", serviceName, params);
// 调用指定服务的接口
String result = SpringCloudHttpUtil.post(
serviceParam,
params,
String.class,
"/liveCode/generate",
null
);
log.info("调用服务:{} 返回结果:{}", serviceName, result);
// 解析返回结果
Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
if (resultMap == null) {
throw new RuntimeException("调用服务失败:返回结果为空");
}
Integer code = (Integer) resultMap.get("code");
if (code == null || code != 0) {
String msg = (String) resultMap.get("msg");
throw new RuntimeException("调用服务失败:" + msg);
}
Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
if (data == null) {
throw new RuntimeException("调用服务失败:data为空");
}
LiveCodeResponse response = new LiveCodeResponse();
response.setH5Url((String) data.get("h5Url"));
response.setQrCodeUrl((String) data.get("qrCodeUrl"));
return response;
} catch (Exception e) {
log.error("获取群活码失败,服务:{}", serviceName, e);
throw new RuntimeException("获取群活码失败:" + e.getMessage(), e);
}
}
}
package cn.com.glsx.stoassistant.utils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
/**
* 签名工具类
*/
public class SignUtil {
private static final String HMAC_SHA256_ALGORITHM = "HmacSHA256";
private static final String SIGN_KEY = "X-Sign";
private static final String SECRET_PARAM = "secret=";
/**
* 生成签名
*/
public static String generateSign(Map<String, Object> params, String secret) {
// 1. 参数排序
List<String> keys = new ArrayList<>(params.keySet());
Collections.sort(keys);
// 2. 拼接参数
StringBuilder sb = new StringBuilder();
for (String key : keys) {
if (SIGN_KEY.equals(key) || params.get(key) == null || "".equals(params.get(key))) {
continue;
}
sb.append(key).append("=").append(params.get(key)).append("&");
}
String stringToSign = sb.toString();
stringToSign = stringToSign + SECRET_PARAM + secret;
// 3. 计算HMAC-SHA256签名
return calculateHmacSha256(secret, stringToSign);
}
/**
* 计算 HMAC-SHA256 签名
*/
private static String calculateHmacSha256(String secret, String data) {
try {
Mac sha256_HMAC = Mac.getInstance(HMAC_SHA256_ALGORITHM);
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), HMAC_SHA256_ALGORITHM);
sha256_HMAC.init(secretKey);
byte[] hashBytes = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
return bytesToHex(hashBytes);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("HMAC-SHA256 algorithm not available", e);
} catch (InvalidKeyException e) {
throw new RuntimeException("Invalid secret key", e);
}
}
/**
* 将字节数组转换为16进制字符串
*/
private static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}
package cn.com.glsx.stoassistant.utils;
import cn.com.glsx.stoassistant.config.GSCServicePoolConfig;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/**
* Spring Cloud HTTP 工具类
*/
@Slf4j
public class SpringCloudHttpUtil {
private final static RestTemplate restTemplate = new RestTemplate();
private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* POST 请求
*/
public static <T> T post(GSCServicePoolConfig.GSCServiceParam serviceParam, Object obj, Class<T> responseType, String method, String sessionId) throws Exception {
long timestamp = System.currentTimeMillis();
if (sessionId != null) {
method = method + "?sessionId=" + sessionId;
}
String url = serviceParam.getBaseurl() + method;
// 创建请求头
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", "application/json");
httpHeaders.add("X-App-Id", serviceParam.getAppId());
httpHeaders.add("X-Timestamp", timestamp + "");
httpHeaders.add("X-Nonce", serviceParam.getNonce());
// 构建参数
Map<String, Object> params = new HashMap<>();
params.put("X-App-Id", serviceParam.getAppId());
params.put("X-Timestamp", timestamp);
params.put("X-Nonce", serviceParam.getNonce());
Map<String, Object> objectMap = getOuterKeyValues(obj);
params.putAll(objectMap);
if (sessionId != null) {
params.put("sessionId", sessionId);
}
// 生成签名
String sign = SignUtil.generateSign(params, serviceParam.getSecret());
httpHeaders.add("X-Sign", sign);
HttpEntity<Object> httpEntity = new HttpEntity<>(objectMap, httpHeaders);
// 远程调用
log.info("请求地址:{},请求参数:{},请求头:{}", url, JSON.toJSON(obj), httpHeaders);
T result = restTemplate.postForObject(url, httpEntity, responseType);
log.info("返回结果:{}", JSON.toJSON(result));
return result;
}
/**
* 获取对象的外层键值对
*/
public static Map<String, Object> getOuterKeyValues(Object obj) {
try {
JsonNode jsonNode = objectMapper.valueToTree(obj);
Map<String, Object> result = new HashMap<>();
jsonNode.fields().forEachRemaining(entry -> {
// 非空才封装
if (entry.getValue().isNull() || entry.getValue().isMissingNode()) {
return;
}
result.put(entry.getKey(), entry.getValue());
});
return result;
} catch (Exception e) {
throw new RuntimeException("JSON转换失败", e);
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!