|
@ -1,12 +1,20 @@ |
|
|
package com.epmet.service.impl; |
|
|
package com.epmet.service.impl; |
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
|
|
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
|
|
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
|
|
import com.epmet.commons.tools.redis.RedisKeys; |
|
|
import com.epmet.commons.tools.utils.ConvertUtils; |
|
|
import com.epmet.commons.tools.utils.ConvertUtils; |
|
|
import com.epmet.dao.CorsConfigDao; |
|
|
import com.epmet.dao.CorsConfigDao; |
|
|
import com.epmet.dto.result.CorsConfigResultDTO; |
|
|
import com.epmet.dto.result.CorsConfigResultDTO; |
|
|
import com.epmet.entity.CorsConfigEntity; |
|
|
import com.epmet.entity.CorsConfigEntity; |
|
|
import com.epmet.service.CorsConfigService; |
|
|
import com.epmet.service.CorsConfigService; |
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.data.redis.core.HashOperations; |
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
import java.util.List; |
|
@ -22,6 +30,11 @@ public class CorsConfigServiceImpl implements CorsConfigService { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private CorsConfigDao corsConfigDao; |
|
|
private CorsConfigDao corsConfigDao; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private RedisTemplate redisTemplate; |
|
|
|
|
|
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @Description 查询配置列表 |
|
|
* @Description 查询配置列表 |
|
|
* @return |
|
|
* @return |
|
@ -33,4 +46,65 @@ public class CorsConfigServiceImpl implements CorsConfigService { |
|
|
LambdaQueryWrapper<CorsConfigEntity> conditions = new LambdaQueryWrapper<>(); |
|
|
LambdaQueryWrapper<CorsConfigEntity> conditions = new LambdaQueryWrapper<>(); |
|
|
return ConvertUtils.sourceToTarget(corsConfigDao.selectList(conditions), CorsConfigResultDTO.class); |
|
|
return ConvertUtils.sourceToTarget(corsConfigDao.selectList(conditions), CorsConfigResultDTO.class); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public CorsConfigResultDTO addConfig(String headerType, String headerValue, String comment) { |
|
|
|
|
|
LambdaQueryWrapper<CorsConfigEntity> w = new LambdaQueryWrapper<>(); |
|
|
|
|
|
w.eq(CorsConfigEntity::getHeaderType, headerType.trim()); |
|
|
|
|
|
w.eq(CorsConfigEntity::getHeaderValue, headerValue.trim()); |
|
|
|
|
|
if (corsConfigDao.selectCount(w) > 0) { |
|
|
|
|
|
throw new RenException(EpmetErrorCode.OPER_CORS_CONFIG_ERROR.getCode(), "已存在该跨域配置,请勿重复添加"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CorsConfigEntity insert = new CorsConfigEntity(); |
|
|
|
|
|
insert.setHeaderType(headerType); |
|
|
|
|
|
insert.setHeaderValue(headerValue); |
|
|
|
|
|
insert.setComment(comment); |
|
|
|
|
|
if (corsConfigDao.insert(insert) == 0) { |
|
|
|
|
|
throw new RenException(EpmetErrorCode.OPER_CORS_CONFIG_ERROR.getCode(), "插入跨域配置出错"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
deleteFromRedis(); |
|
|
|
|
|
return new CorsConfigResultDTO(insert.getId(), headerType, headerValue, comment); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void deleteById(String configId) { |
|
|
|
|
|
if (corsConfigDao.deleteById(configId) == 0) { |
|
|
|
|
|
String msg = "删除出错,不存在该项数据"; |
|
|
|
|
|
throw new RenException(EpmetErrorCode.OPER_CORS_CONFIG_ERROR.getCode(), msg, msg, RenException.MessageMode.CODE_INTERNAL_EXTERNAL); |
|
|
|
|
|
} |
|
|
|
|
|
deleteFromRedis(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void updateById(String configId, String headerType, String headerValue, String comment) { |
|
|
|
|
|
CorsConfigEntity exists = corsConfigDao.selectById(configId); |
|
|
|
|
|
if (exists == null) { |
|
|
|
|
|
String msg = "该项数据不存在"; |
|
|
|
|
|
throw new RenException(EpmetErrorCode.OPER_CORS_CONFIG_ERROR.getCode(), msg, msg, RenException.MessageMode.CODE_INTERNAL_EXTERNAL); |
|
|
|
|
|
} |
|
|
|
|
|
exists.setHeaderType(headerType); |
|
|
|
|
|
exists.setHeaderValue(headerValue); |
|
|
|
|
|
exists.setComment(comment); |
|
|
|
|
|
if (corsConfigDao.updateById(exists) == 0) { |
|
|
|
|
|
throw new RenException(EpmetErrorCode.OPER_CORS_CONFIG_ERROR.getCode(), "更新出错,影响条数为0"); |
|
|
|
|
|
} |
|
|
|
|
|
deleteFromRedis(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @Description 从缓存中删除 |
|
|
|
|
|
* @return |
|
|
|
|
|
* @author wxz |
|
|
|
|
|
* @date 2021.06.25 14:26 |
|
|
|
|
|
*/ |
|
|
|
|
|
private void deleteFromRedis() { |
|
|
|
|
|
try { |
|
|
|
|
|
redisTemplate.delete(RedisKeys.getCorsConfigKey()); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
String errorMsg = ExceptionUtils.getErrorStackTrace(e); |
|
|
|
|
|
logger.error(String.format("从Redis中删除Cors跨域信息失败,程序继续执行。错误信息:%s", errorMsg)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|