数云接口调用封装工具类 优化

master
382696293@qq.com 2 years ago
parent 41a62803aa
commit 0a405f95db

@ -98,14 +98,36 @@ public class ShuYunApiUtils {
} }
/** /**
* *
* *
* @param httpMethod * @param httpMethod
* @param queryParams geturl * @param queryParams geturl
* @param bodyParams postput * @param bodyParams postput
* @param actionMethod * @param actionMethod
*/ */
public static R shuYunHttpRequest(HttpMethod httpMethod, Map queryParams, Object bodyParams, String actionMethod) throws ServiceException { public static <T> R<T> shuYunHttpRequest(HttpMethod httpMethod, Map queryParams, Object bodyParams, String actionMethod, Class<T> clazz) throws ServiceException {
logger.info("数云接口请求地址:{}参数queryParams = {}、bodyParams = {}", actionMethod, queryParams, JSON.toJSONString(bodyParams));
String result = GateWayClient.askGateWay(
httpMethod, shuYunConfig.getUrl(),
queryParams, JSON.toJSONString(bodyParams),
shuYunConfig.getAppid(), shuYunConfig.getSecurity(),
getAccessToken(), actionMethod);
logger.info("返回响应:{}", result);
R<T> r = JSON.parseObject(result, new TypeReference<R<T>>(clazz) {
});
if (r.getCode() == ShuYunHttpStatusConstants.SUCCESS || r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
return r;
} else {
logger.error("调用数云接口发生未知错误:{}", result);
throw new ServiceException(r.getMsg());
}
}
/**
*
*/
@Deprecated
public static R shuYunHttpRequest2(HttpMethod httpMethod, Map queryParams, Object bodyParams, String actionMethod) throws ServiceException {
logger.info("数云接口请求地址:{}参数queryParams = {}、bodyParams = {}", actionMethod, queryParams, JSON.toJSONString(bodyParams)); logger.info("数云接口请求地址:{}参数queryParams = {}、bodyParams = {}", actionMethod, queryParams, JSON.toJSONString(bodyParams));
String result = GateWayClient.askGateWay( String result = GateWayClient.askGateWay(
httpMethod, shuYunConfig.getUrl(), httpMethod, shuYunConfig.getUrl(),
@ -122,22 +144,22 @@ public class ShuYunApiUtils {
} }
} }
public static R shuYunHttpRequest(HttpMethod httpMethod, Map queryParams, String actionMethod) throws ServiceException { public static <T> R<T> httpQueryParamsRequest(HttpMethod httpMethod, Map queryParams, String actionMethod, Class<T> clazz) throws ServiceException {
return shuYunHttpRequest(httpMethod, queryParams, null, actionMethod); return shuYunHttpRequest(httpMethod, queryParams, null, actionMethod, clazz);
} }
public static R shuYunHttpRequest(HttpMethod httpMethod, String bodyParams, String actionMethod) throws ServiceException { public static <T> R<T> httpBodyParamsRequest(HttpMethod httpMethod, Object bodyParams, String actionMethod, Class<T> clazz) throws ServiceException {
return shuYunHttpRequest(httpMethod, null, bodyParams, actionMethod); return shuYunHttpRequest(httpMethod, null, bodyParams, actionMethod, clazz);
} }
/** /**
* *
* https://open.shuyun.com/#/apidoc?type=41&apiId=40
*/ */
public static void registerMember(ShuYunMember member) { public static R registerMember(ShuYunMember member) {
try { try {
shuYunHttpRequest(HttpMethod.POST, null, member, return httpBodyParamsRequest(HttpMethod.POST, member, shuYunConfig.getActionMethod().getRegisterMember(), Object.class);
shuYunConfig.getActionMethod().getRegisterMember());
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口注册会员失败:{}", e.getMessage()); logger.error("请求数云接口注册会员失败:{}", e.getMessage());
throw new ServiceException("请求数云接口注册会员失败"); throw new ServiceException("请求数云接口注册会员失败");
@ -145,34 +167,38 @@ public class ShuYunApiUtils {
} }
/** /**
* *
* https://open.shuyun.com/#/apidoc?type=41&apiId=149
*/ */
public static ShuYunMember queryMember(ShuYunMember member) { public static ShuYunMember queryMember(String memberId, String platCode, String shopId) {
Map<String, String> params = MapUtil.newHashMap();
params.put("id", memberId);
params.put("platCode", platCode);
params.put("shopId", shopId);
try { try {
R r = shuYunHttpRequest(HttpMethod.POST, null, member, R<ShuYunMember> r = httpBodyParamsRequest(HttpMethod.POST, params, shuYunConfig.getActionMethod().getQueryMember(), ShuYunMember.class);
shuYunConfig.getActionMethod().getQueryMember());
if (r.getCode() == ShuYunHttpStatusConstants.SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.SUCCESS) {
return JSON.parseObject(r.getData().toString(), ShuYunMember.class); return r.getData();
} }
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
return null; return null;
} }
logger.error("请求数云接口-会员积分查询失败:{}", r.getMsg()); logger.error("请求数云接口-会员信息查询失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-会员积分查询失败"); throw new ServiceException("请求数云接口-会员信息查询失败");
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口-查询会员信息失败:{}", e.getMessage()); logger.error("请求数云接口-会员信息查询失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-查询会员信息失败"); throw new ServiceException("请求数云接口-会员信息查询失败");
} }
} }
/** /**
* *
* *
* https://open.shuyun.com/#/apidoc?type=41&apiId=36
*/ */
public static void modifyMember(ShuYunMember member) { public static R modifyMember(ShuYunMember member) {
try { try {
shuYunHttpRequest(HttpMethod.PUT, null, member, return httpBodyParamsRequest(HttpMethod.PUT, member, shuYunConfig.getActionMethod().getModifyMember(), Object.class);
shuYunConfig.getActionMethod().getModifyMember());
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口-修改会员信息(除手机号)失败:{}", e.getMessage()); logger.error("请求数云接口-修改会员信息(除手机号)失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-修改会员信息(除手机号)失败"); throw new ServiceException("请求数云接口-修改会员信息(除手机号)失败");
@ -181,11 +207,16 @@ public class ShuYunApiUtils {
/** /**
* *
* https://open.shuyun.com/#/apidoc?type=41&apiId=37
*/ */
public static void modifyMemberMobile(ShuYunMember member) { public static R modifyMemberMobile(String memberId, String platCode, String shopId, String mobile) {
Map<String, String> params = MapUtil.newHashMap();
params.put("id", memberId);
params.put("platCode", platCode);
params.put("shopId", shopId);
params.put("mobile", mobile);
try { try {
shuYunHttpRequest(HttpMethod.PUT, null, member, return httpBodyParamsRequest(HttpMethod.PUT, params, shuYunConfig.getActionMethod().getModifyMemberMobile(), Object.class);
shuYunConfig.getActionMethod().getModifyMemberMobile());
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口-修改会员手机号失败:{}", e.getMessage()); logger.error("请求数云接口-修改会员手机号失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-修改会员手机号失败"); throw new ServiceException("请求数云接口-修改会员手机号失败");
@ -194,11 +225,15 @@ public class ShuYunApiUtils {
/** /**
* *
* https://open.shuyun.com/#/apidoc?type=41&apiId=74
*/ */
public static void unbindMember(ShuYunMember member) { public static R unbindMember(String memberId, String platCode, String shopId) {
Map<String, String> params = MapUtil.newHashMap();
params.put("id", memberId);
params.put("platCode", platCode);
params.put("shopId", shopId);
try { try {
shuYunHttpRequest(HttpMethod.POST, null, member, return httpBodyParamsRequest(HttpMethod.POST, params, shuYunConfig.getActionMethod().getUnbindMember(), Object.class);
shuYunConfig.getActionMethod().getUnbindMember());
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口-解绑会员失败:{}", e.getMessage()); logger.error("请求数云接口-解绑会员失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-解绑会员失败"); throw new ServiceException("请求数云接口-解绑会员失败");
@ -207,11 +242,11 @@ public class ShuYunApiUtils {
/** /**
* *
* https://open.shuyun.com/#/apidoc?type=41&apiId=34
*/ */
public static void pointChange(ShuYunPointChange shuYunPointChange) { public static R pointChange(ShuYunPointChange shuYunPointChange) {
try { try {
shuYunHttpRequest(HttpMethod.POST, null, shuYunPointChange, return httpBodyParamsRequest(HttpMethod.POST, shuYunPointChange, shuYunConfig.getActionMethod().getPointChange(), Object.class);
shuYunConfig.getActionMethod().getPointChange());
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口-会员积分变更失败:{}", e.getMessage()); logger.error("请求数云接口-会员积分变更失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-会员积分变更失败"); throw new ServiceException("请求数云接口-会员积分变更失败");
@ -220,12 +255,12 @@ public class ShuYunApiUtils {
/** /**
* () * ()
* https://open.shuyun.com/#/apidoc?type=41&apiId=33
*/ */
public static ShuYunPageUtil<ShuYunPointChangeLog> pointChangeLogSearch(ShuYunPageReq shuYunPageReq) { public static ShuYunPageUtil<ShuYunPointChangeLog> pointChangeLogSearch(ShuYunPageReq shuYunPageReq) {
try { try {
R r = httpQueryParamsRequest(HttpMethod.GET, BeanUtil.beanToMap(shuYunPageReq),
R r = shuYunHttpRequest(HttpMethod.GET, BeanUtil.beanToMap(shuYunPageReq), null, shuYunConfig.getActionMethod().getPointChangeLogSearch(), Object.class);
shuYunConfig.getActionMethod().getPointChangeLogSearch());
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.info("查询成功,没有数据"); logger.info("查询成功,没有数据");
return new ShuYunPageUtil<>(); return new ShuYunPageUtil<>();
@ -243,19 +278,18 @@ public class ShuYunApiUtils {
/** /**
* *
* TODO:() * https://open.shuyun.com/#/apidoc?type=41&apiId=249
*/ */
public static int pointWillDueSearch(Map map) { public static Integer pointWillDueSearch(Map map) {
try { try {
R r = shuYunHttpRequest(HttpMethod.GET, map, null, R<Integer> r = httpQueryParamsRequest(HttpMethod.GET, map, shuYunConfig.getActionMethod().getPointWillDueSearch(), Integer.class);
shuYunConfig.getActionMethod().getPointWillDueSearch());
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.info("查询成功,没有数据"); logger.info("查询成功,没有数据");
} }
if (r.getCode() != ShuYunHttpStatusConstants.SUCCESS) { if (r.getCode() != ShuYunHttpStatusConstants.SUCCESS) {
logger.error("请求数云接口-查询会员即将过期积分失败:{}", r.getMsg()); logger.error("请求数云接口-查询会员即将过期积分失败:{}", r.getMsg());
} }
return 0; return r.getData();
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口-查询会员即将过期积分失败:{}", e.getMessage()); logger.error("请求数云接口-查询会员即将过期积分失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-查询会员即将过期积分失败"); throw new ServiceException("请求数云接口-查询会员即将过期积分失败");
@ -265,18 +299,17 @@ public class ShuYunApiUtils {
/** /**
* *
* https://open.shuyun.com/#/apidoc?type=23&apiId=86
*/ */
public static String tagCreate(ShuYunTag shuYunTag) { public static Integer tagCreate(ShuYunTag shuYunTag) {
try { try {
R r = shuYunHttpRequest(HttpMethod.POST, null, shuYunTag, R<Map> r = httpBodyParamsRequest(HttpMethod.POST, shuYunTag, shuYunConfig.getActionMethod().getTagCreate(), Map.class);
shuYunConfig.getActionMethod().getTagCreate());
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-创建标签失败:{}", r.getMsg()); logger.error("请求数云接口-创建标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-创建标签失败"); throw new ServiceException("请求数云接口-创建标签失败");
} }
if (r.getData() != null) { if (r.getData() != null) {
JSONObject map = (JSONObject) r.getData(); return (Integer) (r.getData().get("tagId"));
return String.valueOf(map.get("tagId"));
} }
return null; return null;
} catch (Exception e) { } catch (Exception e) {
@ -287,6 +320,7 @@ public class ShuYunApiUtils {
/** /**
* *
* https://open.shuyun.com/#/apidoc?type=23&apiId=85
* *
* @param type 1() * @param type 1()
* @param allValueType true()false() * @param allValueType true()false()
@ -296,8 +330,7 @@ public class ShuYunApiUtils {
params.put("type", type); params.put("type", type);
params.put("allValueType", allValueType); params.put("allValueType", allValueType);
try { try {
R r = shuYunHttpRequest(HttpMethod.GET, params, null, R r = httpQueryParamsRequest(HttpMethod.GET, params, shuYunConfig.getActionMethod().getTagSearch(), Object.class);
shuYunConfig.getActionMethod().getTagSearch());
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-创建标签失败:{}", r.getMsg()); logger.error("请求数云接口-创建标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-创建标签失败"); throw new ServiceException("请求数云接口-创建标签失败");
@ -312,17 +345,19 @@ public class ShuYunApiUtils {
/** /**
* *
* https://open.shuyun.com/#/apidoc?type=23&apiId=153
*/ */
public static void tagDelete(String tagId) { public static R tagDelete(String tagId) {
Map<String, String> params = MapUtil.newHashMap(); Map<String, String> params = MapUtil.newHashMap();
params.put("tagId", tagId); params.put("tagId", tagId);
try { try {
R r = shuYunHttpRequest(HttpMethod.DELETE, null, params, R r = httpBodyParamsRequest(HttpMethod.DELETE, params,
shuYunConfig.getActionMethod().getTagDelete()); shuYunConfig.getActionMethod().getTagDelete(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-删除标签失败:{}", r.getMsg()); logger.error("请求数云接口-删除标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-删除标签失败"); throw new ServiceException("请求数云接口-删除标签失败");
} }
return r;
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口-删除标签失败:{}", e.getMessage()); logger.error("请求数云接口-删除标签失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-删除标签失败"); throw new ServiceException("请求数云接口-删除标签失败");
@ -331,15 +366,17 @@ public class ShuYunApiUtils {
/** /**
* *
* https://open.shuyun.com/#/apidoc?type=23&apiId=198
*/ */
public static void tagUpdate(ShuYunTag shuYunTag) { public static R tagUpdate(ShuYunTag shuYunTag) {
try { try {
R r = shuYunHttpRequest(HttpMethod.PUT, null, shuYunTag, R r = httpBodyParamsRequest(HttpMethod.PUT, shuYunTag,
shuYunConfig.getActionMethod().getTagUpdate()); shuYunConfig.getActionMethod().getTagUpdate(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-修改标签失败:{}", r.getMsg()); logger.error("请求数云接口-修改标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-修改标签失败"); throw new ServiceException("请求数云接口-修改标签失败");
} }
return r;
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口-修改标签失败:{}", e.getMessage()); logger.error("请求数云接口-修改标签失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-修改标签失败"); throw new ServiceException("请求数云接口-修改标签失败");
@ -349,14 +386,15 @@ public class ShuYunApiUtils {
/** /**
* *
*/ */
public static void markUserTag(ShuYunUserTagReq shuYunUserTagReq) { public static R markUserTag(ShuYunUserTagReq shuYunUserTagReq) {
try { try {
R r = shuYunHttpRequest(HttpMethod.POST, null, shuYunUserTagReq, R r = httpBodyParamsRequest(HttpMethod.POST, shuYunUserTagReq,
shuYunConfig.getActionMethod().getMarkUserTag()); shuYunConfig.getActionMethod().getMarkUserTag(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-给客户打上单个标签失败:{}", r.getMsg()); logger.error("请求数云接口-给客户打上单个标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-给客户打上单个标签失败"); throw new ServiceException("请求数云接口-给客户打上单个标签失败");
} }
return r;
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口-给客户打上单个标签失败:{}", e.getMessage()); logger.error("请求数云接口-给客户打上单个标签失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-给客户打上单个标签失败"); throw new ServiceException("请求数云接口-给客户打上单个标签失败");
@ -379,8 +417,8 @@ public class ShuYunApiUtils {
// 标签显示true(全部标签)false(基本标签) // 标签显示true(全部标签)false(基本标签)
params.put("allValueType", allValueType); params.put("allValueType", allValueType);
try { try {
R r = shuYunHttpRequest(HttpMethod.GET, params, null, R r = httpQueryParamsRequest(HttpMethod.GET, params,
shuYunConfig.getActionMethod().getSearchUserTag()); shuYunConfig.getActionMethod().getSearchUserTag(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-查询客户已经被打上的标签失败:{}", r.getMsg()); logger.error("请求数云接口-查询客户已经被打上的标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-查询客户已经被打上的标签标签失败"); throw new ServiceException("请求数云接口-查询客户已经被打上的标签标签失败");
@ -396,7 +434,7 @@ public class ShuYunApiUtils {
/** /**
* *
*/ */
public static void deleteUserTag(String platCode, String shopId, String platAccount, Integer tagId) { public static R deleteUserTag(String platCode, String shopId, String platAccount, Integer tagId) {
Map<String, Object> params = MapUtil.newHashMap(); Map<String, Object> params = MapUtil.newHashMap();
// 平台代码 // 平台代码
params.put("platCode", platCode); params.put("platCode", platCode);
@ -407,12 +445,13 @@ public class ShuYunApiUtils {
// 标签ID // 标签ID
params.put("tagId", tagId); params.put("tagId", tagId);
try { try {
R r = shuYunHttpRequest(HttpMethod.DELETE, null, params, R r = httpBodyParamsRequest(HttpMethod.DELETE, params,
shuYunConfig.getActionMethod().getDeleteUserTag()); shuYunConfig.getActionMethod().getDeleteUserTag(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) { if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-删除客户已经被打上的标签失败:{}", r.getMsg()); logger.error("请求数云接口-删除客户已经被打上的标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-删除客户已经被打上的标签失败"); throw new ServiceException("请求数云接口-删除客户已经被打上的标签失败");
} }
return r;
} catch (Exception e) { } catch (Exception e) {
logger.error("请求数云接口-删除客户已经被打上的标签失败:{}", e.getMessage()); logger.error("请求数云接口-删除客户已经被打上的标签失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-删除客户已经被打上的标签失败"); throw new ServiceException("请求数云接口-删除客户已经被打上的标签失败");

Loading…
Cancel
Save