first commit

This commit is contained in:
2026-11-20 16:51:48 +08:00
parent 5e2891cc23
commit f67b0c9856
206 changed files with 11850 additions and 0 deletions
@@ -0,0 +1,22 @@
package com.wanji.software.tocc.system.maint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
/**
* 养护系统服务。页面接口放在本进程,数据库访问调用基础服务。
*/
@SpringBootApplication
@ComponentScan(basePackages = {
"com.wanji.software.tocc.system.maint",
"com.wanji.software.tocc.maint.support"
})
@EnableFeignClients(basePackages = "com.wanji.software.tocc.infra.maint.api")
public class MaintSystemApplication {
public static void main(String[] args) {
SpringApplication.run(MaintSystemApplication.class, args);
}
}
@@ -0,0 +1,59 @@
package com.wanji.software.tocc.system.maint.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanji.software.tocc.common.exception.BusinessException;
import com.wanji.software.tocc.common.model.Result;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.annotation.Resource;
/**
* 统一异常处理。业务异常回短文案;调用基础服务失败或系统异常记日志后返回固定提示。
*/
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
@Resource
private ObjectMapper objectMapper;
@ExceptionHandler(BusinessException.class)
public Result<Void> handleBusiness(BusinessException exception) {
log.warn("业务异常: {}", exception.getMessage());
return Result.fail(exception.getCode() == null ? 1 : exception.getCode(),
safeMsg(exception.getMessage(), "请求失败"));
}
@ExceptionHandler(FeignException.class)
public Result<Void> handleFeign(FeignException exception) {
log.error("调用基础服务失败", exception);
String remoteMsg = readRemoteMsg(exception);
return Result.fail(safeMsg(remoteMsg, "基础服务暂时不可用"));
}
@ExceptionHandler(Exception.class)
public Result<Void> handle(Exception exception) {
log.error("系统异常", exception);
return Result.fail("系统繁忙,请稍后再试");
}
private String readRemoteMsg(FeignException exception) {
try {
byte[] body = exception.content();
if (body == null || body.length == 0) {
return null;
}
Result<?> result = objectMapper.readValue(body, Result.class);
return result == null ? null : result.getMsg();
} catch (Exception ex) {
return null;
}
}
private String safeMsg(String message, String fallback) {
return message == null || message.trim().isEmpty() ? fallback : message;
}
}
@@ -0,0 +1,70 @@
package com.wanji.software.tocc.system.maint.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanji.software.tocc.common.exception.BusinessException;
import com.wanji.software.tocc.common.model.Result;
import com.wanji.software.tocc.maint.support.CurrentUser;
import com.wanji.software.tocc.maint.support.CurrentUserHolder;
import com.wanji.software.tocc.maint.support.LocalDevAuthFilter;
import feign.RequestInterceptor;
import feign.Response;
import feign.codec.ErrorDecoder;
import feign.Util;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.net.URLEncoder;
/**
* 调用基础服务时带上当前用户;非 2xx 时尽量还原远端 Result 文案。
*/
@Configuration
public class MaintFeignConfig {
@Bean
public RequestInterceptor currentUserInterceptor() {
return template -> {
CurrentUser current = CurrentUserHolder.get();
template.header(LocalDevAuthFilter.USER_ID_HEADER, current.getUserId());
template.header(LocalDevAuthFilter.USER_NAME_HEADER, encode(current.getUserName()));
template.header(LocalDevAuthFilter.ORG_ID_HEADER, current.getOrgId());
};
}
@Bean
public ErrorDecoder maintErrorDecoder(ObjectMapper objectMapper) {
return (methodKey, response) -> {
String remoteMsg = readRemoteMsg(objectMapper, response);
if (remoteMsg != null) {
return new BusinessException(remoteMsg);
}
return new BusinessException("基础服务暂时不可用");
};
}
private String readRemoteMsg(ObjectMapper objectMapper, Response response) {
if (response == null || response.body() == null) {
return null;
}
try {
String body = Util.toString(response.body().asReader(StandardCharsets.UTF_8));
Result<?> result = objectMapper.readValue(body, Result.class);
if (result == null || result.getMsg() == null || result.getMsg().trim().isEmpty()) {
return null;
}
return result.getMsg();
} catch (Exception ex) {
return null;
}
}
private String encode(String value) {
try {
return URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException ex) {
return value;
}
}
}
@@ -0,0 +1,49 @@
package com.wanji.software.tocc.system.maint.controller;
import com.wanji.software.tocc.common.model.Result;
import com.wanji.software.tocc.infra.maint.api.BridgeApi;
import com.wanji.software.tocc.infra.maint.api.MaintHealthApi;
import com.wanji.software.tocc.infra.maint.model.dto.BridgeResp;
import com.wanji.software.tocc.maint.support.CurrentUserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 给前端的接口放在这个包。需要查库时注入基础服务的 Feign 接口,不要在本服务写 Mapper。
*/
@Tag(name = "道路养护管理")
@RestController
@RequestMapping("/maint")
public class MaintController {
@Resource
private MaintHealthApi maintHealthApi;
@Resource
private BridgeApi bridgeApi;
@GetMapping("/health")
@Operation(summary = "检查基础服务是否可用")
public Result<String> health() {
return maintHealthApi.health();
}
@GetMapping("/current-user")
@Operation(summary = "查看本地开发使用的当前用户")
public Result<String> currentUser() {
return Result.ok(CurrentUserHolder.get().getUserName());
}
@GetMapping("/bridge")
@Operation(summary = "查看本地开发使用的当前用户")
public Result<BridgeResp> getBridgeById(@RequestParam("id") String id) {
return Result.ok(bridgeApi.getById(id).getCheckedData());
}
}
@@ -0,0 +1,25 @@
server:
port: 8525
spring:
application:
name: maint-system
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
maint:
infra:
url: http://127.0.0.1:7424
springdoc:
api-docs:
enabled: true
path: /v3/api-docs
swagger-ui:
enabled: true
path: /swagger-ui.html
knife4j:
enable: true
setting:
language: zh_cn