Example #1
0
        public async Task <IActionResult> GetBlogListByPage(int categoryId, string keyWord, int pageSize, int pageIndex)
        {
            //await Task.Delay(5000);
            DataResultDto <List <BlogDto> > dataResultDto = await this._blogService.GetListByPage(0, categoryId, keyWord, "CreateDate desc", pageSize, pageIndex);

            return(Json(dataResultDto));
        }
Example #2
0
        /// <summary>
        /// 获取新闻
        /// </summary>
        /// <param name="count"></param>
        /// <param name="orderString"></param>
        /// <returns></returns>
        public async Task <List <NewsDto> > GetTopNewsByCount(int count, string orderString)
        {
            DataResultDto <List <NewsDto> > dataResultDto = new DataResultDto <List <NewsDto> >();
            List <NewsDto> list = new List <NewsDto>();

            string where = "where IsDelete=0 ";
            string order = string.Format("order by {0}", orderString);
            string sql   = string.Format(" select NewsId,Title,Body,KeyWord,Description,VisitCount,CommentCount,CreateDate,OriginalUrl,Sort,IsDelete from news {0}  {1}  limit 0,{2}", where, order, count);

            IEnumerable <News> nlist = null;

            using (var connection = CreateConnection())
            {
                nlist = await connection.QueryAsync <News>(sql);
            }
            NewsDto newsDto = null;

            foreach (var ib in nlist)
            {
                newsDto                     = Mapper.Map <News, NewsDto>(ib);
                newsDto.User                = new UserDto();
                newsDto.User.UserId         = 1;
                newsDto.User.UserName       = "******";
                newsDto.User.LoginName      = "管理员";
                newsDto.User.UserHeadImaUrl = "/upload/user-head/default.png";
                list.Add(newsDto);
            }
            return(list);
        }
Example #3
0
        public async Task <DataResultDto <List <OpenSourceDto> > > GetList(int HeatType, int count)
        {
            DataResultDto <List <OpenSourceDto> > dataResultDto = new DataResultDto <List <OpenSourceDto> >();
            List <OpenSourceDto> list = new List <OpenSourceDto>();

            string where = $"where IsDelete=0 and HeatType={HeatType}";
            string sql = string.Format("select OpenSourceID,OpenSourceTitle,OpenSourceType,OpenSourceDescribe,OpenSourceContent,HeatType,Sort,CreateDate from `opensource` {0} order by Sort,CreateDate desc limit 0,{1}", where, count);
            IEnumerable <OpenSource> blist = null;

            using (var connection = CreateConnection())
            {
                blist = await connection.QueryAsync <OpenSource>(sql);
            }
            OpenSourceDto openSourceDto = null;

            foreach (var io in blist)
            {
                openSourceDto = Mapper.Map <OpenSource, OpenSourceDto>(io);
                openSourceDto.OpenSourceContent = string.Empty;//优化,不传到显示页面
                list.Add(openSourceDto);
            }
            dataResultDto.Code     = 0;
            dataResultDto.DataList = list;
            return(dataResultDto);
        }
Example #4
0
        public async Task <DataResultDto <UserDto> > CheckUser(UserDto userDto)
        {
            DataResultDto <UserDto> dataResultDto = new DataResultDto <UserDto>();
            User   user = null;
            string sql  = "select UserId,LoginName,UserName,Password,Status,IsAdmin from `user` where LoginName=@LoginName and IsDelete=0 ";

            using (var connect = CreateConnection())
            {
                user = await connect.QueryFirstOrDefaultAsync <User>(sql, new { LoginName = userDto.LoginName });
            }
            if (user == null)
            {
                dataResultDto.Code = 1;
                dataResultDto.Msg  = "登陆失败,用户名不正确!";
            }
            else
            {
                if (user.Status == 0)
                {
                    string pwd  = $"{userDto.Password}{this._webStaticConfig.MD5Code}";
                    string spwd = SecurityTool.MD5Hash(pwd);
                    if (spwd.ToUpper() == user.Password.ToUpper())
                    {
                        UserDto quserDto = new UserDto();
                        quserDto               = Mapper.Map <User, UserDto>(user);
                        dataResultDto.Code     = 0;
                        dataResultDto.Msg      = "登陆成功!";
                        dataResultDto.DataList = quserDto;
                    }
                    else
                    {
                        dataResultDto.Code = 1;
                        dataResultDto.Msg  = "登陆失败,用户名或者密码不对!";
                    }
                }
                else if (user.Status == 1)
                {
                    dataResultDto.Code = 1;
                    dataResultDto.Msg  = "登陆失败,用户正在审核中!";
                }
                else if (user.Status == 2)
                {
                    dataResultDto.Code = 1;
                    dataResultDto.Msg  = "登陆失败,用户已冻结,请联系管理员!";
                }
                else if (user.Status == 3)
                {
                    dataResultDto.Code = 1;
                    dataResultDto.Msg  = "登陆失败,用户已禁用,请联系管理员!";
                }
                else
                {
                    dataResultDto.Code = 1;
                    dataResultDto.Msg  = "登陆失败,用户名或者密码错误!";
                }
            }

            return(dataResultDto);
        }
