fix(api): 修复用户列表接口查询和去重逻辑

- 移除无效的查询参数处理,简化SQL查询语句
- 修复对用户ID的重复数据去重逻辑,确保结果唯一
- 统一代码格式和缩进,提高代码可读性
- 保留按user_id升序排序及查询限制功能
- 保证接口在无匹配路由时返回404状态码
This commit is contained in:
zhouyonggao 2025-12-18 23:06:32 +08:00
parent 9c34250d3e
commit 5608897284
1 changed files with 56 additions and 60 deletions

View File

@ -25,19 +25,15 @@ func YMTUsersHandler(ymt *sql.DB) http.Handler {
} }
func (a *YMTUsersAPI) list(w http.ResponseWriter, r *http.Request) { func (a *YMTUsersAPI) list(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query().Get("q")
limitStr := r.URL.Query().Get("limit") limitStr := r.URL.Query().Get("limit")
limit := 2000 limit := 2000
if limitStr != "" { if limitStr != "" {
if n, err := strconv.Atoi(limitStr); err == nil && n > 0 && n <= 10000 { limit = n } if n, err := strconv.Atoi(limitStr); err == nil && n > 0 && n <= 10000 {
limit = n
}
} }
sql1 := "SELECT DISTINCT user_id, COALESCE(user_name, '') AS name FROM activity WHERE user_id IS NOT NULL" sql1 := "SELECT DISTINCT user_id, COALESCE(user_name, '') AS name FROM activity WHERE user_id IS NOT NULL"
args := []interface{}{} args := []interface{}{}
if q != "" {
sql1 += " AND (CAST(user_id AS CHAR) LIKE ? OR user_name LIKE ?)"
like := "%" + q + "%"
args = append(args, like, like)
}
sql1 += " ORDER BY user_id ASC LIMIT ?" sql1 += " ORDER BY user_id ASC LIMIT ?"
args = append(args, limit) args = append(args, limit)
rows, err := a.ymt.Query(sql1, args...) rows, err := a.ymt.Query(sql1, args...)