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

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开发系列(十一)-后记

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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 评论

查看所有评论

给个评论吧