public async Task <ApiResult> Update([FromBody] AdverViewModel avm) { var res = new ApiResult(); if (!string.IsNullOrWhiteSpace(avm.Title) && avm.TypeID > 0) { try { Advert m = null; m = db.GetId(avm.ID); if (m == null) { m = new Advert(); } m.Title = avm.Title; m.TypeID = avm.TypeID; m.Pic = avm.Pic; m.BackgroundColor = avm.BackgroundColor; m.StartDate = C.DateTimes(avm.StartDate); m.EndDate = C.DateTimes(avm.EndDate); m.IsEnable = avm.IsEnable; m.Url = avm.Url; m.Sorting = avm.Sorting; if (res.success) { res.statusCode = (int)ApiEnum.Status; } // 如有图片上传则保存到本地 if (avm.Pic.Contains("base64")) { string path = "UploadFiles/advert/"; string path2 = Utility.HostAddress + "advert\\"; m.Pic = ImagesUtility.Base64StringToFile(avm.Pic, path2, DateTime.Now.ToString("yyyyMMddHHmmssfff")); if (m.Pic != "") { m.Pic = path + m.Pic; } } res.success = db.Update(m) > 0; } catch (Exception ex) { res.statusCode = (int)ApiEnum.Error; res.msg = ApiEnum.Error.GetEnumText() + ex.Message; } } return(await Task.Run(() => res)); }
public ApiResult <string> Add([FromBody] ArticlePostViewModel vm) { // 以接口的形式返回数据 var res = new ApiResult <string>() { statusCode = (int)ApiEnum.ParameterError }; if (!string.IsNullOrWhiteSpace(vm.Title) && !string.IsNullOrWhiteSpace(vm.ArticleMenuIDs)) { Article m = new Article(); m.Title = vm.Title; m.AddDate = C.DateTimes(vm.AddDate); m.ImgUrl = vm.ImgUrl; m.Type = vm.Type; m.FileUrl = vm.FileUrl; m.Keyword = vm.Keyword; m.Sorting = vm.Sorting; m.IsTop = vm.IsTop; m.State = vm.State; m.Synopsis = vm.Synopsis; m.Contents = vm.Contents; m.Source = vm.Source; m.PageView = vm.PageView; m.Author = vm.Author; m.AddDate = C.DateTimes(m.AddDate); m.FileSize = m.FileSize + ""; m.FileFormat = m.FileFormat + ""; try { if (vm.ImgUrl != null && !string.IsNullOrWhiteSpace(vm.ImgUrl)) { //如有图片上传则保存到本地 if (vm.ImgUrl.Contains("base64")) { string path = "UploadFiles/article/"; string path2 = Utility.HostAddress + "article\\"; m.ImgUrl = ImagesUtility.Base64StringToFile(vm.ImgUrl, path2, DateTime.Now.ToString("yyyyMMddHHmmssfff")); if (m.ImgUrl != "") { m.ImgUrl = path + m.ImgUrl; } } } //if (vm.FileUrl != null && !string.IsNullOrWhiteSpace(vm.FileUrl)) //{ // //如有图片上传则保存到本地 // if (vm.FileUrl.Contains("base64")) // { // string path = "UploadFiles/article/"; // string path2 = Utility.HostAddress + "article\\"; // m.FileUrl = ImagesUtility.Base64StringToFile(vm.FileUrl, path2, DateTime.Now.ToString("yyyyMMddHHmmssfff")); // if (m.FileUrl != "") // m.FileUrl = path + m.FileUrl; // } //} m.ID = db.Insert(m); res.success = m.ID > 0; // 处理文章栏目 if (res.success) { string[] array = vm.ArticleMenuIDs.Split(','); if (array.Length > 0) { List <ArticleMenu_Article> list = new List <ArticleMenu_Article>(); foreach (string item in array) { list.Add(new ArticleMenu_Article { ArticleID = m.ID, ArticleMenuID = C.Int(item) }); } if (articleMenu_ArticleService.Add(list) > 0) { res.msg = "添加成功"; } else { res.msg = "栏目添加失败"; res.success = false; res.statusCode = (int)ApiEnum.Status; } } } else { res.msg = "文章添加失败"; res.success = false; res.statusCode = (int)ApiEnum.Status; } } catch (Exception ex) { res.statusCode = (int)ApiEnum.Error; res.msg = ApiEnum.Error.GetEnumText() + ex.Message; } } else { res.msg = "参数丢失"; } return(res); }