feat:封装正元工具类
This commit is contained in:
1 parent
cd99c104c3
commit
d2e62d2fbf
4 files changed
+107
-77
No files matched your search
-5
@@ -8,11 +8,6 @@ import com.bsd.pjcloud.thirdparty.vo.zy.mer.*;
|
||||
|
||||
public interface ZyMerService {
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
*/
|
||||
ZyMerGetTokenVo getTokenApi();
|
||||
|
||||
/**
|
||||
* 商户信息同步
|
||||
*/
|
||||
|
||||
+14
-70
@@ -12,6 +12,7 @@ import com.bsd.pjcloud.thirdparty.dto.zy.mer.ZyMerSyncDealerInfoDto;
|
||||
import com.bsd.pjcloud.thirdparty.dto.zy.mer.ZyMerSyncEnterpriseInfoDto;
|
||||
import com.bsd.pjcloud.thirdparty.service.impl.ZyMerService;
|
||||
import com.bsd.pjcloud.thirdparty.utils.zy.api.ZyApi;
|
||||
import com.bsd.pjcloud.thirdparty.utils.zy.api.ZyApiTool;
|
||||
import com.bsd.pjcloud.thirdparty.vo.zy.mer.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -36,35 +37,7 @@ public class ZyMerServiceImpl implements ZyMerService {
|
||||
|
||||
private final ZyConfig zyConfig;
|
||||
|
||||
private final StringRedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public ZyMerGetTokenVo getTokenApi() {
|
||||
String url = zyConfig.getBaseUrl() + ZyApi.Mer.GET_TOKEN;
|
||||
url = String.format(url, zyConfig.getAppId(), zyConfig.getAppSecret());
|
||||
try {
|
||||
String res = HttpRequest.get(url)
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.execute()
|
||||
.body();
|
||||
if (ObjectUtil.isEmpty(res)) {
|
||||
throw new RuntimeException("response is empty");
|
||||
}
|
||||
ZyMerGetTokenVo vo = JSONUtil.toBean(res, ZyMerGetTokenVo.class);
|
||||
if (!vo.isSuccess()) {
|
||||
throw new RuntimeException(String.format("请求错误res|%s|url|%s|", res, url));
|
||||
}
|
||||
return vo;
|
||||
} catch (Exception e) {
|
||||
log.error("|{}||getTokenApi|意外的错误error", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private final ZyApiTool zyApiTool;
|
||||
|
||||
/**
|
||||
* 商户信息同步
|
||||
@@ -73,10 +46,10 @@ public class ZyMerServiceImpl implements ZyMerService {
|
||||
public ZyMerSyncDealerInfoVo syncDealerInfoApi(ZyMerSyncDealerInfoDto dto) {
|
||||
String url = zyConfig.getBaseUrl() + ZyApi.Mer.SYNC_DEALER_INFO;
|
||||
try {
|
||||
url = String.format(url, getToken());
|
||||
url = String.format(url, zyApiTool.getToken());
|
||||
Map<String, Object> encryptionBody = new HashMap<>();
|
||||
encryptionBody.put("dealers", dto);
|
||||
String body = getEncryptionDto(encryptionBody);
|
||||
String body = zyApiTool.getEncryptionDto(encryptionBody);
|
||||
String res = HttpRequest.post(url)
|
||||
.body(body)
|
||||
.execute()
|
||||
@@ -90,7 +63,7 @@ public class ZyMerServiceImpl implements ZyMerService {
|
||||
}
|
||||
return vo;
|
||||
} catch (Exception e) {
|
||||
log.error("|{}||syncDealerInfo|意外的错误error", e.getMessage());
|
||||
log.error("|{}||syncDealerInfo|意外的错误error|{}|", TAG, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -102,10 +75,10 @@ public class ZyMerServiceImpl implements ZyMerService {
|
||||
public ZyMerSyncEnterpriseInfoVo syncEnterpriseInfoApi(ZyMerSyncEnterpriseInfoDto dto) {
|
||||
String url = zyConfig.getBaseUrl() + ZyApi.Mer.SYNC_ENTERPRISE_INFO;
|
||||
try {
|
||||
url = String.format(url, getToken());
|
||||
url = String.format(url, zyApiTool.getToken());
|
||||
Map<String, Object> encryptionBody = new HashMap<>();
|
||||
encryptionBody.put("enterprises", dto);
|
||||
String body = getEncryptionDto(encryptionBody);
|
||||
String body = zyApiTool.getEncryptionDto(encryptionBody);
|
||||
String res = HttpRequest.post(url)
|
||||
.body(body)
|
||||
.execute()
|
||||
@@ -119,7 +92,7 @@ public class ZyMerServiceImpl implements ZyMerService {
|
||||
}
|
||||
return vo;
|
||||
} catch (Exception e) {
|
||||
log.error("|{}||syncEnterpriseInfo|意外的错误error", e.getMessage());
|
||||
log.error("|{}||syncEnterpriseInfo|意外的错误error|{}|", TAG, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -131,10 +104,10 @@ public class ZyMerServiceImpl implements ZyMerService {
|
||||
public ZyMerSyncAccountInfoVo syncAccountInfoApi(ZyMerSyncAccountInfoDto dto) {
|
||||
String url = zyConfig.getBaseUrl() + ZyApi.Mer.SYNC_ACCOUNT_INFO;
|
||||
try {
|
||||
url = String.format(url, getToken());
|
||||
url = String.format(url, zyApiTool.getToken());
|
||||
Map<String, Object> encryptionBody = new HashMap<>();
|
||||
encryptionBody.put("accounts", dto);
|
||||
String body = getEncryptionDto(encryptionBody);
|
||||
String body = zyApiTool.getEncryptionDto(encryptionBody);
|
||||
String res = HttpRequest.post(url)
|
||||
.body(body)
|
||||
.execute()
|
||||
@@ -148,7 +121,7 @@ public class ZyMerServiceImpl implements ZyMerService {
|
||||
}
|
||||
return vo;
|
||||
} catch (Exception e) {
|
||||
log.error("|{}||syncAccountInfo|意外的错误error", e.getMessage());
|
||||
log.error("|{}||syncAccountInfo|意外的错误error|{}|", TAG, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -160,10 +133,10 @@ public class ZyMerServiceImpl implements ZyMerService {
|
||||
public ZyMerSyncAccClassInfoVo syncAccClassInfo(ZyMerSyncAccClassInfoDto dto) {
|
||||
String url = zyConfig.getBaseUrl() + ZyApi.Mer.SYNC_ACC_CLASS_INFO;
|
||||
try {
|
||||
url = String.format(url, getToken());
|
||||
url = String.format(url, zyApiTool.getToken());
|
||||
Map<String, Object> encryptionBody = new HashMap<>();
|
||||
encryptionBody.put("accClasses", dto);
|
||||
String body = getEncryptionDto(encryptionBody);
|
||||
String body = zyApiTool.getEncryptionDto(encryptionBody);
|
||||
String res = HttpRequest.post(url)
|
||||
.body(body)
|
||||
.execute()
|
||||
@@ -176,39 +149,10 @@ public class ZyMerServiceImpl implements ZyMerService {
|
||||
throw new RuntimeException(String.format("请求错误res|%s|url|%s|body|%s|", res, url, body));
|
||||
}
|
||||
return vo;
|
||||
}catch (Exception e){
|
||||
log.error("|{}||syncAccClassInfo|意外的错误error", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getToken() {
|
||||
try {
|
||||
String redisKey = CacheConstants.THIRDPARTY_ZY_MER_TOKEN;
|
||||
String token = redisTemplate.opsForValue().get(redisKey);
|
||||
if (ObjectUtil.isEmpty(token)) {
|
||||
ZyMerGetTokenVo vo = getTokenApi();
|
||||
if (ObjectUtil.isNotEmpty(vo)) {
|
||||
String accessToken = vo.getAccess_token();
|
||||
redisTemplate.opsForValue().set(redisKey, accessToken);
|
||||
return accessToken;
|
||||
}
|
||||
}
|
||||
return token;
|
||||
} catch (Exception e) {
|
||||
log.error("|{}|token获取错误无法进行,意外的错误error", e.getMessage());
|
||||
log.error("|{}||syncAccClassInfo|意外的错误error|{}|", TAG, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private <T> String getEncryptionDto(T params) {
|
||||
String json = JSONUtil.toJsonStr(params);
|
||||
String encodedData = Base64.getEncoder().encodeToString(json.getBytes(StandardCharsets.UTF_8));
|
||||
String stringToSign = String.format("data=%s&key=%s", json, zyConfig.getAppId());
|
||||
String sign = md5Hex(stringToSign);
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("data", encodedData);
|
||||
body.put("sign", sign);
|
||||
return JSONUtil.toJsonStr(body);
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package com.bsd.pjcloud.thirdparty.utils.zy.api;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.bsd.pjcloud.common.core.constant.CacheConstants;
|
||||
import com.bsd.pjcloud.thirdparty.config.ZyConfig;
|
||||
import com.bsd.pjcloud.thirdparty.vo.zy.mer.ZyMerGetTokenVo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.apache.commons.codec.digest.DigestUtils.md5Hex;
|
||||
|
||||
@Component
|
||||
@Validated
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class ZyApiTool {
|
||||
|
||||
private final String TAG = "正元请求工具类";
|
||||
|
||||
private final ZyConfig zyConfig;
|
||||
|
||||
private final StringRedisTemplate redisTemplate;
|
||||
|
||||
public ZyMerGetTokenVo getTokenApi() {
|
||||
String url = zyConfig.getBaseUrl() + ZyApi.Mer.GET_TOKEN;
|
||||
url = String.format(url, zyConfig.getAppId(), zyConfig.getAppSecret());
|
||||
try {
|
||||
String res = HttpRequest.get(url)
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.execute()
|
||||
.body();
|
||||
if (ObjectUtil.isEmpty(res)) {
|
||||
throw new RuntimeException("response is empty");
|
||||
}
|
||||
ZyMerGetTokenVo vo = JSONUtil.toBean(res, ZyMerGetTokenVo.class);
|
||||
if (!vo.isSuccess()) {
|
||||
throw new RuntimeException(String.format("请求错误res|%s|url|%s|", res, url));
|
||||
}
|
||||
return vo;
|
||||
} catch (Exception e) {
|
||||
log.error("|{}||getTokenApi|意外的错误error|{}|", TAG, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
try {
|
||||
String redisKey = CacheConstants.THIRDPARTY_ZY_MER_TOKEN;
|
||||
String token = redisTemplate.opsForValue().get(redisKey);
|
||||
if (ObjectUtil.isEmpty(token)) {
|
||||
ZyMerGetTokenVo vo = getTokenApi();
|
||||
if (ObjectUtil.isNotEmpty(vo)) {
|
||||
String accessToken = vo.getAccess_token();
|
||||
redisTemplate.opsForValue().set(redisKey, accessToken);
|
||||
return accessToken;
|
||||
}
|
||||
}
|
||||
return token;
|
||||
} catch (Exception e) {
|
||||
log.error("|{}|token获取错误无法进行,意外的错误error|{}|", TAG, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> String getEncryptionDto(T params) {
|
||||
String json = JSONUtil.toJsonStr(params);
|
||||
String encodedData = Base64.getEncoder().encodeToString(json.getBytes(StandardCharsets.UTF_8));
|
||||
String stringToSign = String.format("data=%s&key=%s", json, zyConfig.getAppId());
|
||||
String sign = md5Hex(stringToSign);
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("data", encodedData);
|
||||
body.put("sign", sign);
|
||||
return JSONUtil.toJsonStr(body);
|
||||
}
|
||||
|
||||
}
|
||||
+6
-2
@@ -7,6 +7,7 @@ import com.bsd.pjcloud.thirdparty.dto.zy.mer.ZyMerSyncAccountInfoDto;
|
||||
import com.bsd.pjcloud.thirdparty.dto.zy.mer.ZyMerSyncDealerInfoDto;
|
||||
import com.bsd.pjcloud.thirdparty.dto.zy.mer.ZyMerSyncEnterpriseInfoDto;
|
||||
import com.bsd.pjcloud.thirdparty.service.impl.ZyMerService;
|
||||
import com.bsd.pjcloud.thirdparty.utils.zy.api.ZyApiTool;
|
||||
import com.bsd.pjcloud.thirdparty.vo.zy.mer.*;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -23,6 +24,9 @@ public class ZyTest {
|
||||
@Resource
|
||||
private ZyMerService zyMerService;
|
||||
|
||||
@Resource
|
||||
private ZyApiTool zyApiTool;
|
||||
|
||||
/************************** start mer start **************************/
|
||||
|
||||
/**
|
||||
@@ -30,8 +34,8 @@ public class ZyTest {
|
||||
*/
|
||||
@Test
|
||||
public void merGetTokenApi() {
|
||||
ZyMerGetTokenVo vo = zyMerService.getTokenApi();
|
||||
System.out.println(1);
|
||||
ZyMerGetTokenVo vo = zyApiTool.getTokenApi();
|
||||
System.out.println(vo);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user