优化拼接查询
This commit is contained in:
45
bin/main.go
45
bin/main.go
@@ -208,6 +208,12 @@ func main() {
|
||||
|
||||
// 屏蔽 /favicon.ico
|
||||
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
||||
defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
})
|
||||
|
||||
http.HandleFunc("/api/default", func(w http.ResponseWriter, r *http.Request) {
|
||||
defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
})
|
||||
|
||||
@@ -264,7 +270,7 @@ func main() {
|
||||
})
|
||||
|
||||
// 获取图片信息列表(分页)
|
||||
http.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.HandleFunc("/api/images", func(w http.ResponseWriter, r *http.Request) {
|
||||
defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
|
||||
|
||||
// 私域: (自己的图片, 自己的文章, 自己的精选集, 点赞收藏精选集)
|
||||
@@ -284,19 +290,17 @@ func main() {
|
||||
}
|
||||
|
||||
// 拼接查询条件, 超级简洁写法
|
||||
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, ","))
|
||||
var addCondition = func(conditions *strings.Builder, key, column string) {
|
||||
if values := QueryConditions(key); len(values) > 0 {
|
||||
conditions.WriteString(fmt.Sprintf(" AND %s IN (%s)", column, strings.Join(values, ",")))
|
||||
}
|
||||
}
|
||||
var conditions strings.Builder
|
||||
addCondition(&conditions, "authors", "author")
|
||||
addCondition(&conditions, "tags", "tag")
|
||||
addCondition(&conditions, "categories", "categorie")
|
||||
addCondition(&conditions, "sets", "sets")
|
||||
|
||||
var ids []int64
|
||||
if similar := QueryConditions("similar"); len(similar) > 0 {
|
||||
id, err := strconv.Atoi(strings.Trim(similar[0], "'"))
|
||||
@@ -311,19 +315,20 @@ func main() {
|
||||
idsStr[i] = strconv.FormatInt(v, 10)
|
||||
}
|
||||
if len(idsStr) > 0 {
|
||||
conditions += fmt.Sprintf(" AND id IN (%s)", strings.Join(idsStr, ",")) // 拼接查询条件
|
||||
conditions.WriteString(fmt.Sprintf(" AND id IN (%s)", strings.Join(idsStr, ","))) // 拼接查询条件
|
||||
}
|
||||
}
|
||||
if conditions != "" {
|
||||
conditions = strings.Replace(conditions, " AND", "", 1) // 去掉第一个 AND
|
||||
conditions = " WHERE" + conditions // 拼接 WHERE
|
||||
if conditions.Len() > 0 {
|
||||
conditionsStr := conditions.String()
|
||||
conditionsStr = strings.Replace(conditionsStr, " AND", "", 1) // 去掉第一个 AND
|
||||
conditionsStr = " WHERE" + conditionsStr // 拼接 WHERE
|
||||
}
|
||||
|
||||
// 获取图片列表
|
||||
var images ListView
|
||||
var image_list []Image
|
||||
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, width, height, content, update_time, create_time, user_id, article_id, article_category_top_id, praise_count, collect_count FROM web_images"+conditions+" LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize)
|
||||
rows, err := mysqlConnection.Database.Query("SELECT id, width, height, content, update_time, create_time, user_id, article_id, article_category_top_id, praise_count, collect_count FROM web_images"+conditions.String()+" LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize)
|
||||
if err != nil {
|
||||
log.Println("获取图片列表失败", err)
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
@@ -431,7 +436,7 @@ func main() {
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions).Scan(&images.Total)
|
||||
err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions.String()).Scan(&images.Total)
|
||||
if err != nil {
|
||||
log.Println("获取图片总数失败", err)
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
@@ -614,7 +619,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("safeParam", safeParam)
|
||||
//fmt.Println("safeParam", safeParam)
|
||||
urls, err := models.GetVideoM3U8(safeParam)
|
||||
if err != nil {
|
||||
log.Println("获取视频链接失败", err)
|
||||
|
Reference in New Issue
Block a user