|
|
|
|
@ -1,7 +1,9 @@
|
|
|
|
|
package com.flossom.system.utils.shuyun;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
|
|
import com.flossom.common.core.constant.CacheConstants;
|
|
|
|
|
import com.flossom.common.core.domain.R;
|
|
|
|
|
@ -19,6 +21,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -57,22 +60,23 @@ public class ShuYunApiUtils {
|
|
|
|
|
public static final int SUCCESS = 10000;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 请求成功,但是结果不符合预期
|
|
|
|
|
* 请求成功,但是业务处理错误,或者执行结果不符合预期
|
|
|
|
|
* 但不是处理异常
|
|
|
|
|
*/
|
|
|
|
|
public static final int HALF_SUCCESS = 14000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取 accessToken,缓存没有就请求数云回调接口,将 accessToken 传递过来
|
|
|
|
|
* 获取 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);
|
|
|
|
|
logger.info("刷新 accessToken 地址:{}", accessTokenUrl);
|
|
|
|
|
try {
|
|
|
|
|
String result = HttpClientUtils.get(accessTokenUrl);
|
|
|
|
|
logger.info("请求数云接口获取accessToken结果:{}", result);
|
|
|
|
|
logger.info("请求数云接口获取 accessToken 结果:{}", result);
|
|
|
|
|
R r = JSON.parseObject(result, R.class);
|
|
|
|
|
if (r.getCode() != R.SUCCESS) {
|
|
|
|
|
logger.error("请求数云接口获取accessToken失败,将直接使用旧的accessToken,失败原因:{}", result);
|
|
|
|
|
@ -87,31 +91,27 @@ public class ShuYunApiUtils {
|
|
|
|
|
return accessToken.getAccessToken();
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 第一次获取会失败,因为回调是异步的,如果还没回调回来,我们就从redis获取缓存了
|
|
|
|
|
* 第一次获取会失败,因为回调是异步的,如果还没回调回来,我们就从 redis 获取缓存了
|
|
|
|
|
*/
|
|
|
|
|
logger.error("从redis获取数云accessToken失败,缓存中没有accessToken,有可能是第一次调用");
|
|
|
|
|
throw new ServiceException("从redis获取数云accessToken失败,缓存中没有accessToken,有可能是第一次调用");
|
|
|
|
|
logger.error("从 redis 获取数云 accessToken 失败,缓存中没有 accessToken,有可能是第一次调用");
|
|
|
|
|
throw new ServiceException("从 redis 获取数云 accessToken 失败,缓存中没有 accessToken,有可能是第一次调用");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 请求基础封装方法
|
|
|
|
|
*
|
|
|
|
|
* @param httpMethod 请求方式
|
|
|
|
|
* @param requestParams get请求时,url地址后的参数
|
|
|
|
|
* @param requestBody post、put 请求时,请求体的数据
|
|
|
|
|
* @param actionMethod 请求方法地址
|
|
|
|
|
* @param httpMethod 请求方式
|
|
|
|
|
* @param queryParams get请求时,url地址后的参数
|
|
|
|
|
* @param bodyParams post、put 请求时,请求体的数据
|
|
|
|
|
* @param actionMethod 请求方法地址
|
|
|
|
|
*/
|
|
|
|
|
public static R shuYunHttpRequest(HttpMethod httpMethod, Map requestParams, String requestBody, String actionMethod) throws ServiceException {
|
|
|
|
|
logger.info("数云接口请求地址:{},参数:requestParams = {}、requestBody = {}", actionMethod, requestParams, requestBody);
|
|
|
|
|
public static R shuYunHttpRequest(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(),
|
|
|
|
|
requestParams,
|
|
|
|
|
requestBody,
|
|
|
|
|
shuYunConfig.getAppid(),
|
|
|
|
|
shuYunConfig.getSecurity(),
|
|
|
|
|
getAccessToken(),
|
|
|
|
|
actionMethod);
|
|
|
|
|
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) {
|
|
|
|
|
@ -122,13 +122,21 @@ public class ShuYunApiUtils {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static R shuYunHttpRequest(HttpMethod httpMethod, Map queryParams, String actionMethod) throws ServiceException {
|
|
|
|
|
return shuYunHttpRequest(httpMethod, queryParams, null, actionMethod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static R shuYunHttpRequest(HttpMethod httpMethod, String bodyParams, String actionMethod) throws ServiceException {
|
|
|
|
|
return shuYunHttpRequest(httpMethod, null, bodyParams, actionMethod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 会员注册
|
|
|
|
|
*/
|
|
|
|
|
public static void registerMember(ShuYunMember member) {
|
|
|
|
|
try {
|
|
|
|
|
shuYunHttpRequest(HttpMethod.POST, null, JSON.toJSONString(member),
|
|
|
|
|
shuYunHttpRequest(HttpMethod.POST, null, member,
|
|
|
|
|
shuYunConfig.getActionMethod().getRegisterMember());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求数云接口注册会员失败:{}", e.getMessage());
|
|
|
|
|
@ -141,7 +149,7 @@ public class ShuYunApiUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static ShuYunMember queryMember(ShuYunMember member) {
|
|
|
|
|
try {
|
|
|
|
|
R r = shuYunHttpRequest(HttpMethod.POST, null, JSON.toJSONString(member),
|
|
|
|
|
R r = shuYunHttpRequest(HttpMethod.POST, null, member,
|
|
|
|
|
shuYunConfig.getActionMethod().getQueryMember());
|
|
|
|
|
if (r.getCode() == ShuYunHttpStatusConstants.SUCCESS) {
|
|
|
|
|
return JSON.parseObject(r.getData().toString(), ShuYunMember.class);
|
|
|
|
|
@ -163,7 +171,7 @@ public class ShuYunApiUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static void modifyMember(ShuYunMember member) {
|
|
|
|
|
try {
|
|
|
|
|
shuYunHttpRequest(HttpMethod.PUT, null, JSON.toJSONString(member),
|
|
|
|
|
shuYunHttpRequest(HttpMethod.PUT, null, member,
|
|
|
|
|
shuYunConfig.getActionMethod().getModifyMember());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求数云接口-修改会员信息(除手机号)失败:{}", e.getMessage());
|
|
|
|
|
@ -176,7 +184,7 @@ public class ShuYunApiUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static void modifyMemberMobile(ShuYunMember member) {
|
|
|
|
|
try {
|
|
|
|
|
shuYunHttpRequest(HttpMethod.PUT, null, JSON.toJSONString(member),
|
|
|
|
|
shuYunHttpRequest(HttpMethod.PUT, null, member,
|
|
|
|
|
shuYunConfig.getActionMethod().getModifyMemberMobile());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求数云接口-修改会员手机号失败:{}", e.getMessage());
|
|
|
|
|
@ -189,7 +197,7 @@ public class ShuYunApiUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static void unbindMember(ShuYunMember member) {
|
|
|
|
|
try {
|
|
|
|
|
shuYunHttpRequest(HttpMethod.POST, null, JSON.toJSONString(member),
|
|
|
|
|
shuYunHttpRequest(HttpMethod.POST, null, member,
|
|
|
|
|
shuYunConfig.getActionMethod().getUnbindMember());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求数云接口-解绑会员失败:{}", e.getMessage());
|
|
|
|
|
@ -202,7 +210,7 @@ public class ShuYunApiUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static void pointChange(ShuYunPointChange shuYunPointChange) {
|
|
|
|
|
try {
|
|
|
|
|
shuYunHttpRequest(HttpMethod.POST, null, JSON.toJSONString(shuYunPointChange),
|
|
|
|
|
shuYunHttpRequest(HttpMethod.POST, null, shuYunPointChange,
|
|
|
|
|
shuYunConfig.getActionMethod().getPointChange());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求数云接口-会员积分变更失败:{}", e.getMessage());
|
|
|
|
|
@ -225,7 +233,8 @@ public class ShuYunApiUtils {
|
|
|
|
|
if (r.getCode() != ShuYunHttpStatusConstants.SUCCESS) {
|
|
|
|
|
logger.error("请求数云接口-会员积分变更记录查询失败:{}", r.getMsg());
|
|
|
|
|
}
|
|
|
|
|
return JSON.parseObject(r.getData().toString(), new TypeReference<ShuYunPageUtil<ShuYunPointChangeLog>>(){});
|
|
|
|
|
return JSON.parseObject(r.getData().toString(), new TypeReference<ShuYunPageUtil<ShuYunPointChangeLog>>() {
|
|
|
|
|
});
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求数云接口-会员积分变更记录查询失败:{}", e.getMessage());
|
|
|
|
|
throw new ServiceException("请求数云接口-会员积分变更记录查询失败");
|
|
|
|
|
@ -257,21 +266,35 @@ public class ShuYunApiUtils {
|
|
|
|
|
/**
|
|
|
|
|
* 创建标签
|
|
|
|
|
*/
|
|
|
|
|
public static void tagCreate(ShuYunTag shuYunTag) {
|
|
|
|
|
public static String tagCreate(ShuYunTag shuYunTag) {
|
|
|
|
|
try {
|
|
|
|
|
R r = shuYunHttpRequest(HttpMethod.POST, null, JSON.toJSONString(shuYunTag),
|
|
|
|
|
R r = shuYunHttpRequest(HttpMethod.POST, null, shuYunTag,
|
|
|
|
|
shuYunConfig.getActionMethod().getTagCreate());
|
|
|
|
|
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 null;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求数云接口-创建标签失败:{}", e.getMessage());
|
|
|
|
|
throw new ServiceException("请求数云接口-创建标签失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void tagSearch(Map params) {
|
|
|
|
|
/**
|
|
|
|
|
* 查询标签
|
|
|
|
|
*
|
|
|
|
|
* @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 = shuYunHttpRequest(HttpMethod.GET, params, null,
|
|
|
|
|
shuYunConfig.getActionMethod().getTagSearch());
|
|
|
|
|
@ -279,11 +302,121 @@ public class ShuYunApiUtils {
|
|
|
|
|
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("请求数云接口-创建标签失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除标签
|
|
|
|
|
*/
|
|
|
|
|
public static void tagDelete(String tagId) {
|
|
|
|
|
Map<String, String> params = MapUtil.newHashMap();
|
|
|
|
|
params.put("tagId", tagId);
|
|
|
|
|
try {
|
|
|
|
|
R r = shuYunHttpRequest(HttpMethod.DELETE, null, params,
|
|
|
|
|
shuYunConfig.getActionMethod().getTagDelete());
|
|
|
|
|
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
|
|
|
|
|
logger.error("请求数云接口-删除标签失败:{}", r.getMsg());
|
|
|
|
|
throw new ServiceException("请求数云接口-删除标签失败");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求数云接口-删除标签失败:{}", e.getMessage());
|
|
|
|
|
throw new ServiceException("请求数云接口-删除标签失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改标签
|
|
|
|
|
*/
|
|
|
|
|
public static void tagUpdate(ShuYunTag shuYunTag) {
|
|
|
|
|
try {
|
|
|
|
|
R r = shuYunHttpRequest(HttpMethod.PUT, null, shuYunTag,
|
|
|
|
|
shuYunConfig.getActionMethod().getTagUpdate());
|
|
|
|
|
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
|
|
|
|
|
logger.error("请求数云接口-修改标签失败:{}", r.getMsg());
|
|
|
|
|
throw new ServiceException("请求数云接口-修改标签失败");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求数云接口-修改标签失败:{}", e.getMessage());
|
|
|
|
|
throw new ServiceException("请求数云接口-修改标签失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 给客户打上单个标签
|
|
|
|
|
*/
|
|
|
|
|
public static void markUserTag(ShuYunUserTagReq shuYunUserTagReq) {
|
|
|
|
|
try {
|
|
|
|
|
R r = shuYunHttpRequest(HttpMethod.POST, null, shuYunUserTagReq,
|
|
|
|
|
shuYunConfig.getActionMethod().getMarkUserTag());
|
|
|
|
|
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
|
|
|
|
|
logger.error("请求数云接口-给客户打上单个标签失败:{}", r.getMsg());
|
|
|
|
|
throw new ServiceException("请求数云接口-给客户打上单个标签失败");
|
|
|
|
|
}
|
|
|
|
|
} 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 = shuYunHttpRequest(HttpMethod.GET, params, null,
|
|
|
|
|
shuYunConfig.getActionMethod().getSearchUserTag());
|
|
|
|
|
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 void 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 = shuYunHttpRequest(HttpMethod.DELETE, null, params,
|
|
|
|
|
shuYunConfig.getActionMethod().getDeleteUserTag());
|
|
|
|
|
if (r.getCode() == ShuYunHttpStatusConstants.HALF_SUCCESS) {
|
|
|
|
|
logger.error("请求数云接口-删除客户已经被打上的标签失败:{}", r.getMsg());
|
|
|
|
|
throw new ServiceException("请求数云接口-删除客户已经被打上的标签失败");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求数云接口-删除客户已经被打上的标签失败:{}", e.getMessage());
|
|
|
|
|
throw new ServiceException("请求数云接口-删除客户已经被打上的标签失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|