Example #5
0
        public async Task <IActionResult> ShowBlog(int id)
        {
            //获取客户端IP
            string userIP = HttpContext.GetUserIp();

            this._logger.LogInformation($"查看用户IP: {userIP}");
            //获取具体的博客
            BlogDto blogDto = await this._blogService.GetOneBlogByBlogIdAsync(id, userIP, true);

            ViewBag.BlogDto = blogDto;
            //获取用户博客的信息(博客分类 博客信息)
            UserDto userDto = await this._userService.GetOneUserByUserIdAsync(blogDto.UserId);

            DetailContentLayoutViewModel.UserDto = userDto;
            ViewBag.DetailContentLayoutViewModel = DetailContentLayoutViewModel;
            TempData["Title"]       = $"{this._webStaticConfig.WebName}-{blogDto.Title}";
            TempData["KeyWord"]     = blogDto.KeyWord;
            TempData["Description"] = blogDto.Description;


            var userBlogCommentList = MemoryCacheTool.GetCacheValue("UserBlogCommentList");

            if (userBlogCommentList != null)
            {
                TempData["UserBlogCommentList"] = (List <BlogCommentDto>)userBlogCommentList;
            }
            else
            {
                DataResultDto <List <BlogCommentDto> > dataResultDto = await this._blogCommentService.GetBlogCommentByUserId(10);

                if (dataResultDto.Code == 0)
                {
                    TempData["UserBlogCommentList"] = dataResultDto.DataList;
                }

                MemoryCacheTool.SetChacheValue("UserBlogCommentList", dataResultDto.DataList, TimeSpan.FromMinutes(30));
            }



            var claim = GetClaim(HttpContext.User.Claims, ClaimTypes.Sid);

            if (claim != null)
            {
                UserDto userDtoLogin = new UserDto();
                userDtoLogin.UserId = int.Parse(claim.Value);
                claim = GetClaim(HttpContext.User.Claims, ClaimTypes.Name);
                userDtoLogin.UserName = claim.Value;
                TempData["LoginUser"] = userDtoLogin;
            }
            return(View());
        }
Example #6
0
        public async Task <IActionResult> LookNews(int id)
        {
            //获取客户端IP
            string userIP = HttpContext.GetUserIp();
            //获取具体的博客
            NewsDto newsDto = await this._newsService.GetOneNewsByNewsIdAsync(id, userIP, false);

            ViewBag.NewsDto = newsDto;
            //获取用户博客的信息(博客分类 博客信息)
            UserDto userDto = await this._userService.GetOneUserByUserIdAsync(this._webStaticConfig.BlogBackUserId);

            DetailContentLayoutViewModel.UserDto = userDto;
            ViewBag.DetailContentLayoutViewModel = DetailContentLayoutViewModel;
            TempData["Title"]       = $"{this._webStaticConfig.WebName}-{newsDto.Title}";
            TempData["KeyWord"]     = newsDto.KeyWord;
            TempData["Description"] = newsDto.Description;

            TempData["IsNews"] = true;
            var userNewsCommentList = MemoryCacheTool.GetCacheValue("UserNewsCommentList");

            if (userNewsCommentList != null)
            {
                TempData["UserNewsCommentList"] = (List <NewsCommentDto>)userNewsCommentList;
            }
            else
            {
                DataResultDto <List <NewsCommentDto> > dataResultDto = await this._newsCommentService.GetNewsCommentByUserId(10);

                if (dataResultDto.Code == 0)
                {
                    TempData["UserNewsCommentList"] = dataResultDto.DataList;
                }

                MemoryCacheTool.SetChacheValue("UserNewsCommentList", dataResultDto.DataList, TimeSpan.FromMinutes(30));
            }



            var claim = GetClaim(HttpContext.User.Claims, ClaimTypes.Sid);

            if (claim != null)
            {
                UserDto userDtoLogin = new UserDto();
                userDtoLogin.UserId = int.Parse(claim.Value);
                claim = GetClaim(HttpContext.User.Claims, ClaimTypes.Name);
                userDtoLogin.UserName = claim.Value;
                TempData["LoginUser"] = userDtoLogin;
            }

            return(View("~/Views/News/NewDetails.cshtml"));
        }
Example #7
0
        /// <summary>
        /// 分页获取开源项目
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="keyWord"></param>
        /// <param name="orderFiled"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public async Task <DataResultDto <List <OpenSourceDto> > > GetListByPage(DateTime?startTime, DateTime?endTime, string keyWord, string orderFiled, int pageSize, int pageIndex)
        {
            DataResultDto <List <OpenSourceDto> > dataResultDto = new DataResultDto <List <OpenSourceDto> >();
            List <OpenSourceDto> list = new List <OpenSourceDto>();

            string where = "where IsDelete=0 ";


            if (startTime != null && endTime != null)
            {
                where += "  and CreateDate BETWEEN @StartTime and @EndTime ";
            }

            if (!string.IsNullOrEmpty(keyWord))
            {
                where += "  and (OpenSourceTitle like @KeyWork or OpenSourceContent like @KeyWork or OpenSourceDescribe like @KeyWork) ";
            }

            string order = string.Empty;

            if (!string.IsNullOrEmpty(orderFiled))
            {
                order = string.Format("order by {0}", orderFiled);
            }

            string sql = string.Format("select OpenSourceID,OpenSourceTitle,OpenSourceType,OpenSourceDescribe,OpenSourceContent,CreateDate from `opensource` {0}  {1} limit {2},{3}", where, order, (pageIndex - 1) * pageSize, pageSize);

            string sqlCount = string.Format("select count(1) from `opensource`  {0}  ", where);

            IEnumerable <OpenSource> blist = null;

            using (var connection = CreateConnection())
            {
                dataResultDto.Count = await connection.QueryFirstAsync <int>(sqlCount, new { StartTime = startTime, EndTime = endTime, KeyWork = string.Format("%{0}%", keyWord) });

                blist = await connection.QueryAsync <OpenSource>(sql, new { StartTime = startTime, EndTime = endTime, KeyWork = string.Format("%{0}%", keyWord), OrderFiled = orderFiled });
            }

            OpenSourceDto openSourceDto = null;

            foreach (var io in blist)
            {
                openSourceDto = Mapper.Map <OpenSource, OpenSourceDto>(io);
                list.Add(openSourceDto);
            }
            dataResultDto.Code     = 0;
            dataResultDto.DataList = list;
            return(dataResultDto);
        }
