数云接口调用工具完善

master
382696293@qq.com 2 years ago
parent 7c8c7936a1
commit c280405432

@ -0,0 +1,210 @@
package com.flossom.miniProgram.utils.shuyun;
/**
*
*/
public class ActionMethod {
/**
* token
*/
private String accessToken;
/**
*
*/
private String queryMember;
/**
*
*/
private String registerMember;
/**
*
*/
private String modifyMember;
/**
*
*/
private String modifyMemberMobile;
/**
*
*/
private String unbindMember;
/**
*
*/
private String pointChange;
/**
*
*/
private String pointChangeLogSearch;
/**
*
*/
private String pointWillDueSearch;
/**
*
*/
private String tagCreate;
/**
*
*/
private String tagDelete;
/**
*
*/
private String tagSearch;
/**
*
*/
private String tagUpdate;
/**
*
*/
private String markUserTag;
/**
*
*/
private String deleteUserTag;
/**
*
*/
private String searchUserTag;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getQueryMember() {
return queryMember;
}
public void setQueryMember(String queryMember) {
this.queryMember = queryMember;
}
public String getRegisterMember() {
return registerMember;
}
public void setRegisterMember(String registerMember) {
this.registerMember = registerMember;
}
public String getModifyMember() {
return modifyMember;
}
public void setModifyMember(String modifyMember) {
this.modifyMember = modifyMember;
}
public String getModifyMemberMobile() {
return modifyMemberMobile;
}
public void setModifyMemberMobile(String modifyMemberMobile) {
this.modifyMemberMobile = modifyMemberMobile;
}
public String getUnbindMember() {
return unbindMember;
}
public void setUnbindMember(String unbindMember) {
this.unbindMember = unbindMember;
}
public String getPointChange() {
return pointChange;
}
public void setPointChange(String pointChange) {
this.pointChange = pointChange;
}
public String getPointChangeLogSearch() {
return pointChangeLogSearch;
}
public void setPointChangeLogSearch(String pointChangeLogSearch) {
this.pointChangeLogSearch = pointChangeLogSearch;
}
public String getPointWillDueSearch() {
return pointWillDueSearch;
}
public void setPointWillDueSearch(String pointWillDueSearch) {
this.pointWillDueSearch = pointWillDueSearch;
}
public String getTagCreate() {
return tagCreate;
}
public void setTagCreate(String tagCreate) {
this.tagCreate = tagCreate;
}
public String getTagDelete() {
return tagDelete;
}
public void setTagDelete(String tagDelete) {
this.tagDelete = tagDelete;
}
public String getTagSearch() {
return tagSearch;
}
public void setTagSearch(String tagSearch) {
this.tagSearch = tagSearch;
}
public String getTagUpdate() {
return tagUpdate;
}
public void setTagUpdate(String tagUpdate) {
this.tagUpdate = tagUpdate;
}
public String getMarkUserTag() {
return markUserTag;
}
public void setMarkUserTag(String markUserTag) {
this.markUserTag = markUserTag;
}
public String getDeleteUserTag() {
return deleteUserTag;
}
public void setDeleteUserTag(String deleteUserTag) {
this.deleteUserTag = deleteUserTag;
}
public String getSearchUserTag() {
return searchUserTag;
}
public void setSearchUserTag(String searchUserTag) {
this.searchUserTag = searchUserTag;
}
}

