|
|
@@ -124,9 +124,9 @@ public class QwAcquisitionAssistantServiceImpl implements IQwAcquisitionAssistan
|
|
|
/**
|
|
|
* 调用企微API并统一处理返回结果
|
|
|
*/
|
|
|
- private <T> T callQwApi(String url, Object request, Class<T> responseClass, String operationName) {
|
|
|
+ private <T> T callQwApi(String qwApiUrl, Object request, Class<T> responseClass, String operationName) {
|
|
|
log.info("调用企微{},参数:{}", operationName, JSON.toJSONString(request));
|
|
|
- String result = HttpUtil.sendAuthPost(request, url);
|
|
|
+ String result = HttpUtil.sendAuthPost(request, qwApiUrl);
|
|
|
log.info("企微{}响应:{}", operationName, result);
|
|
|
|
|
|
if (StringUtils.isEmpty(result)) {
|
|
|
@@ -175,10 +175,10 @@ public class QwAcquisitionAssistantServiceImpl implements IQwAcquisitionAssistan
|
|
|
/**
|
|
|
* 生成scheme
|
|
|
*/
|
|
|
- private String generateScheme(String url) {
|
|
|
- if (StringUtils.isNotEmpty(url)) {
|
|
|
+ private String generateScheme(String qwApiUrl) {
|
|
|
+ if (StringUtils.isNotEmpty(qwApiUrl)) {
|
|
|
try {
|
|
|
- String encodedUrl = java.net.URLEncoder.encode(url, "UTF-8");
|
|
|
+ String encodedUrl = java.net.URLEncoder.encode(qwApiUrl, "UTF-8");
|
|
|
return "weixin://biz/ww/profile/" + encodedUrl;
|
|
|
} catch (Exception e) {
|
|
|
log.warn("生成scheme失败", e);
|
|
|
@@ -218,8 +218,8 @@ public class QwAcquisitionAssistantServiceImpl implements IQwAcquisitionAssistan
|
|
|
}
|
|
|
request.setCursor(cursor);
|
|
|
|
|
|
- String url = buildApiUrl(corpid, corpsecret, QwApiConfig.listAcquisition);
|
|
|
- return callQwApi(url, request, AcquisitionListResponse.class, "获取获客链接列表");
|
|
|
+ String qwApiUrl = buildApiUrl(corpid, corpsecret, QwApiConfig.listAcquisition);
|
|
|
+ return callQwApi(qwApiUrl, request, AcquisitionListResponse.class, "获取获客链接列表");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -431,7 +431,7 @@ public class QwAcquisitionAssistantServiceImpl implements IQwAcquisitionAssistan
|
|
|
// ==================== 获取详情方法 ====================
|
|
|
|
|
|
@Override
|
|
|
- public AcquisitionAssistantDetailVO getDetailWithQw( String linkId) {
|
|
|
+ public AcquisitionAssistantDetailVO getDetailWithQw(String linkId) {
|
|
|
if (StringUtils.isEmpty(linkId)) {
|
|
|
throw new CustomException("链接ID不能为空");
|
|
|
}
|
|
|
@@ -454,8 +454,8 @@ public class QwAcquisitionAssistantServiceImpl implements IQwAcquisitionAssistan
|
|
|
AcquisitionGetRequest request = new AcquisitionGetRequest();
|
|
|
request.setLinkId(linkId);
|
|
|
|
|
|
- String url = buildApiUrl(qwCompany.getCorpId(), qwCompany.getOpenSecret(), QwApiConfig.getAcquisition);
|
|
|
- qwDetail = callQwApi(url, request, AcquisitionGetResponse.class, "获取获客链接详情");
|
|
|
+ String qwApiUrl = buildApiUrl(qwCompany.getCorpId(), qwCompany.getOpenSecret(), QwApiConfig.getAcquisition);
|
|
|
+ qwDetail = callQwApi(qwApiUrl, request, AcquisitionGetResponse.class, "获取获客链接详情");
|
|
|
} catch (Exception e) {
|
|
|
log.error("调用企微API失败", e);
|
|
|
// 如果API调用失败,就返回本地数据
|
|
|
@@ -532,27 +532,44 @@ public class QwAcquisitionAssistantServiceImpl implements IQwAcquisitionAssistan
|
|
|
|
|
|
request.put("priority_option", priorityOption);
|
|
|
}
|
|
|
- //生成随机网页参数编码(字母+数字)
|
|
|
- String randomParam = UniqueStringUtil.generateShortUnique();
|
|
|
- assistant.setPageParam(randomParam);
|
|
|
// 调用企微API
|
|
|
- String url = buildApiUrl(corpid, corpsecret, QwApiConfig.createAcquisition);
|
|
|
+ String qwApiUrl = buildApiUrl(corpid, corpsecret, QwApiConfig.createAcquisition);
|
|
|
|
|
|
- AcquisitionCreateResponse response = callQwApi(url, request, AcquisitionCreateResponse.class, "创建获客链接");
|
|
|
+ AcquisitionCreateResponse response = callQwApi(qwApiUrl, request, AcquisitionCreateResponse.class, "创建获客链接");
|
|
|
+ if (response.getLink() == null || StringUtils.isEmpty(response.getLink().getUrl())) {
|
|
|
+ log.error("企微创建获客链接成功但未返回URL,response: {}", JSON.toJSONString(response));
|
|
|
+ throw new CustomException("创建获客链接成功但未获取到访问URL");
|
|
|
+ }
|
|
|
+ /// 设置企微返回的数据
|
|
|
+ String friendUrl = response.getLink().getUrl();
|
|
|
+ assistant.setUrl(friendUrl); // 保存企微返回的URL
|
|
|
+ assistant.setLinkId(response.getLink().getLinkId());
|
|
|
|
|
|
- // 设置企微返回的数据
|
|
|
- if (response.getLink() != null) {
|
|
|
- assistant.setLinkId(response.getLink().getLinkId());
|
|
|
- assistant.setUrl(response.getLink().getUrl());
|
|
|
- if (response.getLink().getCreateTime() != null) {
|
|
|
- assistant.setQwCreateTime(new Date(response.getLink().getCreateTime() * 1000));
|
|
|
- }
|
|
|
+ if (response.getLink().getCreateTime() != null) {
|
|
|
+ assistant.setQwCreateTime(new Date(response.getLink().getCreateTime() * 1000));
|
|
|
}
|
|
|
|
|
|
+ //生成随机网页参数编码(字母+数字)
|
|
|
+ String randomParam = UniqueStringUtil.generateShortUnique();
|
|
|
+ assistant.setPageParam(randomParam);
|
|
|
+
|
|
|
+ // 生成scheme
|
|
|
+ assistant.setScheme(generateScheme(friendUrl));
|
|
|
+
|
|
|
// 设置本地字段并保存
|
|
|
setLocalFields(assistant, true);
|
|
|
qwAcquisitionAssistantMapper.insertQwAcquisitionAssistant(assistant);
|
|
|
|
|
|
+ // ========== 缓存URL,便于后续通过pageParam访问 ==========
|
|
|
+ try {
|
|
|
+ String cacheKey = QW_ACQUISITION_URL_KEY_PREFIX + randomParam;
|
|
|
+ Integer cacheExpire = 10; // 默认缓存10天
|
|
|
+ redisCache.setCacheObject(cacheKey, friendUrl, cacheExpire, TimeUnit.DAYS);
|
|
|
+ log.info("获客链接URL缓存成功, pageParam: {}, url: {}", randomParam, friendUrl);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 缓存失败不影响主流程,但需要记录日志
|
|
|
+ log.error("获客链接URL缓存失败, pageParam: {}", randomParam, e);
|
|
|
+ }
|
|
|
return assistant;
|
|
|
}
|
|
|
|
|
|
@@ -586,11 +603,21 @@ public class QwAcquisitionAssistantServiceImpl implements IQwAcquisitionAssistan
|
|
|
//request.setMarkSource(assistant.getMarkSource());
|
|
|
|
|
|
// 调用企微API
|
|
|
- String url = buildApiUrl(corpid, corpsecret, QwApiConfig.updateAcquisition);
|
|
|
- AcquisitionUpdateResponse response = callQwApi(url, request, AcquisitionUpdateResponse.class, "更新获客链接");
|
|
|
+ String qwApiUrl = buildApiUrl(corpid, corpsecret, QwApiConfig.updateAcquisition);
|
|
|
+ AcquisitionUpdateResponse response = callQwApi(qwApiUrl, request, AcquisitionUpdateResponse.class, "更新获客链接");
|
|
|
|
|
|
// 更新本地字段
|
|
|
setLocalFields(assistant, false);
|
|
|
+ //TODO重新生成页面参数,需要修改对应redis缓存
|
|
|
+ String oldPageParam = existAssistant.getPageParam();
|
|
|
+ String oldKey = QW_ACQUISITION_URL_KEY_PREFIX + oldPageParam;
|
|
|
+ redisCache.deleteObject(oldKey);
|
|
|
+ String newPageParam = UniqueStringUtil.generateShortUnique();
|
|
|
+ String newKey = QW_ACQUISITION_URL_KEY_PREFIX + newPageParam;
|
|
|
+ Integer cacheExpire = 10;//默认缓存10天
|
|
|
+ redisCache.setCacheObject(newKey, existAssistant.getUrl(), cacheExpire, TimeUnit.DAYS);
|
|
|
+
|
|
|
+ assistant.setPageParam(newPageParam);
|
|
|
|
|
|
int rows = qwAcquisitionAssistantMapper.updateQwAcquisitionAssistant(assistant);
|
|
|
if (rows <= 0) {
|
|
|
@@ -613,7 +640,7 @@ public class QwAcquisitionAssistantServiceImpl implements IQwAcquisitionAssistan
|
|
|
throw new CustomException("链接ID不能为空");
|
|
|
}
|
|
|
|
|
|
- // 查询本地是否存在
|
|
|
+ // 查询本地是否存在(获取完整的记录,包括pageParam)
|
|
|
QwAcquisitionAssistant existAssistant = qwAcquisitionAssistantMapper.selectQwAcquisitionAssistantById(assistant.getId());
|
|
|
if (existAssistant == null) {
|
|
|
throw new CustomException("获客链接不存在");
|
|
|
@@ -624,14 +651,34 @@ public class QwAcquisitionAssistantServiceImpl implements IQwAcquisitionAssistan
|
|
|
request.put("link_id", assistant.getLinkId());
|
|
|
|
|
|
// 调用企微API
|
|
|
- String url = buildApiUrl(corpid, corpsecret, QwApiConfig.deleteAcquisition);
|
|
|
- AcquisitionDeleteResponse response = callQwApi(url, request, AcquisitionDeleteResponse.class, "删除获客链接");
|
|
|
+ String qwApiUrl = buildApiUrl(corpid, corpsecret, QwApiConfig.deleteAcquisition);
|
|
|
+ AcquisitionDeleteResponse response = callQwApi(qwApiUrl, request, AcquisitionDeleteResponse.class, "删除获客链接");
|
|
|
+
|
|
|
+ // ========== 删除Redis缓存 ==========
|
|
|
+ try {
|
|
|
+ // 1. 删除pageParam对应的URL缓存
|
|
|
+ if (StringUtils.isNotEmpty(existAssistant.getPageParam())) {
|
|
|
+ String urlCacheKey = QW_ACQUISITION_URL_KEY_PREFIX + existAssistant.getPageParam();
|
|
|
+ redisCache.deleteObject(urlCacheKey);
|
|
|
+ log.info("删除获客链接URL缓存成功, pageParam: {}, key: {}",
|
|
|
+ existAssistant.getPageParam(), urlCacheKey);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 缓存删除失败不应该影响主流程,但需要记录日志
|
|
|
+ log.error("删除获客链接缓存失败, id: {}, linkId: {}, pageParam: {}",
|
|
|
+ existAssistant.getId(), existAssistant.getLinkId(),
|
|
|
+ existAssistant.getPageParam(), e);
|
|
|
+ }
|
|
|
|
|
|
// 删除本地记录
|
|
|
int rows = qwAcquisitionAssistantMapper.deleteQwAcquisitionAssistantById(assistant.getId());
|
|
|
if (rows <= 0) {
|
|
|
throw new CustomException("本地数据删除失败");
|
|
|
}
|
|
|
+
|
|
|
+ log.info("获客链接删除成功, id: {}, linkId: {}, pageParam: {}",
|
|
|
+ existAssistant.getId(), existAssistant.getLinkId(),
|
|
|
+ existAssistant.getPageParam());
|
|
|
}
|
|
|
|
|
|
// ==================== 查询方法 ====================
|
|
|
@@ -647,22 +694,40 @@ public class QwAcquisitionAssistantServiceImpl implements IQwAcquisitionAssistan
|
|
|
|
|
|
@Override
|
|
|
public String selectQwAcquisitionUrlByPageParam(String pageParam) {
|
|
|
- String key=QW_ACQUISITION_URL_KEY_PREFIX+pageParam;
|
|
|
- String url=null;
|
|
|
+ String key = QW_ACQUISITION_URL_KEY_PREFIX + pageParam;
|
|
|
+ String friendUrl = null;
|
|
|
+
|
|
|
try {
|
|
|
Object cacheObj = redisCache.getCacheObject(key);
|
|
|
if (cacheObj instanceof String) {
|
|
|
- url = (String) cacheObj;
|
|
|
+ friendUrl = (String) cacheObj;
|
|
|
+ // 处理缓存空值的情况
|
|
|
+ if ("NULL".equals(friendUrl)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
log.debug("从缓存获取获客链接url成功,pageParam:{}", pageParam);
|
|
|
+ return friendUrl;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.warn("从缓存获取获客链接url异常, 将重新获取, pageParam:{}", pageParam);
|
|
|
}
|
|
|
- if (url == null){
|
|
|
- url = qwAcquisitionAssistantMapper.selectQwAcquisitionUrlByPageParam(pageParam);
|
|
|
- Integer cacheExpire = 15;//默认缓存15天
|
|
|
- redisCache.setCacheObject(key, url, cacheExpire, TimeUnit.DAYS);
|
|
|
+
|
|
|
+ // 缓存中没有,查询数据库
|
|
|
+ friendUrl = qwAcquisitionAssistantMapper.selectQwAcquisitionUrlByPageParam(pageParam);
|
|
|
+
|
|
|
+ // 缓存处理(包括空值缓存)
|
|
|
+ if (friendUrl == null) {
|
|
|
+ int nullCacheExpire = 10; // 10秒
|
|
|
+ redisCache.setCacheObject(key, "NULL", nullCacheExpire, TimeUnit.SECONDS);
|
|
|
+ log.info("获客链接URL不存在,缓存空值10秒, pageParam:{}", pageParam);
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ // 正常值仍缓存10天
|
|
|
+ Integer cacheExpire = 10;
|
|
|
+ redisCache.setCacheObject(key, friendUrl, cacheExpire, TimeUnit.DAYS);
|
|
|
+ log.info("获客链接URL缓存成功, pageParam:{}", pageParam);
|
|
|
}
|
|
|
- return url;
|
|
|
+
|
|
|
+ return friendUrl;
|
|
|
}
|
|
|
}
|