Example #8
0
        /// <summary>
        /// 分页获取新闻
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="categoryId"></param>
        /// <param name="keyWork"></param>
        /// <param name="orderFiled"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public async Task <DataResultDto <List <NewsDto> > > GetListByPage(DateTime?startTime, DateTime?endTime, string keyWord, string orderFiled, int pageSize, int pageIndex)
        {
            DataResultDto <List <NewsDto> > dataResultDto = new DataResultDto <List <NewsDto> >();
            List <NewsDto> list = new List <NewsDto>();

            string where = "where IsDelete=0 ";
            if (startTime != null && endTime != null)
            {
                where  += "  and CreateDate BETWEEN @StartTime and @EndTime ";
                endTime = endTime.Value.AddDays(1).AddSeconds(-1);
            }

            if (!string.IsNullOrEmpty(keyWord))
            {
                where += "  and (Title like @KeyWork or Body like @KeyWork ) ";
            }

            string order = string.Empty;

            if (!string.IsNullOrEmpty(orderFiled))
            {
                order = string.Format("order by {0}", orderFiled);
            }

            string sql = string.Format("select NewsId,Title,Body,KeyWord,Description,CreateDate,VisitCount,CommentCount,OriginalUrl,Sort,IsDelete from `news`  {0}  {1} limit {2},{3}", where, order, (pageIndex - 1) * pageSize, pageSize);

            string sqlCount = string.Format("select count(1) from `news`   {0}  ", where);

            IEnumerable <News> blist = null;

            using (var connection = CreateConnection())
            {
                dataResultDto.Count = await connection.QueryFirstAsync <int>(sqlCount, new { StartTime = startTime, EndTime = endTime, KeyWork = string.Format("%{0}%", keyWord) });

                blist = await connection.QueryAsync <News>(sql, new { StartTime = startTime, EndTime = endTime, KeyWork = string.Format("%{0}%", keyWord), OrderFiled = orderFiled });
            }

            NewsDto newsDto = null;

            foreach (var ib in blist)
            {
                newsDto = Mapper.Map <News, NewsDto>(ib);
                list.Add(newsDto);
            }
            dataResultDto.Code     = 0;
            dataResultDto.DataList = list;

            return(dataResultDto);
        }
Example #9
0
        /// <summary>
        /// 获取博客所有评论  回复盖楼html
        /// </summary>
        /// <param name="blogId"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task <DataResultDto <string> > GetAll(long blogId, string userName)
        {
            DataResultDto <string> dataResultDto = new DataResultDto <string>();

            dataResultDto.Code     = 1;
            dataResultDto.DataList = string.Empty;
            var blogCommentList = await GetList(blogId);

            if (blogCommentList.Code != 0)
            {
                return(dataResultDto);
            }

            dataResultDto.Code = 0;
            var           bcAllList     = blogCommentList.DataList;                         //所有评论
            var           showList      = bcAllList.OrderByDescending(p => p.NId).ToList(); //找所有没有引用的评论
            StringBuilder reference     = new StringBuilder();
            StringBuilder resultComment = new StringBuilder();

            foreach (var inc in showList)
            {
                var referenceList = new List <NewsCommentDto>(); // 创建当前评论所引用的评论列表
                AddComment(bcAllList, referenceList, inc);       //查询 bci 该评论 给引用的列表、
                referenceList = referenceList.OrderBy(p => p.NId).ToList();
                foreach (var irnc in referenceList)
                {
                    if (reference.Length == 0)
                    {
                        reference.AppendLine(CreateComment_Detail(irnc, userName, ""));
                    }
                    else
                    {
                        string chreference = reference.ToString();
                        reference.Clear();
                        reference.AppendLine(CreateComment_Detail(irnc, userName, chreference));
                    }
                }
                resultComment.AppendLine("<div class='comment'>");
                resultComment.AppendLine(CreateComment_Detail(inc, userName, reference.ToString()));
                resultComment.AppendLine("</div>");
                resultComment.AppendLine("<br/>");
                reference.Clear();
            }

            dataResultDto.DataList = resultComment.ToString();
            return(dataResultDto);
        }