@ -0,0 +1,469 @@
package com.flossom.miniProgram.utils.shuyun;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.flossom.common.core.constant.CacheConstants;
import com.flossom.common.core.domain.R;
import com.flossom.common.core.domain.shuyun.*;
import com.flossom.common.core.exception.ServiceException;
import com.flossom.common.redis.service.RedisService;
import com.flossom.miniProgram.utils.HttpClientUtils;
import com.shuyun.open.sdk.bean.HttpMethod;
import com.shuyun.open.sdk.core.GateWayClient;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
/**
*
*/
@Component
public class ShuYunApiUtils {
protected final static Logger logger = LoggerFactory.getLogger(ShuYunApiUtils.class);
/**
*
*/
private static ShuYunConfig shuYunConfig;
private static RedisService redisService;
@Autowired
public void setShuYunConfig(ShuYunConfig shuYunConfig) {
ShuYunApiUtils.shuYunConfig = shuYunConfig;
}
@Autowired
public void setRedisService(RedisService redisService) {
ShuYunApiUtils.redisService = redisService;
}
/**
* httpcode
*/
public class ShuYunHttpStatusConstants {
/**
*
*/
public static final int SUCCESS = 10000;
/**
*
*
*/
public static final int HALF_SUCCESS = 14000;
}
/**
* accessToken accessToken
*/
public static String getAccessToken() {
Long expiryTime = redisService.getCacheMapValue(CacheConstants.SHUYUN_ACCESS_TOKEN_CACHE, CacheConstants.SHUYUN_ACCESS_TOKEN_CACHE_EXPIRY);
if (expiryTime == null || expiryTime == 0 || LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond() > expiryTime) {
String accessTokenUrl = StringUtils.replace(shuYunConfig.getActionMethod().getAccessToken(), "{appid}", shuYunConfig.getAppid());
logger.info("刷新 accessToken 地址:{}", accessTokenUrl);
try {
String result = HttpClientUtils.get(accessTokenUrl);
logger.info("请求数云接口获取 accessToken 结果:{}", result);
R r = JSON.parseObject(result, R.class);
if (r.getCode() != R.SUCCESS) {
logger.error("请求数云接口获取accessToken失败将直接使用旧的accessToken失败原因{}", result);
}
} catch (Exception e) {
logger.error("请求数云接口获取accessToken失败将直接使用旧的accessToken失败原因{}", e.getMessage());
}
}
String accessTokenStr = redisService.getCacheMapValue(CacheConstants.SHUYUN_ACCESS_TOKEN_CACHE, CacheConstants.SHUYUN_ACCESS_TOKEN_CACHE_VALUE);
if (StringUtils.isNotBlank(accessTokenStr)) {
AccessToken accessToken = JSON.parseObject(accessTokenStr, AccessToken.class);
return accessToken.getAccessToken();
}
/**
* redis
*/
logger.error("从 redis 获取数云 accessToken 失败,缓存中没有 accessToken有可能是第一次调用");
throw new ServiceException("从 redis 获取数云 accessToken 失败,缓存中没有 accessToken有可能是第一次调用");
}
/**
*
*
* @param httpMethod
* @param queryParams geturl
* @param bodyParams postput
* @param actionMethod
*/
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(),
queryParams, JSON.toJSONString(bodyParams),
shuYunConfig.getAppid(), shuYunConfig.getSecurity(),
getAccessToken(), actionMethod);
logger.info("返回响应:{}", result);
R r = JSON.parseObject(result, R.class);
if (r.getCode() == ShuYunHttpStatusConstants.SUCCESS || r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
return r;
} else {
logger.error("调用数云接口发生未知错误:{}", result);
throw new ServiceException(r.getMsg());
}
}
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 <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 R registerMember(ShuYunMember member) {
try {
return httpBodyParamsRequest(HttpMethod.POST, member, shuYunConfig.getActionMethod().getRegisterMember(), Object.class);
} catch (Exception e) {
logger.error("请求数云接口注册会员失败:{}", e.getMessage());
throw new ServiceException("请求数云接口注册会员失败");
}
}
/**
*
* https://open.shuyun.com/#/apidoc?type=41&apiId=149
*/
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<ShuYunMember> r = httpBodyParamsRequest(HttpMethod.POST, params, shuYunConfig.getActionMethod().getQueryMember(), ShuYunMember.class);
if (r.getCode() == ShuYunHttpStatusConstants.SUCCESS) {
return r.getData();
}
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
return null;
}
logger.error("请求数云接口-会员信息查询失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-会员信息查询失败");
} catch (Exception e) {
logger.error("请求数云接口-会员信息查询失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-会员信息查询失败");
}
}
/**
*
*
* https://open.shuyun.com/#/apidoc?type=41&apiId=36
*/
public static R modifyMember(ShuYunMember member) {
try {
return httpBodyParamsRequest(HttpMethod.PUT, member, shuYunConfig.getActionMethod().getModifyMember(), Object.class);
} catch (Exception e) {
logger.error("请求数云接口-修改会员信息(除手机号)失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-修改会员信息(除手机号)失败");
}
}
/**
*
* https://open.shuyun.com/#/apidoc?type=41&apiId=37
*/
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 {
return httpBodyParamsRequest(HttpMethod.PUT, params, shuYunConfig.getActionMethod().getModifyMemberMobile(), Object.class);
} catch (Exception e) {
logger.error("请求数云接口-修改会员手机号失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-修改会员手机号失败");
}
}
/**
*
* https://open.shuyun.com/#/apidoc?type=41&apiId=74
*/
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 {
return httpBodyParamsRequest(HttpMethod.POST, params, shuYunConfig.getActionMethod().getUnbindMember(), Object.class);
} catch (Exception e) {
logger.error("请求数云接口-解绑会员失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-解绑会员失败");
}
}
/**
*
* https://open.shuyun.com/#/apidoc?type=41&apiId=34
*/
public static R pointChange(ShuYunPointChange shuYunPointChange) {
try {
return httpBodyParamsRequest(HttpMethod.POST, shuYunPointChange, shuYunConfig.getActionMethod().getPointChange(), Object.class);
} catch (Exception e) {
logger.error("请求数云接口-会员积分变更失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-会员积分变更失败");
}
}
/**
* ()
* https://open.shuyun.com/#/apidoc?type=41&apiId=33
*/
public static ShuYunPageUtil<ShuYunPointChangeLog> pointChangeLogSearch(ShuYunPageReq shuYunPageReq) {
try {
R r = httpQueryParamsRequest(HttpMethod.GET, BeanUtil.beanToMap(shuYunPageReq),
shuYunConfig.getActionMethod().getPointChangeLogSearch(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.info("查询成功,没有数据");
return new ShuYunPageUtil<>();
}
if (r.getCode() != ShuYunHttpStatusConstants.SUCCESS) {
logger.error("请求数云接口-会员积分变更记录查询失败:{}", r.getMsg());
}
return JSON.parseObject(r.getData().toString(), new TypeReference<ShuYunPageUtil<ShuYunPointChangeLog>>() {
});
} catch (Exception e) {
logger.error("请求数云接口-会员积分变更记录查询失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-会员积分变更记录查询失败");
}
}
/**
*
* https://open.shuyun.com/#/apidoc?type=41&apiId=249
*/
public static Integer pointWillDueSearch(String tenant, String platId, String cardPlanId, String timeType, String startTime, String endTime) {
Map<String, String> params = MapUtil.newHashMap();
params.put("tenant", tenant);
params.put("platId", platId);
params.put("platCode", shuYunConfig.getPlatCode());
params.put("shopId", shuYunConfig.getShopId());
params.put("cardPlanId", cardPlanId);
params.put("timeType", timeType);
params.put("startTime", startTime);
params.put("endTime", endTime);
try {
R<Integer> r = httpQueryParamsRequest(HttpMethod.GET, params, shuYunConfig.getActionMethod().getPointWillDueSearch(), Integer.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.info("查询成功,没有数据");
}
if (r.getCode() != ShuYunHttpStatusConstants.SUCCESS) {
logger.error("请求数云接口-查询会员即将过期积分失败:{}", r.getMsg());
}
return r.getData();
} catch (Exception e) {
logger.error("请求数云接口-查询会员即将过期积分失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-查询会员即将过期积分失败");
}
}
/**
*
* https://open.shuyun.com/#/apidoc?type=23&apiId=86
*/
public static Integer tagCreate(ShuYunTag shuYunTag) {
try {
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) {
return (Integer) (r.getData().get("tagId"));
}
return null;
} catch (Exception e) {
logger.error("请求数云接口-创建标签失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-创建标签失败");
}
}
/**
*
* https://open.shuyun.com/#/apidoc?type=23&apiId=85
*
* @param type 1()
* @param allValueType true()false()
*/
public static List<ShuYunGroupTag> tagSearch(String type, String allValueType) {
Map<String, String> params = MapUtil.newHashMap();
params.put("type", type);
params.put("allValueType", allValueType);
try {
R r = httpQueryParamsRequest(HttpMethod.GET, params, shuYunConfig.getActionMethod().getTagSearch(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-创建标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-创建标签失败");
}
return JSON.parseObject(r.getData().toString(), new TypeReference<List<ShuYunGroupTag>>() {
});
} catch (Exception e) {
logger.error("请求数云接口-创建标签失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-创建标签失败");
}
}
/**
*
* https://open.shuyun.com/#/apidoc?type=23&apiId=153
*/
public static R tagDelete(String tagId) {
Map<String, String> params = MapUtil.newHashMap();
params.put("tagId", tagId);
try {
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("请求数云接口-删除标签失败");
}
}
/**
*
* https://open.shuyun.com/#/apidoc?type=23&apiId=198
*/
public static R tagUpdate(ShuYunTag shuYunTag) {
try {
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("请求数云接口-修改标签失败");
}
}
/**
*
*/
public static R markUserTag(ShuYunUserTagReq shuYunUserTagReq) {
try {
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("请求数云接口-给客户打上单个标签失败");
}
}
/**
*
*/
public static List<ShuYunTagResponse> searchUserTag(String tagType, String platCode, String shopId, String platAccount, String allValueType) {
Map<String, Object> params = MapUtil.newHashMap();
// 标签类型0云标签, 1自定义标签
params.put("tagType", tagType);
// 平台代码
params.put("platCode", platCode);
// 店铺ID数云内部店铺ID
params.put("shopId", shopId);
// 平台账号
params.put("platAccount", platAccount);
// 标签显示true(全部标签)false(基本标签)
params.put("allValueType", allValueType);
try {
R r = httpQueryParamsRequest(HttpMethod.GET, params,
shuYunConfig.getActionMethod().getSearchUserTag(), Object.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.error("请求数云接口-查询客户已经被打上的标签失败:{}", r.getMsg());
throw new ServiceException("请求数云接口-查询客户已经被打上的标签标签失败");
}
return JSON.parseObject(r.getData().toString(), new TypeReference<List<ShuYunTagResponse>>() {
});
} catch (Exception e) {
logger.error("请求数云接口-查询客户已经被打上的标签失败:{}", e.getMessage());
throw new ServiceException("请求数云接口-查询客户已经被打上的标签失败");
}
}
/**
*
*/
public static R deleteUserTag(String platCode, String shopId, String platAccount, Integer tagId) {
Map<String, Object> params = MapUtil.newHashMap();
// 平台代码
params.put("platCode", platCode);
// 店铺ID数云内部店铺ID
params.put("shopId", shopId);
// 平台账号
params.put("platAccount", platAccount);
// 标签ID
params.put("tagId", tagId);
try {
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("请求数云接口-删除客户已经被打上的标签失败");
}
}
}

