From 51338e22b306283f396b15d8085e541466965002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Tue, 11 Apr 2023 07:04:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/main.go | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/bin/main.go b/bin/main.go index c4fca63..62e1b99 100644 --- a/bin/main.go +++ b/bin/main.go @@ -49,6 +49,7 @@ type Images struct { Page int `json:"page"` PageSize int `json:"page_size"` Total int `json:"total"` + Next bool `json:"next"` List []Image `json:"list"` } @@ -68,30 +69,40 @@ func main() { // 排序 // 分页 - // 按标签查询 - var tags string - if list := strings.Split(r.URL.Query().Get("tags"), ","); len(list) > 0 { - for _, tag := range list { - tags += fmt.Sprintf("'%s',", tag) + // 获取查询条件(忽略空值), 超级简洁写法 + QueryConditions := func(key string) (list []string) { + for _, item := range strings.Split(r.URL.Query().Get(key), ",") { + if item != "" { + list = append(list, fmt.Sprintf("'%s'", item)) + } } - tags = tags[:len(tags)-1] - fmt.Println(tags) + return list } - // 按分类查询 - var categories string - if list := strings.Split(r.URL.Query().Get("categories"), ","); len(list) > 0 { - for _, category := range list { - categories += fmt.Sprintf("'%s',", category) - } - categories = categories[:len(categories)-1] - fmt.Println(categories) + // 拼接查询条件, 超级简洁写法 + conditions := "" + if authors := QueryConditions("authors"); len(authors) > 0 { + conditions += fmt.Sprintf(" AND author IN (%s)", strings.Join(authors, ",")) + } + if tags := QueryConditions("tags"); len(tags) > 0 { + conditions += fmt.Sprintf(" AND tag IN (%s)", strings.Join(tags, ",")) + } + if categories := QueryConditions("categories"); len(categories) > 0 { + conditions += fmt.Sprintf(" AND categorie IN (%s)", strings.Join(categories, ",")) + } + if sets := QueryConditions("sets"); len(sets) > 0 { + conditions += fmt.Sprintf(" AND sets IN (%s)", strings.Join(sets, ",")) + } + if conditions != "" { + conditions = strings.Replace(conditions, " AND", "", 1) // 去掉第一个 AND + conditions = " WHERE" + conditions // 拼接 WHERE + fmt.Println(conditions) // 打印查询条件 } // 获取图片列表 var images Images images.Page, images.PageSize = stringToInt(r.URL.Query().Get("page"), 1), stringToInt(r.URL.Query().Get("pageSize"), 10) - rows, err := mysqlConnection.Database.Query("SELECT id, content, update_time, create_time FROM web_images LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize) + rows, err := mysqlConnection.Database.Query("SELECT id, content, update_time, create_time FROM web_images"+conditions+" LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize) if err != nil { log.Println("获取图片列表失败", err) http.Error(w, err.Error(), http.StatusBadRequest) @@ -110,13 +121,16 @@ func main() { } // 获取总数 - err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images").Scan(&images.Total) + err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions).Scan(&images.Total) if err != nil { log.Println("获取图片总数失败", err) http.Error(w, err.Error(), http.StatusBadRequest) return } + // 是否有下一页 + images.Next = images.Total > images.Page*images.PageSize + // 将对象转换为有缩进的JSON输出 data, err := json.MarshalIndent(images, "", " ") if err != nil {