Example #10
0
        /// <summary>
        /// 分页获取新闻
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="categoryId"></param>
        /// <param name="keyWork"></param>
        /// <param name="orderFiled"></param>
        /// <param name="pageSize"></param>
        /// <param name="PageIndex"></param>
        /// <returns></returns>
        public async Task <DataResultDto <List <NewsDto> > > GetListByPage(string keyWork, string orderFiled, int pageSize, int pageIndex)
        {
            DataResultDto <List <NewsDto> > dataResultDto = new DataResultDto <List <NewsDto> >();
            List <NewsDto> list = new List <NewsDto>();

            string where = "where news.IsDelete=0 ";

            if (!string.IsNullOrEmpty(keyWork))
            {
                where += "  and (news.Title like @KeyWork or news.Body like @KeyWork ) ";
            }

            string order = string.Empty;

            if (!string.IsNullOrEmpty(orderFiled))
            {
                order = string.Format("order by news.{0}", orderFiled);
            }

            string sql = string.Format("select news.NewsId,news.Title,news.Body,news.CreateDate,news.VisitCount,news.CommentCount,news.OriginalUrl,news.IsDelete from `news`  " +
                                       " {0}  {1} limit {2},{3}", where, order, (pageIndex - 1) * pageSize, pageSize);

            string sqlCount = string.Format("select count(1) from `news`  ", where);

            IEnumerable <News> blist = null;

            using (var connection = CreateConnection())
            {
                dataResultDto.Count = await connection.QueryFirstAsync <int>(sqlCount, new { KeyWork = string.Format("%{0}%", keyWork) });

                blist = await connection.QueryAsync <News>(sql, new { KeyWork = string.Format("%{0}%", keyWork), OrderFiled = orderFiled });
            }

            NewsDto newsDto = null;

            foreach (var ib in blist)
            {
                newsDto      = Mapper.Map <News, NewsDto>(ib);
                newsDto.Body = string.Empty;//优化
                list.Add(newsDto);
            }
            dataResultDto.Code     = 0;
            dataResultDto.DataList = list;
            return(dataResultDto);
        }
Example #11
0
        /// <summary>
        /// 获取博客
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="count"></param>
        /// <param name="orderString"></param>
        /// <returns></returns>
        public async Task <List <BlogDto> > GetTopBlogByCount(int userId, int count, string orderString)
        {
            DataResultDto <List <BlogDto> > dataResultDto = new DataResultDto <List <BlogDto> >();
            List <BlogDto> list = new List <BlogDto>();

            string where = "where blog.IsDelete=0 ";
            if (userId != 0)
            {
                where += " and blog.UserId=@UserId ";
            }

            string order = string.Format("order by blog.{0}", orderString);

            string sql = string.Format("select  blog.BlogId,blog.CategoryID,blog.Title,blog.Body,blog.Remark,blog.CreateDate,blog.UpdateDate,blog.Sort,blog.UserId,blog.VisitCount,blog.CommentCount,blog.IsEssence,blog.IsDelete,category.CategoryID,category.CategoryName,category.CreateDate,user.UserId,user.LoginName,user.UserName,user.UserHeadImaUrl from `blog`  " +
                                       " left join `category`  on blog.CategoryID=category.CategoryID left join `user` on user.UserId=blog.UserId {0}  {1}  limit 0,{2}", where, order, count);

            IEnumerable <Blog> blist = null;

            using (var connection = CreateConnection())
            {
                blist = await connection.QueryAsync <Blog, Category, User, Blog>(sql, (qblog, qcategory, quser) =>
                {
                    qblog.Category = qcategory;
                    qblog.User     = quser;
                    return(qblog);
                }, new { UserId = userId, OrderFiled = order },
                                                                                 splitOn : "BlogId,CategoryId,UserId");
            }

            BlogDto blogDto = null;

            foreach (var ib in blist)
            {
                blogDto             = Mapper.Map <Blog, BlogDto>(ib);
                blogDto.CategoryDto = Mapper.Map <Category, CategoryDto>(ib.Category);
                blogDto.User        = Mapper.Map <User, UserDto>(ib.User);
                list.Add(blogDto);
            }
            return(list);
        }
Example #12
0
        /// <summary>
        /// 获取新闻所有评论
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public async Task <DataResultDto <List <NewsCommentDto> > > GetList(long newsId)
        {
            DataResultDto <List <NewsCommentDto> > dataResultDto = new DataResultDto <List <NewsCommentDto> >();
            List <NewsCommentDto> list = new List <NewsCommentDto>();
            string sql = "select NId,NewsId,ReferenceId,PostId,PostName,Content,PostDate,user.UserName,user.UserId from newscomment left join user on user.UserId=newscomment.PostId  where NewsId=@NewsId and newscomment.IsDelete=0";
            IEnumerable <NewsComment> bClist = null;

            using (var connection = CreateConnection())
            {
                bClist = await connection.QueryAsync <NewsComment, User, NewsComment>(sql, (qNewsComment, quser) =>
                {
                    qNewsComment.User = quser;
                    return(qNewsComment);
                }
                                                                                      , new { NewsId = newsId }
                                                                                      , splitOn : "UserName");
            }

            NewsCommentDto newsCommentDto = null;

            foreach (var ibc in bClist)
            {
                newsCommentDto = Mapper.Map <NewsComment, NewsCommentDto>(ibc);
                if (ibc.PostId != 0)
                {
                    newsCommentDto.PostId   = ibc.User.UserId;
                    newsCommentDto.PostName = ibc.User.UserName;
                }
                else
                {
                    newsCommentDto.PostId   = ibc.PostId;
                    newsCommentDto.PostName = ibc.PostName;
                }

                list.Add(newsCommentDto);
            }
            dataResultDto.Code     = 0;
            dataResultDto.DataList = list;
            return(dataResultDto);
        }
