110 lines
3.8 KiB
Java
110 lines
3.8 KiB
Java
package com.ruoyi.cai.kit;
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
import com.baomidou.dynamic.datasource.annotation.Slave;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.ruoyi.cai.domain.User;
|
|
import com.ruoyi.cai.enums.SystemConfigEnum;
|
|
import com.ruoyi.cai.manager.SystemConfigManager;
|
|
import com.ruoyi.cai.remote.Y4xClient;
|
|
import com.ruoyi.cai.remote.y4x.Y4xResponse;
|
|
import com.ruoyi.cai.service.UserService;
|
|
import com.ruoyi.cai.ws.constant.RedisConstant;
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.List;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
@Component
|
|
@Slf4j
|
|
public class ShareUrlKit {
|
|
|
|
@Autowired
|
|
private SystemConfigManager systemConfigManager;
|
|
@Autowired
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
@Autowired
|
|
private Y4xClient y4xClient;
|
|
@Autowired
|
|
private UserService userService;
|
|
|
|
private String getKey(String usercode){
|
|
return String.format(RedisConstant.Y4X_REDIS_CACHE, usercode);
|
|
}
|
|
|
|
public String getY4xCache(String usercode){
|
|
String key = getKey(usercode);
|
|
return stringRedisTemplate.opsForValue().get(key);
|
|
}
|
|
|
|
public void setY4xCache(String usercode,String url){
|
|
String key = getKey(usercode);
|
|
stringRedisTemplate.opsForValue().set(key,url,20, TimeUnit.DAYS);
|
|
}
|
|
|
|
public void cleanShareUrlCache(){
|
|
log.info("清除用户分享链接缓存 - 开始");
|
|
int current = 0;
|
|
while (true){
|
|
current++;
|
|
Page<User> page = new Page<>(current,100);
|
|
Page<User> data = userService.page(page);
|
|
List<User> records = data.getRecords();
|
|
if(records.isEmpty()){
|
|
break;
|
|
}
|
|
for (User record : records) {
|
|
try {
|
|
cleanShareUrlCacheByUsercode(record.getUsercode());
|
|
}catch (Exception e){
|
|
log.error("刷新用户分享链接缓存失败",e);
|
|
}
|
|
}
|
|
}
|
|
log.info("清除用户分享链接缓存 - 结束");
|
|
}
|
|
|
|
public void cleanShareUrlCacheByUsercode(String usercode){
|
|
String key = getKey(usercode);
|
|
stringRedisTemplate.delete(key);
|
|
}
|
|
|
|
public String getShareUrl(String usercode){
|
|
String shareType = systemConfigManager.getSystemConfig(SystemConfigEnum.SHARE_TYPE);
|
|
if("2".equals(shareType)){
|
|
String y4xCache = getY4xCache(usercode);
|
|
if(StringUtils.isNotBlank(y4xCache)){
|
|
return y4xCache;
|
|
}
|
|
String shareBaseUrl = systemConfigManager.getSystemConfig(SystemConfigEnum.SHARE_BASE_URL);
|
|
String shareUrl = shareBaseUrl + "?from="+usercode;
|
|
String type = systemConfigManager.getSystemConfig(SystemConfigEnum.Y4X_TYPE);
|
|
String token = systemConfigManager.getSystemConfig(SystemConfigEnum.Y4X_TOKEN);
|
|
try {
|
|
Y4xResponse url = y4xClient.getUrl(type, token, shareUrl);
|
|
if(StringUtils.isBlank(url.getDwz())){
|
|
log.error("y4x请求失败 response={}", JSON.toJSONString(url));
|
|
return getShareUrlVVVV(usercode);
|
|
}
|
|
setY4xCache(usercode,url.getDwz());
|
|
return url.getDwz();
|
|
}catch (Exception e){
|
|
log.error("y4x请求失败",e);
|
|
return getShareUrlVVVV(usercode);
|
|
}
|
|
}
|
|
return getShareUrlVVVV(usercode);
|
|
}
|
|
|
|
public String getShareUrlVVVV(String usercode){
|
|
String shareUrl = systemConfigManager.getSystemConfig(SystemConfigEnum.SHARE_URL);
|
|
return shareUrl + "?from="+usercode;
|
|
}
|
|
|
|
|
|
}
|