Prechádzať zdrojové kódy

增加了用户登录功能

Snow 2 týždňov pred
rodič
commit
391e784d7b

+ 3 - 3
src/main/java/org/snowchen/GreatSonServer/Main.java

@@ -1,7 +1,6 @@
 package org.snowchen.GreatSonServer;
 
 import org.snowchen.GreatSonServer.programinit.ConsoleMenu;
-import org.snowchen.GreatSonServer.programinit.UserRegister;
 
 
 public class Main {
@@ -12,8 +11,9 @@ public class Main {
                  / / __/ ___/ _ \\/ __ `/ __/\\__ \\/ __ \\/ __ \\
                 / /_/ / /  /  __/ /_/ / /_ ___/ / /_/ / / / /
                 \\____/_/   \\___/\\__,_/\\__//____/\\____/_/ /_/\s""");
-        System.out.print("欢迎使用GreatSon库存管理系统!\n  ");
-        ConsoleMenu.MainMenu();
+        System.out.print("欢迎使用GreatSon库存管理系统!\n"+
+        "程序版本:v1.0\n");
+        ConsoleMenu.LoginMenu();
 
     }
 }

+ 2 - 1
src/main/java/org/snowchen/GreatSonServer/databasecore/SQLiteInit.java

@@ -53,7 +53,8 @@ public class SQLiteInit {
                     "user_password TEXT NOT NULL, " +
                     "user_uuid TEXT NOT NULL, " +
                     "user_permission_level INTEGER NOT NULL," + // 修改为 INTEGER
-                    "user_register_time TEXT NOT NULL" + // 修改为 TEXT
+                    "user_register_time TEXT NOT NULL," + // 修改为 TEXT
+                    "user_last_login_time TEXT NOT NULL" +
                     ")";
             try (Statement statement = c.createStatement()) {
                 statement.execute(sqlUserDataTable);

+ 49 - 8
src/main/java/org/snowchen/GreatSonServer/programinit/ConsoleMenu.java

@@ -4,20 +4,61 @@ import java.util.Scanner;
 import org.snowchen.GreatSonServer.databasecore.*;
 
 public class ConsoleMenu {
-    public static void MainMenu() {
+    public static void LoginMenu() {
         int answer = 0;
-        String login_logo = "❌";
-        boolean login_state = false;
-        if (!login_state) login_logo = "❌";
-        else if (login_state) login_logo = "✅";
-        System.out.println("[输入数字执行对应操作]\n" +
-                "当前登录状态:" + login_logo);
-        System.out.println("—————————————\n" +
+        Scanner scanner = new Scanner(System.in);
+        System.out.println(
+                "[输入数字执行对应操作]\n"+
+                "—————————————\n" +
                 "| 1.用户登录 | \n" +
                 "| 2.用户注册 |  \n" +
                 "| 3.退出程序 |  \n" +
                 "—————————————\n" +
                 "请输入功能编号:");
+        answer = scanner.nextInt();
+        if(answer == 1){
+            UserLogin UserLogin = new UserLogin();
+            ChooseMenu();
+        } else if(answer == 2)
+        {
+            UserRegister UserRegister = new UserRegister();
+            ChooseMenu();
+        } else if (answer == 3)
+        {
+            System.out.println("[程序已退出]");
+            System.exit(0);
+        }
+    }
+
+    public static void ChooseMenu(){
+        Scanner scanner = new Scanner(System.in);
+        int answer = 0;
+        System.out.println("——————————————————————\n" +
+                "|1.增加数据 | 4.删除数据|\n" +
+                "|2.更新数据 | 5.初始化数据库|\n" +
+                "|3.查询数据 | 6.退出程序|\n" +
+                "——————————————————————\n" +
+                "请输入功能编号:");
+        answer = scanner.nextInt();
+        if(answer == 1){
+
+        }
+        else if(answer == 2){
+
+        }
+        else if(answer == 3){
+
+        }
+        else if(answer == 4){
+
+        }
+        else if(answer == 5){
+            SQLiteInit.InitDatabase();
 
+        }
+        else if(answer == 6){
+            System.out.println("[程序已退出]");
+            System.exit(0);
+        }
     }
 }

+ 0 - 7
src/main/java/org/snowchen/GreatSonServer/programinit/DataInput.java

@@ -1,7 +0,0 @@
-package org.snowchen.GreatSonServer.programinit;
-
-public class DataInput {
-    String user_name;
-    String password;
-    String UUID;
-}

+ 58 - 19
src/main/java/org/snowchen/GreatSonServer/programinit/UserLogin.java

@@ -3,6 +3,8 @@ package org.snowchen.GreatSonServer.programinit;
 import java.security.NoSuchAlgorithmException;
 import java.sql.*;
 import java.util.Scanner;
+import java.time.LocalDateTime;
+import java.time.temporal.ChronoUnit;
 
 import static org.snowchen.GreatSonServer.programinit.EncryptionModule.hashPassword;
 
@@ -23,27 +25,38 @@ public class UserLogin {
                 System.out.println("用户名不存在,请重新输入:");
                 continue;
             }
-            break;
-        }
 
-        while (true) {
-            System.out.println("请输入密码:");
-            this.user_password = scan.nextLine();
-            if (this.user_password.isEmpty()) {
-                System.out.println("密码不能为空,请重新输入:");
-                continue;
-            }
-            try {
-                this.user_password = hashPassword(this.user_password);
-                if (verifyPassword(this.user_name, this.user_password)) {
-                    System.out.println("登录成功!");
-                    break;
-                } else {
-                    System.out.println("密码错误,请重新输入密码!");
+            // 检查用户上次登录时间
+            LocalDateTime lastLoginTime = getLastLoginTime(this.user_name);
+            LocalDateTime currentTime = LocalDateTime.now();
+            long minutesDifference = ChronoUnit.MINUTES.between(lastLoginTime, currentTime);
+
+            if (minutesDifference <= 10) {
+                System.out.println("已为您自动登录");
+                break;
+            } else {
+                System.out.println("上次登录时间超过10分钟,请输入密码:");
+                while (true) {
+                    System.out.println("请输入密码:");
+                    this.user_password = scan.nextLine();
+                    if (this.user_password.isEmpty()) {
+                        System.out.println("密码不能为空,请重新输入:");
+                        continue;
+                    }
+                    try {
+                        this.user_password = hashPassword(this.user_password);
+                        if (verifyPassword(this.user_name, this.user_password)) {
+                            System.out.println("登录成功!");
+                            break;
+                        } else {
+                            System.out.println("密码错误,请重新输入密码!");
+                        }
+                    } catch (NoSuchAlgorithmException e) {
+                        System.err.println("加密错误: " + e.getMessage());
+                    }
                 }
-            } catch (NoSuchAlgorithmException e) {
-                System.err.println("加密错误: " + e.getMessage());
             }
+            break;
         }
     }
 
@@ -73,6 +86,32 @@ public class UserLogin {
         return false;
     }
 
+    /**
+     * 获取用户的上次登录时间
+     *
+     * @param username 用户名
+     * @return 用户的上次登录时间
+     */
+    private LocalDateTime getLastLoginTime(String username) {
+        String url = "jdbc:sqlite:identifier.sqlite";
+        String sql = "SELECT user_last_login_time FROM user_data WHERE user_name = ?";
+
+        try (Connection conn = DriverManager.getConnection(url);
+             PreparedStatement pstmt = conn.prepareStatement(sql)) {
+
+            pstmt.setString(1, username);
+            try (ResultSet rs = pstmt.executeQuery()) {
+                if (rs.next()) {
+                    Timestamp timestamp = rs.getTimestamp("user_last_login_time");
+                    return timestamp.toLocalDateTime();
+                }
+            }
+        } catch (SQLException e) {
+            System.err.println("数据库错误: " + e.getMessage());
+        }
+        return LocalDateTime.now(); // 如果获取失败,默认为当前时间
+    }
+
     /**
      * 验证密码是否正确
      *
@@ -100,9 +139,9 @@ public class UserLogin {
         return false;
     }
 
-
     public static void main(String[] args) {
         new UserLogin();
     }
 }
 
+

+ 7 - 3
src/main/java/org/snowchen/GreatSonServer/programinit/UserRegister.java

@@ -15,11 +15,12 @@ public class UserRegister {
     private String user_password;
     private String uuid;
     private String user_register_time;
+    private String user_last_login_time;
     private int user_permission_level;
     private String[] UserData; // 将UserData数组定义为私有
 
     public UserRegister() {
-        UserData = new String[5];
+        UserData = new String[6];
         // 用户信息初始化
         System.out.println("请输入用户名:");
         this.user_name = scan.nextLine();
@@ -52,10 +53,12 @@ public class UserRegister {
         // 获取年月日时分秒的时间戳
         this.user_register_time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
         UserData[4] = this.user_register_time;
+        this.user_last_login_time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+        UserData[5] = this.user_last_login_time;
         System.out.println("用户名:" + this.user_name + "\n密码:" + this.user_password + "\nuuid:" + this.uuid + "\n注册时间:" + this.user_register_time);
         insertUserData(UserData);
     }
-    
+
     // 提供一个公共方法来获取UserData数组
     public String[] getUserData() {
         return UserData;
@@ -63,7 +66,7 @@ public class UserRegister {
 
     public void insertUserData(String[] UserData) {
         String url = "jdbc:sqlite:identifier.sqlite"; // SQLite数据库URL
-        String sql = "INSERT INTO user_data (user_name, user_password, user_uuid, user_permission_level, user_register_time) VALUES (?, ?, ?, ?, ?)";
+        String sql = "INSERT INTO user_data (user_name, user_password, user_uuid, user_permission_level, user_register_time, user_last_login_time) VALUES (?, ?, ?, ?, ?,?)";
 
         try (Connection conn = DriverManager.getConnection(url);
              PreparedStatement pstmt = conn.prepareStatement(sql)) {
@@ -73,6 +76,7 @@ public class UserRegister {
             pstmt.setString(3, UserData[2]);
             pstmt.setInt(4, Integer.parseInt(UserData[3])); // 假设user_permission_level是整数
             pstmt.setString(5, UserData[4]); // 假设user_register_time是字符串形式的年月日时分秒时间戳
+            pstmt.setString(6, UserData[5]);
 
             int affectedRows = pstmt.executeUpdate();
             if (affectedRows > 0) {