commit
This commit is contained in:
@@ -7,7 +7,6 @@ import java.lang.annotation.Target;
|
|||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将该注解用在配置上,给后台 ConfigController 自动渲染配置页用
|
* 将该注解用在配置上,给后台 ConfigController 自动渲染配置页用
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import link.at17.mid.tushare.annotation.StaticAttribute;
|
|||||||
import link.at17.mid.tushare.data.models.UpdateMethodInfo;
|
import link.at17.mid.tushare.data.models.UpdateMethodInfo;
|
||||||
import link.at17.mid.tushare.service.UpdateMethodService;
|
import link.at17.mid.tushare.service.UpdateMethodService;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import org.springframework.lang.NonNull;
|
|||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.temporal.Temporal;
|
import java.time.temporal.Temporal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
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.data.typehandler.JsonListTypeHandler;
|
||||||
import link.at17.mid.tushare.system.util.EncryptUtils;
|
import link.at17.mid.tushare.system.util.EncryptUtils;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
package link.at17.mid.tushare.interfaces;
|
package link.at17.mid.tushare.interfaces;
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
|
||||||
import cn.hutool.core.bean.copier.CopyOptions;
|
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import link.at17.mid.tushare.service.ConfigService;
|
import link.at17.mid.tushare.service.ConfigService;
|
||||||
import link.at17.mid.tushare.system.util.SpringContextHolder;
|
import link.at17.mid.tushare.system.util.SpringContextHolder;
|
||||||
@@ -40,32 +36,6 @@ public interface IConfig<T extends IConfig<T>> {
|
|||||||
return (T) this;
|
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>
|
* <code><i>beanFactory</i>.autowireBean(bean)</code>
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package link.at17.mid.tushare.web.controller;
|
package link.at17.mid.tushare.web.controller;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
@@ -47,6 +50,15 @@ public class ConfigController extends BaseController {
|
|||||||
return "admin/manage/reviews/set/system/" + configField;
|
return "admin/manage/reviews/set/system/" + configField;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存配置项统一接口
|
||||||
|
* <p>
|
||||||
|
* 注意该接口和系统初始化配置接口不一样,虽然从 json 中恢复配置,
|
||||||
|
* 但只会替换 json 中指明的字段的值, 其他值不会进行置换
|
||||||
|
* </p>
|
||||||
|
* @param configField 注解 @ConfigInfo 的 field
|
||||||
|
* @param config 配置 json
|
||||||
|
*/
|
||||||
@PostMapping("/set/system/{configField}.html")
|
@PostMapping("/set/system/{configField}.html")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public <T extends IConfig<T>> R<?> saveConfig(@PathVariable String configField, @Validated @RequestBody JsonNode config) throws Exception {
|
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();
|
throw new PageNotFoundException();
|
||||||
}
|
}
|
||||||
return R.judgeThrow(() -> {
|
return R.judgeThrow(() -> {
|
||||||
|
// 找出其中有的字段
|
||||||
|
Set<String> jsonFields = new HashSet<>();
|
||||||
|
config.fieldNames().forEachRemaining(jsonFields::add);
|
||||||
|
|
||||||
T newConfig = (T)new ObjectMapper().treeToValue(config, clazz);
|
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);
|
Method method = this.getClass().getMethod("saveConfig", String.class, JsonNode.class);
|
||||||
MethodParameter methodParameter = new MethodParameter(method, 1);
|
MethodParameter methodParameter = new MethodParameter(method, 1);
|
||||||
@@ -75,10 +94,8 @@ public class ConfigController extends BaseController {
|
|||||||
log.debug("Cannot find a validator for {}", clazz.getName());
|
log.debug("Cannot find a validator for {}", clazz.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
T oldConfig = configService.getConfig(clazz);
|
return newConfig.saveOrUpdate();
|
||||||
oldConfig.mergeFrom(newConfig);
|
|
||||||
return oldConfig.saveOrUpdate();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package link.at17.mid.tushare.web.controller;
|
package link.at17.mid.tushare.web.controller;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.validation.Validator;
|
import org.springframework.validation.Validator;
|
||||||
|
|||||||
Reference in New Issue
Block a user