public bool Insert(int rootCategoryId, ArticleContentDataContract article) { string sql = @"INSERT INTO SA_ArticleContent{0}(CategoryId,CategoryIdList,Title,Author,Description,ArticleContent,PostTime,UpdateTime, WeekHitCount,MonthHitCount,HitCount,HitTime,FlowerCount,EggCount,CommentCount,CollectCount,IsCollect,WarnCount,UserKeyId,Isverify,IsHighPriority) VALUES(@CategoryId,@CategoryIdList,@Title,@Author,@Description,@ArticleContent,@PostTime,@UpdateTime, @WeekHitCount,@MonthHitCount,@HitCount,@HitTime,@FlowerCount,@EggCount,@CommentCount,@CollectCount,@IsCollect,@WarnCount,@UserKeyId,@Isverify,@IsHighPriority); SELECT @@IDENTITY;" ; sql = string.Format(sql, rootCategoryId); var _parm = new SqlParameter[] { new SqlParameter("@CategoryId", article.CategoryId), new SqlParameter("@CategoryIdList", article.CategoryIdList), new SqlParameter("@Title", article.Title ?? string.Empty), new SqlParameter("@Author", article.Author ?? string.Empty), new SqlParameter("@Description", article.Description ?? string.Empty), new SqlParameter("@ArticleContent", article.ArticleContent ?? string.Empty), new SqlParameter("@PostTime", article.PostTime), new SqlParameter("@UpdateTime", article.UpdateTime), new SqlParameter("@WeekHitCount", article.WeekHitCount), new SqlParameter("@MonthHitCount", article.MonthHitCount), new SqlParameter("@HitCount", article.HitCount), new SqlParameter("@HitTime", article.HitTime), new SqlParameter("@FlowerCount", article.FlowerCount), new SqlParameter("@EggCount", article.EggCount), new SqlParameter("@CommentCount", article.CommentCount), new SqlParameter("@CollectCount", article.CollectCount), new SqlParameter("@IsCollect", article.IsCollect), new SqlParameter("@WarnCount", article.WarnCount), new SqlParameter("@UserKeyId", article.UserKeyId), new SqlParameter("@Isverify", article.Isverify), new SqlParameter("@IsHighPriority", article.IsHighPriority) }; article.ArticleID = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.GetConnSting(), CommandType.Text, sql, _parm)); return(article.ArticleID > 0); }
public ReturnResult <long> PublishArticle(ArticleContentDataContract article) { if (null == article) { return(new ReturnResult <long>(101, 0, "数据错误")); } if (string.IsNullOrWhiteSpace(article.Title)) { return(new ReturnResult <long>(104, 0, "帖子标题不允许为空")); } if (string.IsNullOrWhiteSpace(article.ArticleContent)) { return(new ReturnResult <long>(102, 0, "帖子内容不允许为空")); } var parentCategoryList = articleCategoryService.GetParentCategoryList(article.CategoryId); if (!parentCategoryList.Any()) { return(new ReturnResult <long>(103, 0, "文章分类ID错误,未找到指定的ID")); } int rootCategoryId = parentCategoryList.First(); rootCategoryId = rootCategoryId == 0 ? article.CategoryId : rootCategoryId; parentCategoryList.Add(article.CategoryId); article.CategoryIdList = string.Join("|", parentCategoryList); article.PostTime = DateTime.Now; article.UpdateTime = DateTime.Now; article.HitTime = DateTime.Now; article.Description = article.ArticleContent.RemoveHtmlTag().CutString(480); if (article.CategoryId == 390) { article.Title = "[视频]"; } else if (article.CategoryId == 388) { article.Title = "[图片]"; } else { article.Title = string.IsNullOrEmpty(article.Description) ? article.Author : article.Description.CutString(20); } article.Title = article.Title ?? ""; if (ArticleContentRepository.Instance.Insert(rootCategoryId, article)) { return(new ReturnResult <long>(article.ArticleID)); } else { return(new ReturnResult <long>(102, 0, "发布失败.")); } }
public JsonResult Publish(OauthToken token, int categoryId, string title, string content) { try { content = Server.UrlDecode(content); if (string.IsNullOrWhiteSpace(content)) { return(Json(new ReturnResult <string>(101, null, "参数content不能为空"))); } #region 图片解析与上传 try { Regex re = new Regex("<img( ||.*?)src=('|\"|)([^\"|^\']+)('|\"|>| )", RegexOptions.IgnoreCase); MatchCollection matches = re.Matches(content); foreach (Match mh in matches) { string base64Str = mh.Groups[3].Value;//src里面的路径 if (!string.IsNullOrWhiteSpace(base64Str) && base64Str.StartsWith("data:image/")) { string imageData = base64Str.Split(',')[1]; var fileName = string.Format("{0}.jpg", Guid.NewGuid().ToString()); var filePath = string.Format("Upload/images/{0}", Guid.NewGuid().ToString("N").CutString(2).ToLower()); var serverPath = Server.MapPath("~/" + filePath); byte[] imageBytes = Convert.FromBase64String(imageData); if (!System.IO.Directory.Exists(serverPath)) { System.IO.Directory.CreateDirectory(serverPath); } using (MemoryStream ms = new MemoryStream(imageBytes)) { Bitmap bmp = new Bitmap(ms); bmp.Save(serverPath + "/" + fileName); } content = content.Replace(base64Str, string.Format("http://{0}/{1}/{2}", Request.Url.Authority, filePath, fileName)); } } } catch (Exception e) { return(Json(new ReturnResult <string>(102, null, "数据解析错误," + e.ToString()))); } #endregion ArticleContentDataContract aritcle = new ArticleContentDataContract(); aritcle.CategoryId = categoryId; aritcle.Title = title; aritcle.UserKeyId = token.UserKeyId; aritcle.Author = token.User.NickName ?? token.User.UserName; aritcle.ArticleContent = content; return(Json(articleContentService.PublishArticle(aritcle))); } catch (Exception e) { return(Json(new ReturnResult <string>(103, null, "数据解析错误," + e.ToString()))); } }