增加 views
This commit is contained in:
		@@ -1,8 +1,11 @@
 | 
				
			|||||||
package api
 | 
					package api
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@@ -32,6 +35,7 @@ type Article struct {
 | 
				
			|||||||
	Emoji3       int       `json:"emoji3"`
 | 
						Emoji3       int       `json:"emoji3"`
 | 
				
			||||||
	Emoji4       int       `json:"emoji4"`
 | 
						Emoji4       int       `json:"emoji4"`
 | 
				
			||||||
	Emoji5       int       `json:"emoji5"`
 | 
						Emoji5       int       `json:"emoji5"`
 | 
				
			||||||
 | 
						Views        int       `json:"views"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (Article) TableName() string {
 | 
					func (Article) TableName() string {
 | 
				
			||||||
@@ -60,6 +64,7 @@ var articleType = graphql.NewObject(graphql.ObjectConfig{
 | 
				
			|||||||
		"emoji3":        &graphql.Field{Type: graphql.Int, Description: "表情3数量"},
 | 
							"emoji3":        &graphql.Field{Type: graphql.Int, Description: "表情3数量"},
 | 
				
			||||||
		"emoji4":        &graphql.Field{Type: graphql.Int, Description: "表情4数量"},
 | 
							"emoji4":        &graphql.Field{Type: graphql.Int, Description: "表情4数量"},
 | 
				
			||||||
		"emoji5":        &graphql.Field{Type: graphql.Int, Description: "表情5数量"},
 | 
							"emoji5":        &graphql.Field{Type: graphql.Int, Description: "表情5数量"},
 | 
				
			||||||
 | 
							"views":         &graphql.Field{Type: graphql.Int, Description: "浏览量"},
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -211,6 +216,60 @@ var ArticleItems = &graphql.Field{
 | 
				
			|||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections)
 | 
				
			||||||
 | 
							if funk.Contains(items, "views") {
 | 
				
			||||||
 | 
								type ApiResponse struct {
 | 
				
			||||||
 | 
									ID    int `json:"id"`
 | 
				
			||||||
 | 
									Count int `json:"count"`
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 0. 收集要查询的 ID
 | 
				
			||||||
 | 
								var ids []int
 | 
				
			||||||
 | 
								for x := range articles {
 | 
				
			||||||
 | 
									ids = append(ids, articles[x].ID)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								idx := strings.Trim(strings.Replace(fmt.Sprint(ids), " ", ",", -1), "[]")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 1. 发送 GET 请求
 | 
				
			||||||
 | 
								resp, err := http.Get("http://localhost:6005/api/get_views/文章?ids=" + idx)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error making GET request:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								defer resp.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 2. 检查 HTTP 状态码
 | 
				
			||||||
 | 
								if resp.StatusCode != http.StatusOK {
 | 
				
			||||||
 | 
									fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 3. 读取响应体
 | 
				
			||||||
 | 
								body, err := io.ReadAll(resp.Body)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error reading response body:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 4. 解析 JSON 数据到结构体
 | 
				
			||||||
 | 
								var data []ApiResponse
 | 
				
			||||||
 | 
								err = json.Unmarshal(body, &data)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error unmarshalling JSON:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 5. 赋值到数据集
 | 
				
			||||||
 | 
								for _, item := range data {
 | 
				
			||||||
 | 
									for i := range articles {
 | 
				
			||||||
 | 
										if articles[i].ID == item.ID {
 | 
				
			||||||
 | 
											articles[i].Views = item.Count
 | 
				
			||||||
 | 
											continue
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return map[string]interface{}{
 | 
							return map[string]interface{}{
 | 
				
			||||||
			"list":  articles,
 | 
								"list":  articles,
 | 
				
			||||||
			"total": total,
 | 
								"total": total,
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										59
									
								
								api/game.go
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								api/game.go
									
									
									
									
									
								
							@@ -1,7 +1,10 @@
 | 
				
			|||||||
package api
 | 
					package api
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@@ -38,6 +41,7 @@ type Game struct {
 | 
				
			|||||||
	Emoji3       int       `json:"emoji3"`
 | 
						Emoji3       int       `json:"emoji3"`
 | 
				
			||||||
	Emoji4       int       `json:"emoji4"`
 | 
						Emoji4       int       `json:"emoji4"`
 | 
				
			||||||
	Emoji5       int       `json:"emoji5"`
 | 
						Emoji5       int       `json:"emoji5"`
 | 
				
			||||||
 | 
						Views        int       `json:"views"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (Game) TableName() string {
 | 
					func (Game) TableName() string {
 | 
				
			||||||
@@ -70,6 +74,7 @@ var gameType = graphql.NewObject(graphql.ObjectConfig{
 | 
				
			|||||||
		"emoji3":        &graphql.Field{Type: graphql.Int, Description: "表情3数量"},
 | 
							"emoji3":        &graphql.Field{Type: graphql.Int, Description: "表情3数量"},
 | 
				
			||||||
		"emoji4":        &graphql.Field{Type: graphql.Int, Description: "表情4数量"},
 | 
							"emoji4":        &graphql.Field{Type: graphql.Int, Description: "表情4数量"},
 | 
				
			||||||
		"emoji5":        &graphql.Field{Type: graphql.Int, Description: "表情5数量"},
 | 
							"emoji5":        &graphql.Field{Type: graphql.Int, Description: "表情5数量"},
 | 
				
			||||||
 | 
							"views":         &graphql.Field{Type: graphql.Int, Description: "浏览量"},
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -251,6 +256,60 @@ var GameItems = &graphql.Field{
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections)
 | 
				
			||||||
 | 
							if funk.Contains(items, "views") {
 | 
				
			||||||
 | 
								type ApiResponse struct {
 | 
				
			||||||
 | 
									ID    int `json:"id"`
 | 
				
			||||||
 | 
									Count int `json:"count"`
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 0. 收集要查询的 ID
 | 
				
			||||||
 | 
								var ids []int
 | 
				
			||||||
 | 
								for x := range games {
 | 
				
			||||||
 | 
									ids = append(ids, games[x].ID)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								idx := strings.Trim(strings.Replace(fmt.Sprint(ids), " ", ",", -1), "[]")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 1. 发送 GET 请求
 | 
				
			||||||
 | 
								resp, err := http.Get("http://localhost:6005/api/get_views/文章?ids=" + idx)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error making GET request:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								defer resp.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 2. 检查 HTTP 状态码
 | 
				
			||||||
 | 
								if resp.StatusCode != http.StatusOK {
 | 
				
			||||||
 | 
									fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 3. 读取响应体
 | 
				
			||||||
 | 
								body, err := io.ReadAll(resp.Body)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error reading response body:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 4. 解析 JSON 数据到结构体
 | 
				
			||||||
 | 
								var data []ApiResponse
 | 
				
			||||||
 | 
								err = json.Unmarshal(body, &data)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error unmarshalling JSON:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 5. 赋值到数据集
 | 
				
			||||||
 | 
								for _, item := range data {
 | 
				
			||||||
 | 
									for i := range games {
 | 
				
			||||||
 | 
										if games[i].ID == item.ID {
 | 
				
			||||||
 | 
											games[i].Views = item.Count
 | 
				
			||||||
 | 
											continue
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return map[string]interface{}{
 | 
							return map[string]interface{}{
 | 
				
			||||||
			"list":  games,
 | 
								"list":  games,
 | 
				
			||||||
			"total": total,
 | 
								"total": total,
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										59
									
								
								api/image.go
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								api/image.go
									
									
									
									
									
								
							@@ -5,7 +5,9 @@ import (
 | 
				
			|||||||
	"database/sql/driver"
 | 
						"database/sql/driver"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
@@ -14,6 +16,7 @@ import (
 | 
				
			|||||||
	"git.satori.love/gameui/webp/models"
 | 
						"git.satori.love/gameui/webp/models"
 | 
				
			||||||
	"github.com/doug-martin/goqu/v9"
 | 
						"github.com/doug-martin/goqu/v9"
 | 
				
			||||||
	"github.com/graphql-go/graphql"
 | 
						"github.com/graphql-go/graphql"
 | 
				
			||||||
 | 
						"github.com/thoas/go-funk"
 | 
				
			||||||
	"github.com/zhenghaoz/gorse/client"
 | 
						"github.com/zhenghaoz/gorse/client"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -42,6 +45,7 @@ type Image struct {
 | 
				
			|||||||
	Emoji3       int       `json:"emoji3"`
 | 
						Emoji3       int       `json:"emoji3"`
 | 
				
			||||||
	Emoji4       int       `json:"emoji4"`
 | 
						Emoji4       int       `json:"emoji4"`
 | 
				
			||||||
	Emoji5       int       `json:"emoji5"`
 | 
						Emoji5       int       `json:"emoji5"`
 | 
				
			||||||
 | 
						Views        int       `json:"views"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (Image) TableName() string {
 | 
					func (Image) TableName() string {
 | 
				
			||||||
@@ -161,6 +165,7 @@ var imageType = graphql.NewObject(graphql.ObjectConfig{
 | 
				
			|||||||
		"emoji3": &graphql.Field{Type: graphql.Int, Description: "表情3数量"},
 | 
							"emoji3": &graphql.Field{Type: graphql.Int, Description: "表情3数量"},
 | 
				
			||||||
		"emoji4": &graphql.Field{Type: graphql.Int, Description: "表情4数量"},
 | 
							"emoji4": &graphql.Field{Type: graphql.Int, Description: "表情4数量"},
 | 
				
			||||||
		"emoji5": &graphql.Field{Type: graphql.Int, Description: "表情5数量"},
 | 
							"emoji5": &graphql.Field{Type: graphql.Int, Description: "表情5数量"},
 | 
				
			||||||
 | 
							"views":  &graphql.Field{Type: graphql.Int, Description: "浏览量"},
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -616,6 +621,60 @@ var ImageItems = &graphql.Field{
 | 
				
			|||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections)
 | 
				
			||||||
 | 
							if funk.Contains(items, "views") {
 | 
				
			||||||
 | 
								type ApiResponse struct {
 | 
				
			||||||
 | 
									ID    int `json:"id"`
 | 
				
			||||||
 | 
									Count int `json:"count"`
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 0. 收集要查询的 ID
 | 
				
			||||||
 | 
								var ids []int
 | 
				
			||||||
 | 
								for x := range images {
 | 
				
			||||||
 | 
									ids = append(ids, images[x].ID)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								idx := strings.Trim(strings.Replace(fmt.Sprint(ids), " ", ",", -1), "[]")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 1. 发送 GET 请求
 | 
				
			||||||
 | 
								resp, err := http.Get("http://localhost:6005/api/get_views/截图?ids=" + idx)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error making GET request:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								defer resp.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 2. 检查 HTTP 状态码
 | 
				
			||||||
 | 
								if resp.StatusCode != http.StatusOK {
 | 
				
			||||||
 | 
									fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 3. 读取响应体
 | 
				
			||||||
 | 
								body, err := io.ReadAll(resp.Body)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error reading response body:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 4. 解析 JSON 数据到结构体
 | 
				
			||||||
 | 
								var data []ApiResponse
 | 
				
			||||||
 | 
								err = json.Unmarshal(body, &data)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error unmarshalling JSON:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 5. 赋值到数据集
 | 
				
			||||||
 | 
								for _, item := range data {
 | 
				
			||||||
 | 
									for i := range images {
 | 
				
			||||||
 | 
										if images[i].ID == item.ID {
 | 
				
			||||||
 | 
											images[i].Views = item.Count
 | 
				
			||||||
 | 
											continue
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return map[string]interface{}{
 | 
							return map[string]interface{}{
 | 
				
			||||||
			"list":  images,
 | 
								"list":  images,
 | 
				
			||||||
			"total": total,
 | 
								"total": total,
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										59
									
								
								api/work.go
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								api/work.go
									
									
									
									
									
								
							@@ -1,7 +1,10 @@
 | 
				
			|||||||
package api
 | 
					package api
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@@ -34,6 +37,7 @@ type Work struct {
 | 
				
			|||||||
	Emoji3       int       `json:"emoji3"`
 | 
						Emoji3       int       `json:"emoji3"`
 | 
				
			||||||
	Emoji4       int       `json:"emoji4"`
 | 
						Emoji4       int       `json:"emoji4"`
 | 
				
			||||||
	Emoji5       int       `json:"emoji5"`
 | 
						Emoji5       int       `json:"emoji5"`
 | 
				
			||||||
 | 
						Views        int       `json:"views"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (Work) TableName() string {
 | 
					func (Work) TableName() string {
 | 
				
			||||||
@@ -59,6 +63,7 @@ var workType = graphql.NewObject(graphql.ObjectConfig{
 | 
				
			|||||||
		"emoji3":        &graphql.Field{Type: graphql.Int, Description: "表情3数量"},
 | 
							"emoji3":        &graphql.Field{Type: graphql.Int, Description: "表情3数量"},
 | 
				
			||||||
		"emoji4":        &graphql.Field{Type: graphql.Int, Description: "表情4数量"},
 | 
							"emoji4":        &graphql.Field{Type: graphql.Int, Description: "表情4数量"},
 | 
				
			||||||
		"emoji5":        &graphql.Field{Type: graphql.Int, Description: "表情5数量"},
 | 
							"emoji5":        &graphql.Field{Type: graphql.Int, Description: "表情5数量"},
 | 
				
			||||||
 | 
							"views":         &graphql.Field{Type: graphql.Int, Description: "浏览量"},
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -212,6 +217,60 @@ var WorkItems = &graphql.Field{
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections)
 | 
				
			||||||
 | 
							if funk.Contains(items, "views") {
 | 
				
			||||||
 | 
								type ApiResponse struct {
 | 
				
			||||||
 | 
									ID    int `json:"id"`
 | 
				
			||||||
 | 
									Count int `json:"count"`
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 0. 收集要查询的 ID
 | 
				
			||||||
 | 
								var ids []int
 | 
				
			||||||
 | 
								for x := range works {
 | 
				
			||||||
 | 
									ids = append(ids, works[x].ID)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								idx := strings.Trim(strings.Replace(fmt.Sprint(ids), " ", ",", -1), "[]")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 1. 发送 GET 请求
 | 
				
			||||||
 | 
								resp, err := http.Get("http://localhost:6005/api/get_views/文章?ids=" + idx)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error making GET request:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								defer resp.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 2. 检查 HTTP 状态码
 | 
				
			||||||
 | 
								if resp.StatusCode != http.StatusOK {
 | 
				
			||||||
 | 
									fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 3. 读取响应体
 | 
				
			||||||
 | 
								body, err := io.ReadAll(resp.Body)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error reading response body:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 4. 解析 JSON 数据到结构体
 | 
				
			||||||
 | 
								var data []ApiResponse
 | 
				
			||||||
 | 
								err = json.Unmarshal(body, &data)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Println("Error unmarshalling JSON:", err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 5. 赋值到数据集
 | 
				
			||||||
 | 
								for _, item := range data {
 | 
				
			||||||
 | 
									for i := range works {
 | 
				
			||||||
 | 
										if works[i].ID == item.ID {
 | 
				
			||||||
 | 
											works[i].Views = item.Count
 | 
				
			||||||
 | 
											continue
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return map[string]interface{}{
 | 
							return map[string]interface{}{
 | 
				
			||||||
			"list":  works,
 | 
								"list":  works,
 | 
				
			||||||
			"total": total,
 | 
								"total": total,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user