新增计算模块接口
This commit is contained in:
parent
12fe28a2a8
commit
df5d46e9a6
@ -3,6 +3,7 @@ package com.das.common.interceptor;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequestWrapper;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
|
@ -47,52 +47,13 @@ public class DecryptingOncePerRequestFilter extends OncePerRequestFilter {
|
||||
//如果获取到token,则进行解密
|
||||
if (StrUtil.isNotBlank(token)) {
|
||||
token = AESUtil.decrypt(aesKey, token, iv);
|
||||
try {
|
||||
// 检查Token
|
||||
StpUtil.checkLogin();
|
||||
} catch (NotLoginException e) {
|
||||
// 处理未登录异常
|
||||
response.sendError(401, "未登录");
|
||||
return;
|
||||
} catch (NotRoleException | NotPermissionException e) {
|
||||
// 处理权限异常
|
||||
response.sendError(403, "无权限访问");
|
||||
return;
|
||||
}
|
||||
}
|
||||
//如果读取到requestBody,则进行解密
|
||||
String bodyData = readRequestBody(request);
|
||||
if (StrUtil.isNotBlank(bodyData)) {
|
||||
bodyData = AESUtil.decrypt(aesKey, bodyData, iv);
|
||||
DecryptingHttpServletRequestWrapper wrapper = new DecryptingHttpServletRequestWrapper(request, bodyData, token);
|
||||
// 使用自定义的请求包装器替换原始请求
|
||||
filterChain.doFilter(new DecryptingHttpServletRequestWrapper(request, bodyData, token), response);
|
||||
}
|
||||
} else if (POST.matches(method) && StrUtil.isNotBlank(contentType) && contentType.contains(MediaType.MULTIPART_FORM_DATA_VALUE)) {
|
||||
// 上传文件过滤
|
||||
String token = request.getHeader(TOKEN_ATTR_NAME);
|
||||
String iv = request.getHeader(IV_ATTR_NAME);
|
||||
//如果获取到token,则进行解密
|
||||
if (StrUtil.isNotBlank(token)) {
|
||||
token = AESUtil.decrypt(aesKey, token, iv);
|
||||
try {
|
||||
// 检查Token
|
||||
StpUtil.checkLogin();
|
||||
} catch (NotLoginException e) {
|
||||
// 处理未登录异常
|
||||
response.sendError(401, "未登录");
|
||||
return;
|
||||
} catch (NotRoleException | NotPermissionException e) {
|
||||
// 处理权限异常
|
||||
response.sendError(403, "无权限访问");
|
||||
return;
|
||||
}
|
||||
}
|
||||
String id = request.getParameter("id");
|
||||
if (StrUtil.isNotBlank(id)) {
|
||||
StandardMultipartHttpServletRequest multiRequest = new StandardMultipartHttpServletRequest(request);
|
||||
id = AESUtil.decrypt(aesKey, id, iv);
|
||||
// 包装请求
|
||||
HttpServletRequest wrapper = new DecryptUploadWrapper(multiRequest, "id", id, token);
|
||||
filterChain.doFilter(wrapper, response);
|
||||
}
|
||||
} else {
|
||||
|
@ -0,0 +1,59 @@
|
||||
package com.das.modules.calc.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.das.common.result.R;
|
||||
import com.das.modules.calc.domain.vo.CalcModuleVo;
|
||||
import com.das.modules.calc.service.CalcService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 计算模块API接口
|
||||
*/
|
||||
@RequestMapping("/api/calc")
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class CalcController {
|
||||
|
||||
@Autowired
|
||||
CalcService calcService;
|
||||
|
||||
/**
|
||||
* 计算模块查询接口
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/module/list")
|
||||
public R<List<CalcModuleVo>> queryCalcModules(){
|
||||
List<CalcModuleVo> result = calcService.queryCalcModules();
|
||||
return R.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算模块注册接口
|
||||
* @param file 上传的脚本文件
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
@PostMapping("/module/register")
|
||||
public R<?> registerCalcModule(MultipartFile file) throws IOException {
|
||||
try {
|
||||
String content = new String(file.getBytes(), StandardCharsets.UTF_8);
|
||||
calcService.registerCalcModule(content);
|
||||
}
|
||||
catch (Exception ex){
|
||||
log.error("注册计算模块失败", ex);
|
||||
return R.fail(String.format("计算模块注册失败, %s",ex.getMessage()));
|
||||
}
|
||||
return R.success();
|
||||
}
|
||||
}
|
@ -1,18 +1,71 @@
|
||||
package com.das.modules.calc.domain.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Slf4j
|
||||
@TableName("sys_calc_module")
|
||||
@Data
|
||||
public class CalcModule {
|
||||
@TableId(value = "name")
|
||||
private String name;
|
||||
@TableField(value = "localname")
|
||||
private String localName;
|
||||
private String script;
|
||||
private String version;
|
||||
private String description;
|
||||
private Integer disabled;
|
||||
private Date updateTime;
|
||||
private String cron;
|
||||
|
||||
public static final CalcModule of(String content){
|
||||
CalcModule calcModule = new CalcModule();
|
||||
calcModule.setScript(content);
|
||||
try(BufferedReader reader = new BufferedReader(new StringReader(content))){
|
||||
int lineIndex = 0;
|
||||
String line =reader.readLine();
|
||||
while (line != null){
|
||||
switch (lineIndex){
|
||||
case 0:
|
||||
calcModule.setName(line.substring(8).trim());
|
||||
break;
|
||||
case 1:
|
||||
calcModule.setLocalName(line.substring(13).trim());
|
||||
break;
|
||||
case 2:
|
||||
calcModule.setVersion(line.substring(11).trim());
|
||||
break;
|
||||
case 3:
|
||||
calcModule.setCron(line.substring(8).trim());
|
||||
break;
|
||||
case 4:
|
||||
calcModule.setDescription(line.substring(15).trim());
|
||||
break;
|
||||
default:
|
||||
}
|
||||
if (lineIndex == 4){
|
||||
break;
|
||||
}
|
||||
lineIndex++;
|
||||
line = reader.readLine();
|
||||
}
|
||||
if (lineIndex != 4){
|
||||
throw new RuntimeException("脚本格式不正确");
|
||||
}
|
||||
calcModule.setScript(content);
|
||||
} catch (IOException e) {
|
||||
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return calcModule;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
package com.das.modules.calc.domain.vo;
|
||||
|
||||
import com.das.modules.calc.domain.entity.CalcModule;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class CalcModuleVo {
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private String name;
|
||||
private String localName;
|
||||
private String version;
|
||||
private String description;
|
||||
private Integer disabled;
|
||||
private Date updateTime;
|
||||
private String cron;
|
||||
|
||||
public static CalcModuleVo of(final CalcModule calcModule) {
|
||||
CalcModuleVo vo = new CalcModuleVo();
|
||||
vo.setName(calcModule.getName());
|
||||
vo.setLocalName(calcModule.getLocalName());
|
||||
vo.setVersion(calcModule.getVersion());
|
||||
vo.setDescription(calcModule.getDescription());
|
||||
vo.setDisabled(calcModule.getDisabled());
|
||||
vo.setUpdateTime(calcModule.getUpdateTime());
|
||||
vo.setCron(calcModule.getCron());
|
||||
return vo;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package com.das.modules.calc.service;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.das.modules.cache.service.CacheService;
|
||||
import com.das.modules.calc.domain.entity.CalcModule;
|
||||
import com.das.modules.calc.domain.vo.CalcModuleVo;
|
||||
import com.das.modules.calc.functions.FunctionRealData;
|
||||
import com.das.modules.calc.functions.FunctionSaveCalcData;
|
||||
import com.das.modules.calc.mapper.CalcModuleMapper;
|
||||
@ -17,7 +18,13 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 计算引擎服务
|
||||
@ -27,6 +34,8 @@ import java.util.List;
|
||||
@Service
|
||||
public class CalcService {
|
||||
|
||||
public static final int SCRIPT_HEAD_LINE_COUNT = 4;
|
||||
|
||||
@Autowired
|
||||
CalcModuleMapper calcModuleMapper;
|
||||
|
||||
@ -123,4 +132,19 @@ public class CalcService {
|
||||
}
|
||||
|
||||
|
||||
public List<CalcModuleVo> queryCalcModules() {
|
||||
List<CalcModule> scriptModules = calcModuleMapper.selectList(null);
|
||||
|
||||
return scriptModules.stream().map(CalcModuleVo::of).toList();
|
||||
}
|
||||
|
||||
public void registerCalcModule(String content) {
|
||||
CalcModule module = CalcModule.of(content);
|
||||
CalcModule scriptModules = calcModuleMapper.selectById(module.getName());
|
||||
if (scriptModules == null){
|
||||
calcModuleMapper.insert(module);
|
||||
} else {
|
||||
calcModuleMapper.updateById(module);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user