mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-09-29 16:06:36 +08:00
增加网站配置
This commit is contained in:
@@ -57,6 +57,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
registry.addInterceptor(jwtInterceptor())
|
||||
.addPathPatterns("/api/**")
|
||||
.excludePathPatterns("/flow/**")
|
||||
.excludePathPatterns("/api/v1/config/get")
|
||||
.excludePathPatterns("/api/v1/user/login");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,4 +20,6 @@ public class BaseController {
|
||||
@Autowired
|
||||
ForwardService forwardService;
|
||||
|
||||
@Autowired
|
||||
ViteConfigService viteConfigService;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.admin.controller;
|
||||
|
||||
|
||||
import com.admin.common.annotation.RequireRole;
|
||||
import com.admin.common.aop.LogAnnotation;
|
||||
import com.admin.common.lang.R;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 网站配置控制器
|
||||
* </p>
|
||||
*
|
||||
* @author QAQ
|
||||
* @since 2025-07-24
|
||||
*/
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
@RequestMapping("/api/v1/config")
|
||||
public class ViteConfigController extends BaseController {
|
||||
|
||||
/**
|
||||
* 获取所有网站配置
|
||||
* 前端无需权限即可访问,用于获取网站基本信息
|
||||
*/
|
||||
@LogAnnotation
|
||||
@PostMapping("/list")
|
||||
public R getConfigs() {
|
||||
return viteConfigService.getConfigs();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置名获取配置值
|
||||
* 前端无需权限即可访问,用于获取特定配置
|
||||
*/
|
||||
@LogAnnotation
|
||||
@PostMapping("/get")
|
||||
public R getConfigByName(@RequestBody Map<String, Object> params) {
|
||||
String name = params.get("name").toString();
|
||||
return viteConfigService.getConfigByName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新网站配置
|
||||
* 需要管理员权限
|
||||
*/
|
||||
@LogAnnotation
|
||||
@RequireRole
|
||||
@PostMapping("/update")
|
||||
public R updateConfigs(@RequestBody Map<String, String> configMap) {
|
||||
return viteConfigService.updateConfigs(configMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新单个配置项
|
||||
* 需要管理员权限
|
||||
*/
|
||||
@LogAnnotation
|
||||
@RequireRole
|
||||
@PostMapping("/update-single")
|
||||
public R updateConfig(@RequestBody Map<String, Object> params) {
|
||||
String name = params.get("name").toString();
|
||||
String value = params.get("value").toString();
|
||||
return viteConfigService.updateConfig(name, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.admin.entity;
|
||||
|
||||
import com.admin.entity.BaseEntity;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </p>
|
||||
*
|
||||
* @author QAQ
|
||||
* @since 2025-07-24
|
||||
*/
|
||||
@Data
|
||||
public class ViteConfig {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String value;
|
||||
|
||||
private Long time;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.admin.mapper;
|
||||
|
||||
import com.admin.entity.ViteConfig;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author QAQ
|
||||
* @since 2025-07-24
|
||||
*/
|
||||
public interface ViteConfigMapper extends BaseMapper<ViteConfig> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.admin.service;
|
||||
|
||||
import com.admin.entity.ViteConfig;
|
||||
import com.admin.common.lang.R;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 网站配置服务类
|
||||
* </p>
|
||||
*
|
||||
* @author QAQ
|
||||
* @since 2025-07-24
|
||||
*/
|
||||
public interface ViteConfigService extends IService<ViteConfig> {
|
||||
|
||||
/**
|
||||
* 获取所有网站配置
|
||||
* @return 配置Map,key为配置名,value为配置值
|
||||
*/
|
||||
R getConfigs();
|
||||
|
||||
/**
|
||||
* 根据配置名获取配置值
|
||||
* @param name 配置名
|
||||
* @return 配置值
|
||||
*/
|
||||
R getConfigByName(String name);
|
||||
|
||||
/**
|
||||
* 批量更新网站配置
|
||||
* @param configMap 配置Map,key为配置名,value为新的配置值
|
||||
* @return 更新结果
|
||||
*/
|
||||
R updateConfigs(Map<String, String> configMap);
|
||||
|
||||
/**
|
||||
* 更新单个配置项
|
||||
* @param name 配置名
|
||||
* @param value 配置值
|
||||
* @return 更新结果
|
||||
*/
|
||||
R updateConfig(String name, String value);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
package com.admin.service.impl;
|
||||
|
||||
import com.admin.entity.ViteConfig;
|
||||
import com.admin.mapper.ViteConfigMapper;
|
||||
import com.admin.service.ViteConfigService;
|
||||
import com.admin.common.lang.R;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 网站配置服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author QAQ
|
||||
* @since 2025-07-24
|
||||
*/
|
||||
@Service
|
||||
public class ViteConfigServiceImpl extends ServiceImpl<ViteConfigMapper, ViteConfig> implements ViteConfigService {
|
||||
|
||||
// ========== 常量定义 ==========
|
||||
|
||||
/** 成功响应消息 */
|
||||
private static final String SUCCESS_UPDATE_MSG = "配置更新成功";
|
||||
|
||||
/** 错误响应消息 */
|
||||
private static final String ERROR_UPDATE_MSG = "配置更新失败";
|
||||
private static final String ERROR_CONFIG_NOT_FOUND = "配置不存在";
|
||||
private static final String ERROR_CONFIG_NAME_REQUIRED = "配置名称不能为空";
|
||||
private static final String ERROR_CONFIG_VALUE_REQUIRED = "配置值不能为空";
|
||||
|
||||
// ========== 公共接口实现 ==========
|
||||
|
||||
/**
|
||||
* 获取所有网站配置
|
||||
*
|
||||
* @return 包含所有配置的Map
|
||||
*/
|
||||
@Override
|
||||
public R getConfigs() {
|
||||
List<ViteConfig> configList = this.list();
|
||||
Map<String, String> configMap = new HashMap<>();
|
||||
|
||||
for (ViteConfig config : configList) {
|
||||
configMap.put(config.getName(), config.getValue());
|
||||
}
|
||||
|
||||
return R.ok(configMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置名称获取配置值
|
||||
*
|
||||
* @param name 配置名称
|
||||
* @return 配置响应对象
|
||||
*/
|
||||
@Override
|
||||
public R getConfigByName(String name) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
return R.err(ERROR_CONFIG_NAME_REQUIRED);
|
||||
}
|
||||
|
||||
QueryWrapper<ViteConfig> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("name", name);
|
||||
ViteConfig config = this.getOne(queryWrapper);
|
||||
|
||||
if (config == null) {
|
||||
return R.err(ERROR_CONFIG_NOT_FOUND);
|
||||
}
|
||||
|
||||
return R.ok(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新网站配置
|
||||
*
|
||||
* @param configMap 配置Map
|
||||
* @return 更新结果响应
|
||||
*/
|
||||
@Override
|
||||
public R updateConfigs(Map<String, String> configMap) {
|
||||
if (configMap == null || configMap.isEmpty()) {
|
||||
return R.err("配置数据不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
for (Map.Entry<String, String> entry : configMap.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
String value = entry.getValue();
|
||||
|
||||
if (!StringUtils.hasText(name)) {
|
||||
continue; // 跳过无效的配置名
|
||||
}
|
||||
|
||||
updateOrCreateConfig(name, value);
|
||||
}
|
||||
return R.ok(SUCCESS_UPDATE_MSG);
|
||||
} catch (Exception e) {
|
||||
return R.err(ERROR_UPDATE_MSG + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新单个配置项
|
||||
*
|
||||
* @param name 配置名
|
||||
* @param value 配置值
|
||||
* @return 更新结果响应
|
||||
*/
|
||||
@Override
|
||||
public R updateConfig(String name, String value) {
|
||||
// 1. 验证必填字段
|
||||
if (!StringUtils.hasText(name)) {
|
||||
return R.err(ERROR_CONFIG_NAME_REQUIRED);
|
||||
}
|
||||
if (!StringUtils.hasText(value)) {
|
||||
return R.err(ERROR_CONFIG_VALUE_REQUIRED);
|
||||
}
|
||||
|
||||
try {
|
||||
updateOrCreateConfig(name, value);
|
||||
return R.ok(SUCCESS_UPDATE_MSG);
|
||||
} catch (Exception e) {
|
||||
return R.err(ERROR_UPDATE_MSG + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 私有辅助方法 ==========
|
||||
|
||||
/**
|
||||
* 更新或创建配置项
|
||||
* 如果配置存在则更新,不存在则创建
|
||||
*/
|
||||
private void updateOrCreateConfig(String name, String value) {
|
||||
QueryWrapper<ViteConfig> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("name", name);
|
||||
ViteConfig existingConfig = this.getOne(queryWrapper);
|
||||
|
||||
if (existingConfig != null) {
|
||||
// 更新现有配置
|
||||
existingConfig.setValue(value);
|
||||
existingConfig.setTime(System.currentTimeMillis());
|
||||
this.updateById(existingConfig);
|
||||
} else {
|
||||
// 创建新配置
|
||||
ViteConfig newConfig = new ViteConfig();
|
||||
newConfig.setName(name);
|
||||
newConfig.setValue(value);
|
||||
newConfig.setTime(System.currentTimeMillis());
|
||||
this.save(newConfig);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.admin.mapper.ViteConfigMapper">
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user