Example #13
0
        /// <summary>
        /// 获取用户博客的 最新的 几条博客
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="topCount"></param>
        /// <returns></returns>
        public async Task <DataResultDto <List <NewsCommentDto> > > GetNewsCommentByUserId(int topCount)
        {
            DataResultDto <List <NewsCommentDto> > dataResultDto = new DataResultDto <List <NewsCommentDto> >();
            string sql = $"select  NId,NewsId,ReferenceId,PostId,PostName,Content,PostDate from newscomment  where IsDelete=0 order by PostDate desc  limit 0,{topCount}";
            IEnumerable <NewsComment> bClist = null;

            using (var connection = CreateConnection())
            {
                bClist = await connection.QueryAsync <NewsComment>(sql);
            }
            List <NewsCommentDto> list           = new List <NewsCommentDto>();
            NewsCommentDto        newsCommentDto = null;

            foreach (var ibc in bClist)
            {
                newsCommentDto = Mapper.Map <NewsComment, NewsCommentDto>(ibc);
                list.Add(newsCommentDto);
            }
            dataResultDto.Code     = 0;
            dataResultDto.DataList = list;

            return(dataResultDto);
        }
Example #14
0
        public async Task <IActionResult> GetEssenceBlogListByPage(string keyWord, int pageSize, int pageIndex)
        {
            DataResultDto <List <BlogDto> > dataResultDto = await this._blogService.GetListByPage(0, 0, keyWord, "CreateDate desc", pageSize, pageIndex, true);

            return(Json(dataResultDto));
        }
Example #15
0
        public async Task <IActionResult> GetNewsListByPage(string keyWord, int pageSize, int pageIndex)
        {
            DataResultDto <List <NewsDto> > dataResultDto = await this._newsService.GetListByPage(keyWord, "Sort,CreateDate desc", pageSize, pageIndex);

            return(Json(dataResultDto));
        }
Example #16
0
        public async Task <IActionResult> BindingAccount(BindingUserDto bindingUserDto)
        {
            DataResultDto <string> dataResultDto = new DataResultDto <string>();

            try
            {
                if (!ModelState.IsValid)
                {
                    dataResultDto.Code     = 1;
                    dataResultDto.Msg      = "请输入正确的用户名和密码";
                    dataResultDto.DataList = string.Empty;
                    return(Json(dataResultDto));
                }

                if (bindingUserDto.Type == 0)
                {
                    var resultDto = await this._userService.CheckUser(new Domain.EntitiesDto.UserDto()
                    {
                        LoginName = bindingUserDto.LoginName, Password = bindingUserDto.Password
                    });

                    if (resultDto.Code == 0)
                    {
                        var loginUser = resultDto.DataList;
                        await this._gitHubService.UpdateOauthUserAsync(loginUser.UserId, bindingUserDto.LoginID);
                        await SetIdentity(loginUser.UserId, loginUser.UserName, loginUser.IsAdmin);

                        dataResultDto.Code     = 0;
                        dataResultDto.Msg      = resultDto.Msg;
                        dataResultDto.DataList = "/Home/Index";
                    }
                    else
                    {
                        dataResultDto.Code = 1;
                        dataResultDto.Msg  = "绑定失败,用户名或者密码不对";
                    }
                }
                else
                {
                    if (await this._userService.CheckLoginNameAsync(bindingUserDto.LoginName))
                    {
                        dataResultDto.Code     = 1;
                        dataResultDto.Msg      = "登陆用户名已经存在,请重新输入!";
                        dataResultDto.DataList = string.Empty;
                        return(Json(dataResultDto));
                    }

                    await this._userService.AddUserAsync(new UserDto()
                    {
                        LoginName = bindingUserDto.LoginName, UserName = bindingUserDto.UserName, Password = bindingUserDto.Password
                    });

                    var resultDto = await this._userService.CheckUser(new Domain.EntitiesDto.UserDto()
                    {
                        LoginName = bindingUserDto.LoginName, Password = bindingUserDto.Password
                    });

                    var loginUser = resultDto.DataList;
                    await this._gitHubService.UpdateOauthUserAsync(loginUser.UserId, bindingUserDto.LoginID);
                    await SetIdentity(loginUser.UserId, loginUser.UserName, loginUser.IsAdmin);

                    dataResultDto.Code     = 0;
                    dataResultDto.Msg      = resultDto.Msg;
                    dataResultDto.DataList = "/Home/Index";
                }
            }
            catch (Exception ex)
            {
                dataResultDto.Code = 1;
                dataResultDto.Msg  = "绑定失败,请联系管理员!";
                _logger.LogError(ex, "登陆出现异常");
            }
            return(Json(dataResultDto));
        }
