附加文章信息
This commit is contained in:
		
							
								
								
									
										41
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -190,7 +190,7 @@ func main() {
 | 
			
		||||
		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 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 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)
 | 
			
		||||
@@ -199,7 +199,7 @@ func main() {
 | 
			
		||||
		defer rows.Close()
 | 
			
		||||
		for rows.Next() {
 | 
			
		||||
			var image Image
 | 
			
		||||
			rows.Scan(&image.Id, &image.Width, &image.Height, &image.Content, &image.UpdateTime, &image.CreateTime, &image.User.Id)
 | 
			
		||||
			rows.Scan(&image.Id, &image.Width, &image.Height, &image.Content, &image.UpdateTime, &image.CreateTime, &image.User.Id, &image.Article.Id)
 | 
			
		||||
			image.UpdateTime = image.UpdateTime.UTC()
 | 
			
		||||
			image.CreateTime = image.CreateTime.UTC()
 | 
			
		||||
			image.Content = regexp.MustCompile(`http:`).ReplaceAllString(image.Content, "https:")
 | 
			
		||||
@@ -230,7 +230,6 @@ func main() {
 | 
			
		||||
		if len(user_ids) > 0 {
 | 
			
		||||
			// 使用逗号分隔的用户ID列表查询用户信息 strings.Join(strings.Fields(fmt.Sprint(user_ids)), ",")
 | 
			
		||||
			user_ids_str := strings.Trim(strings.Replace(fmt.Sprint(user_ids), " ", ",", -1), "[]")
 | 
			
		||||
			fmt.Println(user_ids_str)
 | 
			
		||||
			rows, err := mysqlConnection.Database.Query("SELECT id, user_name, update_time, create_time FROM web_member WHERE id IN (" + user_ids_str + ")")
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Println("获取用户列表失败", err)
 | 
			
		||||
@@ -256,6 +255,42 @@ func main() {
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 附加图片集信息(第一步: 获取图片集ID列表)
 | 
			
		||||
		var article_ids []int
 | 
			
		||||
		for _, image := range image_list {
 | 
			
		||||
			article_ids = append(article_ids, image.Article.Id)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 附加图片集信息(第二步: 获取图片集信息)
 | 
			
		||||
		var articles []Article
 | 
			
		||||
		if len(article_ids) > 0 {
 | 
			
		||||
			// 使用逗号分隔的图片集ID列表查询图片集信息 strings.Join(strings.Fields(fmt.Sprint(article_ids)), ",")
 | 
			
		||||
			article_ids_str := strings.Trim(strings.Replace(fmt.Sprint(article_ids), " ", ",", -1), "[]")
 | 
			
		||||
			rows, err := mysqlConnection.Database.Query("SELECT id, title, tags, update_time, create_time FROM web_article WHERE id IN (" + article_ids_str + ")")
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Println("获取图片集列表失败", err)
 | 
			
		||||
				http.Error(w, err.Error(), http.StatusBadRequest)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			defer rows.Close()
 | 
			
		||||
			for rows.Next() {
 | 
			
		||||
				var article Article
 | 
			
		||||
				rows.Scan(&article.Id, &article.Title, &article.Tags, &article.UpdateTime, &article.CreateTime)
 | 
			
		||||
				article.UpdateTime = article.UpdateTime.UTC()
 | 
			
		||||
				article.CreateTime = article.CreateTime.UTC()
 | 
			
		||||
				articles = append(articles, article)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 附加图片集信息(第三步: 将图片集信息附加到图片信息中)
 | 
			
		||||
		for i, image := range image_list {
 | 
			
		||||
			for _, article := range articles {
 | 
			
		||||
				if image.Article.Id == article.Id {
 | 
			
		||||
					image_list[i].Article = article
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 将 []Image 转换为 []interface{}
 | 
			
		||||
		images.List = make([]interface{}, len(image_list))
 | 
			
		||||
		for i, v := range image_list {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user