Esempio n. 1
0
        public IActionResult TestLogin()
        {
            try
            {
                using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();

                UserInfo userInfo = new UserInfo()
                {
                    id       = 1,
                    Email    = "Test",
                    AuthRole = new List <AuthRole>()
                    {
                        AuthRole.User
                    }
                };
                string token = Guid.NewGuid().ToString();
                AuthRedis.SetToken(userInfo, token, LoginType.LimitWeb);
                return(Ok(new ApiResponse(new { token })));
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw ex;
            }
        }
        public IActionResult PictureUpload(PictureDto body)
        {
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            if (!string.IsNullOrEmpty(body.PictureTitle) && !string.IsNullOrEmpty(body.PictureExplain))
            {
                string token = _httpContext.HttpContext.Request.Headers["Authorization"];

                AuthRedis.GetUserByToken(token, out UserInfo userInfo);
                PictureInfo PictureInfos = new PictureInfo()
                {
                    Id               = SequenceID.GetSequenceID(),
                    CreateTime       = DateTime.Now,
                    Disable          = false,
                    PictureContent   = body.url,
                    UserID           = userInfo.id,
                    LastModifiedTime = DateTime.Now,
                    PictureExplain   = body.PictureExplain,
                    PictureTitle     = body.PictureTitle,
                    RecommendIndex   = body.Index,
                    PictureType      = body.PictureType,
                    PhotoType        = body.PhotoType
                };
                context.Add(PictureInfos);
                context.SaveChanges();
                PictureRedis.Del();
            }
            return(Ok(new ApiResponse()));
        }
        [AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口
        public IActionResult WordDel(long id)
        {
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            var wordinfo = context.WordInfo.Single(x => x.Id == id);

            wordinfo.Disable = true;
            context.SaveChanges();
            return(Ok(new ApiResponse()));
        }
        //[AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口
        public IActionResult PhotoList(PhotoType photoType)
        {
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            int total = 0;
            List <PictureListDto> PictureListDtos = new List <PictureListDto>();

            if (!PictureRedis.GetAll(out List <PictureInfo> Picture))
            {
                Picture = context.PictureInfo.Where(x => x.Disable == false).Include(x => x.Users).ToList();
                total   = Picture.Count();
                if (Picture != null && Picture.Count > 0)
                {
                    PictureRedis.SaveAll(Picture);
                }
                Picture = Picture.Where(x => x.Disable == false && x.PictureType == PictureType.Product && x.PhotoType == photoType).OrderByDescending(x => x.RecommendIndex).ToList();

                foreach (var item in Picture)
                {
                    PictureListDto PictureListDto = new PictureListDto()
                    {
                        ID             = item.Id,
                        CreateTime     = item.CreateTime,
                        Index          = item.RecommendIndex,
                        UserName       = item.Users.UserName,
                        PictureExplain = item.PictureExplain,
                        PictureTitle   = item.PictureTitle,
                        PictureUrl     = item.PictureContent,
                        PictureType    = item.PictureType
                    };
                    PictureListDtos.Add(PictureListDto);
                }
            }
            else
            {
                total   = Picture.Count();
                Picture = Picture.Where(x => x.Disable == false && x.PictureType == PictureType.Product && x.PhotoType == photoType).OrderByDescending(x => x.RecommendIndex).ToList();

                foreach (var item in Picture)
                {
                    PictureListDto PictureListDto = new PictureListDto()
                    {
                        ID             = item.Id,
                        CreateTime     = item.CreateTime,
                        Index          = item.RecommendIndex,
                        UserName       = item.Users.UserName,
                        PictureExplain = item.PictureExplain,
                        PictureTitle   = item.PictureTitle,
                        PictureUrl     = item.PictureContent,
                        PictureType    = item.PictureType
                    };
                    PictureListDtos.Add(PictureListDto);
                }
            }
            return(Ok(new ApiResponse(PictureListDtos, total)));
        }
        [AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口
        public IActionResult WordDetails(long ID)
        {
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            var         wordList  = context.WordInfo.Single(x => x.Disable == false && x.Id == ID);
            WordLessDto WordInfos = new WordLessDto()
            {
                CreateTime  = wordList.CreateTime,
                HtmlContent = wordList.HtmlContent,
                HtmlExplain = wordList.HtmlExplain,
                HtmlTitle   = wordList.HtmlTitle,
            };

            return(Ok(new ApiResponse(WordInfos)));
        }
        //[AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口
        public IActionResult PictureInfo(long ID)
        {
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            var WordLists = context.WordInfo.Single(x => x.Disable == false && x.PictureID == ID);

            WordInfoDto WordList = new WordInfoDto()
            {
                CreateTime   = WordLists.CreateTime,
                HtmlContent  = WordLists.HtmlContent,
                HtmlExplain  = WordLists.HtmlExplain,
                HtmlTitle    = WordLists.HtmlTitle,
                AttachedPath = WordLists.AttachedPath
            };

            return(Ok(new ApiResponse(WordList)));
        }
        [AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口
        public IActionResult PictureDel(long id)
        {
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            var PictureInfo = context.PictureInfo.Single(x => x.Id == id);
            var WordInfo    = context.WordInfo.Where(x => x.PictureID == id);

            PictureInfo.Disable = true;
            foreach (var item in WordInfo)
            {
                item.Disable = true;
            }
            context.SaveChanges();
            WordRedis.Del();
            PictureRedis.Del();

            return(Ok(new ApiResponse()));
        }
        public bool moreAdd(int i)
        {
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            Test test = new Test();

            for (int j = 0; j <= i; j++)
            {
                test = new Test()
                {
                    Id   = SequenceID.GetSequenceID(),
                    Name = "hangfire"
                };
                context.Add(test);
                context.SaveChanges();
            }
            TestRedis.Del();
            return(true);
        }
        [AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口
        public IActionResult PictureDetails(long ID)
        {
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            var PictureList = context.PictureInfo.Single(x => x.Disable == false && x.Id == ID);

            PictureLessDto PictureDto = new PictureLessDto()
            {
                CreateTime     = PictureList.CreateTime,
                PictureUrl     = PictureList.PictureContent,
                PictureExplain = PictureList.PictureExplain,
                PictureTitle   = PictureList.PictureTitle,
                Index          = PictureList.RecommendIndex,
                UserName       = PictureList.Users.UserName,
                PictureType    = PictureList.PictureType
            };

            return(Ok(new ApiResponse(PictureDto)));
        }
        [AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口
        public IActionResult WordsUpLoad(WordDto body)
        {
            string token = _httpContext.HttpContext.Request.Headers["Authorization"];

            AuthRedis.GetUserByToken(token, out UserInfo userInfo);
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            if (body.Eid == null)
            {
                int count = context.WordInfo.Where(x => x.PictureID == body.id).Count();
                if (count > 0)
                {
                    return(Ok(new ApiResponse(code: CodeAndMessage.已存在对应的资讯文档)));
                }
                WordInfo WordInfos = new WordInfo()
                {
                    Id               = SequenceID.GetSequenceID(),
                    CreateTime       = DateTime.Now,
                    Disable          = false,
                    HtmlContent      = body.HtmlContent,
                    PictureID        = body.id,
                    LastModifiedTime = DateTime.Now,
                    HtmlExplain      = body.HtmlExplain,
                    HtmlTitle        = body.HtmlTitle,
                    AttachedPath     = body.AttachedPath
                };
                context.Add(WordInfos);
                context.SaveChanges();
                WordRedis.Del();
            }
            else
            {
                var WordInfo = context.WordInfo.Single(x => x.Id == body.Eid);
                WordInfo.LastModifiedTime = DateTime.Now;
                WordInfo.HtmlContent      = body.HtmlContent;
                WordInfo.HtmlExplain      = body.HtmlExplain;
                WordInfo.HtmlTitle        = body.HtmlTitle;
                WordInfo.AttachedPath     = body.AttachedPath;
                context.SaveChanges();
                WordRedis.Del();
            }
            return(Ok(new ApiResponse()));
        }
        [AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口
        public IActionResult PictureInfo(PictureSelectDto body)
        {
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            int total = 0;
            List <PictureListDto> PictureListDtos = new List <PictureListDto>();

            if (!PictureRedis.GetAll(out List <PictureInfo> Picture))
            {
                Picture = context.PictureInfo.Where(x => x.Disable == false).Include(x => x.Users).ToList();
                total   = Picture.Count();
                if (Picture != null && Picture.Count > 0)
                {
                    PictureRedis.SaveAll(Picture);
                }

                if (body.StartTime != null)
                {
                    Picture = Picture.Where(x => x.CreateTime >= body.StartTime).ToList();
                }
                if (body.EndTime != null)
                {
                    Picture = Picture.Where(x => x.CreateTime <= body.EndTime).ToList();
                }
                if (body.PictureType != null)
                {
                    Picture = Picture.Where(x => x.PictureType == body.PictureType).ToList();
                }

                Picture = Picture.OrderByDescending(x => x.RecommendIndex).Skip(body.pageSize * (body.pageNum - 1)).Take(body.pageSize).ToList();

                foreach (var item in Picture)
                {
                    PictureListDto PictureListDto = new PictureListDto()
                    {
                        ID             = item.Id,
                        CreateTime     = item.CreateTime,
                        Index          = item.RecommendIndex,
                        UserName       = item.Users.UserName,
                        PictureExplain = item.PictureExplain,
                        PictureTitle   = item.PictureTitle,
                        PictureUrl     = item.PictureContent,
                        PictureType    = item.PictureType
                    };
                    PictureListDtos.Add(PictureListDto);
                }
            }
            else
            {
                total = Picture.Count();
                if (body.StartTime != null)
                {
                    Picture = Picture.Where(x => x.CreateTime >= body.StartTime).ToList();
                }
                if (body.EndTime != null)
                {
                    Picture = Picture.Where(x => x.CreateTime <= body.EndTime).ToList();
                }
                if (body.PictureType != null)
                {
                    Picture = Picture.Where(x => x.PictureType == body.PictureType).ToList();
                }

                Picture = Picture.OrderByDescending(x => x.RecommendIndex).Skip(body.pageSize * (body.pageNum - 1)).Take(body.pageSize).ToList();

                foreach (var item in Picture)
                {
                    PictureListDto PictureListDto = new PictureListDto()
                    {
                        ID             = item.Id,
                        CreateTime     = item.CreateTime,
                        Index          = item.RecommendIndex,
                        UserName       = item.Users.UserName,
                        PictureExplain = item.PictureExplain,
                        PictureTitle   = item.PictureTitle,
                        PictureUrl     = item.PictureContent,
                        PictureType    = item.PictureType
                    };
                    PictureListDtos.Add(PictureListDto);
                }
            }
            return(Ok(new ApiResponse(PictureListDtos, total)));
        }
        //[AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口
        public IActionResult PictureList()
        {
            using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
            int total = 0;
            List <PictureListDto> PictureListDtos = new List <PictureListDto>();

            if (!PictureRedis.GetAll(out List <PictureInfo> Picture))
            {
                Picture = context.PictureInfo.Where(x => x.Disable == false).OrderByDescending(x => x.RecommendIndex).Include(x => x.Users).ToList();
                total   = Picture.Count();
                if (Picture != null && Picture.Count > 0)
                {
                    PictureRedis.SaveAll(Picture);
                }
                string token = _httpContext.HttpContext.Request.Headers["Authorization"];
                if (string.IsNullOrEmpty(token))
                {
                    Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList();
                }
                else
                {
                    if (!AuthRedis.GetUserByToken(token, out UserInfo userInfo))
                    {
                        Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList();
                    }
                    else
                    {
                        //注册账号时间不能超过俩个小时
                        if (DateTime.Now.Hour - userInfo.CreateTime.Hour > 2 && userInfo.LoginType.First() != LoginType.FreeWeb)
                        {
                            Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList();
                        }
                    }
                    Picture = Picture.Where(x => x.Disable == false && x.PictureType == PictureType.News).ToList();
                }
                foreach (var item in Picture)
                {
                    PictureListDto PictureListDto = new PictureListDto()
                    {
                        ID             = item.Id,
                        CreateTime     = item.CreateTime,
                        Index          = item.RecommendIndex,
                        UserName       = item.Users.UserName,
                        PictureExplain = item.PictureExplain,
                        PictureTitle   = item.PictureTitle,
                        PictureUrl     = item.PictureContent,
                        PictureType    = item.PictureType
                    };
                    PictureListDtos.Add(PictureListDto);
                }
            }
            else
            {
                total = Picture.Count();

                string token = _httpContext.HttpContext.Request.Headers["Authorization"];
                if (string.IsNullOrEmpty(token))
                {
                    Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList();
                }
                else
                {
                    if (!AuthRedis.GetUserByToken(token, out UserInfo userInfo))
                    {
                        Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList();
                    }
                    else
                    {
                        //注册账号时间不能超过俩个小时
                        if (DateTime.Now.Hour - userInfo.CreateTime.Hour > 2 && userInfo.LoginType.First() != LoginType.FreeWeb)
                        {
                            Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList();
                        }
                    }
                    Picture = Picture.Where(x => x.Disable == false && x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).ToList();
                }
                foreach (var item in Picture)
                {
                    PictureListDto PictureListDto = new PictureListDto()
                    {
                        ID             = item.Id,
                        CreateTime     = item.CreateTime,
                        Index          = item.RecommendIndex,
                        UserName       = item.Users.UserName,
                        PictureExplain = item.PictureExplain,
                        PictureTitle   = item.PictureTitle,
                        PictureUrl     = item.PictureContent,
                        PictureType    = item.PictureType
                    };
                    PictureListDtos.Add(PictureListDto);
                }
            }
            return(Ok(new ApiResponse(PictureListDtos, total)));
        }
        [AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口
        public IActionResult WordInfo(WordSelectDto body)
        {
            int total = 0;
            List <WordListDto> wordListDtos = new List <WordListDto>();

            if (!WordRedis.GetAll(out List <WordInfo> Word))
            {
                using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();
                Word  = context.WordInfo.Where(x => x.Disable == false).Include(x => x.PictureInfos).Include(x => x.PictureInfos.Users).ToList();
                total = Word.Count();
                if (Word != null && Word.Count > 0)
                {
                    WordRedis.SaveAll(Word);
                }
                if (body.StartTime != null)
                {
                    Word = Word.Where(x => x.CreateTime >= body.StartTime).ToList();
                }
                if (body.EndTime != null)
                {
                    Word = Word.Where(x => x.CreateTime <= body.EndTime).ToList();
                }

                Word = Word.OrderByDescending(x => x.CreateTime).Skip(body.pageSize * (body.pageNum - 1)).Take(body.pageSize).ToList();

                foreach (var item in Word)
                {
                    WordListDto WordInfos = new WordListDto()
                    {
                        ID           = item.Id,
                        CreateTime   = item.CreateTime,
                        HtmlContent  = item.HtmlContent,
                        HtmlExplain  = item.HtmlExplain,
                        UserName     = item.PictureInfos.Users.UserName,
                        HtmlTitle    = item.HtmlTitle,
                        PictureTitle = item.PictureInfos.PictureTitle
                    };
                    wordListDtos.Add(WordInfos);
                }
                return(Ok(new ApiResponse(wordListDtos, total)));
            }
            else
            {
                total = Word.Count();
                Word  = Word.OrderByDescending(x => x.CreateTime).Skip(body.pageSize * (body.pageNum - 1)).Take(body.pageSize).ToList();

                foreach (var item in Word)
                {
                    WordListDto WordInfos = new WordListDto()
                    {
                        ID           = item.Id,
                        CreateTime   = item.CreateTime,
                        HtmlContent  = item.HtmlContent,
                        HtmlExplain  = item.HtmlExplain,
                        UserName     = item.PictureInfos.Users.UserName,
                        HtmlTitle    = item.HtmlTitle,
                        PictureTitle = item.PictureInfos.PictureTitle
                    };
                    wordListDtos.Add(WordInfos);
                }
                return(Ok(new ApiResponse(wordListDtos, total)));
            }
        }