Exemplo n.º 1
0
        public async Task <IActionResult> Create(InviteEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    PersonView person = await this.personService.GetCurrentPersonViewAsync();

                    InviteCode code = await this.inviteService.InsertAsync(model, person);

                    return(this.Json(AjaxResult.CreateByContext(code)));
                }
                catch (ModelException ex)
                {
                    return(this.Json(ex.ToAjaxResult()));
                }
                catch (Exception ex)
                {
                    return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
                }
            }
            else
            {
                return(this.Json(ModelState.ToAjaxResult()));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> ModifyAsk(PostEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await this.postService.ModifyAsync(model, this.User.GetUserId().Value);

                    AjaxResult result = new AjaxResult
                    {
                        Message = "提交成功",
                        Context = Url.Action("Details", "Question", new { id = model.Id })
                    };
                    return(this.Json(result));
                }
                catch (ModelException ex)
                {
                    return(this.Json(ex.ToAjaxResult()));
                }
                catch (Exception ex)
                {
                    return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
                }
            }
            else
            {
                return(this.Json(ModelState.ToAjaxResult()));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> BlockPost(Guid postId)
        {
            try
            {
                PersonView loginPerson = await this.personService.GetCurrentPersonViewAsync();

                Post post = await this.postService.GetPostByIdAsync(postId);

                if (post == null)
                {
                    throw new Exception("该帖子不存在");
                }
                bool isSelf  = loginPerson.Id == post.PersonId;
                bool isAdmin = loginPerson.RoleType == RoleType.Admin || loginPerson.RoleType == RoleType.Master || loginPerson.RoleType == RoleType.SuperAdmin;
                if (isSelf || isAdmin)
                {
                    await this.postService.BlockAsync(postId, loginPerson);

                    return(Json(AjaxResult.CreateDefaultSuccess()));
                }
                else
                {
                    throw new Exception("没有该操作的权限");
                }
            }
            catch (Exception ex)
            {
                return(Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(CommentEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (model.Id == null)
                    {
                        throw new Exception("评论ID不能为空");
                    }
                    Guid?   loginUserId = this.User.GetUserId();
                    Comment comment     = await this.commentService.GetByIdAsync(model.Id.Value);

                    if (comment == null || comment.IsDelete)
                    {
                        throw new Exception("该评论不存在,或已被删除");
                    }
                    else if (loginUserId == null || loginUserId.Value != comment.PersonId)
                    {
                        throw new Exception("没有该操作的权限");
                    }
                    else if (string.IsNullOrWhiteSpace(model.Content))
                    {
                        throw new Exception("内容不能为空");
                    }
                    else
                    {
                        string htmlContent = this.htmlService.ClearHtml(model.Content);
                        string textContent = this.htmlService.HtmlToText(htmlContent);
                        if (string.IsNullOrWhiteSpace(htmlContent))
                        {
                            throw new Exception("内容不能为空");
                        }
                        else
                        {
                            await this.commentService.ModifyAsync(comment.Id, htmlContent, textContent);

                            return(Json(AjaxResult.CreateDefaultSuccess()));
                        }
                    }
                }
                catch (ModelException ex)
                {
                    return(this.Json(ex.ToAjaxResult()));
                }
                catch (Exception ex)
                {
                    return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
                }
            }
            else
            {
                return(this.Json(ModelState.ToAjaxResult()));
            }
        }
Exemplo n.º 5
0
 public IActionResult IsLogin()
 {
     if (this.HttpContext.User.Identity.IsAuthenticated)
     {
         return(this.Json(AjaxResult.CreateDefaultSuccess()));
     }
     else
     {
         return(this.Json(AjaxResult.CreateByMessage("", false)));
     }
 }
Exemplo n.º 6
0
        public async Task <IActionResult> ChangeStatus([FromBody] Topic topic)
        {
            try
            {
                int result = await this.topicService.ChangeStatusAsync(topic);

                return(this.Json(AjaxResult.CreateDefaultSuccess()));
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 7
0
        public async Task <IActionResult> RestoreMute(Guid id)
        {
            try
            {
                await this.personService.RestoreMuteByIdAsync(id);

                return(this.Json(AjaxResult.CreateDefaultSuccess()));
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 8
0
        public async Task <IActionResult> GetAll()
        {
            try
            {
                List <InviteCode> codes = await this.inviteService.GetAllAsync();

                return(this.Json(AjaxResult.CreateByContext(codes)));
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 9
0
        public async Task <IActionResult> Add(Guid postId)
        {
            try
            {
                await this.favoriteService.InsertFavoriteAsync(postId);

                return(Json(AjaxResult.CreateDefaultSuccess()));
            }
            catch (Exception ex)
            {
                return(Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 10
0
        public async Task <IActionResult> Delete(Guid id)
        {
            try
            {
                await this.commentService.DeleteAsync(id, this.User.GetUserId().Value);

                return(Json(AjaxResult.CreateDefaultSuccess()));
            }
            catch (Exception ex)
            {
                return(Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 11
0
        public async Task <IActionResult> GetAll()
        {
            try
            {
                List <Tag> tags = await this.tagService.GetAllAsync();

                return(this.Json(AjaxResult.CreateByContext(tags)));
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 12
0
        public async Task <ActionResult> Delete([FromBody] Tag tag)
        {
            try
            {
                await this.tagService.DeleteAsync(tag);

                return(this.Json(AjaxResult.CreateDefaultSuccess()));
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 13
0
        public async Task <IActionResult> Search(string search)
        {
            try
            {
                List <Tag> tags = await this.tagService.SearchAsync(search);

                return(this.Json(AjaxResult.CreateByContext(tags)));
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 14
0
        public async Task <IActionResult> SetTop(Guid postId, bool isTop)
        {
            try
            {
                await this.postService.SetTopAsync(postId, isTop);

                return(Json(AjaxResult.CreateDefaultSuccess()));
            }
            catch (Exception ex)
            {
                return(Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 15
0
        public async Task <IActionResult> ChangeCodeRegister(bool status)
        {
            try
            {
                await this.inviteService.ChangeCodeRegister(status);

                return(this.Json(AjaxResult.CreateDefaultSuccess()));
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 16
0
        public async Task <IActionResult> Delete(string code)
        {
            try
            {
                await this.inviteService.DeleteAsync(code);

                return(this.Json(AjaxResult.CreateDefaultSuccess()));
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 17
0
        public async Task <IActionResult> ChangeStatus([FromBody] Tag tag)
        {
            try
            {
                Tag newTag = await tagService.ChangeBestStatusAsync(tag, !tag.IsBest);

                return(this.Json(AjaxResult.CreateByContext(newTag)));
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 18
0
        public async Task <IActionResult> LikeComment(Guid id)
        {
            try
            {
                await this.zanService.ZanCommentAsync(id);

                Comment comment = await this.commentService.GetByIdAsync(id);

                return(Json(AjaxResult.CreateByContext(comment.LikeNum)));
            }
            catch (Exception ex)
            {
                return(Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 19
0
        public async Task <IActionResult> GetAll()
        {
            try
            {
                List <Topic> topics = await this.topicService.GetAllAsync(true, Library.Enums.SearchType.ALL);

                AjaxResult result = AjaxResult.CreateByContext(topics);
                return(this.Json(result));
            }
            catch (Exception e)
            {
                AjaxResult result = AjaxResult.CreateByMessage(e.Message, false);
                return(this.Json(result));
            }
        }
Exemplo n.º 20
0
        public async Task <IActionResult> LikePost(Guid id)
        {
            try
            {
                await this.zanService.ZanPostAsync(id);

                Post post = await this.postService.GetPostByIdAsync(id);

                return(Json(AjaxResult.CreateByContext(post.LikeNum)));
            }
            catch (Exception ex)
            {
                return(Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 21
0
        public async Task <IActionResult> ModifyAvatar(IFormFile file)
        {
            try
            {
                PersonView person = await this.personService.GetCurrentPersonViewAsync();

                UploadInfo info = await this.uploadService.UploadImageAsync(file, true);

                await this.personService.ModifyAvatarAsync(person, info);

                return(this.Json(AjaxResult.CreateByContext(info.UrlPath)));
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 22
0
        public async Task <IActionResult> Register(AccountRegisterModel model)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(this.Json(AjaxResult.CreateByMessage("您已经登录,请勿重复注册", false)));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    string code = HttpContext.Session.GetVerifyCode();
                    if (code.ToLower() == model.Code.ToLower())
                    {
                        try
                        {
                            await personService.Register(model);

                            return(this.Json(AjaxResult.CreateByMessage("")));
                        }
                        catch (ModelException ex)
                        {
                            return(this.Json(ex.ToAjaxResult()));
                        }
                        catch (Exception ex)
                        {
                            return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
                        }
                    }
                    else
                    {
                        await HttpContext.Session.RefreshVerifyCodeAsync();

                        AjaxResult result = new AjaxResult();
                        result.Success = false;
                        result.ErrorMessages.Add(nameof(model.Code), "验证码不正确,请重新输入");
                        return(this.Json(result));
                    }
                }
                else
                {
                    return(this.Json(ModelState.ToAjaxResult()));
                }
            }
        }
Exemplo n.º 23
0
        public async Task <IActionResult> RoleModify(Guid id, RoleType role)
        {
            try
            {
                PersonView loginUser = await this.personService.GetCurrentPersonViewAsync();

                PersonView DoUser = await this.personService.GetPersonViewAsync(id);

                if (DoUser == null)
                {
                    throw new Exception("该用户不存在");
                }
                else if (DoUser.RoleType == RoleType.SuperAdmin)
                {
                    throw new Exception("无权修改网站管理员");
                }
                else if (role == RoleType.SuperAdmin)
                {
                    throw new Exception("无权设置为网站管理员");
                }
                else if (loginUser.RoleType != RoleType.Admin && loginUser.RoleType != RoleType.SuperAdmin)
                {
                    throw new Exception("仅管理员拥有操作权限");
                }
                else if (loginUser.RoleType == RoleType.Admin && (role == RoleType.Admin || role == RoleType.SuperAdmin))
                {
                    throw new Exception("该操作无权限");
                }
                else
                {
                    await this.personService.ModifyRoleAsync(DoUser.Id, role);

                    return(this.Json(AjaxResult.CreateDefaultSuccess()));
                }
            }
            catch (Exception ex)
            {
                return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
            }
        }
Exemplo n.º 24
0
 /// <summary>
 /// 转换为AJAX返回对象
 /// </summary>
 public static AjaxResult ToAjaxResult(this ModelStateDictionary modelState)
 {
     if (modelState == null)
     {
         throw new ArgumentNullException(nameof(modelState));
     }
     if (modelState.IsValid)
     {
         return(AjaxResult.CreateByMessage("操作成功"));
     }
     else
     {
         Dictionary <string, string> err = modelState.GetAllErrors();
         AjaxResult result = new AjaxResult();
         result.Success = false;
         foreach (var item in err)
         {
             result.ErrorMessages.Add(item.Key, item.Value);
         }
         return(result);
     }
 }
Exemplo n.º 25
0
        public async Task <IActionResult> ModifyPassword(ProfileModifyPasswordModel model)
        {
            if (ModelState.IsValid)
            {
                string code = HttpContext.Session.GetVerifyCode();
                if (code.ToLower() == model.Code.ToLower())
                {
                    try
                    {
                        await personService.ModifyPasswordAsync(model);

                        return(this.Json(AjaxResult.CreateDefaultSuccess()));
                    }
                    catch (ModelException ex)
                    {
                        return(this.Json(ex.ToAjaxResult()));
                    }
                    catch (Exception ex)
                    {
                        return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
                    }
                }
                else
                {
                    await HttpContext.Session.RefreshVerifyCodeAsync();

                    AjaxResult result = new AjaxResult();
                    result.Success = false;
                    result.ErrorMessages.Add(nameof(model.Code), "验证码不正确,请重新输入");
                    return(this.Json(result));
                }
            }
            else
            {
                return(this.Json(ModelState.ToAjaxResult()));
            }
        }
Exemplo n.º 26
0
        public async Task <IActionResult> Info(ProfileInfoEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await this.personService.ModifyNickNameAsync(model);

                    return(this.Json(AjaxResult.CreateDefaultSuccess()));
                }
                catch (ModelException ex)
                {
                    return(this.Json(ex.ToAjaxResult()));
                }
                catch (Exception ex)
                {
                    return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
                }
            }
            else
            {
                return(this.Json(ModelState.ToAjaxResult()));
            }
        }
Exemplo n.º 27
0
        public async Task <ActionResult> Edit(TopicEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Topic topic = await topicService.ModifyAsync(model);

                    return(this.Json(AjaxResult.CreateByContext(topic)));
                }
                catch (ModelException ex)
                {
                    return(this.Json(ex.ToAjaxResult()));
                }
                catch (Exception ex)
                {
                    return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
                }
            }
            else
            {
                return(this.Json(ModelState.ToAjaxResult()));
            }
        }
Exemplo n.º 28
0
        public async Task <ActionResult> Create(TagEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Tag tag = await tagService.CreateAsync(model);

                    return(this.Json(AjaxResult.CreateByContext(tag)));
                }
                catch (ModelException ex)
                {
                    return(this.Json(ex.ToAjaxResult()));
                }
                catch (Exception ex)
                {
                    return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
                }
            }
            else
            {
                return(this.Json(ModelState.ToAjaxResult()));
            }
        }