Esempio n. 1
0
        public async Task <ActionResult> CategoryList(string userId, 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(CategoryList), new { userId = currentUserId }));
            }
            IUserManager    userManager    = new UserManager();
            IArticleManager articleManager = new ArticleManager();
            Guid            userIdGuid;

            Guid.TryParse(userId, out userIdGuid);
            if (!await userManager.ExistsUser(userIdGuid) || userIdGuid == Guid.Empty)
            {
                ErrorController.message = "未能找到对应ID的用户,请稍后再试";
                return(RedirectToAction("IllegalOperationError", "Error"));                                                                  //返回错误页面
            }
            var categorys = await articleManager.GetAllCategoriesByUserId(userIdGuid, (pageIndex - 1), pageSize);                            //数据库是从0开始的

            var dataCount = await articleManager.GetCategoryDataCount(userIdGuid);                                                           //分类总数

            ViewBag.PageCount     = dataCount % pageSize == 0 ? dataCount / pageSize : dataCount / pageSize + 1;                             //总页数
            ViewBag.PageIndex     = pageIndex;                                                                                               //当前页数
            ViewBag.IsCurrentUser = currentUserId.Trim() == "" ? false : userIdGuid == Guid.Parse(currentUserId) ? true : false;             //是否为当前登陆用户
            ViewBag.IsFocused     = currentUserId.Trim() == "" ? false : await userManager.IsFocused(Guid.Parse(currentUserId), userIdGuid); //id为空也视为没关注

            ViewBag.RequestId = userIdGuid;                                                                                                  //当前请求id
            ViewBag.User      = await userManager.GetUserById(userIdGuid);

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

            ViewBag.CategoriesCount = dataCount;                                          //查找分类总数
            return(View(categorys));
        }
Esempio n. 2
0
        public async Task <ActionResult> GetCategoryList(string userId, 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(GetCategoryList), new { userId = currentUserId }));
            }
            IUserManager    userManager    = new UserManager();
            IArticleManager articleManager = new ArticleManager();
            Guid            userIdGuid;

            Guid.TryParse(userId, out userIdGuid);
            if (!await userManager.ExistsUser(userIdGuid) || userIdGuid == Guid.Empty)
            {
                return(Json(new { result = "未能找到对应ID的用户,请稍后再试", status = "fail" }, JsonRequestBehavior.AllowGet));                       //返回错误信息
            }
            var categoriesInfo = await articleManager.GetAllCategoriesByUserId(userIdGuid, (pageIndex - 1), pageSize);                   //数据库是从0开始的

            var dataCount = await articleManager.GetCategoryDataCount(userIdGuid);                                                       //分类总数

            var pageCount     = dataCount % pageSize == 0 ? dataCount / pageSize : dataCount / pageSize + 1;                             //总页数
            var isCurrentUser = currentUserId.Trim() == "" ? false : userIdGuid == Guid.Parse(currentUserId) ? true : false;             //是否为当前登陆用户
            var isFocused     = currentUserId.Trim() == "" ? false : await userManager.IsFocused(Guid.Parse(currentUserId), userIdGuid); //id为空也视为没关注

            var requestId = userIdGuid;                                                                                                  //当前请求id
            var userInfo  = await userManager.GetUserById(userIdGuid);

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

            var categoriesCount = dataCount;                                          //查找分类总数

            return(Json(new { status = "ok", categoriesInfo, pageCount, pageIndex, pageSize, isCurrentUser, isFocused, requestId, userInfo, articlesCount, categoriesCount }, JsonRequestBehavior.AllowGet));
        }