|
|
@@ -46,22 +46,24 @@ public class CmccUtil {
|
|
|
|
|
|
/**
|
|
|
* 生成签名
|
|
|
+ *
|
|
|
* @param values
|
|
|
* @return
|
|
|
*/
|
|
|
- private static String generate(List<String> values){
|
|
|
- if(ObjectUtils.isEmpty(values)){
|
|
|
+ private static String generate(List<String> values) {
|
|
|
+ if (ObjectUtils.isEmpty(values)) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
//删除null值
|
|
|
- while(values.remove(null)){}
|
|
|
+ while (values.remove(null)) {
|
|
|
+ }
|
|
|
|
|
|
//排序、拼接
|
|
|
Collections.sort(values);
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
for (String value : values) {
|
|
|
- if(value!=null && StringUtils.isNotEmpty(value)){
|
|
|
+ if (value != null && StringUtils.isNotEmpty(value)) {
|
|
|
sb.append(value);
|
|
|
}
|
|
|
}
|
|
|
@@ -70,53 +72,51 @@ public class CmccUtil {
|
|
|
|
|
|
/**
|
|
|
* 签名
|
|
|
+ *
|
|
|
* @param cmccRequest 请求参数
|
|
|
*/
|
|
|
- private static <T extends CmccRequest> T sign(T cmccRequest){
|
|
|
+ private static <T extends CmccRequest> T sign(T cmccRequest) {
|
|
|
//转化为参数
|
|
|
Map<String, Object> stringObjectMap = BeanUtil.toMap(cmccRequest);
|
|
|
//参数集合
|
|
|
Set<String> param = new TreeSet<>();
|
|
|
for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
|
|
|
//排除空值和请求号
|
|
|
- if(ObjectUtils.isNotEmpty(stringObjectEntry.getValue())){
|
|
|
- if(stringObjectEntry.getValue() instanceof String){
|
|
|
- param.add((String) stringObjectEntry.getValue());
|
|
|
- }else if(stringObjectEntry.getValue() instanceof List){
|
|
|
- param.addAll((Collection<? extends String>) stringObjectEntry.getValue());
|
|
|
- }
|
|
|
+ if (ObjectUtils.isNotEmpty(stringObjectEntry.getValue())) {
|
|
|
+ param.add((String) stringObjectEntry.getValue());
|
|
|
}
|
|
|
}
|
|
|
//添加SignKey
|
|
|
param.add(cmccPointConfig.getSignKey());
|
|
|
- String paramValue = ArrayUtil.join(param.toArray(),"");
|
|
|
- log.warn("CMCC-排序后参数:{}",paramValue);
|
|
|
+ String paramValue = ArrayUtil.join(param.toArray(), "");
|
|
|
+ log.warn("CMCC-排序后参数:{}", paramValue);
|
|
|
//进行签名
|
|
|
cmccRequest.setSign(SecureUtil.md5(paramValue));
|
|
|
// cmccRequest.setCheckType("01");
|
|
|
// cmccRequest.setSign(SecureUtil.md5(cmccRequest.getRequestId()+cmccPointConfig.getSignKey()));
|
|
|
- log.warn("CMCC-签名后参数:{}",JSONObject.toJSONString(cmccRequest));
|
|
|
+ log.warn("CMCC-签名后参数:{}", JSONObject.toJSONString(cmccRequest));
|
|
|
return cmccRequest;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加参数
|
|
|
+ *
|
|
|
* @param cmccRequest
|
|
|
* @param callback
|
|
|
*/
|
|
|
- private static <T extends CmccRequest> T checkParam(T cmccRequest,boolean callback){
|
|
|
+ private static <T extends CmccRequest> T checkParam(T cmccRequest, boolean callback) {
|
|
|
cmccRequest.setPartnerId(cmccPointConfig.getPartnerId());
|
|
|
cmccRequest.setStoreId(cmccPointConfig.getStoreId());
|
|
|
//判断reuqestId是否为空
|
|
|
- if(StringUtils.isEmpty(cmccRequest.getRequestId())){
|
|
|
+ if (StringUtils.isEmpty(cmccRequest.getRequestId())) {
|
|
|
cmccRequest.setRequestId(IdUtil.randomUUID());
|
|
|
}
|
|
|
//判断reqTime是否为空
|
|
|
- if(StringUtils.isEmpty(cmccRequest.getReqTime())){
|
|
|
+ if (StringUtils.isEmpty(cmccRequest.getReqTime())) {
|
|
|
cmccRequest.setReqTime(dateTimeFormat.format(DateUtil.now()));
|
|
|
}
|
|
|
//设置回调地址
|
|
|
- if(callback){
|
|
|
+ if (callback) {
|
|
|
cmccRequest.setCallbackUrl(cmccPointConfig.getCallbackUrl());
|
|
|
}
|
|
|
return cmccRequest;
|
|
|
@@ -124,78 +124,84 @@ public class CmccUtil {
|
|
|
|
|
|
/**
|
|
|
* 查询移动积分
|
|
|
+ *
|
|
|
* @param cmccRequest
|
|
|
* @return
|
|
|
*/
|
|
|
public static CmccResponse queryCmccBalance(CmccRequest cmccRequest) throws CmccRequestException {
|
|
|
- if(ObjectUtils.isEmpty(cmccRequest) || StringUtils.isEmpty(cmccRequest.getMobile())){
|
|
|
+ if (ObjectUtils.isEmpty(cmccRequest) || StringUtils.isEmpty(cmccRequest.getMobile())) {
|
|
|
throw new CmccRequestException("参数不能为空");
|
|
|
}
|
|
|
- return sendCmccRequest(cmccRequest,"/api/kb/queryCmccBalance",true);
|
|
|
+ return sendCmccRequest(cmccRequest, "/api/kb/queryCmccBalance", true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 移动积分订单创建
|
|
|
+ *
|
|
|
* @param cmccPlaceOrderRequest
|
|
|
* @return
|
|
|
*/
|
|
|
public static CmccResponse placeOrder(CmccPlaceOrderRequest cmccPlaceOrderRequest) throws CmccRequestException {
|
|
|
- if(ObjectUtils.isEmpty(cmccPlaceOrderRequest) || StringUtils.isEmpty(cmccPlaceOrderRequest.getMobile())
|
|
|
+ if (ObjectUtils.isEmpty(cmccPlaceOrderRequest) || StringUtils.isEmpty(cmccPlaceOrderRequest.getMobile())
|
|
|
|| StringUtils.isEmpty(cmccPlaceOrderRequest.getOutOrderId()) || StringUtils.isEmpty(cmccPlaceOrderRequest.getPoints())
|
|
|
|| StringUtils.isEmpty(cmccPlaceOrderRequest.getSessionId()) || StringUtils.isEmpty(cmccPlaceOrderRequest.getFingerprint())
|
|
|
- || ObjectUtils.isEmpty(cmccPlaceOrderRequest.getProductItemList()) || StringUtils.isEmpty(cmccPlaceOrderRequest.getProductId())
|
|
|
- || StringUtils.isEmpty(cmccPlaceOrderRequest.getNum())){
|
|
|
+ || ObjectUtils.isEmpty(cmccPlaceOrderRequest.getProductItemList()) || ObjectUtils.isEmpty(cmccPlaceOrderRequest.getProductItemList())) {
|
|
|
throw new CmccRequestException("参数不能为空");
|
|
|
}
|
|
|
- return sendCmccRequest(cmccPlaceOrderRequest, "/api/kb/placeOrder",false);
|
|
|
+ return sendCmccRequest(cmccPlaceOrderRequest, "/api/kb/placeOrder", false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 移动发送短信
|
|
|
+ *
|
|
|
* @param cmccPlaceOrderRequest
|
|
|
* @return
|
|
|
*/
|
|
|
public static CmccResponse sendCmccSms(CmccPlaceOrderRequest cmccPlaceOrderRequest) throws CmccRequestException {
|
|
|
- if(ObjectUtils.isEmpty(cmccPlaceOrderRequest) || StringUtils.isEmpty(cmccPlaceOrderRequest.getMobile())
|
|
|
- || StringUtils.isEmpty(cmccPlaceOrderRequest.getOutOrderId())){
|
|
|
+ if (ObjectUtils.isEmpty(cmccPlaceOrderRequest) || StringUtils.isEmpty(cmccPlaceOrderRequest.getMobile())
|
|
|
+ || StringUtils.isEmpty(cmccPlaceOrderRequest.getOutOrderId())) {
|
|
|
throw new CmccRequestException("参数不能为空");
|
|
|
}
|
|
|
//设置请求
|
|
|
- return sendCmccRequest(cmccPlaceOrderRequest, "/api/kb/sendCmccSms",false);
|
|
|
+ return sendCmccRequest(cmccPlaceOrderRequest, "/api/kb/sendCmccSms", false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 订单支付接口
|
|
|
+ *
|
|
|
* @param cmccDectOrderRequest
|
|
|
* @return
|
|
|
*/
|
|
|
public static CmccResponse dectOrder(CmccDectOrderRequest cmccDectOrderRequest) throws CmccRequestException {
|
|
|
- if(ObjectUtils.isEmpty(cmccDectOrderRequest) || StringUtils.isEmpty(cmccDectOrderRequest.getMobile())
|
|
|
+ if (ObjectUtils.isEmpty(cmccDectOrderRequest) || StringUtils.isEmpty(cmccDectOrderRequest.getMobile())
|
|
|
|| StringUtils.isEmpty(cmccDectOrderRequest.getOutOrderId()) || StringUtils.isEmpty(cmccDectOrderRequest.getSmsCode())
|
|
|
|| StringUtils.isEmpty(cmccDectOrderRequest.getSessionId()) || StringUtils.isEmpty(cmccDectOrderRequest.getFingerprint())
|
|
|
- || StringUtils.isEmpty(cmccDectOrderRequest.getMachinetype())){
|
|
|
+ || StringUtils.isEmpty(cmccDectOrderRequest.getMachinetype())) {
|
|
|
throw new CmccRequestException("参数不能为空");
|
|
|
}
|
|
|
- return sendCmccRequest(cmccDectOrderRequest,"/api/kb/dectOrder",false);
|
|
|
+ return sendCmccRequest(cmccDectOrderRequest, "/api/kb/dectOrder", false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送CMCC请求
|
|
|
+ *
|
|
|
* @param cmccRequest 请求
|
|
|
- * @param url 地址
|
|
|
- * @param callback 是否需要回调
|
|
|
+ * @param url 地址
|
|
|
+ * @param callback 是否需要回调
|
|
|
* @return
|
|
|
* @throws CmccRequestException
|
|
|
*/
|
|
|
- private static CmccResponse sendCmccRequest(CmccRequest cmccRequest, String url,boolean callback) throws CmccRequestException {
|
|
|
+ private static CmccResponse sendCmccRequest(CmccRequest cmccRequest, String url, boolean callback) throws CmccRequestException {
|
|
|
+ //进行签名
|
|
|
+ sign(checkParam(cmccRequest, callback));
|
|
|
//设置请求
|
|
|
HttpResponse httpResponse = HttpRequest.post(new StringBuilder(cmccPointConfig.getUrl()).append(url).toString())
|
|
|
//添加参数,进行签名
|
|
|
- .body(JSONObject.toJSONString(sign(checkParam(cmccRequest, callback)))).execute();
|
|
|
- log.info("CMCC-请求响应内容:{}",httpResponse.body());
|
|
|
+ .body(JSONObject.toJSONString(cmccRequest)).execute();
|
|
|
+ log.info("CMCC-请求响应内容:{}", httpResponse.body());
|
|
|
JSONObject jsonObject = JSONObject.parseObject(httpResponse.body());
|
|
|
CmccResponse cmccResponse;
|
|
|
- if(ObjectUtils.isNotEmpty(jsonObject.get("respCode"))){
|
|
|
+ if (ObjectUtils.isNotEmpty(jsonObject.get("respCode"))) {
|
|
|
cmccResponse = CmccResponse
|
|
|
.builder()
|
|
|
.resultCode(jsonObject.getString("respCode"))
|
|
|
@@ -203,12 +209,12 @@ public class CmccUtil {
|
|
|
.requestId(jsonObject.getString("requestId"))
|
|
|
.data(jsonObject.getString("data"))
|
|
|
.build();
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
cmccResponse = jsonObject.toJavaObject(CmccResponse.class);
|
|
|
}
|
|
|
//判断状态是否成功
|
|
|
if (!StringUtils.equals(cmccResponse.getResultCode(), CmccResponseStatus.SUCCESS.getCode())
|
|
|
- && (!callback || !StringUtils.equals(cmccResponse.getResultCode(),CmccResponseStatus.AUTH.getCode()))) {
|
|
|
+ && (!callback || !StringUtils.equals(cmccResponse.getResultCode(), CmccResponseStatus.AUTH.getCode()))) {
|
|
|
throw new CmccRequestException(cmccResponse.getMessage());
|
|
|
}
|
|
|
return cmccResponse;
|