珂珂的个人博客 - 一个程序猿的个人网站

FYJ.Blogs开发系列(五)-前台基页Controller

FYJ.Blogs开发系列(一)

FYJ.Blogs开发系列(二)-后台文章列表页View

FYJ.Blogs开发系列(三)-后台文章编辑页View

FYJ.Blogs开发系列(四)-后台文章Controller

FYJ.Blogs开发系列(五)-前台基页Controller

FYJ.Blogs开发系列(六)-前台主页Controller

FYJ.Blogs开发系列(七)-前台文章Controller

FYJ.Blogs开发系列(八)-自定义路由

FYJ.Blogs开发系列(九)-查询文章函数

FYJ.Blogs开发系列(十)-主页查询存储过程

FYJ.Blogs开发系列(十一)-后记

using Blogs.Entity;
using Blogs.UI.Main.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Caching;
using System.Web.Mvc;
using System.Linq;
using System.Xml;
using FYJ.Web;
using FYJ.Common;

namespace Blogs.UI.Main.Controllers
{
    public class BaseController : Controller
    {
        protected Blogs.IDAL.IBlog BlogDal
        {
            get
            {
                return FYJ.Web.IocFactory<Blogs.IDAL.IBlog>.Instance;
            }
        }

        protected Blogs.IDAL.IArticle ArticleDal
        {
            get
            {
                return FYJ.Web.IocFactory<Blogs.IDAL.IArticle>.Instance;
            }
        }

        protected BlogInfo Info
        {
            get
            {
                return RouteData.Values["blogInfo"] as BlogInfo;
            }
        }

        protected BlogUser CurrentUser
        {
            get
            {
                return BlogDal.GetCurrentUser();
            }
        }

        protected string BlogID
        {
            get
            {
                return Info.blogID + "";
            }
        }

        protected string ThemeName
        {
            get
            {
                return String.IsNullOrEmpty(Info.themeName) ? "Default" : Info.themeName;
            }
        }
        #region 私有
        private string GetTongjiHtml(string blogID)
        {
            CacheDependency depend = new CacheDependency(Server.MapPath("~/App_Data/site.xml"));
            object tongji = FYJ.Web.CacheAccess.GetFromCache("tongji");
            if (tongji == null)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("~/App_Data/site.xml"));
                tongji = doc.GetElementsByTagName("tongji")[0].InnerText;
                FYJ.Web.CacheAccess.SaveToCache("tongji", tongji, depend);
            }

            return tongji as string;
        }
        #endregion

        protected BlogPageModel GetModel()
        {
            DataSet ds = BlogDal.GetBlogDataSet(this.Info.blogID + "");
            if (ds.Tables[0].Rows.Count > 0)
            {
                Models.BlogPageModel model = new BlogPageModel();
                model.SitemapUrl = Info.BaseUrl + "/sitemap.xml";
                model.linkScript = Info.linkScript;
                model.BlogTitle = ds.Tables[0].Rows[0]["blogTitle"].ToString();
                model.Logo = ds.Tables[0].Rows[0]["blogLogo"].ToString();
                model.Menus = ds.Tables[9].DataTableToModel<Entity.blog_tb_menu>();
                model.BlogUrl = FYJ.Common.HttpHelper.GetRootPath(Request.Url.ToString());
                model.NewArticles = this.DataTableToArticle(ds.Tables[1]);
                model.MostViewArticles = this.DataTableToArticle(ds.Tables[2]);
                model.MostCommentArticles = this.DataTableToArticle(ds.Tables[3]);
                model.Months = ds.Tables[4].DataTableToModel<MonthModel>();
                model.categorys = ds.Tables[5].DataTableToModel<CategoryModel>();
                model.Tags = ds.Tables[6].DataTableToModel<TagModel>();
                model.ArticleCount = Convert.ToInt32(ds.Tables[7].Rows[0][0]);
                model.ViewCount = Convert.ToInt32(ds.Tables[7].Rows[0][1]);
                model.CommentCount = Convert.ToInt32(ds.Tables[7].Rows[0][2]);
                model.TopArticles = new List<ArticleModel>();
                model.Title = ds.Tables[0].Rows[0]["blogTitle"].ToString();
                model.LinkCollection = ds.Tables[10].DataTableToModel<LinkModel>();
                model.RandomArticles = ds.Tables[11].DataTableToModel<ArticleModel>();
                model.Social = ds.Tables[12].DataTableToModel<Entity.blog_tb_blog_social>().FirstOrDefault();

                bool isNeedTongji = (Request.UserHostAddress != "127.0.0.1" && !Request.UserHostAddress.StartsWith("192.168."));
                string fixTongji = "<script type=\"text/javascript\" src=\"http://fyj.me/service/visit.php?siteID=" + BlogID + "\"></script>";
                fixTongji += "<a href=\"http://webscan.360.cn/index/checkwebsite/url/kecq.com\"><img border=\"0\" src=\"http://img.webscan.360.cn/status/pai/hash/d57c7254b581c09711deae524057ffaa\" /></a>";
                if (isNeedTongji)
                {
                    model.TongjiHtml = ds.Tables[0].Rows[0]["tongji"].ToString() + fixTongji;
                }
                else
                {
                    //本地访问统计
                    if (System.Configuration.ConfigurationManager.AppSettings["isTongjiLocal"] == "true")
                    {
                        model.TongjiHtml = fixTongji;
                    }
                }
                return model;
            }

            return null;
        }

        protected virtual IEnumerable<ArticleModel> DataTableToArticle(DataTable dt)
        {
            IEnumerable<ArticleModel> articles = dt.DataTableToModel<ArticleModel>();

            int index = 0;
            foreach (ArticleModel item in articles)
            {
                item.Title = dt.Rows[index]["articleTitle2"].ToString();
                if (dt.Columns.Contains("tagUrls") && dt.Rows[index]["tagUrls"] != null)
                {
                    string[] tagUrls = dt.Rows[index]["tagUrls"].ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string[] tagDisplays = dt.Rows[index]["tagDisplays"].ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    List<TagModel> list = new List<TagModel>();
                    for (int i = 0; i < tagUrls.Length; i++)
                    {
                        TagModel model = new TagModel { tagDisplay = tagDisplays[i], Url = tagUrls[i] };
                        list.Add(model);
                    }
                    item.TagCollection = list;
                }
                index++;
            }

            return articles;
        }
    }
}



上一篇:wordpress3.6 修改的地方

下一篇:个人代码全部开源


0 评论

查看所有评论

给个评论吧