Procházet zdrojové kódy

取消 searchapi 功能(测试不好使), 将用户逻辑代码放到 service

zhenghao před 5 měsíci
rodič
revize
eaba38cab8

+ 1 - 26
sql/pg.sql

@@ -57,29 +57,4 @@ COMMENT ON COLUMN user_list.user_name IS '用户名';
 COMMENT ON COLUMN user_list.user_password IS '密码';
 COMMENT ON COLUMN user_list.create_time IS '创建时间';
 COMMENT ON COLUMN user_list.update_time IS '更新时间';
-COMMENT ON COLUMN user_list.is_deleted IS '是否删除';
-
-
--- public.search_api definition
-
--- Drop table
-
-DROP TABLE IF EXISTS search_api;
-CREATE TABLE search_api (
-    id bigserial primary key, -- 自增主键
-    s_method text DEFAULT 'GET'::text NULL, -- 请求方法
-    s_url text NULL, -- 请求地址
-    s_param text NULL, -- 请求参数
-    s_description text NULL, -- 接口说明
-    s_tsdescription tsvector NULL -- 接口说明的全文检索
-);
-CREATE INDEX ON public.search_api USING gin (s_tsdescription);
-
--- Column comments
-
-COMMENT ON COLUMN public.search_api.id IS '自增主键';
-COMMENT ON COLUMN public.search_api.s_method IS '请求方法';
-COMMENT ON COLUMN public.search_api.s_url IS '请求地址';
-COMMENT ON COLUMN public.search_api.s_param IS '请求参数';
-COMMENT ON COLUMN public.search_api.s_description IS '接口说明';
-COMMENT ON COLUMN public.search_api.s_tsdescription IS '接口说明的全文检索';
+COMMENT ON COLUMN user_list.is_deleted IS '是否删除';

+ 1 - 1
src/main/java/cn/jlsxwkj/common/R/GlobalRestExceptionHandler.java

@@ -42,7 +42,7 @@ public class GlobalRestExceptionHandler {
         }
         if (e instanceof SaTokenException) {
             errorHandler.setStatus(((SaTokenException)e).getCode());
-            errorHandler.setMessage("账户未登录/未注册");
+            errorHandler.setMessage("账户未登录");
         }
         LogError errorHandlerToLogError = new LogError().castErrorHandlerToLogError(errorHandler);
         errorHandlerToLogError.setErrorInfo(e.getMessage());

+ 4 - 23
src/main/java/cn/jlsxwkj/moudles/chat/ChatController.java

@@ -1,10 +1,6 @@
 package cn.jlsxwkj.moudles.chat;
 
-import cn.jlsxwkj.moudles.chathistory.ChatHistory;
-import cn.jlsxwkj.moudles.chathistory.ChatHistoryService;
-import cn.jlsxwkj.moudles.userlist.UserListService;
 import io.swagger.v3.oas.annotations.Operation;
-import io.swagger.v3.oas.annotations.Parameter;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import jakarta.annotation.Resource;
 import org.springframework.http.MediaType;
@@ -13,7 +9,6 @@ import org.springframework.web.multipart.MultipartFile;
 import reactor.core.publisher.Flux;
 
 import java.io.IOException;
-import java.util.List;
 
 /**
  * @author zh
@@ -25,43 +20,29 @@ public class ChatController {
 
 	@Resource
 	private ChatService chatService;
-	@Resource
-	private ChatHistoryService chatHistoryService;
-	@Resource
-	private UserListService userListService;
 
 	@Operation(summary = "上传文档")
 	@PostMapping("/upload")
-	public String uploadDoc(
-			@Parameter(name = "file", description = "文件") @RequestBody MultipartFile file) throws IOException {
+	public String uploadDoc(@RequestBody MultipartFile file) throws IOException {
 		return chatService.uploadDocument(file);
 	}
 
 	@Operation(summary = "搜索文档")
 	@PostMapping("/search")
-	public String searchDoc(
-			@Parameter(name = "keyword", description = "关键词") @RequestParam String keyword) {
+	public String searchDoc(@RequestParam String keyword) {
 		return chatService.search(keyword);
 	}
 
-	@Operation(summary = "获取历史对话")
-	@PostMapping("/getHistory")
-	public List<ChatHistory> selectHistory() {
-		return chatHistoryService.selectHistoryByUserId();
-	}
-
 	@Operation(summary = "问答文档流")
 	@PostMapping(value = "/chatStream", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
-	public Flux<String> chatStream(
-			@Parameter(name = "message", description = "消息") @RequestParam String message) {
+	public Flux<String> chatStream(@RequestParam String message) {
 		return chatService.chatStream(message);
 	}
 
 	@Operation(summary = "问答文档")
 	@PostMapping(value = "/chat")
 	@Deprecated
-	public String chat(
-			@Parameter(name = "message", description = "消息") @RequestParam String message) {
+	public String chat(@RequestParam String message) {
 		return chatService.chat(message);
 	}
 }

+ 26 - 0
src/main/java/cn/jlsxwkj/moudles/chathistory/ChatHistoryController.java

@@ -0,0 +1,26 @@
+package cn.jlsxwkj.moudles.chathistory;
+
+import io.swagger.v3.oas.annotations.Operation;
+import jakarta.annotation.Resource;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+/**
+ * @author zh
+ */
+@RestController
+@RequestMapping("/chat/history/")
+public class ChatHistoryController {
+
+    @Resource
+    private ChatHistoryService chatHistoryService;
+
+    @Operation(summary = "获取历史对话")
+    @PostMapping("/getHistory")
+    public List<ChatHistory> selectHistory() {
+        return chatHistoryService.selectHistoryByUserId();
+    }
+}

