public static object GetChannels(IRequest request)
        {
            if (!request.IsUserLoggin)
            {
                throw new Exception("用户认证失败");
            }

            var nodes = new List <object>();

            try
            {
                var siteId = request.GetPostInt("siteId");

                var groupInfo = GroupDao.GetGroupInfo(GroupUserDao.GetGroupId(request.UserName)) ?? Utils.GetDefaultGroupInfo(Utils.GetConfigInfo());
                var adminName = groupInfo.WritingAdmin;

                var nodeIdList = Main.Instance.ChannelApi.GetChannelIdList(siteId, adminName);

                foreach (var nodeId in nodeIdList)
                {
                    var channelInfo = Main.Instance.ChannelApi.GetChannelInfo(siteId, nodeId);
                    nodes.Add(new
                    {
                        channelInfo.Id,
                        channelInfo.ChannelName
                    });
                }
            }
            catch { }

            return(nodes);
        }
        public static object Login(IRequest request)
        {
            var account  = request.GetPostString("account");
            var password = request.GetPostString("password");

            string userName;
            string errorMessage;

            if (!Main.Instance.UserApi.Validate(account, password, out userName, out errorMessage))
            {
                Main.Instance.UserApi.UpdateLastActivityDateAndCountOfFailedLogin(userName);
                throw new Exception(errorMessage);
            }

            Main.Instance.UserApi.UpdateLastActivityDateAndCountOfLogin(userName);
            var user  = Main.Instance.UserApi.GetUserInfoByUserName(userName);
            var group = GroupDao.GetGroupInfo(GroupUserDao.GetGroupId(user.UserName)) ??
                        Utils.GetDefaultGroupInfo(Utils.GetConfigInfo());

            request.UserLogin(userName);

            return(new
            {
                User = user,
                Group = group
            });
        }
        public static object LoadConfig(IRequest request)
        {
            var dict = new Dictionary <string, List <MenuInfo> >();

            foreach (var parentInfo in MenuDao.GetMenuInfoList(0))
            {
                dict.Add(parentInfo.Title, MenuDao.GetMenuInfoList(parentInfo.Id));
            }
            var systemConfig = Main.Instance.ConfigApi.SystemConfig;
            var homeConfig   = Utils.GetConfigInfo();

            var user  = Main.Instance.UserApi.GetUserInfoByUserName(request.UserName);
            var group = GroupDao.GetGroupInfo(GroupUserDao.GetGroupId(request.UserName)) ??
                        Utils.GetDefaultGroupInfo(Utils.GetConfigInfo());

            var weiboUrl  = string.Empty;
            var weixinUrl = string.Empty;
            var qqUrl     = string.Empty;

            var loginPlugin = Main.Instance.PluginApi.GetPlugin <LoginPlugin>(LoginPlugin.PluginId);

            if (loginPlugin != null)
            {
                if (loginPlugin.IsOAuthReady(OAuthType.Weibo))
                {
                    weiboUrl = loginPlugin.GetOAuthLoginUrl(OAuthType.Weibo, string.Empty);
                }
                if (loginPlugin.IsOAuthReady(OAuthType.Weixin))
                {
                    weixinUrl = loginPlugin.GetOAuthLoginUrl(OAuthType.Weixin, string.Empty);
                }
                if (loginPlugin.IsOAuthReady(OAuthType.Qq))
                {
                    qqUrl = loginPlugin.GetOAuthLoginUrl(OAuthType.Qq, string.Empty);
                }
            }

            return(new
            {
                IsUserRegistrationAllowed = systemConfig.GetBool(SystemConfigAttribute.IsUserRegistrationAllowed),
                IsUserFindPassword = systemConfig.GetBool(SystemConfigAttribute.IsUserFindPassword),
                homeConfig.HomeUrl,
                homeConfig.Title,
                homeConfig.Copyright,
                homeConfig.BeianNo,
                homeConfig.LogoUrl,
                homeConfig.DefaultAvatarUrl,
                weiboUrl,
                weixinUrl,
                qqUrl,
                Menus = dict,
                User = user,
                Group = group
            });
        }
        public static object CreateContent(IRequest request)
        {
            if (!request.IsUserLoggin)
            {
                throw new Exception("用户认证失败");
            }

            var siteId    = request.GetPostInt("siteId");
            var channelId = request.GetPostInt("channelId");

            var groupInfo = GroupDao.GetGroupInfo(GroupUserDao.GetGroupId(request.UserName)) ?? Utils.GetDefaultGroupInfo(Utils.GetConfigInfo());
            var adminName = groupInfo.WritingAdmin;

            var contentInfo = Main.Instance.ContentApi.NewInstance();

            var bodyStream = new StreamReader(request.HttpRequest.InputStream);

            bodyStream.BaseStream.Seek(0, SeekOrigin.Begin);
            var raw      = bodyStream.ReadToEnd();
            var postData = !string.IsNullOrEmpty(raw) ? JObject.Parse(raw) : new JObject();
            var form     = new NameValueCollection();

            foreach (var item in postData)
            {
                form[item.Key] = item.Value.ToString();
            }

            contentInfo.Load(form);

            contentInfo.IsChecked        = false;
            contentInfo.SiteId           = siteId;
            contentInfo.ChannelId        = channelId;
            contentInfo.AddUserName      = adminName;
            contentInfo.WritingUserName  = request.UserName;
            contentInfo.LastEditUserName = adminName;
            contentInfo.AddDate          = DateTime.Now;
            contentInfo.LastEditDate     = DateTime.Now;

            var contentId = Main.Instance.ContentApi.Insert(siteId, channelId, contentInfo);

            Main.Instance.UserApi.AddLog(request.UserName, "新增稿件", contentInfo.Title);

            if (groupInfo.Id > 0)
            {
                groupInfo.LastWritingSiteId    = siteId;
                groupInfo.LastWritingChannelId = channelId;
                GroupDao.Update(groupInfo);
            }

            return(new
            {
                Id = contentId
            });
        }
        public static object GetSites(IRequest request)
        {
            if (!request.IsUserLoggin)
            {
                throw new Exception("用户认证失败");
            }

            var groupInfo = GroupDao.GetGroupInfo(GroupUserDao.GetGroupId(request.UserName)) ?? Utils.GetDefaultGroupInfo(Utils.GetConfigInfo());
            var adminName = groupInfo.WritingAdmin;

            return(Main.Instance.SiteApi.GetSiteInfoList(adminName));
        }
        public void Page_Load(object sender, EventArgs e)
        {
            if (!Main.Instance.AdminApi.IsPluginAuthorized)
            {
                HttpContext.Current.Response.Write("<h1>未授权访问</h1>");
                HttpContext.Current.Response.End();
                return;
            }

            _configInfo = Utils.GetConfigInfo();

            if (!string.IsNullOrEmpty(Request.QueryString["groupId"]))
            {
                _groupInfo = GroupDao.GetGroupInfo(Convert.ToInt32(Request.QueryString["groupId"])) ??
                             Utils.GetDefaultGroupInfo(_configInfo);
            }
            else
            {
                _groupInfo = new GroupInfo();
            }

            if (IsPostBack)
            {
                return;
            }

            TbGroupName.Text = _groupInfo.GroupName;
            if (_groupInfo.Id > 0)
            {
                var userNameList = GroupUserDao.GetUserNameList(_groupInfo.Id);
                TbUsers.Text = string.Join(",", userNameList);
            }

            if (string.IsNullOrEmpty(Request.QueryString["add"]) && _groupInfo.Id == 0)
            {
                TbGroupName.Enabled = false;
                PhUsers.Visible     = false;
            }

            PhSettings.Visible = _groupInfo.IsWriting;
            Utils.SelectListItems(DdlIsEnabled, _groupInfo.IsWriting.ToString());
            PhSettings.Visible = _groupInfo.IsWriting;
            TbAdmin.Text       = _groupInfo.WritingAdmin;

            BtnReturn.Attributes.Add("onclick", $"location.href='{PageWriting.GetRedirectUrl()}';return false");
        }
        public static object GetContents(IRequest request)
        {
            if (!request.IsUserLoggin)
            {
                throw new Exception("用户认证失败");
            }

            try
            {
                var siteId    = request.GetPostInt("siteId");
                var channelId = request.GetPostInt("channelId");

                var searchType = Utils.FilterSqlAndXss(request.GetPostString("searchType"));
                var keyword    = Utils.FilterSqlAndXss(request.GetPostString("keyword"));
                var dateFrom   = Utils.FilterSqlAndXss(request.GetPostString("dateFrom"));
                var dateTo     = Utils.FilterSqlAndXss(request.GetPostString("dateTo"));
                var page       = request.GetPostInt("page");

                const int limit  = 30;
                var       offset = (page - 1) * limit;

                var groupInfo = GroupDao.GetGroupInfo(GroupUserDao.GetGroupId(request.UserName)) ?? Utils.GetDefaultGroupInfo(Utils.GetConfigInfo());
                var adminName = groupInfo.WritingAdmin;

                var nodeIdList = new List <int> {
                    channelId
                };

                var writingChannelIdList = Main.Instance.ChannelApi.GetChannelIdList(siteId, adminName);
                foreach (var writingChannelId in writingChannelIdList)
                {
                    var writingChannelInfo = Main.Instance.ChannelApi.GetChannelInfo(siteId, writingChannelId);
                    if (Utils.In(writingChannelInfo.ParentsPath, channelId.ToString()))
                    {
                        nodeIdList.Add(writingChannelInfo.Id);
                    }
                }

                var whereString = new StringBuilder($"WHERE WritingUserName = '******' ");

                whereString.Append(nodeIdList.Count == 1
                    ? $"AND SiteId = {siteId} AND ChannelId = {nodeIdList[0]} "
                    : $"AND SiteId = {siteId} AND ChannelId IN ({string.Join(",", nodeIdList)})");

                var dateString = string.Empty;
                if (!string.IsNullOrEmpty(dateFrom))
                {
                    dateString = $" AND AddDate >= '{dateFrom}' ";
                }
                if (!string.IsNullOrEmpty(dateTo))
                {
                    dateString += $" AND AddDate <= '{Convert.ToDateTime(dateTo).AddDays(1)}' ";
                }

                whereString.Append(string.IsNullOrEmpty(keyword)
                    ? dateString
                    : $"AND ([{searchType}] LIKE '%{keyword}%') {dateString} ");

                var orderString = "ORDER BY IsTop DESC, AddDate DESC, Id DESC";

                var contentInfoList = Main.Instance.ContentApi.GetContentInfoList(siteId, channelId,
                                                                                  whereString.ToString(), orderString, limit, offset);

                var totalCount = Main.Instance.ContentApi.GetCount(siteId, channelId,
                                                                   whereString.ToString());
                var totalPage = totalCount == 0 ? 0 : Convert.ToInt32(totalCount / limit);

                return(new
                {
                    Results = contentInfoList,
                    TotalPage = totalPage
                });
            }
            catch { }

            return(new
            {
                Results = new List <IContentInfo>(),
                TotalPage = 1
            });
        }