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
130d2751
authored
Mar 29, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: SearchUsers (close #160)
parent
0465b186
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
2 deletions
+21
-2
model/user.go
+21
-2
No files found.
model/user.go
View file @
130d2751
...
...
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"one-api/common"
"strconv"
"strings"
"time"
...
...
@@ -72,8 +73,26 @@ func GetAllUsers(startIdx int, num int) (users []*User, err error) {
return
users
,
err
}
func
SearchUsers
(
keyword
string
)
(
users
[]
*
User
,
err
error
)
{
err
=
DB
.
Omit
(
"password"
)
.
Where
(
"id = ? or username LIKE ? or email LIKE ? or display_name LIKE ?"
,
keyword
,
keyword
+
"%"
,
keyword
+
"%"
,
keyword
+
"%"
)
.
Find
(
&
users
)
.
Error
func
SearchUsers
(
keyword
string
)
([]
*
User
,
error
)
{
var
users
[]
*
User
var
err
error
// 尝试将关键字转换为整数ID
keywordInt
,
err
:=
strconv
.
Atoi
(
keyword
)
if
err
==
nil
{
// 如果转换成功,按照ID搜索用户
err
=
DB
.
Unscoped
()
.
Omit
(
"password"
)
.
Where
(
"id = ?"
,
keywordInt
)
.
Find
(
&
users
)
.
Error
if
err
!=
nil
||
len
(
users
)
>
0
{
// 如果依据ID找到用户或者发生错误,返回结果或错误
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
return
users
,
err
}
...
...
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