+ 0 - 16
src/main/java/cn/jlsxwkj/moudles/searchapi/SearchApi.java

@@ -1,16 +0,0 @@
-package cn.jlsxwkj.moudles.searchapi;
-
-import lombok.Data;
-
-/**
- * @author zh
- */
-@Data
-public class SearchApi {
-    private Integer id;
-    private String sMethod;
-    private String sUrl;
-    private String sParam;
-    private String sDescription;
-    private String sTsdescription;
-}

+ 7 - 20
src/main/java/cn/jlsxwkj/moudles/userlist/UserListController.java

@@ -1,11 +1,10 @@
 package cn.jlsxwkj.moudles.userlist;
 
 import cn.dev33.satoken.stp.StpUtil;
+import io.swagger.v3.oas.annotations.Operation;
 import jakarta.annotation.Resource;
 import org.springframework.web.bind.annotation.*;
 
-import javax.security.auth.login.AccountException;
-import javax.security.auth.login.FailedLoginException;
 import javax.security.auth.login.LoginException;
 
 /**
@@ -18,33 +17,21 @@ public class UserListController {
     @Resource
     private UserListService userListService;
 
+    @Operation(summary = "登录")
     @PostMapping("/login")
     public String login(@RequestParam String userName,
                           @RequestParam String userPassword) throws LoginException {
-        UserList userList = userListService.checkUser(userName, userPassword);
-        if (userList != null) {
-            if (!StpUtil.isLogin()) {
-                String userId = userList.getUserId();
-                StpUtil.login(userId);
-                return "登录成功";
-            }
-            throw new LoginException("请勿重复登录");
-        }
-        throw new FailedLoginException("用户未注册/密码错误");
+        return userListService.checkUser(userName, userPassword);
     }
 
+    @Operation(summary = "注册")
     @PostMapping("/auth")
     public String auth(@RequestParam String userName,
-                        @RequestParam String userPassword) throws AccountException {
-        if (userListService.checkUser(userName, userPassword) != null) {
-            throw new AccountException("用户已注册, 请勿重复注册");
-        }
-        if (userListService.authUser(userName, userPassword) > 0) {
-            return "注册成功";
-        }
-        throw new AccountException("注册失败");
+                        @RequestParam String userPassword) throws LoginException {
+        return userListService.authUser(userName, userPassword);
     }
 
+    @Operation(summary = "登出")
     @GetMapping("/logout")
     public String logout() {
         StpUtil.logout();

+ 1 - 3
src/main/java/cn/jlsxwkj/moudles/userlist/UserListMapper.java

@@ -21,10 +21,8 @@ insert into user_list(user_id, user_name, user_password
 select * 
 from user_list
 where user_name = #{userName} 
-and user_password = #{userPassword}
 and is_deleted = 0
 limit 1
 """)
-    UserList checkUser(@Param("userName") String userName,
-                       @Param("userPassword") String userPassword);
+    UserList checkUser(@Param("userName") String userName);
 }

+ 29 - 5
src/main/java/cn/jlsxwkj/moudles/userlist/UserListService.java

@@ -1,10 +1,15 @@
 package cn.jlsxwkj.moudles.userlist;
 
 import cn.dev33.satoken.secure.SaSecureUtil;
+import cn.dev33.satoken.stp.StpUtil;
 import cn.hutool.core.lang.Snowflake;
 import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
 
+import javax.security.auth.login.AccountException;
+import javax.security.auth.login.FailedLoginException;
+import javax.security.auth.login.LoginException;
+
 /**
  * @author zh
  */
@@ -15,15 +20,34 @@ public class UserListService {
     private UserListMapper userListMapper;
     private final Snowflake snowflake = new Snowflake();
 
-    public Integer authUser(String userName, String password) {
+    public String authUser(String userName, String userPassword) throws AccountException {
+        if (userListMapper.checkUser(userName) != null) {
+            throw new AccountException("用户已注册, 请勿重复注册");
+        }
         UserList userList = new UserList();
         userList.setUserId(snowflake.nextIdStr());
         userList.setUserName(userName);
-        userList.setUserPassword(SaSecureUtil.sha512(password));
-        return userListMapper.authUser(userList);
+        userList.setUserPassword(SaSecureUtil.sha512(userPassword));
+        Integer rows = userListMapper.authUser(userList);
+        if (rows > 0) {
+            return "注册成功";
+        }
+        throw new AccountException("注册失败");
     }
 
-    public UserList checkUser(String userName, String userPassword) {
-        return userListMapper.checkUser(userName, SaSecureUtil.sha512(userPassword));
+    public String checkUser(String userName, String userPassword) throws LoginException {
+        UserList userList = userListMapper.checkUser(userName);
+        if (userList != null) {
+            if (!userList.getUserPassword().contains(SaSecureUtil.sha512(userPassword))) {
+                throw new FailedLoginException("密码错误");
+            }
+            if (!StpUtil.isLogin()) {
+                String userId = userList.getUserId();
+                StpUtil.login(userId);
+                return "登录成功";
+            }
+            throw new LoginException("请勿重复登录");
+        }
+        throw new FailedLoginException("用户未注册");
     }
 }

+ 1 - 1
src/main/resources/application-dev.yml

@@ -1,6 +1,6 @@
 spring:
   datasource:
-    url: jdbc:postgresql://localhost:5432/postgres
+    url: jdbc:postgresql://localhost:5433/postgres
     username: postgres
     password: postgres
   ai: