/// <summary>
 /// 将HomeDynamicSettings转换成HomeDynamicModel
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public HomeDynamicModel ToHomeDynamicModel(HomeDynamicSettings item)
 {
     ImageUrl    = item.ImageUrl;
     Type        = item.Type;
     RedirectUrl = item.RedirectUrl;
     Id          = item.Id;
     LabelText   = item.LabelText;
     return(this);
 }
        /// <summary>
        /// 将HomeDynamicModel转换成HomeDynamicSettings
        /// </summary>
        /// <returns></returns>
        public HomeDynamicSettings AsHomeDynamic()
        {
            HomeDynamicSettings dynamic = HomeDynamicSettings.New();

            dynamic.ImageUrl    = ImageUrl;
            dynamic.Type        = Type;
            dynamic.RedirectUrl = RedirectUrl;
            dynamic.Id          = Id;
            dynamic.LabelText   = LabelText;
            return(dynamic);
        }
Exemple #3
0
        public async Task <IActionResult> HomeDynamicAddUpdateAsync([FromServices] IHostingEnvironment env, IFormFile files, HomeDynamicModel model)
        {
            ValidateInput(model, ModelState);
            if (model.Id == 0)
            {
                model.IsUpdate = false;
            }
            else
            {
                model.IsUpdate = true;
            }

            string message = string.Empty;


            if (ModelState.IsValid)
            {
                if (model.ImageUrl.IsNullOrWhiteSpace())
                {
                    var  file          = Request.Form.Files[0];
                    long size          = Request.Form.Files.Sum(f => f.Length);
                    var  filePath      = string.Format("/Uploads/Images/{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd"));
                    var  qiniuFilePath = string.Format("{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd"));
                    if (!Directory.Exists(env.WebRootPath + filePath))
                    {
                        Directory.CreateDirectory(env.WebRootPath + filePath);
                    }
                    if (file != null)
                    {
                        var          fileExtension = Path.GetExtension(file.FileName);
                        const string fileFilt      = ".gif|.jpg|.jpeg|.png|.webp|";
                        const string saveSuffix    = ".jpeg";
                        if (fileExtension == null)
                        {
                            return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件没有后缀")));
                        }
                        if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1)
                        {
                            return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件不是图片")));
                        }
                        if (size > 1024 * 1024 * 2)
                        {
                            return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件不能大于2M")));
                        }
                        var strDateTime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                        var strRan      = Convert.ToString(new Random().Next(100, 999));
                        var saveName    = strDateTime + strRan;
                        var useQiNiu    = BuilderContext.Configuration["UseQiNiu"];
                        var qiniuHost   = BuilderContext.Configuration["QiNiuHost"];
                        var pathName    = string.Empty;

                        string hostPath      = WebSiteUtility.MapPhysicsToUrl($"{filePath}{saveName}");
                        string qiniuHostPath = WebSiteUtility.MapPhysicsToUrl($"{qiniuHost}{qiniuFilePath}{saveName}");

                        using (var stream = new FileStream($"{env.WebRootPath}{filePath}{saveName}{saveSuffix}", FileMode.Create))
                        {
                            await file.CopyToAsync(stream);

                            stream.Flush();
                            pathName       = stream.Name;
                            model.ImageUrl = $"{Request.Scheme}://{Request.Host}{hostPath}{saveSuffix}";
                        }
                        if (useQiNiu.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                        {
                            await Task.Run(() =>
                            {
                                QiniuManager qiniu = new QiniuManager();
                                string savekey     = $"{qiniuFilePath}{saveName}";
                                byte[] data        = System.IO.File.ReadAllBytes(pathName);
                                qiniu.ByteUpload(data, savekey);
                            });

                            model.ImageUrl = qiniuHostPath;
                        }
                    }
                }

                if (model.IsUpdate)
                {
                    HomeDynamicSettings dynamic = homeDynamicSettingsService.GetHomeDynamicSingle(model.Id);
                    dynamic.DateLastModified = DateTime.Now;
                    dynamic.ImageUrl         = model.ImageUrl;
                    dynamic.LabelText        = model.LabelText;
                    dynamic.RedirectUrl      = model.RedirectUrl;
                    homeDynamicSettingsService.UpdateHomeDynamic(dynamic, out message);
                }
                else
                {
                    HomeDynamicSettings dynamic = model.AsHomeDynamic();
                    dynamic.Userid       = UserContext.CurrentUser.UserId;
                    dynamic.DisplayOrder = homeDynamicSettingsService.GetMaxDisplayOrder(model.Type);
                    homeDynamicSettingsService.AddHomeDynamic(dynamic, out message);
                }
                return(Json(new StatusMessageData(StatusMessageType.Success, "保存成功")));
            }
            return(Json(new StatusMessageData(StatusMessageType.Error, "保存失败")));
        }