获取用户信息
This commit is contained in:
		@@ -120,32 +120,41 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
			},
 | 
								},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
 | 
							Resolve: func(p graphql.ResolveParams) (interface{}, error) {
 | 
				
			||||||
 | 
								// 返回字段
 | 
				
			||||||
			var fields []string
 | 
								var fields []string
 | 
				
			||||||
			requestedFields := p.Info.FieldASTs[0].SelectionSet.Selections
 | 
								requestedFields := p.Info.FieldASTs[0].SelectionSet.Selections
 | 
				
			||||||
			for _, field := range requestedFields {
 | 
								for _, field := range requestedFields {
 | 
				
			||||||
				fieldAST, ok := field.(*ast.Field)
 | 
									fieldAST, ok := field.(*ast.Field)
 | 
				
			||||||
				if ok {
 | 
									if ok {
 | 
				
			||||||
 | 
										if fieldAST.Name.Value == "user" {
 | 
				
			||||||
 | 
											fields = append(fields, "user_id")
 | 
				
			||||||
 | 
										} else if fieldAST.Name.Value == "article" {
 | 
				
			||||||
 | 
											fields = append(fields, "article_category_top_id")
 | 
				
			||||||
 | 
										} else {
 | 
				
			||||||
						fields = append(fields, fieldAST.Name.Value)
 | 
											fields = append(fields, fieldAST.Name.Value)
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			fields_str := strings.Join(fields, ",")
 | 
								fields_str := strings.Join(fields, ",")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 筛选条件
 | 
				
			||||||
			var where []string
 | 
								var where []string
 | 
				
			||||||
			if p.Args["id"] != nil {
 | 
								if p.Args["id"] != nil {
 | 
				
			||||||
				where = append(where, fmt.Sprintf("id=%d", p.Args["id"]))
 | 
									where = append(where, fmt.Sprintf("id=%d", p.Args["id"]))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if p.Args["width"] != nil {
 | 
								if p.Args["width"] != nil {
 | 
				
			||||||
				where = append(where, fmt.Sprintf("width='%s'", p.Args["width"]))
 | 
									where = append(where, fmt.Sprintf("width=%d", p.Args["width"]))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if p.Args["height"] != nil {
 | 
								if p.Args["height"] != nil {
 | 
				
			||||||
				where = append(where, fmt.Sprintf("height='%s'", p.Args["height"]))
 | 
									where = append(where, fmt.Sprintf("height=%d", p.Args["height"]))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if p.Args["content"] != nil {
 | 
								if p.Args["content"] != nil {
 | 
				
			||||||
				where = append(where, fmt.Sprintf("content='%s'", p.Args["content"]))
 | 
									where = append(where, fmt.Sprintf("content='%s'", p.Args["content"]))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			// 筛选条件
 | 
					 | 
				
			||||||
			where_str := strings.Join(where, " AND ")
 | 
								where_str := strings.Join(where, " AND ")
 | 
				
			||||||
			fmt.Println(where_str)
 | 
								fmt.Println(where_str)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 执行查询
 | 
				
			||||||
			var query strings.Builder
 | 
								var query strings.Builder
 | 
				
			||||||
			query.WriteString(fmt.Sprintf("SELECT %s FROM web_images WHERE %s LIMIT %s", fields_str, where_str, "10"))
 | 
								query.WriteString(fmt.Sprintf("SELECT %s FROM web_images WHERE %s LIMIT %s", fields_str, where_str, "10"))
 | 
				
			||||||
			fmt.Println(query.String())
 | 
								fmt.Println(query.String())
 | 
				
			||||||
@@ -155,48 +164,39 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
				fmt.Println("获取图像列表失败", err)
 | 
									fmt.Println("获取图像列表失败", err)
 | 
				
			||||||
				return nil, err
 | 
									return nil, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			fmt.Println(images)
 | 
					
 | 
				
			||||||
 | 
								// 获取用户信息(如果图像列表不为空且请求字段中包含user)
 | 
				
			||||||
 | 
								if len(images) > 0 && strings.Contains(fields_str, "user_id") {
 | 
				
			||||||
 | 
									// 取到所有的用户ID, 去除重复
 | 
				
			||||||
 | 
									user_ids := make(map[int]bool)
 | 
				
			||||||
 | 
									for _, image := range images {
 | 
				
			||||||
 | 
										user_ids[image.UserID] = true
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									// map 转换为数组
 | 
				
			||||||
 | 
									uniqueIds := make([]int, 0, len(user_ids))
 | 
				
			||||||
 | 
									for id := range user_ids {
 | 
				
			||||||
 | 
										uniqueIds = append(uniqueIds, id)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									// 合并为以逗号分隔的字符串
 | 
				
			||||||
 | 
									user_ids_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(uniqueIds)), ","), "[]")
 | 
				
			||||||
 | 
									fmt.Println(user_ids_str)
 | 
				
			||||||
 | 
									// 查询用户信息
 | 
				
			||||||
 | 
									var users []User
 | 
				
			||||||
 | 
									if err := connection.Select(&users, fmt.Sprintf("SELECT id,user_name,avatar,rank,create_time,update_time FROM web_member WHERE id IN (%s)", user_ids_str)); err != nil {
 | 
				
			||||||
 | 
										fmt.Println("获取用户列表失败", err)
 | 
				
			||||||
 | 
										return nil, err
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									// 将用户信息与图像信息关联
 | 
				
			||||||
 | 
									for i, image := range images {
 | 
				
			||||||
 | 
										for _, user := range users {
 | 
				
			||||||
 | 
											if image.UserID == user.ID {
 | 
				
			||||||
 | 
												images[i].User = user
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			return images, nil
 | 
								return images, nil
 | 
				
			||||||
			//return []interface{}{
 | 
					 | 
				
			||||||
			//	map[string]interface{}{
 | 
					 | 
				
			||||||
			//		"id":                      1,
 | 
					 | 
				
			||||||
			//		"width":                   100,
 | 
					 | 
				
			||||||
			//		"height":                  100,
 | 
					 | 
				
			||||||
			//		"content":                 "content",
 | 
					 | 
				
			||||||
			//		"article_category_top_id": 1,
 | 
					 | 
				
			||||||
			//		"praise_count":            1,
 | 
					 | 
				
			||||||
			//		"collect_count":           1,
 | 
					 | 
				
			||||||
			//		"create_time":             "2018-01-01 00:00:00",
 | 
					 | 
				
			||||||
			//		"update_time":             "2018-01-01 00:00:00",
 | 
					 | 
				
			||||||
			//		"user": map[string]interface{}{
 | 
					 | 
				
			||||||
			//			"id":     1,
 | 
					 | 
				
			||||||
			//			"name":   "user1",
 | 
					 | 
				
			||||||
			//			"age":    10,
 | 
					 | 
				
			||||||
			//			"info":   "info",
 | 
					 | 
				
			||||||
			//			"price":  1.1,
 | 
					 | 
				
			||||||
			//			"avatar": "",
 | 
					 | 
				
			||||||
			//		},
 | 
					 | 
				
			||||||
			//	},
 | 
					 | 
				
			||||||
			//	map[string]interface{}{
 | 
					 | 
				
			||||||
			//		"id":                      2,
 | 
					 | 
				
			||||||
			//		"width":                   100,
 | 
					 | 
				
			||||||
			//		"height":                  100,
 | 
					 | 
				
			||||||
			//		"content":                 "content",
 | 
					 | 
				
			||||||
			//		"article_category_top_id": 1,
 | 
					 | 
				
			||||||
			//		"praise_count":            1,
 | 
					 | 
				
			||||||
			//		"collect_count":           1,
 | 
					 | 
				
			||||||
			//		"create_time":             "2018-01-01 00:00:00",
 | 
					 | 
				
			||||||
			//		"update_time":             "2018-01-01 00:00:00",
 | 
					 | 
				
			||||||
			//		"user": map[string]interface{}{
 | 
					 | 
				
			||||||
			//			"id":     2,
 | 
					 | 
				
			||||||
			//			"name":   "user2",
 | 
					 | 
				
			||||||
			//			"age":    20,
 | 
					 | 
				
			||||||
			//			"info":   "info",
 | 
					 | 
				
			||||||
			//			"price":  2.2,
 | 
					 | 
				
			||||||
			//			"avatar": "",
 | 
					 | 
				
			||||||
			//		},
 | 
					 | 
				
			||||||
			//	},
 | 
					 | 
				
			||||||
			//}, nil
 | 
					 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,8 +2,6 @@ package api
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	"git.satori.love/gameui/webp/models"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Image struct {
 | 
					type Image struct {
 | 
				
			||||||
@@ -15,10 +13,10 @@ type Image struct {
 | 
				
			|||||||
	PraiseCount          int       `json:"praise_count"            db:"praise_count"`
 | 
						PraiseCount          int       `json:"praise_count"            db:"praise_count"`
 | 
				
			||||||
	CollectCount         int       `json:"collect_count"           db:"collect_count"`
 | 
						CollectCount         int       `json:"collect_count"           db:"collect_count"`
 | 
				
			||||||
	UserID               int       `json:"user_id"                 db:"user_id"`
 | 
						UserID               int       `json:"user_id"                 db:"user_id"`
 | 
				
			||||||
	User                 models.User    `json:"user"                    db:"user"`
 | 
						User                 User      `json:"user"                    db:"user"`
 | 
				
			||||||
	Article              models.Article `json:"article"                 db:"article"`
 | 
						Article              Article   `json:"article"                 db:"article"`
 | 
				
			||||||
	CreateTime           time.Time      `json:"createTime"              db:"createTime"`
 | 
						CreateTime           time.Time `json:"create_time"             db:"create_time"`
 | 
				
			||||||
	UpdateTime           time.Time      `json:"updateTime"              db:"updateTime"`
 | 
						UpdateTime           time.Time `json:"update_time"             db:"update_time"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type User struct {
 | 
					type User struct {
 | 
				
			||||||
@@ -29,3 +27,11 @@ type User struct {
 | 
				
			|||||||
	CreateTime time.Time `json:"create_time" db:"create_time"`
 | 
						CreateTime time.Time `json:"create_time" db:"create_time"`
 | 
				
			||||||
	UpdateTime time.Time `json:"update_time" db:"update_time"`
 | 
						UpdateTime time.Time `json:"update_time" db:"update_time"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Article struct {
 | 
				
			||||||
 | 
						Id         int       `json:"id"          db:"id"`
 | 
				
			||||||
 | 
						Title      string    `json:"title"       db:"title"`
 | 
				
			||||||
 | 
						Tags       string    `json:"tags"        db:"tags"`
 | 
				
			||||||
 | 
						CreateTime time.Time `json:"create_time" db:"create_time"`
 | 
				
			||||||
 | 
						UpdateTime time.Time `json:"update_time" db:"update_time"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user