Exemplo n.º 1
0
 /// <summary>
 /// 使用使用者模型物件,新增使用者
 /// </summary>
 /// <param name="userModel">使用者模型物件</param>
 /// <returns>新增結果</returns>
 public PoResult CreateUser(UserCreateVm userModel)
 {
     try
     {
         var isEmailExit = this.Database.Users.Any(o => o.Email == userModel.Email);
         if (isEmailExit)
         {
             return(PoResult.Fail("電子郵件重複"));
         }
         var isUserNameExit = this.Database.Users.Any(o => o.UserName == userModel.UserName);
         if (isUserNameExit)
         {
             return(PoResult.Fail("使用者名稱重複重複"));
         }
         this.Database.Users.Add(new User
         {
             Id               = Ci.Sequential.Guid.Create(),
             Email            = userModel.Email,
             UserName         = userModel.UserName,
             Password         = PasswordHash(userModel.Password),
             CreateTime       = DateTime.Now,
             HeadPortraitPath = userModel.ImagePath
         });
         this.Database.SaveChanges();
         return(PoResult.PoSuccess());
     }
     catch (Exception e)
     {
         return(PoResult.Exception(e));
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 使用帳號、密碼,驗證使用者
 /// </summary>
 /// <param name="account">帳號</param>
 /// <param name="password">密碼</param>
 /// <returns>驗證結果</returns>
 public PoResult VerifyUser(string account, string password)
 {
     try
     {
         var userResult = this.GetUser(account);
         if (!userResult.Success)
         {
             return(PoResult.Fail("帳號不存在"));
         }
         return(userResult.Data.Password == PasswordHash(password) ? PoResult.PoSuccess() : PoResult.Fail("密碼錯誤"));
     }
     catch (Exception e)
     {
         return(PoResult.Exception(e));
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// 使用使用者物件,新增使用者
 /// </summary>
 /// <param name="userModel">使用者物件</param>
 /// <returns>新增結果</returns>
 public PoResult CreateUser(UserCreateVm userModel)
 {
     try
     {
         var isUserExist = this.Database.Users.Any(o => o.Account == userModel.Account);
         if (isUserExist)
         {
             return(PoResult.Fail("帳號重複"));
         }
         this.Database.Users.Add(new User
         {
             Id         = Ci.Sequential.Guid.Create(),
             Account    = userModel.Account,
             Password   = PasswordHash(userModel.Password),
             CreateTime = DateTime.Now
         });
         this.Database.SaveChanges();
         return(PoResult.PoSuccess());
     }
     catch (Exception e)
     {
         return(PoResult.Exception(e));
     }
 }
Exemplo n.º 4
0
        public ActionResult SendMessage(string messageContext, HttpPostedFileBase[] images, Guid?parentMessageId = null)
        {
            // 驗證
            if (messageContext.Length > 300)
            {
                return(this.Json(PoResult.Fail("訊息內容超出限制,最多為300字")));
            }

            // 驗證
            if (messageContext.Length == 0 && images == null)
            {
                return(this.Json(PoResult.Fail("沒有可以儲存的訊息內容")));
            }

            // 組裝model
            var messageCreateModel = new MessageCreateModel
            {
                UserId          = this.CurrentUser.Id,
                Context         = messageContext,
                ParentMessageId = parentMessageId
            };

            if (images != null)
            {
                foreach (var image in images)
                {
                    if (image == null || image.ContentLength == 0)
                    {
                        continue;
                    }

                    if (image.ContentLength > 1024 * 1024)
                    {
                        return(this.Json(PoResult.Fail($"檔案「{image.FileName}」大小超出限制,最大1MB")));
                    }

                    try
                    {
                        using (new System.Drawing.Bitmap(image.InputStream))
                        {
                            // ignore
                        }
                    }
                    catch (Exception)
                    {
                        return(this.Json(PoResult.Fail($"檔案「{image.FileName}」格式錯誤,請確定上傳的是圖片")));
                    }

                    image.InputStream.Position = 0;
                }

                foreach (var image in images)
                {
                    var fileResult = image.SaveAsLocal("AttachmentImages");
                    messageCreateModel.Images.Add(($"{fileResult.OriName}{fileResult.Extension}", fileResult.VirtualPath));
                }
            }

            var saveResult = this.messageService.SaveMessage(messageCreateModel);

            if (saveResult.Success)
            {
                var messageListVmResult = this.messageService.GetMessageListVm(saveResult.Data.Id);
                if (messageListVmResult.Success)
                {
                    if (saveResult.Data.ParentMessageId == null)
                    {
                        // not reply
                        this.hub.Clients.All.updateMessage(messageListVmResult.Data.MessageId, "newMessage", messageListVmResult.Data);
                    }
                    else
                    {
                        // is reply
                        this.hub.Clients.All.updateMessage(saveResult.Data.ParentMessageId, "newReply", messageListVmResult.Data);
                    }
                }
            }
            else
            {
                if (images != null)
                {
                    foreach (var image in messageCreateModel.Images)
                    {
                        var mapPath = this.Server.MapPath(image.Path);
                        if (System.IO.File.Exists(mapPath))
                        {
                            System.IO.File.Delete(mapPath);
                        }
                    }
                }
            }

            return(this.Json(new PoResult {
                Success = saveResult.Success, Message = saveResult.Message
            }));
        }
Exemplo n.º 5
0
        public ActionResult UpdateMessage(Guid messageId, string context, Guid[] deleteImages, HttpPostedFileBase[] images)
        {
            // 驗證
            if (context.Length > 300)
            {
                return(this.Json(PoResult.Fail("訊息內容超出限制,最多為300字")));
            }


            // 驗證
            if (context.Length == 0 && images == null && deleteImages == null)
            {
                return(this.Json(PoResult.Fail("沒有可以儲存的訊息內容")));
            }

            // 組裝model
            var messageUpdateModel = new MessageUpdateModel()
            {
                MessageId           = messageId,
                Context             = context,
                DeleteAttachmentIds = deleteImages?.ToList() ?? new List <Guid>()
            };

            if (images != null)
            {
                foreach (var image in images)
                {
                    if (image == null || image.ContentLength == 0)
                    {
                        continue;
                    }

                    if (image.ContentLength > 1024 * 1024)
                    {
                        return(this.Json(PoResult.Fail($"檔案「{image.FileName}」大小超出限制,最大1MB")));
                    }

                    try
                    {
                        using (new System.Drawing.Bitmap(image.InputStream))
                        {
                            // ignore
                        }
                    }
                    catch (Exception)
                    {
                        return(this.Json(PoResult.Fail($"檔案「{image.FileName}」格式錯誤,請確定上傳的是圖片")));
                    }

                    image.InputStream.Position = 0;
                }

                foreach (var image in images)
                {
                    var fileResult = image.SaveAsLocal("AttachmentImages");
                    messageUpdateModel.NewAttachments.Add(($"{fileResult.OriName}{fileResult.Extension}", fileResult.VirtualPath));
                }
            }

            var updateResult = this.messageService.UpdateMessage(this.CurrentUser.Id, messageUpdateModel);

            if (updateResult.Success)
            {
                var messageListVmResult = this.messageService.GetMessageListVm(updateResult.Data.Id);
                if (messageListVmResult.Success)
                {
                    this.hub.Clients.All.updateMessage(messageListVmResult.Data.MessageId, "updateMessage", messageListVmResult.Data);
                }
            }
            else
            {
                if (images != null)
                {
                    foreach (var image in messageUpdateModel.NewAttachments)
                    {
                        var mapPath = this.Server.MapPath(image.Path);
                        if (System.IO.File.Exists(mapPath))
                        {
                            System.IO.File.Delete(mapPath);
                        }
                    }
                }
            }

            return(this.Json(new PoResult {
                Success = updateResult.Success, Message = updateResult.Message
            }));
        }