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

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

@ -98,14 +98,36 @@ public class ShuYunApiUtils {
}
/**
*
*
*
* @param httpMethod
* @param queryParams geturl
* @param bodyParams postput
* @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));
String result = GateWayClient.askGateWay(
httpMethod, shuYunConfig.getUrl(),
@ -122,22 +144,22 @@ public class ShuYunApiUtils {
}
}
public static R shuYunHttpRequest(HttpMethod httpMethod, Map queryParams, String actionMethod) throws ServiceException {
return shuYunHttpRequest(httpMethod, queryParams, null, actionMethod);
public static <T> R<T> httpQueryParamsRequest(HttpMethod httpMethod, Map queryParams, String actionMethod, Class<T> clazz) throws ServiceException {
return shuYunHttpRequest(httpMethod, queryParams, null, actionMethod, clazz);
}
public static R shuYunHttpRequest(HttpMethod httpMethod, String bodyParams, String actionMethod) throws ServiceException {
return shuYunHttpRequest(httpMethod, null, bodyParams, actionMethod);
public static <T> R<T> httpBodyParamsRequest(HttpMethod httpMethod, Object bodyParams, String actionMethod, Class<T> clazz) throws ServiceException {
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 {
shuYunHttpRequest(HttpMethod.POST, null, member,
shuYunConfig.getActionMethod().getRegisterMember());
return httpBodyParamsRequest(HttpMethod.POST, member, shuYunConfig.getActionMethod().getRegisterMember(), Object.class);
} catch (Exception e) {
logger.error("请求数云接口注册会员失败:{}", e.getMessage());
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 {
R r = shuYunHttpRequest(HttpMethod.POST, null, member,
shuYunConfig.getActionMethod().getQueryMember());
R<ShuYunMember> r = httpBodyParamsRequest(HttpMethod.POST, params, shuYunConfig.getActionMethod().getQueryMember(), ShuYunMember.class);
if (r.getCode() == ShuYunHttpStatusConstants.SUCCESS) {
return JSON.parseObject(r.getData().toString(), ShuYunMember.class);
return r.getData();
}
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
return null;
}
logger.error("请求数云接口-会员积分查询失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-会员积分查询失败");
logger.error("请求数云接口-会员信息查询失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-会员信息查询失败");
} catch (Exception e) {
logger.error("请求数云接口-查询会员信息失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-查询会员信息失败");
logger.error("请求数云接口-会员信息查询失败:{}", e.getMessage());
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 {
shuYunHttpRequest(HttpMethod.PUT, null, member,
shuYunConfig.getActionMethod().getModifyMember());
return httpBodyParamsRequest(HttpMethod.PUT, member, shuYunConfig.getActionMethod().getModifyMember(), Object.class);
} catch (Exception e) {
logger.error("请求数云接口-修改会员信息(除手机号)失败:{}", e.getMessage());
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 {
shuYunHttpRequest(HttpMethod.PUT, null, member,
shuYunConfig.getActionMethod().getModifyMemberMobile());
return httpBodyParamsRequest(HttpMethod.PUT, params, shuYunConfig.getActionMethod().getModifyMemberMobile(), Object.class);
} catch (Exception e) {
logger.error("请求数云接口-修改会员手机号失败:{}", e.getMessage());
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 {
shuYunHttpRequest(HttpMethod.POST, null, member,
shuYunConfig.getActionMethod().getUnbindMember());
return httpBodyParamsRequest(HttpMethod.POST, params, shuYunConfig.getActionMethod().getUnbindMember(), Object.class);
} catch (Exception e) {
logger.error("请求数云接口-解绑会员失败:{}", e.getMessage());
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 {
shuYunHttpRequest(HttpMethod.POST, null, shuYunPointChange,
shuYunConfig.getActionMethod().getPointChange());
return httpBodyParamsRequest(HttpMethod.POST, shuYunPointChange, shuYunConfig.getActionMethod().getPointChange(), Object.class);
} catch (Exception e) {
logger.error("请求数云接口-会员积分变更失败:{}", e.getMessage());
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) {
try {
R r = shuYunHttpRequest(HttpMethod.GET, BeanUtil.beanToMap(shuYunPageReq), null,
shuYunConfig.getActionMethod().getPointChangeLogSearch());
R r = httpQueryParamsRequest(HttpMethod.GET, BeanUtil.beanToMap(shuYunPageReq),
shuYunConfig.getActionMethod().getPointChangeLogSearch(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.info("查询成功,没有数据");
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 {
R r = shuYunHttpRequest(HttpMethod.GET, map, null,
shuYunConfig.getActionMethod().getPointWillDueSearch());
R<Integer> r = httpQueryParamsRequest(HttpMethod.GET, map, shuYunConfig.getActionMethod().getPointWillDueSearch(), Integer.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.info("查询成功,没有数据");
}
if (r.getCode() != ShuYunHttpStatusConstants.SUCCESS) {
logger.error("请求数云接口-查询会员即将过期积分失败:{}", r.getMsg());
}
return 0;
return r.getData();
} catch (Exception e) {
logger.error("请求数云接口-查询会员即将过期积分失败:{}", e.getMessage());
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 {
R r = shuYunHttpRequest(HttpMethod.POST, null, shuYunTag,
shuYunConfig.getActionMethod().getTagCreate());
R<Map> r = httpBodyParamsRequest(HttpMethod.POST, shuYunTag, shuYunConfig.getActionMethod().getTagCreate(), Map.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-创建标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-创建标签失败");
}
if (r.getData() != null) {
JSONObject map = (JSONObject) r.getData();
return String.valueOf(map.get("tagId"));
return (Integer) (r.getData().get("tagId"));
}
return null;
} catch (Exception e) {
@ -287,6 +320,7 @@ public class ShuYunApiUtils {
/**
*
* https://open.shuyun.com/#/apidoc?type=23&apiId=85
*
* @param type 1()
* @param allValueType true()false()
@ -296,8 +330,7 @@ public class ShuYunApiUtils {
params.put("type", type);
params.put("allValueType", allValueType);
try {
R r = shuYunHttpRequest(HttpMethod.GET, params, null,
shuYunConfig.getActionMethod().getTagSearch());
R r = httpQueryParamsRequest(HttpMethod.GET, params, shuYunConfig.getActionMethod().getTagSearch(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-创建标签失败:{}", r.getMsg());
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();
params.put("tagId", tagId);
try {
R r = shuYunHttpRequest(HttpMethod.DELETE, null, params,
shuYunConfig.getActionMethod().getTagDelete());
R r = httpBodyParamsRequest(HttpMethod.DELETE, params,
shuYunConfig.getActionMethod().getTagDelete(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-删除标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-删除标签失败");
}
return r;
} catch (Exception e) {
logger.error("请求数云接口-删除标签失败:{}", e.getMessage());
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 {
R r = shuYunHttpRequest(HttpMethod.PUT, null, shuYunTag,
shuYunConfig.getActionMethod().getTagUpdate());
R r = httpBodyParamsRequest(HttpMethod.PUT, shuYunTag,
shuYunConfig.getActionMethod().getTagUpdate(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-修改标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-修改标签失败");
}
return r;
} catch (Exception e) {
logger.error("请求数云接口-修改标签失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-修改标签失败");
@ -349,14 +386,15 @@ public class ShuYunApiUtils {
/**
*
*/
public static void markUserTag(ShuYunUserTagReq shuYunUserTagReq) {
public static R markUserTag(ShuYunUserTagReq shuYunUserTagReq) {
try {
R r = shuYunHttpRequest(HttpMethod.POST, null, shuYunUserTagReq,
shuYunConfig.getActionMethod().getMarkUserTag());
R r = httpBodyParamsRequest(HttpMethod.POST, shuYunUserTagReq,
shuYunConfig.getActionMethod().getMarkUserTag(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-给客户打上单个标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-给客户打上单个标签失败");
}
return r;
} catch (Exception e) {
logger.error("请求数云接口-给客户打上单个标签失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-给客户打上单个标签失败");
@ -379,8 +417,8 @@ public class ShuYunApiUtils {
// 标签显示true(全部标签)false(基本标签)
params.put("allValueType", allValueType);
try {
R r = shuYunHttpRequest(HttpMethod.GET, params, null,
shuYunConfig.getActionMethod().getSearchUserTag());
R r = httpQueryParamsRequest(HttpMethod.GET, params,
shuYunConfig.getActionMethod().getSearchUserTag(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-查询客户已经被打上的标签失败:{}", r.getMsg());
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();
// 平台代码
params.put("platCode", platCode);
@ -407,12 +445,13 @@ public class ShuYunApiUtils {
// 标签ID
params.put("tagId", tagId);
try {
R r = shuYunHttpRequest(HttpMethod.DELETE, null, params,
shuYunConfig.getActionMethod().getDeleteUserTag());
R r = httpBodyParamsRequest(HttpMethod.DELETE, params,
shuYunConfig.getActionMethod().getDeleteUserTag(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-删除客户已经被打上的标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-删除客户已经被打上的标签失败");
}
return r;
} catch (Exception e) {
logger.error("请求数云接口-删除客户已经被打上的标签失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-删除客户已经被打上的标签失败");

Loading…
Cancel
Save