Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.9</version>
</dependency>
</dependencies>

<build>
Expand Down
15 changes: 15 additions & 0 deletions sql/create_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- 用户表
create table if not exists "user"
(
id bigserial primary key,
"userAccount" varchar(256) not null,
"userPassword" varchar(512) not null,
"userName" varchar(256) null,
"userAvatar" varchar(1024) null,
"userRole" varchar(256) default 'user' not null,
"createTime" timestamp default current_timestamp not null,
"updateTime" timestamp default current_timestamp not null,
"isDelete" smallint default 0 not null
);

create index idx_userAccount on "user" ("userAccount");
8 changes: 3 additions & 5 deletions src/main/java/com/yupi/yuaiagent/YuAiAgentApplication.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.yupi.yuaiagent;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = {
// 为了便于大家开发调试和部署,取消数据库自动配置,需要使用 PgVector 时把 DataSourceAutoConfiguration.class 删除
DataSourceAutoConfiguration.class
})
@SpringBootApplication
@MapperScan("com.yupi.yuaiagent.mapper")
public class YuAiAgentApplication {

public static void main(String[] args) {
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/yupi/yuaiagent/common/BaseResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.yupi.yuaiagent.common;

import lombok.Data;
import java.io.Serializable;

@Data
public class BaseResponse<T> implements Serializable {
private int code;
private T data;
private String message;

public BaseResponse(int code, T data, String message) {
this.code = code;
this.data = data;
this.message = message;
}

public BaseResponse(int code, T data) {
this(code, data, "");
}

public BaseResponse(ErrorCode errorCode) {
this(errorCode.getCode(), null, errorCode.getMessage());
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/yupi/yuaiagent/common/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.yupi.yuaiagent.common;

public enum ErrorCode {
SUCCESS(0, "ok"),
PARAMS_ERROR(40000, "请求参数错误"),
NOT_LOGIN_ERROR(40100, "未登录"),
NO_AUTH_ERROR(40101, "无权限"),
NOT_FOUND_ERROR(40400, "请求数据不存在"),
FORBIDDEN_ERROR(40300, "禁止访问"),
SYSTEM_ERROR(50000, "系统内部异常"),
OPERATION_ERROR(50001, "操作失败");

private final int code;
private final String message;

ErrorCode(int code, String message) {
this.code = code;
this.message = message;
}

public int getCode() {
return code;
}

public String getMessage() {
return message;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/yupi/yuaiagent/common/ResultUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.yupi.yuaiagent.common;

public class ResultUtils {
public static <T> BaseResponse<T> success(T data) {
return new BaseResponse<>(0, data, "ok");
}

public static BaseResponse error(ErrorCode errorCode) {
return new BaseResponse<>(errorCode);
}

public static BaseResponse error(int code, String message) {
return new BaseResponse<>(code, null, message);
}

public static BaseResponse error(ErrorCode errorCode, String message) {
return new BaseResponse<>(errorCode.getCode(), null, message);
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/yupi/yuaiagent/config/AuthInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.yupi.yuaiagent.config;

import com.yupi.yuaiagent.common.ErrorCode;
import com.yupi.yuaiagent.exception.BusinessException;
import com.yupi.yuaiagent.model.entity.User;
import com.yupi.yuaiagent.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class AuthInterceptor implements HandlerInterceptor {

@Resource
private UserService userService;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 放行OPTIONS请求
if ("OPTIONS".equals(request.getMethod())) {
return true;
}
// 获取当前登录用户
try {
User user = userService.getLoginUser(request);
if (user == null) {
throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
}
} catch (Exception e) {
throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
}
return true;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/yupi/yuaiagent/config/InterceptorConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.yupi.yuaiagent.config;

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

@Resource
private AuthInterceptor authInterceptor;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/user/login", "/user/register", "/doc.html", "/webjars/**", "/v3/api-docs/**", "/swagger-ui/**", "/error", "/favicon.ico", "/swagger-ui.html");
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/yupi/yuaiagent/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.yupi.yuaiagent.controller;

import com.yupi.yuaiagent.common.BaseResponse;
import com.yupi.yuaiagent.common.ErrorCode;
import com.yupi.yuaiagent.common.ResultUtils;
import com.yupi.yuaiagent.exception.BusinessException;
import com.yupi.yuaiagent.model.dto.user.UserLoginRequest;
import com.yupi.yuaiagent.model.dto.user.UserRegisterRequest;
import com.yupi.yuaiagent.model.entity.User;
import com.yupi.yuaiagent.model.vo.LoginUserVO;
import com.yupi.yuaiagent.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

@Resource
private UserService userService;

@PostMapping("/register")
public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
if (userRegisterRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
String userAccount = userRegisterRequest.getUserAccount();
String userPassword = userRegisterRequest.getUserPassword();
String checkPassword = userRegisterRequest.getCheckPassword();
long result = userService.userRegister(userAccount, userPassword, checkPassword);
return ResultUtils.success(result);
}

@PostMapping("/login")
public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
if (userLoginRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
String userAccount = userLoginRequest.getUserAccount();
String userPassword = userLoginRequest.getUserPassword();
LoginUserVO loginUserVO = userService.userLogin(userAccount, userPassword, request);
return ResultUtils.success(loginUserVO);
}

@PostMapping("/logout")
public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
if (request == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean result = userService.userLogout(request);
return ResultUtils.success(result);
}

@GetMapping("/get/login")
public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
User user = userService.getLoginUser(request);
return ResultUtils.success(userService.getLoginUserVO(user));
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/yupi/yuaiagent/exception/BusinessException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.yupi.yuaiagent.exception;

import com.yupi.yuaiagent.common.ErrorCode;

public class BusinessException extends RuntimeException {
private final int code;

public BusinessException(int code, String message) {
super(message);
this.code = code;
}

public BusinessException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.code = errorCode.getCode();
}

public BusinessException(ErrorCode errorCode, String message) {
super(message);
this.code = errorCode.getCode();
}

public int getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.yupi.yuaiagent.exception;

import com.yupi.yuaiagent.common.BaseResponse;
import com.yupi.yuaiagent.common.ErrorCode;
import com.yupi.yuaiagent.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

@ExceptionHandler(BusinessException.class)
public BaseResponse<?> businessExceptionHandler(BusinessException e) {
log.error("BusinessException", e);
return ResultUtils.error(e.getCode(), e.getMessage());
}

@ExceptionHandler(RuntimeException.class)
public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
log.error("RuntimeException", e);
return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/yupi/yuaiagent/mapper/UserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.yupi.yuaiagent.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yupi.yuaiagent.model.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yupi.yuaiagent.model.dto.user;

import lombok.Data;
import java.io.Serializable;

@Data
public class UserLoginRequest implements Serializable {
private String userAccount;
private String userPassword;
private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.yupi.yuaiagent.model.dto.user;

import lombok.Data;
import java.io.Serializable;

@Data
public class UserRegisterRequest implements Serializable {
private String userAccount;
private String userPassword;
private String checkPassword;
private static final long serialVersionUID = 1L;
}
65 changes: 65 additions & 0 deletions src/main/java/com/yupi/yuaiagent/model/entity/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.yupi.yuaiagent.model.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
* 用户
*/
@TableName(value = "user")
@Data
public class User implements Serializable {

/**
* id
*/
@TableId(type = IdType.AUTO)
private Long id;

/**
* 账号
*/
private String userAccount;

/**
* 密码
*/
private String userPassword;

/**
* 用户昵称
*/
private String userName;

/**
* 用户头像
*/
private String userAvatar;

/**
* 用户角色:user/admin/ban
*/
private String userRole;

/**
* 是否删除
*/
@TableLogic
private Integer isDelete;

/**
* 创建时间
*/
private Date createTime;

/**
* 更新时间
*/
private Date updateTime;

@TableField(exist = false)
private static final long serialVersionUID = 1L;
}
Loading