Example #1
0
        public async Task <ActionResult> GetArticleList(string userId, string categoryId = null, int pageIndex = 1, int pageSize = 7)
        {
            //获取当前登陆的id,cookie的id需要解密
            string userCookieId = ""; string message;

            if (Request.Cookies["userId"] != null)
            {
                if (!JwtHelper.GetJwtDecode(Request.Cookies["userId"].Value, out userCookieId, out message))
                {
                    return(Json(new { result = message, status = "fail" }, JsonRequestBehavior.AllowGet));//返回错误信息
                }
            }
            string currentUserId = Session["userId"] == null ? userCookieId : Session["userId"].ToString();

            if (userId == null && currentUserId != null && currentUserId.Trim() != "")
            {
                return(RedirectToAction(nameof(GetArticleList), new { userId = currentUserId }));
            }
            //需要返回前端 总页数 当前页数 单个页面数量
            IArticleManager articleManager = new ArticleManager();
            IUserManager    userManager    = new UserManager();
            Guid            userIdGuid;

            Guid.TryParse(userId, out userIdGuid);
            if (!await userManager.ExistsUser(userIdGuid) || userIdGuid == Guid.Empty)
            {
                return(Json(new { result = "未能找到对应ID的用户,请稍后再试", status = "fail" }, JsonRequestBehavior.AllowGet));//返回错误信息
            }
            //新增按照用户id和分类id查找文章,默认为不找分类id
            Guid categoryIdGuid;

            Guid.TryParse(categoryId, out categoryIdGuid);
            List <ArticleDto> articlesInfo;
            int dataCount;

            if (categoryIdGuid == Guid.Empty)
            {
                articlesInfo = await articleManager.GetAllArticlesByUserId(userIdGuid, (pageIndex - 1), pageSize); //数据库是从0开始的

                dataCount = await articleManager.GetArticleDataCount(userIdGuid);                                  //文章总数
            }
            else
            {
                articlesInfo = await articleManager.GetAllArticlesByUserIdAndCategoryId(userIdGuid, categoryIdGuid, (pageIndex - 1), pageSize); //数据库是从0开始的

                dataCount = await articleManager.GetArticleDataCount(userIdGuid, categoryIdGuid);                                               //文章总数
            }
            var pageCount = dataCount % pageSize == 0 ? dataCount / pageSize : dataCount / pageSize + 1;                                        //总页数
            var category  = categoryIdGuid == Guid.Empty ? null : await articleManager.GetOneCategoryById(categoryIdGuid);

            var isCurrentUser = currentUserId.Trim() == "" ? false : userIdGuid == Guid.Parse(currentUserId) ? true : false;         //是否为当前登陆用户
            var requestId     = userIdGuid;                                                                                          //当前请求id
            var tenTags       = await articleManager.GetCategoriesByCount(userIdGuid, 10);                                           //返回10个分类

            var articlesCount = await articleManager.GetArticleDataCount(userIdGuid);                                                //查找文章总数

            var categoriesCount = await articleManager.GetCategoryDataCount(userIdGuid);                                             //查找分类总数

            var isFocused = currentUserId.Trim() == "" ? false : await userManager.IsFocused(Guid.Parse(currentUserId), userIdGuid); //id为空也视为没关注

            var userInfo = await userManager.GetUserById(userIdGuid);

            var pageMatchArticleCount = dataCount;

            return(Json(new { status = "ok", articlesInfo, pageMatchArticleCount, pageCount, pageIndex, pageSize, category, isCurrentUser, requestId, tenTags, articlesCount, categoriesCount, isFocused, userInfo }, JsonRequestBehavior.AllowGet));
        }
Example #2
0
        public async Task <ActionResult> ArticleList(string userId, string categoryId = null, int pageIndex = 1, int pageSize = 7)
        {
            //获取当前登陆的id,cookie的id需要解密
            string userCookieId = ""; string message;

            if (Request.Cookies["userId"] != null)
            {
                if (!JwtHelper.GetJwtDecode(Request.Cookies["userId"].Value, out userCookieId, out message))
                {
                    ErrorController.message = message;
                    return(RedirectToAction("IllegalOperationError", "Error"));//返回错误页面
                }
            }
            string currentUserId = Session["userId"] == null ? userCookieId : Session["userId"].ToString();

            if (userId == null && currentUserId != null && currentUserId.Trim() != "")
            {
                return(RedirectToAction(nameof(ArticleList), new { userId = currentUserId }));
            }
            //需要返回前端 总页数 当前页数 单个页面数量
            IArticleManager articleManager = new ArticleManager();
            IUserManager    userManager    = new UserManager();
            Guid            userIdGuid;

            Guid.TryParse(userId, out userIdGuid);
            if (!await userManager.ExistsUser(userIdGuid) || userIdGuid == Guid.Empty)
            {
                ErrorController.message = "未能找到对应ID的用户,请稍后再试";
                return(RedirectToAction("IllegalOperationError", "Error"));//返回错误页面
            }
            //新增按照用户id和分类id查找文章,默认为不找分类id
            Guid categoryIdGuid;

            Guid.TryParse(categoryId, out categoryIdGuid);
            List <ArticleDto> articles;
            int dataCount;

            if (categoryIdGuid == Guid.Empty)
            {
                articles = await articleManager.GetAllArticlesByUserId(userIdGuid, (pageIndex - 1), pageSize); //数据库是从0开始的

                dataCount = await articleManager.GetArticleDataCount(userIdGuid);                              //文章总数
            }
            else
            {
                articles = await articleManager.GetAllArticlesByUserIdAndCategoryId(userIdGuid, categoryIdGuid, (pageIndex - 1), pageSize); //数据库是从0开始的

                dataCount = await articleManager.GetArticleDataCount(userIdGuid, categoryIdGuid);                                           //文章总数
            }
            ViewBag.PageCount = dataCount % pageSize == 0 ? dataCount / pageSize : dataCount / pageSize + 1;                                //总页数
            ViewBag.PageIndex = pageIndex;                                                                                                  //当前页数
            ViewBag.PageSize  = pageSize;                                                                                                   //当前显示数目
            ViewBag.Category  = categoryIdGuid == Guid.Empty ? null : await articleManager.GetOneCategoryById(categoryIdGuid);

            ViewBag.IsCurrentUser = currentUserId.Trim() == "" ? false : userIdGuid == Guid.Parse(currentUserId) ? true : false;         //是否为当前登陆用户
            ViewBag.RequestId     = userIdGuid;                                                                                          //当前请求id
            ViewBag.TenTags       = await articleManager.GetCategoriesByCount(userIdGuid, 10);                                           //返回10个分类

            ViewBag.ArticlesCount = await articleManager.GetArticleDataCount(userIdGuid);                                                //查找文章总数

            ViewBag.CategoriesCount = await articleManager.GetCategoryDataCount(userIdGuid);                                             //查找分类总数

            ViewBag.IsFocused = currentUserId.Trim() == "" ? false : await userManager.IsFocused(Guid.Parse(currentUserId), userIdGuid); //id为空也视为没关注

            ViewBag.User = await userManager.GetUserById(userIdGuid);

            return(View(articles));
        }