/// <summary> /// 获取下一张壁纸 /// </summary> /// <returns></returns> public ImgInfo GetNextImage() { string type = string.Empty; do { if (orderNum >= this.TypeIndexs.Count) { orderNum = 0; } // 获取类型 type = this.TypeIndexs.ElementAt(orderNum++).Key; //int type = this.TypeIndexs.ElementAt(random.Next(0, this.TypeIndexs.Count - 1)).Key; } while (this.TypeIndexs.Count > 1 && this.typeImageList[type].Total == 0); this.TypeIndexs[type]++; int typeIndex = this.TypeIndexs[type]; // 判断是否超过最大记录数据 if (typeIndex >= this.typeImageList[type].Total) { this.TypeIndexs[type] = 0; typeIndex = 0; } // 判断是否在获取的记录之前 if (typeIndex < this.typeImageList[type].StartIndex // 如果Image列表为空,则尝试重新获取 || typeImageList[type].Images == null || typeImageList[type].Images.Count == 0) { ImageListTotal imgList = GetImageListTotal(type, typeIndex, this.Count); if (type != "down") { CacheIage(imgList.data); } typeImageList[type].StartIndex = typeIndex; typeImageList[type].Images = imgList.data; } if (this.typeImageList[type].Total == 0) { throw new Exception("当前分类下没有壁纸,请选择其他类型。"); } // 判断是否在获取的记录之后 if (typeIndex > this.typeImageList[type].EndIndex) { ImageListTotal imgList = GetImageListTotal(type, typeIndex, this.Count); if (type != "down") { CacheIage(imgList.data); } typeImageList[type].Images.AddRange(imgList.data); } int curIndex = typeIndex - this.typeImageList[type].StartIndex; if (curIndex >= typeImageList[type].Images.Count) { throw new Exception($"下一张图片信息获取异常:获取到的Images集合为{typeImageList[type].Images.Count},下一张图片索引为{curIndex}"); } return(typeImageList[type].Images[curIndex]); }
/// <summary> /// 根据关键字搜索 /// </summary> /// <param name="keyword"></param> /// <param name="start"></param> /// <param name="count"></param> /// <returns></returns> public static ImageListTotal GetImageListByKW(string keyword, int start = 0, int count = 30) { string jsonStr = string.Empty; // 获取最新的 if (string.IsNullOrEmpty(keyword)) { keyword = "可爱"; } jsonStr = WebHelper.HttpGet(string.Format(Const.Url_ListBySearch, keyword, start, count)); ImageListTotal listTotal = JsonHelper.DeserializeJsonToObject <ImageListTotal>(jsonStr); return(listTotal); }
/// <summary> /// 根据壁纸类型加载壁纸 /// </summary> /// <param name="picType">壁纸类型(love:最爱|down:下载的|0:最新|>0:其他分类)</param> /// <param name="index"></param> /// <returns></returns> public void LoadImage(string picType = "0", int index = 0) { if (thread == null) { this.picType = picType; this.picIndex = index; curImageListTotal = null; scr.ScrollToTop(); panel.Children.Clear(); this.OnComplate += ImageList_OnComplate; thread = new Thread(new ThreadStart(NextImages)); thread.Name = "LoadImageList_" + picType; thread.Start(); } }
/// <summary> /// 壁纸列表数据加载完成事件 /// </summary> /// <param name="total">壁纸列表</param> /// <param name="msg">返回的消息</param> private void ImageList_OnComplate(ImageListTotal total, string msg) { if (total != null && total.data.Count > 0) { var list = total.data; foreach (var picInfo in list) { Image img = new Image(); img.Tag = PageType.MPA; img.Height = imgSize.Height; img.Width = imgSize.Width; img.Margin = margin; img.MouseDown += Image_MouseDown; Grid grid = new Grid(); grid.Children.Add(new LoadingCircle()); grid.Children.Add(img); downQueue.Queue(img, picInfo, picInfo.GetUrlBySize((int)img.Width, (int)img.Height)); grid.MouseEnter += Grid_MouseEnter; grid.MouseLeave += Grid_MouseLeave; panel.Children.Add(grid); grid.Tag = this.picIndex; picInfo.Index = this.picIndex++; } if (curImageListTotal == null) { curImageListTotal = total; } else { curImageListTotal.data.AddRange(list); } } else if (!string.IsNullOrEmpty(msg)) { Growl.Info(msg); } zoomImage.Source = null; // 如果当前是单页显示模式,则需要加载新的壁纸类型的第一张壁纸 if (this.Content is Grid pGrid && pGrid.Name == "zoomGrid") { if (curImageListTotal != null && curImageListTotal.data.Count > 0) { this.ShowBigPic(curImageListTotal.data[0]); } } }
/// <summary> /// 获取图片列表 /// </summary> /// <param name="type">默认0(最新数据)</param> /// <param name="start">默认0</param> /// <param name="count">默认30</param> /// <returns></returns> public static ImageListTotal GetImageList(string type = "0", int start = 0, int count = 30) { string jsonStr = string.Empty; // 获取最新的 if (type == "0") { jsonStr = WebHelper.HttpGet(string.Format(Const.Url_ListTopNew, start, count)); } else//根据分类id获取 { jsonStr = WebHelper.HttpGet(string.Format(Const.Url_ListByType, type, start, count)); } ImageListTotal listTotal = JsonHelper.DeserializeJsonToObject <ImageListTotal>(jsonStr); return(listTotal); }
/// <summary> /// 分页获取下载的壁纸 /// </summary> /// <param name="loveType"></param> /// <param name="start"></param> /// <param name="count"></param> /// <returns></returns> public static ImageListTotal GetDownList(int start, int count, bool orderDesc = false) { ImageListTotal total = new ImageListTotal(); var query = db.Queryable <Picture, Download>((p, d) => new object[] { JoinType.Inner, p.Id == d.PictureId }) .OrderByIF(!orderDesc, (p, d) => d.Time, OrderByType.Asc) .OrderByIF(orderDesc, (p, d) => d.Time, OrderByType.Desc) .Select((p, d) => new ImgInfo() { class_id = p.Type, id = p.Id.ToString(), url = p.Url, tag = p.Tag }); total.total = query.Count(); total.data = query.Skip(start).Take(count).ToList(); return(total); }
/// <summary> /// 下一组图片 /// </summary> public void NextImages() { if (curImageListTotal == null || this.picIndex < curImageListTotal.total) { ImageListTotal total = null; if (string.IsNullOrEmpty(keyWord)) { total = ImageHelper.GetImageListTotal(this.picType, this.picIndex, 24); } else { total = WebImage.GetImageListByKW(keyWord, this.picIndex, 24); } string msg = string.Empty; if (total.total == 0) { switch (this.picType) { case "wall": msg = "您还没有设置过壁纸"; break; // 设置壁纸 case "love": msg = "您还没有收藏过壁纸"; break; // 收藏的 case "down": msg = "您还没有下载过壁纸"; break; //下载的 default: if (!string.IsNullOrEmpty(keyWord)) //搜索 { msg = $"未搜索到与【{keyWord}】相关的壁纸"; } break; } } this.Dispatcher.BeginInvoke(this.OnComplate, new Object[] { total, msg }); } else if (curImageListTotal != null && this.picIndex >= curImageListTotal.total) { Growl.Info($"已经滑到底了,总共{curImageListTotal.total}张壁纸"); } }
/// <summary> /// 根据关键字搜索壁纸,并加载壁纸列表 /// </summary> /// <param name="keyWord"></param> /// <returns></returns> public int SearchImage(string keyWord) { this.keyWord = keyWord; this.picIndex = 0; curImageListTotal = null; scr.ScrollToTop(); panel.Children.Clear(); if (this.OnComplate == null) { this.OnComplate += ImageList_OnComplate; } NextImages(); zoomImage.Source = null; // 如果当前是单页显示模式,则需要加载新的壁纸类型的第一张壁纸 if (this.Content is Grid pGrid && pGrid.Name == "zoomGrid") { if (curImageListTotal != null && curImageListTotal.data.Count > 0) { this.ShowBigPic(curImageListTotal.data[0]); } } return(curImageListTotal == null ? 0 : curImageListTotal.total); }
/// <summary> /// 分页获取历史壁纸的壁纸 /// </summary> /// <param name="wallType">记录类型</param> /// <param name="start"></param> /// <param name="count"></param> /// <returns></returns> public static ImageListTotal GetLWallList(RecordType wallType, int start, int count, bool destinct = false, bool orderDesc = false) { ImageListTotal total = new ImageListTotal(); var query = db.Queryable <Picture, Record>((p, r) => new object[] { JoinType.Inner, p.Id == r.PictureId }) .Where((p, r) => r.Type == (int)wallType) .OrderByIF(!orderDesc, (p, r) => r.Time, OrderByType.Asc) .OrderByIF(orderDesc, (p, r) => r.Time, OrderByType.Desc) .Select((p, r) => new ImgInfo() { class_id = p.Type, id = p.Id.ToString(), url = p.Url, tag = p.Tag }); if (destinct) { query = query.Distinct(); } total.total = query.Count(); total.data = query.Skip(start).Take(count).ToList(); return(total); }
/// <summary> /// 获取图片数据 /// </summary> /// <param name="types">壁纸类型数组</param> /// <param name="start">壁纸开始index</param> /// <param name="count">获取的壁纸数量</param> /// <returns></returns> public static ImageListTotal GetImageList(string[] types, int start = 0, int count = 30) { if (types != null && types.Length > 0) { ImageListTotal total = new ImageListTotal(); total.data = new List <ImgInfo>(); int price = count / types.Length; for (int i = 0; i < types.Length; i++) { if (i + 1 == types.Length) { price += count - (price * types.Length); } ImageListTotal temp = GetImageList(types[i], (start / types.Length), price); //total.consume += temp.consume; total.total += temp.total; total.data.AddRange(temp.data); } total.data = total.data.OrderBy(o => o.id).ToList(); return(total); } return(null); }
/// <summary> /// 根据类型获取分页的图片数据(包括:壁纸分类、收藏、下载) /// </summary> /// <param name="type"></param> /// <param name="start"></param> /// <param name="count"></param> /// <returns></returns> public static ImageListTotal GetImageListTotal(string type, int start = 0, int count = 24) { ImageListTotal total = null; switch (type) { case "wall": // 壁纸历史记录 total = UserDataManage.GetLWallList(RecordType.ManualWallpaper, start, count, true, true); break; case "love": // 收藏的 total = UserDataManage.GetLoveList(LoveType.Love, start, count, true); break; case "down": //下载的 total = UserDataManage.GetDownList(start, count, true); break; case "recommend": //根据收藏推荐的 if (recommendTotal == 0) { int topTag = 2; // 获取标签记录最高的前几个(数值越大,获取的Tag越多) int rateBase = 3; // 倍率基数(数值越大,每个Tag获取的数据越多) recommendList.Clear(); List <TagRecord> tops = UserDataManage.GetTopTagList(topTag); if (tops.Count > 0) { // 计算倍率,防止收藏数量太多造成获取的数量太多 decimal rate = (decimal)count * rateBase / tops[0].RecordCount; foreach (TagRecord top in tops) { int tempCount = (int)(top.RecordCount * rate); ImageListTotal tempTotal = WebImage.GetImageListByKW(top.TagName, 0, tempCount); recommendTotal += tempTotal.total; recommendList.AddRange(tempTotal.data); } // 去重和排序 List <string> ids = recommendList.GroupBy(h => h.id).Select(h => h.Key).Distinct().ToList(); recommendList = ids.GroupJoin( recommendList, h => h, h => h.id, (k, v) => v.FirstOrDefault()) .OrderByDescending(h => h.create_time).ToList(); } } if (start < recommendList.Count) { total = new ImageListTotal(); total.total = recommendList.Count; total.data = new List <ImgInfo>(); total.data = recommendList.Skip(start).Take(count).ToList(); } break; default: total = WebImage.GetImageList(type, start, count); /*// 暂时不考虑,没有想到比较好的解决方法 * if (ConfigManage.Base.ExcludeDislike) * { * * }*/ break; } return(total); }