diff --git a/controller/user.go b/controller/user.go index 309ce49..fe596a0 100644 --- a/controller/user.go +++ b/controller/user.go @@ -216,7 +216,8 @@ func GetAllUsers(c *gin.Context) { func SearchUsers(c *gin.Context) { keyword := c.Query("keyword") - users, err := model.SearchUsers(keyword) + group := c.Query("group") + users, err := model.SearchUsers(keyword, group) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, diff --git a/model/user.go b/model/user.go index f3ce5ee..f943c17 100644 --- a/model/user.go +++ b/model/user.go @@ -73,27 +73,36 @@ func GetAllUsers(startIdx int, num int) (users []*User, err error) { return users, err } -func SearchUsers(keyword string) ([]*User, error) { - var users []*User - var err error +func SearchUsers(keyword string, group 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 + keywordInt, err := strconv.Atoi(keyword) + if err == nil { + // 如果转换成功,按照ID和可选的组别搜索用户 + query := DB.Unscoped().Omit("password").Where("`id` = ?", keywordInt) + if group != "" { + query = query.Where("`group` = ?", group) // 使用反引号包围group + } + 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) { diff --git a/web/src/components/UsersTable.js b/web/src/components/UsersTable.js index 50fe7a0..209148a 100644 --- a/web/src/components/UsersTable.js +++ b/web/src/components/UsersTable.js @@ -235,6 +235,8 @@ const UsersTable = () => { const [activePage, setActivePage] = useState(1); const [searchKeyword, setSearchKeyword] = useState(''); const [searching, setSearching] = useState(false); + const [searchGroup, setSearchGroup] = useState(''); + const [groupOptions, setGroupOptions] = useState([]); const [userCount, setUserCount] = useState(ITEMS_PER_PAGE); const [showAddUser, setShowAddUser] = useState(false); const [showEditUser, setShowEditUser] = useState(false); @@ -298,6 +300,7 @@ const UsersTable = () => { .catch((reason) => { showError(reason); }); + fetchGroups().then(); }, []); const manageUser = async (username, action, record) => { @@ -340,15 +343,15 @@ const UsersTable = () => { } }; - const searchUsers = async () => { - if (searchKeyword === '') { + const searchUsers = async (searchKeyword, searchGroup) => { + if (searchKeyword === '' && searchGroup === '') { // if keyword is blank, load files instead. await loadUsers(0); setActivePage(1); return; } 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; if (success) { setUsers(data); @@ -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 ( <> { handleClose={closeEditUser} editingUser={editingUser} > -
- handleKeywordChange(value)} - /> + { + searchUsers(searchKeyword, searchGroup); + }} + labelPosition='left' + > +
+ + handleKeywordChange(value)} + /> + { + setSearchGroup(value); + searchUsers(searchKeyword, value); + }} + /> + + +