object top(string id) { if (string.IsNullOrEmpty(id)) { return new { code = -1, msg = "要置顶的文章ID不能为空" } } ; var site = (Site)jc["site"]; using (ILinqContext <Posts> cx = Posts.CreateContext()) { var post = (from q in cx where q.Id == id && q.SiteId == site.Id && q.IsDeleted == false select q).FirstOrDefault(); if (post == null) { return new { code = -2, msg = "要置顶的文章未找到,置顶失败" } } ; //设置当前栏目下置顶文章为不置顶,再更新当前文章为置顶状态 Posts.Where("CategoryId = {0}", post.CategoryId).Set("IsTop", false).Update(); post.IsTop = true; cx.SubmitChanges(); } return(new { code = 1, msg = "置顶成功" }); }
object delete_completely(string[] ids) { var site = (Site)jc["site"]; using (ILinqContext <Posts> cx = Posts.CreateContext()) { var query = (from q in cx where q.SiteId == site.Id && q.IsDeleted == true select q); if (ids.Length > 0) { query = (from q in cx where new List <string>(ids).Contains(q.Id) && q.SiteId == site.Id && q.IsDeleted == true select q); } var posts = query.ToList(); if (posts.Count == 0) { return new { code = -2, msg = "指定的文章未查询到,文章可能已经彻底删除或者未放置回收站" } } ; foreach (var item in posts) { cx.Remove(item); } cx.SubmitChanges(true); } return(new { code = 1, msg = "成功将指定的文章删除" }); }
object un_top(string id) { if (string.IsNullOrEmpty(id)) { return new { code = -1, msg = "要取消置顶的文章ID不能为空" } } ; var site = (Site)jc["site"]; using (ILinqContext <Posts> cx = Posts.CreateContext()) { var post = (from q in cx where q.Id == id && q.SiteId == site.Id && q.IsDeleted == false select q).FirstOrDefault(); if (post == null) { return new { code = -2, msg = "要取消置顶的文章未找到,取消置顶失败" } } ; post.IsTop = false; cx.SubmitChanges(); } return(new { code = 1, msg = "取消置顶成功" }); }
object delete(string userId) { var site = (Site)jc["site"]; using (ILinqContext <SiteUsers> cx = SiteUsers.CreateContext()) { var relation = (from q in cx where q.SiteId == site.Id && q.UserId == userId select q).FirstOrDefault(); if (relation == null) { return new { code = -1, msg = "指定的用户在该站点下不存在" } } ; if (relation.UserId == jc.UserName) { return new { code = -2, msg = "不能删除自己的账号" } } ; //删除栏目与用户的关系 CategoryUsers.Where("SiteId = {0}", site.Id).Where("UserId = {0}", relation.UserId).Delete(); //删除站点用户关系 cx.Remove(relation); cx.SubmitChanges(); } return(new { code = 1, msg = "删除成功" }); }
object reset(string userId) { var site = (Site)jc["site"]; var relation = (from q in SiteUsers.CreateContext() where q.SiteId == site.Id && q.UserId == userId select q).FirstOrDefault(); if (relation == null) { return new { code = -1, msg = "指定的用户在该站点下不存在" } } ; using (ILinqContext <User> cx = User.CreateContext()) { var user = User.Get(cx, relation.UserId); if (user == null) { return new { code = -2, msg = "指定的用户已不存在" } } ; //重置密码 user.UpdatePassword("111111"); cx.SubmitChanges(); } return(new { code = 1, msg = "重置成功" }); }
object delete_category_user(string id) { var site = (Site)jc["site"]; using (ILinqContext <CategoryUsers> cx = CategoryUsers.CreateContext()) { var relation = (from q in cx where q.SiteId == site.Id && q.Id == id select q).FirstOrDefault(); if (relation == null) { return new { code = -1, msg = "指定的栏目与用户的关系不存在" } } ; //TODO 之前创建的文章的作者如何处理? cx.Remove(relation); cx.SubmitChanges(); } return(new { code = 1, msg = "删除成功" }); } #endregion }
object delete(string[] ids, bool confirmed) { if (ids.Length == 0) { return new { code = -1, msg = "要删除的文章ID不能为空" } } ; var site = (Site)jc["site"]; using (ILinqContext <Posts> cx = Posts.CreateContext()) { var posts = (from q in cx where new List <string>(ids).Contains(q.Id) && q.SiteId == site.Id && q.IsDeleted == false select q).ToList(); if (posts.Count == 1 && !confirmed && posts[0].IsPublished) { return new { code = 2, msg = "文章已被发布,是否确认删除" } } ; foreach (var post in posts) { post.IsDeleted = true; } cx.SubmitChanges(); } return(new { code = 1, msg = "移至回收站成功" }); }
object delete(string id) { var site = (Site)jc["site"]; #region 校验用户对站点的权限 var relation = (from q in SiteUsers.CreateContext() where q.UserId == jc.UserName && q.SiteId == site.Id select q).FirstOrDefault(); //如果没有站点的管理权限 if (relation == null || relation.PermissionLevel != PermissionLevel.ADMIN) { return new { code = 403, msg = "没有权限访问" } } ; #endregion using (ILinqContext <Category> cx = Category.CreateContext()) { var category = (from q in cx where q.Id == id && q.SiteId == site.Id select q).FirstOrDefault(); if (category == null) { return new { code = -1, msg = "指定的栏目不存在,删除失败" } } ; if (Category.Where("ParentId = {0}", category.Id).Where("SiteId = {0}", site.Id).Count() > 0) { return new { code = -2, msg = "指定的栏目下存在子栏目,不能删除" } } ; cx.Remove(category); cx.SubmitChanges(); //更新父级是否有子集 Category.Where("Id = {0}", category.ParentId).Set("HasChildren", Category.Where("ParentId = {0}", category.ParentId).Count() > 0).Update(); } return(new { code = 1, msg = "删除成功" }); }
object update_audit(string[] ids, bool pass) { if (ids.Length == 0) { return new { code = -1, msg = "要审核的文章ID不能为空" } } ; var site = (Site)jc["site"]; using (ILinqContext <Posts> cx = Posts.CreateContext()) { var posts = (from q in cx where q.SiteId == site.Id && new List <string>(ids).Contains(q.Id) && q.Status == Status.PENDING select q).ToList(); if (posts.Count == 0) { return new { code = -2, msg = "指定的文章ID在当前站点下未找到,可能文章已经审核过,请刷新后尝试" } } ; foreach (var item in posts) { if (pass) { item.Status = Status.AUDIT_PASSED; item.IsPublished = true; item.DatePublished = DateTime.Now; item.PublishUserId = jc.UserName; } else { //TODO item.Status = Status.AUDIT_FAILD; item.IsPublished = false; } } cx.SubmitChanges(true); return(new { code = 1, msg = "审核成功", count = posts.Count }); } }
object delete(string id) { using (ILinqContext <Widget> cx = Widget.CreateContext()) { var widget = Widget.Get(cx, id); if (widget == null) { return new { code = -1, msg = "指定的挂件不存在,删除失败" } } ; cx.Remove(widget); cx.SubmitChanges(); } return(new { code = 1, msg = "删除成功" }); }
public static DictSchemas GetsByCategory(ILinqContext <DictSchema> cx, string siteid, string type, string category) { List <DictSchema> list = (from q in cx where q.SiteId == siteid && q.Type == type && q.Category == category orderby q.SortOrder ascending select q).ToList(); // re group List <DictSchema> result = list.FindAll(delegate(DictSchema s) { return(string.IsNullOrEmpty(s.ParentId)); }); foreach (DictSchema s in result) { regroup(s, list); } return(new DictSchemas(result)); }
object add_exist_user(string userId, string permission) { var user = User.Get(userId); if (user == null) { return new { code = -1, msg = "指定的用户不存在" } } ; var site = (Site)jc["site"]; using (ILinqContext <SiteUsers> cx = SiteUsers.CreateContext()) { var relation = (from q in cx where q.UserId == user.Id && q.SiteId == site.Id select q).FirstOrDefault(); if (relation != null) { return new { code = -2, msg = "指定的用户在站点下已经存在" } } ; relation = new SiteUsers(); relation.Id = StringUtil.UniqueId(); relation.SiteId = site.Id; relation.DateCreated = DateTime.Now; relation.UserId = user.Id; cx.Add(relation, true); relation.PermissionLevel = StringEnum <PermissionLevel> .SafeParse(permission); cx.SubmitChanges(); } return(new { code = 1, msg = "用户添加成功" }); }
object unpublish(string[] ids) { if (ids.Length == 0) { return new { code = -1, msg = "要取消发布的文章不能为空" } } ; var site = (Site)jc["site"]; using (ILinqContext <Posts> cx = Posts.CreateContext()) { var posts = (from q in cx where new List <string>(ids).Contains(q.Id) && q.SiteId == site.Id && q.IsPublished == true select q).ToList(); if (posts.Count == 0) { return new { code = -2, msg = "指定的文章未查询到" } } ; foreach (var item in posts) { item.Status = Status.DRAFT; item.IsPublished = false; item.PublishUserId = string.Empty; item.DatePublished = DateTime.MinValue; } cx.SubmitChanges(true); } return(new { code = 1, msg = "发布成功" }); }
object delete(string id, bool confirmed) { using (ILinqContext <Site> cx = Site.CreateContext()) { var site = Site.Get(cx, id); if (site == null) { return new { code = -1, msg = "指定的站点不存在,删除失败" } } ; if (!confirmed && Widget.Where("SiteId = {0}", site.Id).Count() != 0) { return new { code = -2, msg = "指定的站点下有挂件内容,若确认删除则挂件内容也一并删除,是否确认删除" } } ; Widget.Where("SiteId = {0}", site.Id).Delete(); cx.Remove(site); cx.SubmitChanges(); } return(new { code = 1, msg = "删除成功" }); } #endregion #region 制站点 #endregion } }
object publish(string[] ids) { if (ids.Length == 0) { return new { code = -1, msg = "要发布的文章不能为空" } } ; var site = (Site)jc["site"]; using (ILinqContext <Posts> cx = Posts.CreateContext()) { var posts = (from q in cx where new List <string>(ids).Contains(q.Id) && q.SiteId == site.Id && q.Status == Status.DRAFT && q.IsDeleted == false select q).ToList(); if (posts.Count == 0) { return new { code = -2, msg = "指定的未发布文章未查询到,文章可能已经被删除或者已发布" } } ; foreach (var item in posts) { item.Status = Status.NEED_NOT_AUDIT; item.IsPublished = true; item.PublishUserId = jc.UserName; item.DatePublished = DateTime.Now; } cx.SubmitChanges(true); } return(new { code = 1, msg = "发布成功" }); }
object restore(string[] ids) { if (ids.Length == 0) { return new { code = -1, msg = "要恢复的文章ID不能为空" } } ; var site = (Site)jc["site"]; using (ILinqContext <Posts> cx = Posts.CreateContext()) { var posts = (from q in cx where q.SiteId == site.Id && new List <string>(ids).Contains(q.Id) select q).ToList(); if (posts.Count == 0) { return new { code = -2, msg = "指定的文章ID在当前站点下未找到,请刷新后尝试" } } ; foreach (var item in posts) { item.IsDeleted = false; } cx.SubmitChanges(true); return(new { code = 1, msg = "恢复成功", count = posts.Count }); } } #endregion } }
object save(string id, string title, string url, string parentId, int sortOrder, bool needLogin2Read, bool showInMenu) { #region 校验参数 if (string.IsNullOrWhiteSpace(title)) { return new { code = -1, msg = "栏目名称不能为空" } } ; #region 校验父级栏目是否存在 Category parent = null; if (!string.IsNullOrWhiteSpace(parentId)) { parent = Category.Get(parentId); if (parent == null) { return new { code = -2, msg = "指定的父级栏目不存在" } } ; } #endregion title = title.Trim(); if (string.IsNullOrWhiteSpace(url)) { url = Kiss.Utils.Pinyin.GetInitials(title); } if (title.Length > 50) { return new { code = -3, msg = "栏目的标题长度不能大于50个字符" } } ; if (url.Length > 20) { return new { code = -4, msg = "栏目的URL长度不能大于50个字符" } } ; var site = (Site)jc["site"]; #region 校验用户对站点的权限 var relation = (from q in SiteUsers.CreateContext() where q.UserId == jc.UserName && q.SiteId == site.Id select q).FirstOrDefault(); //如果没有站点的管理权限 if (relation == null || relation.PermissionLevel != PermissionLevel.ADMIN) { return new { code = 403, msg = "没有权限访问" } } ; #endregion #endregion using (ILinqContext <Category> cx = Category.CreateContext()) using (ILinqContext <Category> cx_children = Category.CreateContext()) { var category = (from q in cx where q.Id == id && q.SiteId == site.Id select q).FirstOrDefault(); if (category == null) { if (Category.Where("Title = {0}", title).Where("SiteId = {0}", site.Id).Where("ParentId = {0}", (parent == null ? string.Empty : parent.Id)).Count() != 0) { return new { code = -5, msg = "已经存在相同的栏目名称,请更换其他栏目名称" } } ; if (Category.Where("Url = {0}", url).Where("SiteId = {0}", site.Id).Where("ParentId = {0}", (parent == null ? string.Empty : parent.Id)).Count() != 0) { return new { code = -6, msg = "已经存在相同的栏目URL,请更换其他栏目URL" } } ; } else { if (category.Title != title && Category.Where("Title = {0}", title).Where("SiteId = {0}", site.Id).Where("ParentId = {0}", (parent == null ? string.Empty : parent.Id)).Count() != 0) { return new { code = -5, msg = "已经存在相同的栏目名称,请更换其他栏目名称" } } ; if (category.Url != url && Category.Where("Url = {0}", url).Where("SiteId = {0}", site.Id).Where("ParentId = {0}", (parent == null ? string.Empty : parent.Id)).Count() != 0) { return new { code = -6, msg = "已经存在相同的栏目URL,请更换其他栏目URL" } } ; } if (category == null) { category = new Category(); category.Id = StringUtil.UniqueId(); category.DateCreated = DateTime.Now; category.UserId = jc.UserName; category.SiteId = site.Id; cx.Add(category, true); } category.Title = title; category.Url = url; category.ParentId = parent == null ? string.Empty : parent.Id; category.SortOrder = sortOrder; category.NeedLogin2Read = needLogin2Read; category.ShowInMenu = showInMenu; #region 子集栏目信息变更 category.NodePath = parent == null ? category.Id : string.Format("{0}/{1}", parent.NodePath, category.Id); var children = (from q in cx_children where q.NodePath.StartsWith(string.Format("{0}/", category.NodePath)) select q).ToList(); foreach (var item in children) { item.NodePath = string.Format("{0}/{1}", category.NodePath, item.Id); } category.HasChildren = children.Count > 0; #endregion cx.SubmitChanges(); cx_children.SubmitChanges(true); //将父级栏目更新为有子集 if (parent != null) { Category.Where("Id = {0}", parent.Id).Set("HasChildren", 1).Update(); } } return(new { code = 1, msg = "保存成功" }); }
public static List <T> Gets(ILinqContext <T> context, t[] ids) { return(Repository.Gets(context, ids)); }
object add_category_user(string userId, bool all, string[] categoryIds) { #region 校验参数 var site = (Site)jc["site"]; var user = User.Get(userId); if (user == null) { return new { code = -1, msg = "指定的用户不存在" } } ; if (!all && categoryIds.Length == 0) { return new { code = -2, msg = "要添加的栏目不能为空" } } ; #region 处理栏目 List <Category> categories = null; IQueryable <Category> query = null; if (all) { query = (from q in Category.CreateContext() where q.SiteId == site.Id select q); } else { query = (from q in Category.CreateContext() where q.SiteId == site.Id && new List <string>(categoryIds).Contains(q.Id) select q); } categories = query.ToList(); #endregion if (categories.Count == 0) { return new { code = -3, msg = "指定的栏目不存在" } } ; #endregion using (ILinqContext <CategoryUsers> cx = CategoryUsers.CreateContext()) { var relations = (from q in CategoryUsers.CreateContext() where q.SiteId == site.Id && q.UserId == user.Id select q).ToList(); foreach (var item in categories) { //若存在,则不再增加 if (relations.FirstOrDefault(a => { return(a.CategoryId == item.Id); }) != null) { continue; } var relation = new CategoryUsers(); relation.Id = StringUtil.UniqueId(); relation.CategoryId = item.Id; relation.UserId = user.Id; relation.SiteId = site.Id; relation.DateCreated = DateTime.Now; cx.Add(relation, true); } cx.SubmitChanges(true); } return(new { code = 1, msg = "保存成功" }); }
public static List <T> Gets(ILinqContext <T> context, string commaDelimitedIds) { return(Repository.Gets(context, commaDelimitedIds)); }
public static List <T> GetsAll(ILinqContext <T> context) { return(Repository.GetsAll(context)); }
object save(string userId, string userName, string displayName, string mobile, string email, string permission) { #region 校验数据 var site = (Site)jc["site"]; if (string.IsNullOrWhiteSpace(userName)) { return new { code = -1, msg = "用户名不能为空" } } ; if (string.IsNullOrWhiteSpace(displayName)) { return new { code = -2, msg = "显示名称不能为空" } } ; userName = userName.Trim(); displayName = displayName.Trim(); mobile = string.IsNullOrWhiteSpace(mobile) ? string.Empty : mobile.Trim(); email = string.IsNullOrWhiteSpace(email) ? string.Empty : email.Trim(); if (userName.Length > 50) { return new { code = -3, msg = "用户名字符不能超过50" } } ; if (displayName.Length > 50) { return new { code = -4, msg = "显示名字符符不能超过50" } } ; if (!Regex.IsMatch(userName, "^[a-zA-Z0-9_]+$")) { return new { code = -5, msg = "用户名只能是 英文/数字/下划线 组成" } } ; #endregion using (ILinqContext <User> cx = User.CreateContext()) using (ILinqContext <SiteUsers> cx_relation = SiteUsers.CreateContext()) { #region 构造用户信息 User user = User.Get(cx, userId); if (user == null) { if (User.Where("UserName = {0}", userName).Count() > 0) { return new { code = -6, msg = "指定的用户名已经存在,请更换其他用户名" } } ; user = new User(); user.Id = StringUtil.UniqueId(); user.OrgId = "org"; user.DateCreate = DateTime.Now; user.IsValid = true; user.DateLastVisit = DateTime.Now; user.UserName = userName; //update password user.UpdatePassword("111111"); DictSchema schema = DictSchema.GetByName("users", "config"); if (schema != null && schema["first_login_resetpwd"] != null && schema["first_login_resetpwd"].ToBoolean()) { user["needmodifyPwd"] = true.ToString(); } cx.Add(user, true); } user.DisplayName = displayName; user.Mobile = mobile; user.Email = email; #endregion #region 构造站点用户关系数据 var relation = (from q in cx_relation where q.SiteId == site.Id && q.UserId == user.Id select q).FirstOrDefault(); if (relation == null) { relation = new SiteUsers(); relation.Id = StringUtil.UniqueId(); relation.SiteId = site.Id; relation.DateCreated = DateTime.Now; relation.UserId = user.Id; cx_relation.Add(relation, true); } relation.PermissionLevel = StringEnum <PermissionLevel> .SafeParse(permission); #endregion cx.SubmitChanges(); cx_relation.SubmitChanges(); } return(new { code = 1, msg = "用户添加成功" }); }
public static T Get(ILinqContext <T> context, t id) { return(Repository.Get(context, id)); }
object import() { #region 参数验证 if (jc.Context.Request.Files.Count == 0) { return new { code = -1, msg = "请选择要上传的JSON数据" } } ; var file = jc.Context.Request.Files[0]; if (!file.FileName.EndsWith(".json")) { return new { code = -2, msg = "必须上传一个JSON文件" } } ; var categories = new JavaScriptSerializer().Deserialize <List <Category> >(Encoding.UTF8.GetString(file.InputStream.ToBytes())); if (categories.Count == 0) { return new { code = -3, msg = "指定的JSON文件中没有数据" } } ; //根据层级排序,按层级由浅至深来处理 categories = (from q in categories orderby StringUtil.Split(q.NodePath, "/", true, true).Count() select q).ToList(); #endregion var site = (Site)jc["site"]; using (ILinqContext <Category> cx = Category.CreateContext()) { foreach (var item in categories) { var category = new Category(); category.Id = item.Id; category.DateCreated = DateTime.Now; category.UserId = jc.UserName; category.SiteId = site.Id; category.Title = item.Title; category.Url = item.Url; category.ParentId = item.ParentId; category.SortOrder = item.SortOrder; category.NeedLogin2Read = item.NeedLogin2Read; category.ShowInMenu = item.ShowInMenu; #region 处理子集数据 category.HasChildren = item.HasChildren; category.NodePath = item.NodePath.Replace(item.Id, category.Id); if (category.HasChildren) { var children = categories.Where(a => { return(a.ParentId == item.Id); }).ToList(); foreach (var child in children) { child.ParentId = category.Id; child.NodePath = child.NodePath.Replace(item.NodePath, category.NodePath); } } #endregion #region 扩展字段 foreach (string key in item.ExtAttrs.Keys) { category[key] = item.ExtAttrs[key]; } category.SerializeExtAttrs(); #endregion cx.Add(category, true); } cx.SubmitChanges(true); } return(new { code = 1, msg = "导入成功" }); } #endregion } }
object save(string id, string title, string subTitle, string content, string summary, string categoryId, int viewCount, int sortOrder, string imageUrl, string dateCreated, bool isTop, bool publish) { #region 校验参数 if (string.IsNullOrWhiteSpace(title)) { return new { code = -1, msg = "文章标题不能为空" } } ; if (string.IsNullOrWhiteSpace(content)) { return new { code = -2, msg = "文章内容不能为空" } } ; title = title.Trim(); content = content.Trim(); if (!string.IsNullOrWhiteSpace(subTitle)) { subTitle = subTitle.Trim(); } if (title.Length > 50) { return new { code = -5, msg = "文章标题的长度不能超过50个字符" } } ; if (!string.IsNullOrWhiteSpace(subTitle) && subTitle.Length > 100) { return new { code = -6, msg = "文章副标题的长度不能超过100个字符" } } ; if (viewCount < 0) { return new { code = -7, msg = "文章的查看次数不能设置为小于0" } } ; //过滤文章内容中是否有脚本 content = Regex.Replace(content, @"<script[\s\S]+</script *>", string.Empty, RegexOptions.IgnoreCase); content = Regex.Replace(content, @"<[^>]*(src|href)[\s\S]*=[\s\S]*script:[^>]*>", string.Empty, RegexOptions.IgnoreCase); //获取纯文本 var text = StringUtil.TrimHtml(content); //如果摘要为空,则默认提供一份100字的摘要 if (string.IsNullOrWhiteSpace(summary)) { summary = StringUtil.Trim(text, 100); } if (summary.Length > 2000) { return new { code = -4, msg = "摘要的长度不能超过2000个字符" } } ; #region 获取文章所属分类 Category category = category = Category.Get(categoryId); if (category == null) { return new { code = -3, msg = "指定的分类不存在" } } ; #endregion #endregion var site = (Site)jc["site"]; using (ILinqContext <Posts> cx = Posts.CreateContext()) { var post = (from q in cx where q.Id == id && q.SiteId == site.Id select q).FirstOrDefault(); if (post == null) { post = new Posts(); post.Id = StringUtil.UniqueId(); post.DateCreated = !string.IsNullOrEmpty(dateCreated) ? dateCreated.ToDateTime() : DateTime.Now; post.UserId = jc.UserName; post.SiteId = site.Id; post.DisplayName = jc.User.Info.DisplayName; cx.Add(post, true); } post.Title = title; post.SubTitle = title; post.Content = content; post.Text = text; post.Summary = summary; post.CategoryId = category.Id; post.SortOrder = sortOrder; post.ViewCount = viewCount == 0 ? post.ViewCount : viewCount; //同一个栏目下只能有一个文章被置顶 if (isTop) { Posts.Where("CategoryId = {0}", post.CategoryId).Set("IsTop", false).Update(); } post.IsTop = isTop; #region 处理图片 post.ImageUrl = imageUrl ?? string.Empty; if (string.IsNullOrEmpty(post.ImageUrl)) { Match match = new Regex(@"(?i)<img\b(?:(?!src=).)*src=(['""]?)(?<src>[^'""\s>]+)\1[^>]*>").Match(post.Content); if (match.Success) { post.ImageUrl = match.Groups["src"].Value; } } #endregion #region 审核 if (publish && (post.Status == Status.DRAFT || post.Status == Status.AUDIT_FAILD)) { var relation = (SiteUsers)jc["relation"]; if (relation.PermissionLevel == PermissionLevel.EDIT && site.NeedAuditPost) { post.Status = Status.PENDING; } else { //站点管理员 或者 站点不需要审核文章 则直接发布 post.Status = Status.NEED_NOT_AUDIT; post.IsPublished = true; post.PublishUserId = jc.UserName; post.DatePublished = DateTime.Now; } } #endregion //OnBeforeSave post.OnBeforeSave(new Posts.BeforeSaveEventArgs { Properties = jc.Params }); cx.SubmitChanges(); //OnAfterSave post.OnAfterSave(new Posts.AfterSaveEventArgs()); return(new { code = 1, id = post.Id, msg = "保存成功", is_pending = post.Status == Status.PENDING }); } }
object save(string id, string name, string title, string containerId, int sortOrder, string props) { #region 参数校验 if (string.IsNullOrEmpty(name)) { return new { code = -1, msg = "挂件名称不能为空" } } ; if (string.IsNullOrEmpty(title)) { return new { code = -2, msg = "挂件显示名称不能为空" } } ; if (string.IsNullOrEmpty(containerId)) { return new { code = -5, msg = "挂件占位ID不能为空" } } ; name = name.Trim(); title = title.Trim(); containerId = containerId.Trim(); if (name.Length > 20) { return new { code = -3, msg = "挂件名称长度不能超过20个字符" } } ; if (title.Length > 50) { return new { code = -4, msg = "挂件显示名称长度不能超过50个字符" } } ; if (containerId.Length > 50) { return new { code = -6, msg = "挂件占位ID长度不能超过50个字符" } } ; #endregion var site = (Site)jc["site"]; using (ILinqContext <Widget> cx = Widget.CreateContext()) { var widget = Widget.Get(cx, id); if (widget == null) { widget = new Widget(); widget.Id = StringUtil.UniqueId(); widget.DateCreated = DateTime.Now; widget.UserId = jc.UserName; widget.SiteId = site.Id; cx.Add(widget, true); } widget.Name = name; widget.Title = title; widget.ContainerId = containerId; widget.SortOrder = sortOrder; #region 处理扩展字段信息 if (!string.IsNullOrEmpty(props)) { try { var extends = new Kiss.Json.JavaScriptSerializer().Deserialize <Dictionary <string, string> >(props); foreach (var item in extends.Keys) { widget[item] = extends[item]; } //序列化扩展字段 widget.SerializeExtAttrs(); } catch (Exception ex) { logger.Error(ExceptionUtil.WriteException(ex)); return(new { code = -3, msg = "扩展字段格式不正确" }); } } #endregion cx.SubmitChanges(); } return(new { code = 1, msg = "保存成功" }); }
object save(string id, string title, string domain, string keyWords, string description, string theme, int sortOrder, int needAuditPost) { #region 校验参数 & 校验参数的长度 if (string.IsNullOrWhiteSpace(title)) { return new { code = -1, msg = "站点名称不能为空" } } ; if (string.IsNullOrWhiteSpace(domain)) { return new { code = -2, msg = "站点域名不能为空" } } ; Regex regex = new Regex(@"^[\w\.]+$"); if (!regex.IsMatch(domain)) { return new { code = -3, msg = "站点域名支持大小写英文以及点号,下划线" } } ; title = title.Trim(); domain = domain.Trim(); keyWords = string.IsNullOrWhiteSpace(keyWords) ? string.Empty : keyWords.Trim(); description = string.IsNullOrWhiteSpace(description) ? string.Empty : description.Trim(); theme = string.IsNullOrEmpty(theme) ? "default" : theme.Trim(); if (title.Length > 100) { return new { code = -4, msg = "站点标题不能超过100个字符" } } ; if (domain.Length > 100) { return new { code = -5, msg = "站点域名不能超过100个字符" } } ; if (keyWords.Length > 500) { return new { code = -6, msg = "站点关键字不能超过500个字符" } } ; if (description.Length > 1000) { return new { code = -7, msg = "站点描述不能超过1000个字符" } } ; if (theme.Length > 20) { return new { code = -8, msg = "站点的主题名不能超过20个字符" } } ; #endregion #region 校验LOGO文件 string logo = string.Empty; try { if (jc.Context.Request.Files.Count > 0) { var file = jc.Context.Request.Files["logo"]; var extension = Path.GetExtension(file.FileName); if (string.IsNullOrEmpty(extension)) { return new { code = -9, msg = "LOGO文件只能是 JPG、GIF、PNG 图片文件" } } ; extension = extension.Substring(1).ToLowerInvariant(); if (extension != "jpg" && extension != "gif" && extension != "png") { return new { code = -9, msg = "LOGO文件只能是 JPG、GIF、PNG 图片文件" } } ; if (file.InputStream.Length > 1024 * 1024) { return new { code = -10, msg = "LOGO文件的大小不能超过 1MB" } } ; logo = Convert.ToBase64String(file.InputStream.ToBytes()); //存储为 BASE64 格式的 logo = string.Format("data:image/{0};base64,{1}", extension, logo); } } catch (Exception ex) { logger.Error(ExceptionUtil.WriteException(ex)); return(new { code = -11, msg = "LOGO 存储失败,请联系管理员" }); } #endregion #region 校验ICO文件 string ico = string.Empty; try { if (jc.Context.Request.Files.Count > 0) { var file = jc.Context.Request.Files["ico"]; var extension = Path.GetExtension(file.FileName); if (string.IsNullOrEmpty(extension)) { return new { code = -12, msg = "ICO文件只能是 ICO 图片文件" } } ; extension = extension.Substring(1).ToLowerInvariant(); if (extension != "ico") { return new { code = -12, msg = "ICO文件只能是 ICO 图片文件" } } ; if (file.InputStream.Length > 1024 * 1024) { return new { code = -13, msg = "ICO文件的大小不能超过 1MB" } } ; ico = Convert.ToBase64String(file.InputStream.ToBytes()); //存储为 BASE64 格式的 ico = string.Format("data:image/x-icon;base64,{1}", extension, ico); } } catch (Exception ex) { logger.Error(ExceptionUtil.WriteException(ex)); return(new { code = -14, msg = "ICO 图标存储失败,请联系管理员" }); } #endregion using (ILinqContext <Site> cx = Site.CreateContext()) using (ILinqContext <SiteUsers> cx_relation = SiteUsers.CreateContext()) { var site = Site.Get(cx, id); #region 校验站点数据是否存在相同的 if (site == null) { if (Site.Where("Title = {0}", title).Count() != 0) { return new { code = -15, msg = "已经存在相同的站点名称,请更换其他站点名称" } } ; if (Site.Where("Domain = {0}", domain).Count() != 0) { return new { code = -16, msg = "已经存在相同的站点域名,请更换其他站点名称" } } ; } else { if (site.Title != title && Site.Where("Title = {0}", title).Count() != 0) { return new { code = -15, msg = "已经存在相同的站点名称,请更换其他站点名称" } } ; if (site.Domain != domain && Site.Where("Domain = {0}", domain).Count() != 0) { return new { code = -16, msg = "已经存在相同的站点域名,请更换其他站点名称" } } ; } #endregion if (site == null) { site = new Site(); site.Id = StringUtil.UniqueId(); site.DateCreated = DateTime.Now; site.UserId = jc.UserName; cx.Add(site, true); #region 将当前用户加入该站点 var relation = new SiteUsers(); relation.Id = StringUtil.UniqueId(); relation.DateCreated = DateTime.Now; relation.SiteId = site.Id; relation.UserId = jc.UserName; relation.PermissionLevel = PermissionLevel.ADMIN; cx_relation.Add(relation, true); #endregion } site.Title = title; site.Domain = domain; site.KeyWords = keyWords; site.Description = description; site.Theme = theme; site.SortOrder = sortOrder; site.Logo = logo; site.ICO = ico; site.NeedAuditPost = needAuditPost.ToBoolean(); cx.SubmitChanges(); cx_relation.SubmitChanges(); } return(new { code = 1, msg = "保存成功" }); }
object import() { #region 参数验证 if (jc.Context.Request.Files.Count == 0) { return new { code = -1, msg = "请选择要上传的JSON数据" } } ; var file = jc.Context.Request.Files[0]; if (!file.FileName.EndsWith(".json")) { return new { code = -2, msg = "必须上传一个JSON文件" } } ; var jsons = new JavaScriptSerializer().Deserialize <List <Widget> >(Encoding.UTF8.GetString(file.InputStream.ToBytes())); if (jsons.Count == 0) { return new { code = -3, msg = "指定的JSON文件中没有数据" } } ; #endregion var site = (Site)jc["site"]; using (ILinqContext <Widget> cx = Widget.CreateContext()) { foreach (var item in jsons) { var widget = new Widget(); widget.Id = StringUtil.UniqueId(); widget.DateCreated = DateTime.Now; widget.ContainerId = item.ContainerId; widget.Name = item.Name; widget.SiteId = site.Id; widget.SortOrder = item.SortOrder; widget.Title = item.Title; widget.UserId = jc.UserName; #region 扩展字段 foreach (string key in item.ExtAttrs.Keys) { widget[key] = item.ExtAttrs[key]; } widget.SerializeExtAttrs(); #endregion cx.Add(widget, true); } cx.SubmitChanges(true); } return(new { code = 1, msg = "导入成功" }); } #endregion } }
public static List <T> ImportFromXml(XmlDocument xml) { Type t = typeof(T); List <PropertyInfo> props = new List <PropertyInfo>(t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)); List <T> list = new List <T>(); XmlNode node = xml.DocumentElement.SelectSingleNode("//" + t.FullName); if (node == null) { return(list); } foreach (XmlNode n in node.ChildNodes) { T obj = Activator.CreateInstance <T>(); foreach (XmlAttribute attr in n.Attributes) { PropertyInfo pi = props.Find(p => { return(p.Name.Equals(attr.Name, StringComparison.InvariantCultureIgnoreCase)); }); if (pi == null || !pi.CanWrite) { continue; } pi.SetValue(obj, TypeConvertUtil.ConvertTo(attr.Value, pi.PropertyType), null); } list.Add(obj); } ILinqContext <T> context = CreateContext(); // remove old items // List <t> ids = new List <t>(); foreach (var item in list) { ids.Add(item.Id); } List <T> old_list = Gets(context, StringUtil.CollectionToCommaDelimitedString(ids)); foreach (var item in old_list) { if (list.Find((T obj) => { return(item.Id.Equals(obj.Id)); }) != null) { context.Remove(item); } } // add new items // foreach (var item in list) { context.Add(item, true); } context.SubmitChanges(true); return(list); }