重构后端服务架构并优化前端错误处理
This commit is contained in:
56
backend-java/user-service/pom.xml
Normal file
56
backend-java/user-service/pom.xml
Normal 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>
|
||||
19
backend-java/user-service/run.sh
Executable file
19
backend-java/user-service/run.sh
Executable 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"
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
20
backend-java/user-service/src/main/resources/application.yml
Normal file
20
backend-java/user-service/src/main/resources/application.yml
Normal 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
|
||||
20
backend-java/user-service/target/classes/application.yml
Normal file
20
backend-java/user-service/target/classes/application.yml
Normal 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
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user