This commit is contained in:
2025-12-31 12:15:21 +08:00
parent c72bb3270d
commit 5d4636ccc8
7 changed files with 22 additions and 42 deletions

View File

@@ -7,7 +7,6 @@ import java.lang.annotation.Target;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
* 将该注解用在配置上,给后台 ConfigController 自动渲染配置页用

View File

@@ -16,7 +16,6 @@ import link.at17.mid.tushare.annotation.StaticAttribute;
import link.at17.mid.tushare.data.models.UpdateMethodInfo;
import link.at17.mid.tushare.service.UpdateMethodService;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

View File

@@ -14,7 +14,6 @@ import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.Temporal;
import java.util.List;

View File

@@ -7,8 +7,6 @@ import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import link.at17.mid.tushare.data.typehandler.JsonListTypeHandler;
import link.at17.mid.tushare.system.util.EncryptUtils;
import lombok.Data;

View File

@@ -1,11 +1,7 @@
package link.at17.mid.tushare.interfaces;
import java.util.Objects;
import org.springframework.stereotype.Component;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import jakarta.annotation.PostConstruct;
import link.at17.mid.tushare.service.ConfigService;
import link.at17.mid.tushare.system.util.SpringContextHolder;
@@ -40,32 +36,6 @@ public interface IConfig<T extends IConfig<T>> {
return (T) this;
}
/**
* 合并到 other且返回合并后的 other
* @return
*/
public default T mergeTo(T other) {
if (!Objects.equals(this, other)) {
BeanUtil.copyProperties(this, other,
CopyOptions.create().setIgnoreNullValue(true));
}
return other;
}
/**
* 合并其他,并返回本身
* @param other
* @return
*/
@SuppressWarnings("unchecked")
public default T mergeFrom(T other) {
if (!Objects.equals(this, other)) {
BeanUtil.copyProperties(other, this,
CopyOptions.create().setIgnoreNullValue(true));
}
return (T) this;
}
/**
* 初始化完成之后的方法,会在
* <code><i>beanFactory</i>.autowireBean(bean)</code>

View File

@@ -1,8 +1,11 @@
package link.at17.mid.tushare.web.controller;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Controller;
@@ -47,6 +50,15 @@ public class ConfigController extends BaseController {
return "admin/manage/reviews/set/system/" + configField;
}
/**
* 保存配置项统一接口
* <p>
* 注意该接口和系统初始化配置接口不一样,虽然从 json 中恢复配置,
* 但只会替换 json 中指明的字段的值, 其他值不会进行置换
* </p>
* @param configField 注解 @ConfigInfo 的 field
* @param config 配置 json
*/
@PostMapping("/set/system/{configField}.html")
@ResponseBody
public <T extends IConfig<T>> R<?> saveConfig(@PathVariable String configField, @Validated @RequestBody JsonNode config) throws Exception {
@@ -55,7 +67,14 @@ public class ConfigController extends BaseController {
throw new PageNotFoundException();
}
return R.judgeThrow(() -> {
// 找出其中有的字段
Set<String> jsonFields = new HashSet<>();
config.fieldNames().forEachRemaining(jsonFields::add);
T newConfig = (T)new ObjectMapper().treeToValue(config, clazz);
T oldConfig = configService.getConfig(clazz);
// 把除了本次更新以外的字段复制过去
BeanUtils.copyProperties(oldConfig, newConfig, ArrayUtils.toStringArray(jsonFields.toArray()));
Method method = this.getClass().getMethod("saveConfig", String.class, JsonNode.class);
MethodParameter methodParameter = new MethodParameter(method, 1);
@@ -76,9 +95,7 @@ public class ConfigController extends BaseController {
}
}
T oldConfig = configService.getConfig(clazz);
oldConfig.mergeFrom(newConfig);
return oldConfig.saveOrUpdate();
return newConfig.saveOrUpdate();
});
}

View File

@@ -1,8 +1,6 @@
package link.at17.mid.tushare.web.controller;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Validator;