fix:优化文章显示效果

pull/536/head
lifei6671 2019-03-11 18:09:19 +08:00
parent b1979a3fdb
commit 0856b9e42f
2 changed files with 129 additions and 116 deletions

View File

@ -53,7 +53,7 @@ func (c *BlogController) Index() {
c.JsonResult(6001, "文章密码不正确") c.JsonResult(6001, "文章密码不正确")
} else if blog.BlogStatus == "password" && password == blog.Password { } else if blog.BlogStatus == "password" && password == blog.Password {
//如果密码输入正确则存入session中 //如果密码输入正确则存入session中
c.CruSession.Set(blogReadSession, blogId) _ = c.CruSession.Set(blogReadSession, blogId)
c.JsonResult(0, "OK") c.JsonResult(0, "OK")
} }
c.JsonResult(0, "OK") c.JsonResult(0, "OK")
@ -61,8 +61,11 @@ func (c *BlogController) Index() {
//如果不存在已输入密码的标记 //如果不存在已输入密码的标记
c.TplName = "blog/index_password.tpl" c.TplName = "blog/index_password.tpl"
} }
if blog.BlogType != 1 {
//加载文章附件 //加载文章附件
blog.LinkAttach(); _ = blog.LinkAttach()
}
c.Data["Model"] = blog c.Data["Model"] = blog
c.Data["Content"] = template.HTML(blog.BlogRelease) c.Data["Content"] = template.HTML(blog.BlogRelease)

View File