Example #17
0
        public async Task <IActionResult> Register(UserDto userDto)
        {
            DataResultDto <string> dataResultDto = new DataResultDto <string>();

            try
            {
                if (!ModelState.IsValid)
                {
                    dataResultDto.Code     = 1;
                    dataResultDto.Msg      = "输入的格式不对";
                    dataResultDto.DataList = string.Empty;
                    return(Json(dataResultDto));
                }
                //string vCode = HttpContext.Session.GetString("LoginValidateCode");
                //if (string.IsNullOrEmpty(vCode))
                //{
                //    dataResultDto.Code = 1;
                //    dataResultDto.Msg = "验证码已过期,请重新刷新!";
                //    dataResultDto.DataList = string.Empty;
                //    return Json(dataResultDto);
                //}
                //if (vCode.ToUpper().Trim() != userDto.ChcekCode.ToUpper().Trim())
                //{
                //    dataResultDto.Code = 1;
                //    dataResultDto.Msg = "验证码不正确,请重新输入!";
                //    dataResultDto.DataList = string.Empty;
                //    return Json(dataResultDto);
                //}
                if (await this._userService.CheckLoginNameAsync(userDto.LoginName))
                {
                    dataResultDto.Code     = 1;
                    dataResultDto.Msg      = "登陆用户名已经存在,请重新输入!";
                    dataResultDto.DataList = string.Empty;
                    return(Json(dataResultDto));
                }
                userDto.LoginName = userDto.LoginName.Trim();
                string userIP = HttpContext.GetUserIp();
                var    mObj   = MemoryCacheTool.GetCacheValue(userIP);
                if (mObj == null)
                {
                    var userRegister = new UserRegister();
                    mObj = userRegister;
                    MemoryCacheTool.SetChacheValue(userIP, userRegister, TimeSpan.FromMinutes(120));
                }
                else
                {
                    var ur = (UserRegister)mObj;
                    if (ur.RegisterCount > MaxCount && DateTime.Now.Subtract(ur.RegisterTime).TotalMinutes <= MaxMinute)
                    {
                        dataResultDto.Code     = 1;
                        dataResultDto.Msg      = "你的IP注册次数过多!";
                        dataResultDto.DataList = string.Empty;
                        return(Json(dataResultDto));
                    }
                }

                if (await this._userService.AddUserAsync(userDto))
                {
                    userDto = await this._userService.GetUserByLoginNameAsync(userDto.LoginName);

                    if (userDto == null)
                    {
                        dataResultDto.Code = 1;
                        dataResultDto.Msg  = "注册程序异常,请联系管理员!";
                        return(Json(dataResultDto));
                    }

                    var ur = (UserRegister)mObj;
                    ur.ClientIP = userIP;
                    ur.RegisterCount++;
                    ur.RegisterTime = DateTime.Now;

                    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                    identity.AddClaim(new Claim(ClaimTypes.Sid, userDto.UserId.ToString()));
                    identity.AddClaim(new Claim(ClaimTypes.Name, userDto.UserName));
                    identity.AddClaim(new Claim(ClaimTypes.Role, "User"));
                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

                    dataResultDto.Code     = 0;
                    dataResultDto.Msg      = "注册成功!";
                    dataResultDto.DataList = "/User/Home/Index";
                }
                else
                {
                    dataResultDto.Code = 1;
                    dataResultDto.Msg  = "注册失败!";
                }
            }
            catch (Exception ex)
            {
                logger.LogError("注册失败", ex);
                dataResultDto.Code = 1;
                dataResultDto.Msg  = "注册程序异常,请联系管理员!";
            }
            return(Json(dataResultDto));
        }
Example #18
0
        public async Task <IActionResult> Index(int?id, int?categoryId)
        {
            UserDto userDtoLogin = new UserDto();

            if (categoryId != null && categoryId != 0)
            {
                userDtoLogin = await this._userService.GetOneUserByCategoryIDAsync((int)categoryId);
            }
            else
            {
                if (id == null)
                {
                    var claim = GetClaim(HttpContext.User.Claims, ClaimTypes.Sid);
                    if (claim != null)
                    {
                        userDtoLogin.UserId = int.Parse(claim.Value);
                        claim = GetClaim(HttpContext.User.Claims, ClaimTypes.Name);
                        userDtoLogin.UserName = claim.Value;
                        TempData["LoginUser"] = userDtoLogin;
                    }
                }
                else
                {
                    userDtoLogin = await this._userService.GetOneUserByUserIdAsync((int)id);

                    var claim = GetClaim(HttpContext.User.Claims, ClaimTypes.Sid);
                    if (claim != null)
                    {
                        TempData["LoginUser"] = userDtoLogin;
                    }
                }
            }

            ViewBag.UserId     = userDtoLogin.UserId;
            ViewBag.categoryId = categoryId == null ? 0 : (int)categoryId;
            DetailContentLayoutViewModel.UserDto = userDtoLogin;
            ViewBag.DetailContentLayoutViewModel = DetailContentLayoutViewModel;
            DetailContentLayoutViewModel.UserDto = userDtoLogin;
            ViewBag.DetailContentLayoutViewModel = DetailContentLayoutViewModel;
            TempData["Title"]       = $"{this._webStaticConfig.WebName}-{userDtoLogin.UserName}";
            TempData["KeyWord"]     = _webStaticConfig.WebKeyWord;
            TempData["Description"] = _webStaticConfig.WebDescription;

            var userBlogCommentList = MemoryCacheTool.GetCacheValue("UserBlogCommentList");

            if (userBlogCommentList != null)
            {
                TempData["UserBlogCommentList"] = (List <BlogCommentDto>)userBlogCommentList;
            }
            else
            {
                DataResultDto <List <BlogCommentDto> > dataResultDto = await this._blogCommentService.GetBlogCommentByUserId(10);

                if (dataResultDto.Code == 0)
                {
                    TempData["UserBlogCommentList"] = dataResultDto.DataList;
                }

                MemoryCacheTool.SetChacheValue("UserBlogCommentList", dataResultDto.DataList, TimeSpan.FromMinutes(30));
            }

            return(View());
        }
