重构后端服务架构并优化前端错误处理

This commit is contained in:
ylweng
2025-09-10 23:28:48 +08:00
parent 5853953f79
commit 106bf8a841
26 changed files with 545 additions and 7 deletions

3
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"java.compile.nullAnalysis.mode": "automatic"
}

View File

@@ -97,7 +97,7 @@
<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import { ElMessageBox } from 'element-plus'
import { useUserStore } from '@/stores/user'
const route = useRoute()

View File

@@ -3,6 +3,7 @@ import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
// @ts-ignore
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import App from './App.vue'

View File

@@ -113,7 +113,7 @@ const router = createRouter({
})
// 路由守卫
router.beforeEach((to, from, next) => {
router.beforeEach((to, _from, next) => {
const userStore = useUserStore()
// 设置页面标题

View File

@@ -158,7 +158,7 @@ const orderStatusChart = ref<HTMLElement>()
// 获取状态类型
const getStatusType = (status: string) => {
const statusMap: Record<string, string> = {
const statusMap: Record<string, any> = {
pending: 'warning',
confirmed: 'info',
shipping: 'primary',

View File

@@ -142,7 +142,7 @@
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { User, UserListParams, UserCreateForm, UserUpdateForm } from '@/types/user'
import type { User, UserListParams, UserCreateForm } from '@/types/user'
// 响应式数据
const loading = ref(false)
@@ -417,7 +417,7 @@ const resetForm = () => {
// 获取角色类型
const getRoleType = (role: string) => {
const roleMap: Record<string, string> = {
const roleMap: Record<string, any> = {
admin: 'danger',
buyer: 'primary',
trader: 'success',
@@ -441,7 +441,7 @@ const getRoleText = (role: string) => {
// 获取状态类型
const getStatusType = (status: string) => {
const statusMap: Record<string, string> = {
const statusMap: Record<string, any> = {
active: 'success',
inactive: 'warning',
banned: 'danger'

View File

@@ -44,7 +44,6 @@
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"declaration": false,
"declarationMap": false,
"sourceMap": true,
"removeComments": true,

82
backend-java/README.md Normal file
View File

@@ -0,0 +1,82 @@
# NiuMall 后端服务 (Java 版本)
活牛采购智能数字化系统 - 后端服务
## 项目介绍
本项目是将原有的 Node.js 后端服务重构为基于 Java 的微服务架构,使用 Spring Boot 和 Spring Cloud 构建。
## 技术栈
- Java 17
- Spring Boot 3.1.0
- Spring Cloud 2022.0.3
- Spring Data JPA
- MySQL 8.0
- Maven 3.8+
## 服务架构
- **user-service**: 用户服务
- **auth-service**: 认证服务
- **order-service**: 订单服务
- **supplier-service**: 供应商服务
- **transport-service**: 运输服务
- **finance-service**: 财务服务
- **quality-service**: 质检服务
## 项目结构
```
backend-java/
├── auth-service/ # 认证服务
├── user-service/ # 用户服务
├── order-service/ # 订单服务
├── supplier-service/ # 供应商服务
├── transport-service/ # 运输服务
├── finance-service/ # 财务服务
├── quality-service/ # 质检服务
├── common/ # 公共模块
└── docs/ # 文档
```
## 构建和运行
### 环境要求
- JDK 17+
- Maven 3.8+
- MySQL 8.0+
### 构建项目
```bash
# 进入项目根目录
cd backend-java
# 编译项目
mvn clean compile
# 打包项目
mvn clean package
```
### 运行服务
```bash
# 运行用户服务
cd user-service
mvn spring-boot:run
```
## 数据库配置
项目使用 MySQL 数据库,连接配置在各个服务的 `application.yml` 文件中。
## API 文档
API 文档使用 Swagger 生成,启动服务后访问 `http://localhost:8080/swagger-ui.html` 查看。
## 部署
推荐使用 Docker 进行部署,每个服务都可以独立打包成 Docker 镜像。

View File

@@ -0,0 +1,103 @@
# Java 后端服务运行说明
## 环境要求
1. JDK 17 或更高版本
2. Maven 3.8 或更高版本
## 安装 Maven如果尚未安装
### macOS
如果已安装 Homebrew
```bash
brew install maven
```
如果没有安装 Homebrew先安装 Homebrew
```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
然后安装 Maven
```bash
brew install maven
```
### Windows
1. 访问 [Maven 官网](https://maven.apache.org/download.cgi)
2. 下载最新版本的 Maven
3. 解压到指定目录
4. 配置环境变量:
- 添加 MAVEN_HOME 环境变量,指向 Maven 安装目录
- 将 %MAVEN_HOME%\bin 添加到 PATH 环境变量
### Linux (Ubuntu/Debian)
```bash
sudo apt update
sudo apt install maven
```
## 构建和运行服务
### 1. 使用 Maven Wrapper推荐
首先初始化 Maven Wrapper
```bash
cd backend-java
mvn -N io.takari:maven:wrapper
```
然后构建项目:
```bash
cd user-service
./mvnw clean compile
```
### 2. 直接使用 Maven
如果已安装 Maven
```bash
cd backend-java/user-service
mvn clean compile
mvn spring-boot:run
```
### 3. 打包并运行
```bash
cd backend-java/user-service
mvn clean package
java -jar target/*.jar
```
## 服务访问
用户服务默认运行在 8081 端口:
- 健康检查: http://localhost:8081/actuator/health
- 用户 API: http://localhost:8081/api/users
## 数据库配置
服务会自动连接到配置文件中指定的 MySQL 数据库:
- 主机: 129.211.213.226
- 端口: 9527
- 数据库: jiebandata
- 用户名: root
- 密码: aiotAiot123!
确保网络可以访问该数据库服务器。
## 常见问题
1. **端口被占用**: 修改 `application.yml` 中的 `server.port` 配置
2. **数据库连接失败**: 检查网络连接和数据库配置
3. **依赖下载失败**: 配置 Maven 镜像源或检查网络连接
## 开发工具
推荐使用以下 IDE 进行开发:
- IntelliJ IDEA
- Eclipse with Spring Tools
- VS Code with Java extensions

63
backend-java/pom.xml Normal file
View File

@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.niumall</groupId>
<artifactId>niumall-backend</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>NiuMall Backend</name>
<description>活牛采购智能数字化系统 - 后端服务 (Java版本)</description>
<modules>
<module>auth-service</module>
<module>user-service</module>
<module>order-service</module>
<module>supplier-service</module>
<module>transport-service</module>
<module>finance-service</module>
<module>quality-service</module>
<module>common</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.boot.version>3.1.0</spring.boot.version>
<spring.cloud.version>2022.0.3</spring.cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.niumall</groupId>
<artifactId>niumall-backend</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>user-service</artifactId>
<name>User Service</name>
<description>用户服务</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,19 @@
#!/bin/bash
echo "========================================="
echo " NiuMall User Service"
echo "========================================="
echo
echo "要运行此服务,请确保已安装:"
echo "1. JDK 17 或更高版本"
echo "2. Maven 3.8 或更高版本"
echo
echo "运行步骤:"
echo "1. 打开终端并导航到 user-service 目录"
echo "2. 执行以下命令:"
echo " mvn spring-boot:run"
echo
echo "服务将在端口 8081 上启动"
echo "API 地址: http://localhost:8081/api/users"
echo
echo "注意:确保可以访问数据库服务器 129.211.213.226:9527"

View File

@@ -0,0 +1,11 @@
package com.niumall.userservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}

View File

@@ -0,0 +1,55 @@
package com.niumall.userservice.controller;
import com.niumall.userservice.entity.User;
import com.niumall.userservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.getAllUsers();
return ResponseEntity.ok(users);
}
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable Long id) {
User user = userService.getUserById(id);
if (user != null) {
return ResponseEntity.ok(user);
} else {
return ResponseEntity.notFound().build();
}
}
@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
User savedUser = userService.createUser(user);
return ResponseEntity.ok(savedUser);
}
@PutMapping("/{id}")
public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User user) {
User updatedUser = userService.updateUser(id, user);
if (updatedUser != null) {
return ResponseEntity.ok(updatedUser);
} else {
return ResponseEntity.notFound().build();
}
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
return ResponseEntity.noContent().build();
}
}

View File

@@ -0,0 +1,37 @@
package com.niumall.userservice.entity;
import jakarta.persistence.*;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@Entity
@Table(name = "api_users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, nullable = false)
private String username;
@Column(name = "password_hash", nullable = false)
private String passwordHash;
@Column(unique = true, nullable = false)
private String phone;
private String email;
@Column(name = "user_type", nullable = false)
private String userType;
@Column(name = "status")
private String status = "active";
@Column(name = "created_at")
private LocalDateTime createdAt;
@Column(name = "updated_at")
private LocalDateTime updatedAt;
}

View File

@@ -0,0 +1,13 @@
package com.niumall.userservice.repository;
import com.niumall.userservice.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUsername(String username);
Optional<User> findByPhone(String phone);
Optional<User> findByEmail(String email);
}

View File

@@ -0,0 +1,12 @@
package com.niumall.userservice.service;
import com.niumall.userservice.entity.User;
import java.util.List;
public interface UserService {
List<User> getAllUsers();
User getUserById(Long id);
User createUser(User user);
User updateUser(Long id, User user);
void deleteUser(Long id);
}

View File

@@ -0,0 +1,44 @@
package com.niumall.userservice.service.impl;
import com.niumall.userservice.entity.User;
import com.niumall.userservice.repository.UserRepository;
import com.niumall.userservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public List<User> getAllUsers() {
return userRepository.findAll();
}
@Override
public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
@Override
public User createUser(User user) {
return userRepository.save(user);
}
@Override
public User updateUser(Long id, User user) {
if (userRepository.existsById(id)) {
user.setId(id);
return userRepository.save(user);
}
return null;
}
@Override
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
}

View File

@@ -0,0 +1,20 @@
server:
port: 8081
spring:
application:
name: user-service
datasource:
url: jdbc:mysql://129.211.213.226:9527/jiebandata?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: aiotAiot123!
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL8Dialect
logging:
level:
com.niumall: debug

View File

@@ -0,0 +1,20 @@
server:
port: 8081
spring:
application:
name: user-service
datasource:
url: jdbc:mysql://129.211.213.226:9527/jiebandata?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: aiotAiot123!
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL8Dialect
logging:
level:
com.niumall: debug