From 0a405f95db1c50e2e06c05a46e45dc6f1ceb3846 Mon Sep 17 00:00:00 2001 From: "382696293@qq.com" <382696293@qq.com> Date: Wed, 20 Mar 2024 16:27:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E4=BA=91=E6=8E=A5=E5=8F=A3=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=B0=81=E8=A3=85=E5=B7=A5=E5=85=B7=E7=B1=BB=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/utils/shuyun/ShuYunApiUtils.java | 159 +++++++++++------- 1 file changed, 99 insertions(+), 60 deletions(-) diff --git a/flossom-modules/flossom-system/src/main/java/com/flossom/system/utils/shuyun/ShuYunApiUtils.java b/flossom-modules/flossom-system/src/main/java/com/flossom/system/utils/shuyun/ShuYunApiUtils.java index 3bcf03f..88378ed 100644 --- a/flossom-modules/flossom-system/src/main/java/com/flossom/system/utils/shuyun/ShuYunApiUtils.java +++ b/flossom-modules/flossom-system/src/main/java/com/flossom/system/utils/shuyun/ShuYunApiUtils.java @@ -98,14 +98,36 @@ public class ShuYunApiUtils { } /** - * 请求基础封装方法 + * 请求基础封装方法 (增加泛型适配) * * @param httpMethod 请求方式 * @param queryParams get请求时,url地址后的参数 * @param bodyParams post、put 请求时,请求体的数据 * @param actionMethod 请求方法地址 */ - public static R shuYunHttpRequest(HttpMethod httpMethod, Map queryParams, Object bodyParams, String actionMethod) throws ServiceException { + public static R shuYunHttpRequest(HttpMethod httpMethod, Map queryParams, Object bodyParams, String actionMethod, Class 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 r = JSON.parseObject(result, new TypeReference>(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 R httpQueryParamsRequest(HttpMethod httpMethod, Map queryParams, String actionMethod, Class 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 R httpBodyParamsRequest(HttpMethod httpMethod, Object bodyParams, String actionMethod, Class 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 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 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 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 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 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 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 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 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 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("请求数云接口-删除客户已经被打上的标签失败");