mirror of https://github.com/veypi/OneAuth.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
|
1 week ago
|
//
|
||
|
|
// Copyright (C) 2024 veypi <i@veypi.com>
|
||
|
|
// 2025-03-04 16:08:06
|
||
|
|
// Distributed under terms of the MIT license.
|
||
|
|
//
|
||
|
|
|
||
|
|
package auth
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/veypi/vbase/cfg"
|
||
|
|
"github.com/veypi/vbase/models"
|
||
|
|
"github.com/veypi/vigo"
|
||
|
|
)
|
||
|
|
|
||
|
|
// PublicUserInfo 公开的用户信息(仅包含无关紧要的信息)
|
||
|
|
type PublicUserInfo struct {
|
||
|
|
ID string `json:"id"`
|
||
|
|
Username string `json:"username"`
|
||
|
|
Nickname string `json:"nickname"`
|
||
|
|
Avatar string `json:"avatar"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// SearchUsersRequest 搜索用户请求
|
||
|
|
type SearchUsersRequest struct {
|
||
|
|
Keyword *string `json:"keyword" src:"query" desc:"搜索关键词(用户名或昵称)"`
|
||
|
|
Limit int `json:"limit" src:"query" default:"20" desc:"返回数量限制"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// SearchUsersResponse 搜索用户响应
|
||
|
|
type SearchUsersResponse struct {
|
||
|
|
Items []PublicUserInfo `json:"items"`
|
||
|
|
Total int64 `json:"total"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// searchUsers 搜索用户(公开接口,仅返回公开信息)
|
||
|
|
func searchUsers(x *vigo.X, req *SearchUsersRequest) (*SearchUsersResponse, error) {
|
||
|
|
if req.Limit <= 0 || req.Limit > 50 {
|
||
|
|
req.Limit = 20
|
||
|
|
}
|
||
|
|
|
||
|
|
db := cfg.DB().Model(&models.User{}).Where("status = ?", models.UserStatusActive)
|
||
|
|
|
||
|
|
// 搜索关键词
|
||
|
|
if req.Keyword != nil && *req.Keyword != "" {
|
||
|
|
keyword := "%" + *req.Keyword + "%"
|
||
|
|
db = db.Where("username LIKE ? OR nickname LIKE ?", keyword, keyword)
|
||
|
|
}
|
||
|
|
|
||
|
|
var total int64
|
||
|
|
if err := db.Count(&total).Error; err != nil {
|
||
|
|
return nil, vigo.ErrInternalServer.WithError(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
var users []models.User
|
||
|
|
if err := db.Order("created_at DESC").Limit(req.Limit).Find(&users).Error; err != nil {
|
||
|
|
return nil, vigo.ErrInternalServer.WithError(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 转换为公开信息
|
||
|
|
items := make([]PublicUserInfo, len(users))
|
||
|
|
for i, user := range users {
|
||
|
|
items[i] = PublicUserInfo{
|
||
|
|
ID: user.ID,
|
||
|
|
Username: user.Username,
|
||
|
|
Nickname: user.Nickname,
|
||
|
|
Avatar: user.Avatar,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return &SearchUsersResponse{
|
||
|
|
Items: items,
|
||
|
|
Total: total,
|
||
|
|
}, nil
|
||
|
|
}
|