新增顺丰同城的业务代码
This commit is contained in:
parent
ca349af446
commit
8336994c30
@ -0,0 +1,4 @@
|
|||||||
|
package com.suisung.mall.shop.api.service;
|
||||||
|
|
||||||
|
public interface SFExpressApiService {
|
||||||
|
}
|
||||||
@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||||
|
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
|
||||||
|
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
|
||||||
|
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
|
||||||
|
* Vestibulum commodo. Ut rhoncus gravida arcu.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.suisung.mall.shop.api.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.suisung.mall.shop.api.service.SFExpressApiService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class SFExpressApiServiceImpl implements SFExpressApiService {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(SFExpressApiServiceImpl.class);
|
||||||
|
|
||||||
|
@Value("${sf-express.appid}")
|
||||||
|
private Long appId;
|
||||||
|
@Value("${sf-express.appkey}")
|
||||||
|
private String appKey;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成顺丰同城请求签名,参考官网:https://commit-openic.sf-express.com/#/apidoc
|
||||||
|
*
|
||||||
|
* @param postData
|
||||||
|
* @param appId
|
||||||
|
* @param appKey
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String generateOpenSign(String postData, Long appId, String appKey) {
|
||||||
|
try {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append(postData);
|
||||||
|
sb.append("&" + appId + "&" + appKey);
|
||||||
|
|
||||||
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
|
byte[] md5 = md.digest(sb.toString().getBytes("utf-8"));
|
||||||
|
int i;
|
||||||
|
StringBuffer buf = new StringBuffer("");
|
||||||
|
for (byte b : md5) {
|
||||||
|
i = b;
|
||||||
|
if (i < 0) {
|
||||||
|
i += 256;
|
||||||
|
}
|
||||||
|
if (i < 16) {
|
||||||
|
buf.append("0");
|
||||||
|
}
|
||||||
|
buf.append(Integer.toHexString(i));
|
||||||
|
}
|
||||||
|
return buf.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证签名是否一致
|
||||||
|
*
|
||||||
|
* @param sign
|
||||||
|
* @param postData
|
||||||
|
* @param appId
|
||||||
|
* @param appKey
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean checkOpenSign(String sign, String postData, Long appId, String appKey) {
|
||||||
|
if (StrUtil.isBlank(sign) || StrUtil.isBlank(postData) || StrUtil.isBlank(appKey) || appId == null || appId <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String newSign = generateOpenSign(postData, appId, appKey);
|
||||||
|
if (sign.equals(newSign)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user