Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e00a4684
authored
Apr 20, 2024
by
iszcz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
用户管理页新增分组查询
parent
b26e53bc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
36 deletions
+95
-36
controller/user.go
+2
-1
model/user.go
+30
-21
web/src/components/UsersTable.js
+63
-14
No files found.
controller/user.go
View file @
e00a4684
...
@@ -216,7 +216,8 @@ func GetAllUsers(c *gin.Context) {
...
@@ -216,7 +216,8 @@ func GetAllUsers(c *gin.Context) {
func
SearchUsers
(
c
*
gin
.
Context
)
{
func
SearchUsers
(
c
*
gin
.
Context
)
{
keyword
:=
c
.
Query
(
"keyword"
)
keyword
:=
c
.
Query
(
"keyword"
)
users
,
err
:=
model
.
SearchUsers
(
keyword
)
group
:=
c
.
Query
(
"group"
)
users
,
err
:=
model
.
SearchUsers
(
keyword
,
group
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
...
...
model/user.go
View file @
e00a4684
...
@@ -73,27 +73,36 @@ func GetAllUsers(startIdx int, num int) (users []*User, err error) {
...
@@ -73,27 +73,36 @@ func GetAllUsers(startIdx int, num int) (users []*User, err error) {
return
users
,
err
return
users
,
err
}
}
func
SearchUsers
(
keyword
string
)
([]
*
User
,
error
)
{
func
SearchUsers
(
keyword
string
,
group
string
)
([]
*
User
,
error
)
{
var
users
[]
*
User
var
users
[]
*
User
var
err
error
var
err
error
// 尝试将关键字转换为整数ID
// 尝试将关键字转换为整数ID
keywordInt
,
err
:=
strconv
.
Atoi
(
keyword
)
keywordInt
,
err
:=
strconv
.
Atoi
(
keyword
)
if
err
==
nil
{
if
err
==
nil
{
// 如果转换成功,按照ID搜索用户
// 如果转换成功,按照ID和可选的组别搜索用户
err
=
DB
.
Unscoped
()
.
Omit
(
"password"
)
.
Where
(
"id = ?"
,
keywordInt
)
.
Find
(
&
users
)
.
Error
query
:=
DB
.
Unscoped
()
.
Omit
(
"password"
)
.
Where
(
"`id` = ?"
,
keywordInt
)
if
err
!=
nil
||
len
(
users
)
>
0
{
if
group
!=
""
{
// 如果依据ID找到用户或者发生错误,返回结果或错误
query
=
query
.
Where
(
"`group` = ?"
,
group
)
// 使用反引号包围group
return
users
,
err
}
}
err
=
query
.
Find
(
&
users
)
.
Error
}
if
err
!=
nil
||
len
(
users
)
>
0
{
return
users
,
err
// 如果ID转换失败或者没有找到用户,依据其他字段进行模糊搜索
}
err
=
DB
.
Unscoped
()
.
Omit
(
"password"
)
.
}
Where
(
"username LIKE ? OR email LIKE ? OR display_name LIKE ?"
,
keyword
+
"%"
,
keyword
+
"%"
,
keyword
+
"%"
)
.
Find
(
&
users
)
.
Error
err
=
nil
return
users
,
err
query
:=
DB
.
Unscoped
()
.
Omit
(
"password"
)
likeCondition
:=
"`username` LIKE ? OR `email` LIKE ? OR `display_name` LIKE ?"
if
group
!=
""
{
query
=
query
.
Where
(
"("
+
likeCondition
+
") AND `group` = ?"
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
group
)
}
else
{
query
=
query
.
Where
(
likeCondition
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
)
}
err
=
query
.
Find
(
&
users
)
.
Error
return
users
,
err
}
}
func
GetUserById
(
id
int
,
selectAll
bool
)
(
*
User
,
error
)
{
func
GetUserById
(
id
int
,
selectAll
bool
)
(
*
User
,
error
)
{
...
...
web/src/components/UsersTable.js
View file @
e00a4684
...
@@ -235,6 +235,8 @@ const UsersTable = () => {
...
@@ -235,6 +235,8 @@ const UsersTable = () => {
const
[
activePage
,
setActivePage
]
=
useState
(
1
);
const
[
activePage
,
setActivePage
]
=
useState
(
1
);
const
[
searchKeyword
,
setSearchKeyword
]
=
useState
(
''
);
const
[
searchKeyword
,
setSearchKeyword
]
=
useState
(
''
);
const
[
searching
,
setSearching
]
=
useState
(
false
);
const
[
searching
,
setSearching
]
=
useState
(
false
);
const
[
searchGroup
,
setSearchGroup
]
=
useState
(
''
);
const
[
groupOptions
,
setGroupOptions
]
=
useState
([]);
const
[
userCount
,
setUserCount
]
=
useState
(
ITEMS_PER_PAGE
);
const
[
userCount
,
setUserCount
]
=
useState
(
ITEMS_PER_PAGE
);
const
[
showAddUser
,
setShowAddUser
]
=
useState
(
false
);
const
[
showAddUser
,
setShowAddUser
]
=
useState
(
false
);
const
[
showEditUser
,
setShowEditUser
]
=
useState
(
false
);
const
[
showEditUser
,
setShowEditUser
]
=
useState
(
false
);
...
@@ -298,6 +300,7 @@ const UsersTable = () => {
...
@@ -298,6 +300,7 @@ const UsersTable = () => {
.
catch
((
reason
)
=>
{
.
catch
((
reason
)
=>
{
showError
(
reason
);
showError
(
reason
);
});
});
fetchGroups
().
then
();
},
[]);
},
[]);
const
manageUser
=
async
(
username
,
action
,
record
)
=>
{
const
manageUser
=
async
(
username
,
action
,
record
)
=>
{
...
@@ -340,15 +343,15 @@ const UsersTable = () => {
...
@@ -340,15 +343,15 @@ const UsersTable = () => {
}
}
};
};
const
searchUsers
=
async
()
=>
{
const
searchUsers
=
async
(
searchKeyword
,
searchGroup
)
=>
{
if
(
searchKeyword
===
''
)
{
if
(
searchKeyword
===
''
&&
searchGroup
===
''
)
{
// if keyword is blank, load files instead.
// if keyword is blank, load files instead.
await
loadUsers
(
0
);
await
loadUsers
(
0
);
setActivePage
(
1
);
setActivePage
(
1
);
return
;
return
;
}
}
setSearching
(
true
);
setSearching
(
true
);
const
res
=
await
API
.
get
(
`/api/user/search?keyword=
${
searchKeyword
}
`
);
const
res
=
await
API
.
get
(
`/api/user/search?keyword=
${
searchKeyword
}
&group=
${
searchGroup
}
`
);
const
{
success
,
message
,
data
}
=
res
.
data
;
const
{
success
,
message
,
data
}
=
res
.
data
;
if
(
success
)
{
if
(
success
)
{
setUsers
(
data
);
setUsers
(
data
);
...
@@ -409,6 +412,25 @@ const UsersTable = () => {
...
@@ -409,6 +412,25 @@ const UsersTable = () => {
}
}
};
};
const
fetchGroups
=
async
()
=>
{
try
{
let
res
=
await
API
.
get
(
`/api/group/`
);
// add 'all' option
// res.data.data.unshift('all');
if
(
res
===
undefined
)
{
return
;
}
setGroupOptions
(
res
.
data
.
data
.
map
((
group
)
=>
({
label
:
group
,
value
:
group
,
})),
);
}
catch
(
error
)
{
showError
(
error
.
message
);
}
};
return
(
return
(
<>
<>
<
AddUser
<
AddUser
...
@@ -422,17 +444,44 @@ const UsersTable = () => {
...
@@ -422,17 +444,44 @@ const UsersTable = () => {
handleClose
=
{
closeEditUser
}
handleClose
=
{
closeEditUser
}
editingUser
=
{
editingUser
}
editingUser
=
{
editingUser
}
><
/EditUser
>
><
/EditUser
>
<
Form
onSubmit
=
{
searchUsers
}
>
<
Form
<
Form
.
Input
onSubmit
=
{()
=>
{
label
=
'搜索关键字'
searchUsers
(
searchKeyword
,
searchGroup
);
icon
=
'search'
}}
field
=
'keyword'
labelPosition
=
'left'
iconPosition
=
'left'
>
placeholder
=
'搜索用户的 ID,用户名,显示名称,以及邮箱地址 ...'
<
div
style
=
{{
display
:
'flex'
}}
>
value
=
{
searchKeyword
}
<
Space
>
loading
=
{
searching
}
<
Form
.
Input
onChange
=
{(
value
)
=>
handleKeywordChange
(
value
)}
label
=
'搜索关键字'
/
>
icon
=
'search'
field
=
'keyword'
iconPosition
=
'left'
placeholder
=
'搜索用户的 ID,用户名,显示名称,以及邮箱地址 ...'
value
=
{
searchKeyword
}
loading
=
{
searching
}
onChange
=
{(
value
)
=>
handleKeywordChange
(
value
)}
/
>
<
Form
.
Select
field
=
'group'
label
=
'分组'
optionList
=
{
groupOptions
}
onChange
=
{(
value
)
=>
{
setSearchGroup
(
value
);
searchUsers
(
searchKeyword
,
value
);
}}
/
>
<
Button
label
=
'查询'
type
=
'primary'
htmlType
=
'submit'
className
=
'btn-margin-right'
style
=
{{
marginRight
:
8
}}
>
查询
<
/Button
>
<
/Space
>
<
/div
>
<
/Form
>
<
/Form
>
<
Table
<
Table
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment