From 0907fa6994cc2b90194dec9b5a074093b50ef9e5 Mon Sep 17 00:00:00 2001
From: wozulong <>
Date: Thu, 21 Mar 2024 15:02:47 +0800
Subject: [PATCH] fix next uid
Signed-off-by: wozulong <>
---
controller/user.go | 10 ++++--
model/user.go | 16 ++++++++-
router/api-router.go | 2 +-
web/src/components/GitHubOAuth.js | 2 ++
web/src/components/LinuxDoOAuth.js | 2 ++
web/src/components/RegisterForm.js | 2 ++
web/src/components/UsersTable.js | 52 ++++++++++++++++++++++--------
7 files changed, 69 insertions(+), 17 deletions(-)
diff --git a/controller/user.go b/controller/user.go
index b002855..88791f0 100644
--- a/controller/user.go
+++ b/controller/user.go
@@ -516,7 +516,7 @@ func UpdateSelf(c *gin.Context) {
return
}
-func DeleteUser(c *gin.Context) {
+func HardDeleteUser(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusOK, gin.H{
@@ -525,7 +525,7 @@ func DeleteUser(c *gin.Context) {
})
return
}
- originUser, err := model.GetUserById(id, false)
+ originUser, err := model.GetUserByIdUnscoped(id, false)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
@@ -549,6 +549,12 @@ func DeleteUser(c *gin.Context) {
})
return
}
+
+ c.JSON(http.StatusOK, gin.H{
+ "success": true,
+ "message": "",
+ })
+ return
}
func DeleteSelf(c *gin.Context) {
diff --git a/model/user.go b/model/user.go
index 36c0962..45ce483 100644
--- a/model/user.go
+++ b/model/user.go
@@ -65,7 +65,7 @@ func CheckUserExistOrDeleted(username string, email string) (bool, error) {
func GetMaxUserId() int {
var user User
- DB.Last(&user)
+ DB.Unscoped().Last(&user)
return user.Id
}
@@ -93,6 +93,20 @@ func GetUserById(id int, selectAll bool) (*User, error) {
return &user, err
}
+func GetUserByIdUnscoped(id int, selectAll bool) (*User, error) {
+ if id == 0 {
+ return nil, errors.New("id 为空!")
+ }
+ user := User{Id: id}
+ var err error = nil
+ if selectAll {
+ err = DB.Unscoped().First(&user, "id = ?", id).Error
+ } else {
+ err = DB.Unscoped().Omit("password").First(&user, "id = ?", id).Error
+ }
+ return &user, err
+}
+
func GetUserIdByAffCode(affCode string) (int, error) {
if affCode == "" {
return 0, errors.New("affCode 为空!")
diff --git a/router/api-router.go b/router/api-router.go
index ad506b1..89d93e1 100644
--- a/router/api-router.go
+++ b/router/api-router.go
@@ -63,7 +63,7 @@ func SetApiRouter(router *gin.Engine) {
adminRoute.POST("/", controller.CreateUser)
adminRoute.POST("/manage", controller.ManageUser)
adminRoute.PUT("/", controller.UpdateUser)
- adminRoute.DELETE("/:id", controller.DeleteUser)
+ adminRoute.DELETE("/:id", controller.HardDeleteUser)
}
}
optionRoute := apiRouter.Group("/option")
diff --git a/web/src/components/GitHubOAuth.js b/web/src/components/GitHubOAuth.js
index 50c6306..e364727 100644
--- a/web/src/components/GitHubOAuth.js
+++ b/web/src/components/GitHubOAuth.js
@@ -18,6 +18,8 @@ const GitHubOAuth = () => {
const res = await API.get(`/api/oauth/github?code=${code}&state=${state}&aff=${aff}`);
const { success, message, data } = res.data;
if (success) {
+ localStorage.removeItem('aff');
+
if (message === 'bind') {
showSuccess('绑定成功!');
navigate('/setting');
diff --git a/web/src/components/LinuxDoOAuth.js b/web/src/components/LinuxDoOAuth.js
index 97af0a8..eedeb39 100644
--- a/web/src/components/LinuxDoOAuth.js
+++ b/web/src/components/LinuxDoOAuth.js
@@ -18,6 +18,8 @@ const LinuxDoOAuth = () => {
const res = await API.get(`/api/oauth/linuxdo?code=${code}&state=${state}&aff=${aff}`);
const { success, message, data } = res.data;
if (success) {
+ localStorage.removeItem('aff');
+
if (message === 'bind') {
showSuccess('绑定成功!');
navigate('/setting');
diff --git a/web/src/components/RegisterForm.js b/web/src/components/RegisterForm.js
index 1f26b63..150297b 100644
--- a/web/src/components/RegisterForm.js
+++ b/web/src/components/RegisterForm.js
@@ -69,6 +69,8 @@ const RegisterForm = () => {
);
const { success, message } = res.data;
if (success) {
+ localStorage.removeItem('aff');
+
navigate('/login');
showSuccess('注册成功!');
} else {
diff --git a/web/src/components/UsersTable.js b/web/src/components/UsersTable.js
index 7757afa..949d2c6 100644
--- a/web/src/components/UsersTable.js
+++ b/web/src/components/UsersTable.js
@@ -112,19 +112,33 @@ const UsersTable = () => {
>
}
- {
- manageUser(record.username, 'delete', record).then(() => {
- removeRecord(record.id);
- });
- }}
- >
-
-
+ {
+ record.DeletedAt !== null ? {
+ hardDeleteUser(record.id).then(() => {
+ removeRecord(record.id);
+ });
+ }}
+ >
+
+ : {
+ manageUser(record.username, 'delete', record).then(() => {
+ record.DeletedAt = new Date();
+ });
+ }}
+ >
+
+
+ }
)
}];
@@ -216,9 +230,21 @@ const UsersTable = () => {
setUsers(newUsers);
} else {
showError(message);
+ throw new Error(message);
}
};
+ const hardDeleteUser = async (userId) => {
+ const res = await API.delete('/api/user/' + userId);
+ const { success, message } = res.data;
+ if (success) {
+ showSuccess('操作成功完成!');
+ } else {
+ showError(message);
+ throw new Error(message);
+ }
+ }
+
const renderStatus = (status) => {
switch (status) {
case 1: