增加 views
This commit is contained in:
59
api/image.go
59
api/image.go
@@ -5,7 +5,9 @@ import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -14,6 +16,7 @@ import (
|
||||
"git.satori.love/gameui/webp/models"
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
"github.com/graphql-go/graphql"
|
||||
"github.com/thoas/go-funk"
|
||||
"github.com/zhenghaoz/gorse/client"
|
||||
)
|
||||
|
||||
@@ -42,6 +45,7 @@ type Image struct {
|
||||
Emoji3 int `json:"emoji3"`
|
||||
Emoji4 int `json:"emoji4"`
|
||||
Emoji5 int `json:"emoji5"`
|
||||
Views int `json:"views"`
|
||||
}
|
||||
|
||||
func (Image) TableName() string {
|
||||
@@ -161,6 +165,7 @@ var imageType = graphql.NewObject(graphql.ObjectConfig{
|
||||
"emoji3": &graphql.Field{Type: graphql.Int, Description: "表情3数量"},
|
||||
"emoji4": &graphql.Field{Type: graphql.Int, Description: "表情4数量"},
|
||||
"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
|
||||
}
|
||||
|
||||
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{}{
|
||||
"list": images,
|
||||
"total": total,
|
||||
|
Reference in New Issue
Block a user