Example #19
0
        public async Task <IActionResult> GetListByPageInChildCate(int userID, int categoryId, string keyWord, int pageSize, int pageIndex)
        {
            DataResultDto <List <BlogDto> > dataResultDto = await this._blogService.GetListByPageInChildCate(userID, categoryId, keyWord, "CreateDate desc", pageSize, pageIndex, false);

            return(Json(dataResultDto));
        }
Example #20
0
        public async Task <IActionResult> Login(UserDto userDto)
        {
            DataResultDto <string> dataResultDto = new DataResultDto <string>();

            try
            {
                if (!ModelState.IsValid)
                {
                    dataResultDto.Code     = 1;
                    dataResultDto.Msg      = "请输入正确的用户名和密码";
                    dataResultDto.DataList = string.Empty;
                    return(Json(dataResultDto));
                }
                //string vCode = HttpContext.Session.GetString("LoginValidateCode");
                //logger.LogDebug($"zhongzw sessionid:{HttpContext.Session.Id},{vCode}");
                //if (string.IsNullOrEmpty(vCode))
                //{
                //    dataResultDto.Code = 1;
                //    dataResultDto.Msg = "验证码已过期,请重新刷新!";
                //    dataResultDto.DataList = string.Empty;
                //    return Json(dataResultDto);
                //}
                //if (vCode.ToUpper().Trim() != userDto.ChcekCode.ToUpper().Trim())
                //{
                //    dataResultDto.Code = 1;
                //    dataResultDto.Msg = "验证码不正确,请重新输入!";
                //    dataResultDto.DataList = string.Empty;
                //    return Json(dataResultDto);
                //}
                var resultDto = await this._userService.CheckUser(new Domain.EntitiesDto.UserDto()
                {
                    LoginName = userDto.LoginName, Password = userDto.Password
                });

                if (resultDto.Code == 0)
                {
                    var loginUser = resultDto.DataList;
                    var identity  = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                    identity.AddClaim(new Claim(ClaimTypes.Sid, loginUser.UserId.ToString()));
                    identity.AddClaim(new Claim(ClaimTypes.Name, loginUser.UserName));
                    identity.AddClaim(new Claim(ClaimTypes.Role, loginUser.IsAdmin ? "Admin" : "User"));
                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

                    dataResultDto.Code = 0;
                    dataResultDto.Msg  = resultDto.Msg;
                    if (loginUser.IsAdmin)
                    {
                        dataResultDto.DataList = "/Admin/Home/Index";
                    }
                    else
                    {
                        dataResultDto.DataList = "/User/Home/Index";
                    }
                }
                else
                {
                    dataResultDto.Code = 1;
                    dataResultDto.Msg  = resultDto.Msg;
                }
            }
            catch
            {
                dataResultDto.Code = 1;
                dataResultDto.Msg  = "登陆出现异常,请联系管理员!";
            }
            return(Json(dataResultDto));
        }
Example #21
0
        /// <summary>
        /// 分页获取博客
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="categoryId"></param>
        /// <param name="keyWork"></param>
        /// <param name="orderFiled"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public async Task <DataResultDto <List <BlogDto> > > GetListByPage(DateTime?startTime, DateTime?endTime, int userId, int categoryId, string keyWord, string orderFiled, int pageSize, int pageIndex)
        {
            DataResultDto <List <BlogDto> > dataResultDto = new DataResultDto <List <BlogDto> >();
            List <BlogDto> list = new List <BlogDto>();

            try
            {
                string where = "where blog.IsDelete=0 ";
                if (userId != 0)
                {
                    where += " and blog.UserId=@UserId ";
                }
                if (categoryId != 0)
                {
                    where += " and blog.CategoryID=@CategoryID ";
                }

                if (startTime != null && endTime != null)
                {
                    where  += "  and blog.CreateDate BETWEEN @StartTime and @EndTime ";
                    endTime = endTime.Value.AddDays(1).AddSeconds(-1);
                }

                if (!string.IsNullOrEmpty(keyWord))
                {
                    where += "  and (blog.Title like @KeyWork or Body like @KeyWork or category.CategoryName like @KeyWork) ";
                }

                string order = string.Empty;
                if (!string.IsNullOrEmpty(orderFiled))
                {
                    order = string.Format("order by blog.{0}", orderFiled);
                }

                string sql = string.Format("select blog.BlogId,blog.CategoryID,blog.Title,blog.Body,blog.Remark,blog.CreateDate,blog.UpdateDate,blog.Sort,blog.UserId,blog.VisitCount,blog.CommentCount,blog.IsEssence,blog.IsHomePage,blog.IsDelete,category.CategoryID,category.CategoryName,category.CreateDate,user.UserId,user.LoginName,user.UserName,user.UserHeadImaUrl from `blog`  " +
                                           " left join `category`  on blog.CategoryID=category.CategoryID left join `user` on user.UserId=blog.UserId {0}  {1} limit {2},{3}", where, order, (pageIndex - 1) * pageSize, pageSize);

                string sqlCount = string.Format("select count(1) from `blog`  left join `category` on blog.CategoryID=category.CategoryID left join `user` on user.UserId=blog.UserId {0}  ", where);

                IEnumerable <Blog> blist = null;
                using (var connection = CreateConnection())
                {
                    dataResultDto.Count = await connection.QueryFirstAsync <int>(sqlCount, new { StartTime = startTime, EndTime = endTime, UserId = userId, CategoryID = categoryId, KeyWork = string.Format("%{0}%", keyWord) });

                    blist = await connection.QueryAsync <Blog, Category, User, Blog>(sql, (qblog, qcategory, quser) =>
                    {
                        qblog.Category = qcategory;
                        qblog.User     = quser;
                        return(qblog);
                    }, new { StartTime = startTime, EndTime = endTime, UserId = userId, CategoryID = categoryId, KeyWork = string.Format("%{0}%", keyWord), OrderFiled = orderFiled },
                                                                                     splitOn : "BlogId,CategoryId,UserId");
                }

                BlogDto blogDto = null;
                foreach (var ib in blist)
                {
                    blogDto             = Mapper.Map <Blog, BlogDto>(ib);
                    blogDto.Body        = string.Empty;//优化,不传到显示页面
                    blogDto.CategoryDto = Mapper.Map <Category, CategoryDto>(ib.Category);
                    blogDto.User        = Mapper.Map <User, UserDto>(ib.User);
                    list.Add(blogDto);
                }
                dataResultDto.Code     = 0;
                dataResultDto.DataList = list;
            }
            catch
            {
                dataResultDto.Code     = 1;
                dataResultDto.DataList = null;
            }
            return(dataResultDto);
        }