@ -0,0 +1,99 @@
package com.flossom.miniProgram.utils.shuyun;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
/**
*
*
* @author flossom
*/
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "shuyun")
public class ShuYunConfig {
/**
*
*/
private String url;
/**
* appid
*/
private String appid;
/**
* security
*/
private String security;
/**
*
*/
private String authValue;
private String platCode;
private String shopId;
private ActionMethod actionMethod;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getSecurity() {
return security;
}
public void setSecurity(String security) {
this.security = security;
}
public ActionMethod getActionMethod() {
return actionMethod;
}
public void setActionMethod(ActionMethod actionMethod) {
this.actionMethod = actionMethod;
}
public String getPlatCode() {
return platCode;
}
public void setPlatCode(String platCode) {
this.platCode = platCode;
}
public String getShopId() {
return shopId;
}
public void setShopId(String shopId) {
this.shopId = shopId;
}
public String getAuthValue() {
return authValue;
}
public void setAuthValue(String authValue) {
this.authValue = authValue;
}
}

@ -279,9 +279,18 @@ public class ShuYunApiUtils {
*
* https://open.shuyun.com/#/apidoc?type=41&apiId=249
*/
public static Integer pointWillDueSearch(Map map) {
public static Integer pointWillDueSearch(String tenant, String platId, String cardPlanId, String timeType, String startTime, String endTime) {
Map<String, String> params = MapUtil.newHashMap();
params.put("tenant", tenant);
params.put("platId", platId);
params.put("platCode", shuYunConfig.getPlatCode());
params.put("shopId", shuYunConfig.getShopId());
params.put("cardPlanId", cardPlanId);
params.put("timeType", timeType);
params.put("startTime", startTime);
params.put("endTime", endTime);
try {
R<Integer> r = httpQueryParamsRequest(HttpMethod.GET, map, shuYunConfig.getActionMethod().getPointWillDueSearch(), Integer.class);
R<Integer> r = httpQueryParamsRequest(HttpMethod.GET, params, shuYunConfig.getActionMethod().getPointWillDueSearch(), Integer.class);
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
logger.info("查询成功,没有数据");
}

Loading…
Cancel
Save