@ -1,16 +1,16 @@
package models package models
import ( import (
"time" "bytes"
"github.com/lifei6671/mindoc/conf"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego"
"github.com/lifei6671/mindoc/cache"
"fmt" "fmt"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
"strings" "github.com/astaxie/beego"
"bytes" "github.com/astaxie/beego/orm"
"github.com/lifei6671/mindoc/cache"
"github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/utils" "github.com/lifei6671/mindoc/utils"
"strings"
"time"
) )
//博文表 //博文表
@ -61,24 +61,24 @@ type Blog struct {
} }
// 多字段唯一键 // 多字段唯一键
func (m *Blog) TableUnique() [][]string { func (b *Blog) TableUnique() [][]string {
return [][]string{ return [][]string{
{"blog_id", "blog_identify"}, {"blog_id", "blog_identify"},
} }
} }
// TableName 获取对应数据库表名. // TableName 获取对应数据库表名.
func (m *Blog) TableName() string { func (b *Blog) TableName() string {
return "blogs" return "blogs"
} }
// TableEngine 获取数据使用的引擎. // TableEngine 获取数据使用的引擎.
func (m *Blog) TableEngine() string { func (b *Blog) TableEngine() string {
return "INNODB" return "INNODB"
} }
func (m *Blog) TableNameWithPrefix() string { func (b *Blog) TableNameWithPrefix() string {
return conf.GetDatabasePrefix() + m.TableName() return conf.GetDatabasePrefix() + b.TableName()
} }
func NewBlog() *Blog { func NewBlog() *Blog {
@ -88,121 +88,132 @@ func NewBlog() *Blog {
} }
//根据文章ID查询文章 //根据文章ID查询文章
func (b *Blog) Find(blogId int) (*Blog,error) { func (b *Blog) Find(blogId int) (*Blog, error) {
o := orm.NewOrm() o := orm.NewOrm()
err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id",blogId).One(b) err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id", blogId).One(b)
if err != nil { if err != nil {
beego.Error("查询文章时失败 -> ",err) beego.Error("查询文章时失败 -> ", err)
return nil,err return nil, err
} }
return b.Link() return b.Link()
} }
//从缓存中读取文章 //从缓存中读取文章
func (b *Blog) FindFromCache(blogId int) (blog *Blog,err error) { func (b *Blog) FindFromCache(blogId int) (blog *Blog, err error) {
key := fmt.Sprintf("blog-id-%d",blogId); key := fmt.Sprintf("blog-id-%d", blogId);
var temp Blog var temp Blog
err = cache.Get(key,&temp); err = cache.Get(key, &temp);
if err == nil { if err == nil {
b = &temp b = &temp
b.Link() b.Link()
beego.Debug("从缓存读取文章成功 ->", key) beego.Debug("从缓存读取文章成功 ->", key)
return b,nil return b, nil
}else { } else {
beego.Error("读取缓存失败 ->",err) beego.Error("读取缓存失败 ->", err)
} }
blog,err = b.Find(blogId) blog, err = b.Find(blogId)
if err == nil { if err == nil {
//默认一个小时 //默认一个小时
if err := cache.Put(key,blog,time.Hour * 1); err != nil { if err := cache.Put(key, blog, time.Hour*1); err != nil {
beego.Error("将文章存入缓存失败 ->",err) beego.Error("将文章存入缓存失败 ->", err)
} }
} }
return return
} }
//查找指定用户的指定文章 //查找指定用户的指定文章
func (b *Blog) FindByIdAndMemberId(blogId,memberId int) (*Blog,error) { func (b *Blog) FindByIdAndMemberId(blogId, memberId int) (*Blog, error) {
o := orm.NewOrm() o := orm.NewOrm()
err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id",blogId).Filter("member_id",memberId).One(b) err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id", blogId).Filter("member_id", memberId).One(b)
if err != nil { if err != nil {
beego.Error("查询文章时失败 -> ",err) beego.Error("查询文章时失败 -> ", err)
return nil,err return nil, err
} }
return b.Link() return b.Link()
} }
//根据文章标识查询文章 //根据文章标识查询文章
func (b *Blog) FindByIdentify(identify string) (*Blog,error) { func (b *Blog) FindByIdentify(identify string) (*Blog, error) {
o := orm.NewOrm() o := orm.NewOrm()
err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_identify",identify).One(b) err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_identify", identify).One(b)
if err != nil { if err != nil {
beego.Error("查询文章时失败 -> ",err) beego.Error("查询文章时失败 -> ", err)
return nil,err return nil, err
} }
return b,nil return b, nil
} }
//获取指定文章的链接内容 //获取指定文章的链接内容
func (b *Blog)Link() (*Blog,error) { func (b *Blog) Link() (*Blog, error) {
o := orm.NewOrm() o := orm.NewOrm()
//如果是链接文章,则需要从链接的项目中查找文章内容 //如果是链接文章,则需要从链接的项目中查找文章内容
if b.BlogType == 1 && b.DocumentId > 0 { if b.BlogType == 1 && b.DocumentId > 0 {
doc := NewDocument() doc := NewDocument()
if err := o.QueryTable(doc.TableNameWithPrefix()).Filter("document_id",b.DocumentId).One(doc,"release","markdown","identify","book_id");err != nil { if err := o.QueryTable(doc.TableNameWithPrefix()).Filter("document_id", b.DocumentId).One(doc, "release", "markdown", "identify", "book_id"); err != nil {
beego.Error("查询文章链接对象时出错 -> ",err) beego.Error("查询文章链接对象时出错 -> ", err)
}else{ } else {
b.DocumentIdentify = doc.Identify b.DocumentIdentify = doc.Identify
b.BlogRelease = doc.Release b.BlogRelease = doc.Release
//目前仅支持markdown文档进行链接 //目前仅支持markdown文档进行链接
b.BlogContent = doc.Markdown b.BlogContent = doc.Markdown
book := NewBook() book := NewBook()
if err := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id",doc.BookId).One(book,"identify");err != nil { if err := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", doc.BookId).One(book, "identify"); err != nil {
beego.Error("查询关联文档的项目时出错 ->",err) beego.Error("查询关联文档的项目时出错 ->", err)
}else{ } else {
b.BookIdentify = book.Identify b.BookIdentify = book.Identify
b.BookId = doc.BookId b.BookId = doc.BookId
} }
//处理链接文档存在源文档修改时间的问题
if content, err := goquery.NewDocumentFromReader(bytes.NewBufferString(b.BlogRelease)); err == nil {
content.Find(".wiki-bottom").Remove()
if html,err := content.Html();err == nil {
b.BlogRelease = html
} else {
beego.Error("处理文章失败 ->",err)
}
}else {
beego.Error("处理文章失败 ->",err)
}
} }
} }
if b.ModifyAt > 0{ if b.ModifyAt > 0 {
member := NewMember() member := NewMember()
if err := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id",b.ModifyAt).One(member,"real_name","account"); err == nil { if err := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id", b.ModifyAt).One(member, "real_name", "account"); err == nil {
if member.RealName != ""{ if member.RealName != "" {
b.ModifyRealName = member.RealName b.ModifyRealName = member.RealName
}else{ } else {
b.ModifyRealName = member.Account b.ModifyRealName = member.Account
} }
} }
} }
if b.MemberId > 0 { if b.MemberId > 0 {
member := NewMember() member := NewMember()
if err := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id",b.MemberId).One(member,"real_name","account","avatar"); err == nil { if err := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id", b.MemberId).One(member, "real_name", "account", "avatar"); err == nil {
if member.RealName != ""{ if member.RealName != "" {
b.CreateName = member.RealName b.CreateName = member.RealName
}else{ } else {
b.CreateName = member.Account b.CreateName = member.Account
} }
b.MemberAvatar = member.Avatar b.MemberAvatar = member.Avatar
} }
} }
return b,nil return b, nil
} }
//判断指定的文章标识是否存在 //判断指定的文章标识是否存在
func (b *Blog) IsExist(identify string) bool { func (b *Blog) IsExist(identify string) bool {
o := orm.NewOrm() o := orm.NewOrm()
return o.QueryTable(b.TableNameWithPrefix()).Filter("blog_identify",identify).Exist() return o.QueryTable(b.TableNameWithPrefix()).Filter("blog_identify", identify).Exist()
} }
//保存文章 //保存文章
@ -211,10 +222,10 @@ func (b *Blog) Save(cols ...string) error {
if b.OrderIndex <= 0 { if b.OrderIndex <= 0 {
blog := NewBlog() blog := NewBlog()
if err := o.QueryTable(blog.TableNameWithPrefix()).OrderBy("-blog_id").Limit(1).One(blog,"blog_id");err == nil{ if err := o.QueryTable(blog.TableNameWithPrefix()).OrderBy("-blog_id").Limit(1).One(blog, "blog_id"); err == nil {
b.OrderIndex = blog.BlogId + 1; b.OrderIndex = blog.BlogId + 1;
}else{ } else {
c,_ := o.QueryTable(b.TableNameWithPrefix()).Count() c, _ := o.QueryTable(b.TableNameWithPrefix()).Count()
b.OrderIndex = int(c) + 1 b.OrderIndex = int(c) + 1
} }
} }
@ -224,14 +235,14 @@ func (b *Blog) Save(cols ...string) error {
if b.BlogId > 0 { if b.BlogId > 0 {
b.Modified = time.Now() b.Modified = time.Now()
_,err = o.Update(b,cols...) _, err = o.Update(b, cols...)
key := fmt.Sprintf("blog-id-%d", b.BlogId ) key := fmt.Sprintf("blog-id-%d", b.BlogId)
cache.Delete(key) cache.Delete(key)
}else{ } else {
b.Created = time.Now() b.Created = time.Now()
_,err = o.Insert(b) _, err = o.Insert(b)
} }
return err return err
@ -273,7 +284,7 @@ func (b *Blog) Processor() *Blog {
} }
//分页查询文章列表 //分页查询文章列表
func (b *Blog) FindToPager(pageIndex, pageSize int,memberId int,status string) (blogList []*Blog, totalCount int, err error) { func (b *Blog) FindToPager(pageIndex, pageSize int, memberId int, status string) (blogList []*Blog, totalCount int, err error) {
o := orm.NewOrm() o := orm.NewOrm()
@ -282,30 +293,29 @@ func (b *Blog) FindToPager(pageIndex, pageSize int,memberId int,status string) (
query := o.QueryTable(b.TableNameWithPrefix()) query := o.QueryTable(b.TableNameWithPrefix())
if memberId > 0 { if memberId > 0 {
query = query.Filter("member_id",memberId) query = query.Filter("member_id", memberId)
} }
if status != "" { if status != "" {
query = query.Filter("blog_status",status) query = query.Filter("blog_status", status)
} }
_, err = query.OrderBy("-order_index", "-blog_id").Offset(offset).Limit(pageSize).All(&blogList)
_,err = query.OrderBy("-order_index","-blog_id").Offset(offset).Limit(pageSize).All(&blogList)
if err != nil { if err != nil {
if err == orm.ErrNoRows { if err == orm.ErrNoRows {
err = nil err = nil
} }
beego.Error("获取文章列表时出错 ->",err) beego.Error("获取文章列表时出错 ->", err)
return return
} }
count,err := query.Count() count, err := query.Count()
if err != nil { if err != nil {
beego.Error("获取文章数量时出错 ->",err) beego.Error("获取文章数量时出错 ->", err)
return nil,0,err return nil, 0, err
} }
totalCount = int(count) totalCount = int(count)
for _,blog := range blogList { for _, blog := range blogList {
if blog.BlogType == 1 { if blog.BlogType == 1 {
blog.Link() blog.Link()
} }
@ -318,47 +328,47 @@ func (b *Blog) FindToPager(pageIndex, pageSize int,memberId int,status string) (
func (b *Blog) Delete(blogId int) error { func (b *Blog) Delete(blogId int) error {
o := orm.NewOrm() o := orm.NewOrm()
_,err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id",blogId).Delete() _, err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id", blogId).Delete()
if err != nil { if err != nil {
beego.Error("删除文章失败 ->",err) beego.Error("删除文章失败 ->", err)
} }
return err return err
} }
//查询下一篇文章 //查询下一篇文章
func (b *Blog) QueryNext(blogId int) (*Blog,error) { func (b *Blog) QueryNext(blogId int) (*Blog, error) {
o := orm.NewOrm() o := orm.NewOrm()
blog := NewBlog() blog := NewBlog()
if err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id",blogId).One(blog,"order_index"); err != nil { if err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id", blogId).One(blog, "order_index"); err != nil {
beego.Error("查询文章时出错 ->",err) beego.Error("查询文章时出错 ->", err)
return b,err return b, err
} }
err := o.QueryTable(b.TableNameWithPrefix()).Filter("order_index__gte",blog.OrderIndex).Filter("blog_id__gt",blogId).OrderBy("order_index","blog_id").One(blog) err := o.QueryTable(b.TableNameWithPrefix()).Filter("order_index__gte", blog.OrderIndex).Filter("blog_id__gt", blogId).OrderBy("order_index", "blog_id").One(blog)
if err != nil && err != orm.ErrNoRows{ if err != nil && err != orm.ErrNoRows {
beego.Error("查询文章时出错 ->",err) beego.Error("查询文章时出错 ->", err)
} }
return blog,err return blog, err
} }
//查询下一篇文章 //查询下一篇文章
func (b *Blog) QueryPrevious(blogId int) (*Blog,error) { func (b *Blog) QueryPrevious(blogId int) (*Blog, error) {
o := orm.NewOrm() o := orm.NewOrm()
blog := NewBlog() blog := NewBlog()
if err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id",blogId).One(blog,"order_index"); err != nil { if err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id", blogId).One(blog, "order_index"); err != nil {
beego.Error("查询文章时出错 ->",err) beego.Error("查询文章时出错 ->", err)
return b,err return b, err
} }
err := o.QueryTable(b.TableNameWithPrefix()).Filter("order_index__lte",blog.OrderIndex).Filter("blog_id__lt",blogId).OrderBy("-order_index","-blog_id").One(blog) err := o.QueryTable(b.TableNameWithPrefix()).Filter("order_index__lte", blog.OrderIndex).Filter("blog_id__lt", blogId).OrderBy("-order_index", "-blog_id").One(blog)
if err != nil && err != orm.ErrNoRows{ if err != nil && err != orm.ErrNoRows {
beego.Error("查询文章时出错 ->",err) beego.Error("查询文章时出错 ->", err)
} }
return blog,err return blog, err
} }
//关联文章附件 //关联文章附件
@ -369,14 +379,14 @@ func (b *Blog) LinkAttach() (err error) {
var attachList []*Attachment var attachList []*Attachment
//当不是关联文章时用文章ID去查询附件 //当不是关联文章时用文章ID去查询附件
if b.BlogType != 1 || b.DocumentId <= 0 { if b.BlogType != 1 || b.DocumentId <= 0 {
_, err = o.QueryTable(NewAttachment().TableNameWithPrefix()).Filter("document_id", b.BlogId).Filter("book_id",0).All(&attachList) _, err = o.QueryTable(NewAttachment().TableNameWithPrefix()).Filter("document_id", b.BlogId).Filter("book_id", 0).All(&attachList)
if err != nil && err != orm.ErrNoRows{ if err != nil && err != orm.ErrNoRows {
beego.Error("查询文章附件时出错 ->", err) beego.Error("查询文章附件时出错 ->", err)
} }
}else { } else {
_, err = o.QueryTable(NewAttachment().TableNameWithPrefix()).Filter("document_id", b.DocumentId).Filter("book_id", b.BookId).All(&attachList) _, err = o.QueryTable(NewAttachment().TableNameWithPrefix()).Filter("document_id", b.DocumentId).Filter("book_id", b.BookId).All(&attachList)
if err != nil && err != orm.ErrNoRows{ if err != nil && err != orm.ErrNoRows {
beego.Error("查询文章附件时出错 ->", err) beego.Error("查询文章附件时出错 ->", err)
} }
} }