Example #22
0
        /// <summary>
        /// 分页获取博客 包括子类   二级
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="categoryId"></param>
        /// <param name="keyWork"></param>
        /// <param name="orderFiled"></param>
        /// <param name="pageSize"></param>
        /// <param name="PageIndex"></param>
        /// <returns></returns>
        public async Task <DataResultDto <List <BlogDto> > > GetListByPageInChildCate(int userId, int categoryId, string keyWork, string orderFiled, int pageSize, int pageIndex, bool IsEssence = false)
        {
            var cateList = await this._categoryService.GetAllCategoryByUserId(categoryId, userId);

            List <int> listCate = new List <int>();

            listCate.Add(categoryId);
            foreach (var ic in cateList)
            {
                listCate.Add(ic.CategoryId);
            }


            DataResultDto <List <BlogDto> > dataResultDto = new DataResultDto <List <BlogDto> >();
            List <BlogDto> list = new List <BlogDto>();

            string where = "where blog.IsDelete=0 ";
            if (userId != 0)
            {
                where += " and blog.UserId=@UserId ";
            }
            if (categoryId != 0)
            {
                if (listCate.Count == 1)
                {
                    where += $" and blog.CategoryID={categoryId}";
                }
                else
                {
                    where += $" and blog.CategoryID in({string.Join(',', listCate)})";
                }
            }
            if (IsEssence)
            {
                where += " and blog.IsEssence=1 ";
            }

            if (!string.IsNullOrEmpty(keyWork))
            {
                where += "  and (blog.Title like @KeyWork or Body like @KeyWork or category.CategoryName like @KeyWork) ";
            }

            string order = string.Empty;

            if (!string.IsNullOrEmpty(orderFiled))
            {
                order = string.Format("order by blog.{0}", orderFiled);
            }

            string sql = string.Format("select blog.BlogId,blog.CategoryID,blog.Title,blog.Body,blog.Remark,blog.CreateDate,blog.UpdateDate,blog.Sort,blog.UserId,blog.VisitCount,blog.CommentCount,blog.IsEssence,blog.IsDelete,category.CategoryID,category.CategoryName,category.CreateDate,user.UserId,user.LoginName,user.UserName,user.UserHeadImaUrl from `blog`  " +
                                       " left join `category`  on blog.CategoryID=category.CategoryID left join `user` on user.UserId=blog.UserId {0}  {1} limit {2},{3}", where, order, (pageIndex - 1) * pageSize, pageSize);

            string sqlCount = string.Format("select count(1) from `blog`  left join `category` on blog.CategoryID=category.CategoryID left join `user` on user.UserId=blog.UserId {0}  ", where);

            IEnumerable <Blog> blist = null;

            using (var connection = CreateConnection())
            {
                dataResultDto.Count = await connection.QueryFirstAsync <int>(sqlCount, new { UserId = userId, CategoryID = categoryId, KeyWork = string.Format("%{0}%", keyWork) });

                blist = await connection.QueryAsync <Blog, Category, User, Blog>(sql, (qblog, qcategory, quser) =>
                {
                    qblog.Category = qcategory;
                    qblog.User     = quser;
                    return(qblog);
                }, new { UserId = userId, KeyWork = string.Format("%{0}%", keyWork), OrderFiled = orderFiled },
                                                                                 splitOn : "BlogId,CategoryId,UserId");
            }
            BlogDto blogDto = null;

            foreach (var ib in blist)
            {
                blogDto             = Mapper.Map <Blog, BlogDto>(ib);
                blogDto.Body        = string.Empty;
                blogDto.CategoryDto = Mapper.Map <Category, CategoryDto>(ib.Category);
                blogDto.User        = Mapper.Map <User, UserDto>(ib.User);
                list.Add(blogDto);
            }
            dataResultDto.Code     = 0;
            dataResultDto.DataList = list;
            return(dataResultDto);
        }