2018-07-13 16:46:40 +08:00
|
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
2019-03-11 18:09:19 +08:00
|
|
|
|
"bytes"
|
2018-07-23 16:25:39 +08:00
|
|
|
|
"fmt"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2018-07-24 17:54:24 +08:00
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2021-03-26 11:34:02 +08:00
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-25 10:42:18 +08:00
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
|
|
|
|
"github.com/beego/beego/v2/server/web"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"github.com/mindoc-org/mindoc/cache"
|
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
|
|
|
|
"github.com/mindoc-org/mindoc/utils"
|
2018-07-13 16:46:40 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 博文表
|
2018-07-13 16:46:40 +08:00
|
|
|
|
type Blog struct {
|
2019-03-11 18:09:19 +08:00
|
|
|
|
BlogId int `orm:"pk;auto;unique;column(blog_id)" json:"blog_id"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//文章标题
|
2022-10-28 12:33:36 +08:00
|
|
|
|
BlogTitle string `orm:"column(blog_title);size(500);description(文章标题)" json:"blog_title"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//文章标识
|
2022-10-28 12:33:36 +08:00
|
|
|
|
BlogIdentify string `orm:"column(blog_identify);size(100);unique;description(文章标识)" json:"blog_identify"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//排序序号
|
2022-10-28 12:33:36 +08:00
|
|
|
|
OrderIndex int `orm:"column(order_index);type(int);default(0);description(排序序号)" json:"order_index"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//所属用户
|
2022-10-28 12:33:36 +08:00
|
|
|
|
MemberId int `orm:"column(member_id);type(int);default(0);index;description(所属用户)" json:"member_id"`
|
2018-07-18 17:10:24 +08:00
|
|
|
|
//用户头像
|
2019-03-11 18:09:19 +08:00
|
|
|
|
MemberAvatar string `orm:"-" json:"member_avatar"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//文章类型:0 普通文章/1 链接文章
|
2022-10-28 12:33:36 +08:00
|
|
|
|
BlogType int `orm:"column(blog_type);type(int);default(0);description(文章类型: 0普通文章/1 链接文章)" json:"blog_type"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//链接到的项目中的文档ID
|
2022-10-28 12:33:36 +08:00
|
|
|
|
DocumentId int `orm:"column(document_id);type(int);default(0);description(链接到的项目中的文档ID)" json:"document_id"`
|
2018-07-18 17:10:24 +08:00
|
|
|
|
//文章的标识
|
|
|
|
|
DocumentIdentify string `orm:"-" json:"document_identify"`
|
|
|
|
|
//关联文档的项目标识
|
2019-03-11 18:09:19 +08:00
|
|
|
|
BookIdentify string `orm:"-" json:"book_identify"`
|
2018-07-18 17:10:24 +08:00
|
|
|
|
//关联文档的项目ID
|
2019-03-11 18:09:19 +08:00
|
|
|
|
BookId int `orm:"-" json:"book_id"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//文章摘要
|
2022-10-28 12:33:36 +08:00
|
|
|
|
BlogExcerpt string `orm:"column(blog_excerpt);size(1500);description(文章摘要)" json:"blog_excerpt"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//文章内容
|
2022-10-28 12:33:36 +08:00
|
|
|
|
BlogContent string `orm:"column(blog_content);type(text);null;description(文章内容)" json:"blog_content"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//发布后的文章内容
|
2022-10-28 12:33:36 +08:00
|
|
|
|
BlogRelease string `orm:"column(blog_release);type(text);null;description(发布后的文章内容)" json:"blog_release"`
|
2018-07-17 19:13:11 +08:00
|
|
|
|
//文章当前的状态,枚举enum(’publish’,’draft’,’password’)值,publish为已 发表,draft为草稿,password 为私人内容(不会被公开) 。默认为publish。
|
2022-10-28 12:33:36 +08:00
|
|
|
|
BlogStatus string `orm:"column(blog_status);size(100);default(publish);description(状态:publish为已发表-默认,draft:草稿,password :私人内容-不会被公开)" json:"blog_status"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//文章密码,varchar(100)值。文章编辑才可为文章设定一个密码,凭这个密码才能对文章进行重新强加或修改。
|
2022-10-28 12:33:36 +08:00
|
|
|
|
Password string `orm:"column(password);size(100);description(文章密码)" json:"-"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//最后修改时间
|
2022-10-28 12:33:36 +08:00
|
|
|
|
Modified time.Time `orm:"column(modify_time);type(datetime);auto_now;description(最后修改时间)" json:"modify_time"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//修改人id
|
2022-10-28 12:33:36 +08:00
|
|
|
|
ModifyAt int `orm:"column(modify_at);type(int);description(修改人id)" json:"-"`
|
2018-07-17 19:13:11 +08:00
|
|
|
|
ModifyRealName string `orm:"-" json:"modify_real_name"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//创建时间
|
2022-10-28 12:33:36 +08:00
|
|
|
|
Created time.Time `orm:"column(create_time);type(datetime);auto_now_add;description(创建时间)" json:"create_time"`
|
2019-03-11 18:09:19 +08:00
|
|
|
|
CreateName string `orm:"-" json:"create_name"`
|
2018-07-17 19:13:11 +08:00
|
|
|
|
//版本号
|
2022-10-28 12:33:36 +08:00
|
|
|
|
Version int64 `orm:"type(bigint);column(version);description(版本号)" json:"version"`
|
2018-07-18 17:10:24 +08:00
|
|
|
|
//附件列表
|
|
|
|
|
AttachList []*Attachment `orm:"-" json:"attach_list"`
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 多字段唯一键
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) TableUnique() [][]string {
|
2018-07-13 16:46:40 +08:00
|
|
|
|
return [][]string{
|
|
|
|
|
{"blog_id", "blog_identify"},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName 获取对应数据库表名.
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) TableName() string {
|
2018-07-13 16:46:40 +08:00
|
|
|
|
return "blogs"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableEngine 获取数据使用的引擎.
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) TableEngine() string {
|
2018-07-13 16:46:40 +08:00
|
|
|
|
return "INNODB"
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) TableNameWithPrefix() string {
|
|
|
|
|
return conf.GetDatabasePrefix() + b.TableName()
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewBlog() *Blog {
|
|
|
|
|
return &Blog{
|
2018-07-17 19:13:11 +08:00
|
|
|
|
BlogStatus: "public",
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 根据文章ID查询文章
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) Find(blogId int) (*Blog, error) {
|
2018-07-13 16:46:40 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id", blogId).One(b)
|
2018-07-13 16:46:40 +08:00
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询文章时失败 -> ", err)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return nil, err
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 17:10:24 +08:00
|
|
|
|
return b.Link()
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
2018-07-23 16:25:39 +08:00
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 从缓存中读取文章
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) FindFromCache(blogId int) (blog *Blog, err error) {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
key := fmt.Sprintf("blog-id-%d", blogId)
|
2018-07-25 14:46:56 +08:00
|
|
|
|
var temp Blog
|
2021-03-23 21:55:50 +08:00
|
|
|
|
err = cache.Get(key, &temp)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err == nil {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
b = &temp
|
|
|
|
|
b.Link()
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Debug("从缓存读取文章成功 ->", key)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return b, nil
|
|
|
|
|
} else {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("读取缓存失败 ->", err)
|
2018-07-23 16:25:39 +08:00
|
|
|
|
}
|
2018-07-25 14:46:56 +08:00
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
blog, err = b.Find(blogId)
|
2018-07-23 16:25:39 +08:00
|
|
|
|
if err == nil {
|
|
|
|
|
//默认一个小时
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err := cache.Put(key, blog, time.Hour*1); err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("将文章存入缓存失败 ->", err)
|
2018-07-23 18:08:07 +08:00
|
|
|
|
}
|
2018-07-23 16:25:39 +08:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 查找指定用户的指定文章
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) FindByIdAndMemberId(blogId, memberId int) (*Blog, error) {
|
2018-07-13 19:04:51 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id", blogId).Filter("member_id", memberId).One(b)
|
2018-07-13 19:04:51 +08:00
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询文章时失败 -> ", err)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return nil, err
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 17:10:24 +08:00
|
|
|
|
return b.Link()
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
2019-03-11 18:09:19 +08:00
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 根据文章标识查询文章
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) FindByIdentify(identify string) (*Blog, error) {
|
2018-07-13 16:46:40 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_identify", identify).One(b)
|
2018-07-13 16:46:40 +08:00
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询文章时失败 -> ", err)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return nil, err
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return b, nil
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 获取指定文章的链接内容
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) Link() (*Blog, error) {
|
2018-07-13 16:46:40 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
//如果是链接文章,则需要从链接的项目中查找文章内容
|
2018-07-18 17:10:24 +08:00
|
|
|
|
if b.BlogType == 1 && b.DocumentId > 0 {
|
2018-07-13 16:46:40 +08:00
|
|
|
|
doc := NewDocument()
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err := o.QueryTable(doc.TableNameWithPrefix()).Filter("document_id", b.DocumentId).One(doc, "release", "markdown", "identify", "book_id"); err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询文章链接对象时出错 -> ", err)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
} else {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
b.DocumentIdentify = doc.Identify
|
2018-07-13 16:46:40 +08:00
|
|
|
|
b.BlogRelease = doc.Release
|
2019-03-11 18:09:19 +08:00
|
|
|
|
|
2018-07-13 16:46:40 +08:00
|
|
|
|
//目前仅支持markdown文档进行链接
|
|
|
|
|
b.BlogContent = doc.Markdown
|
2018-07-18 17:10:24 +08:00
|
|
|
|
book := NewBook()
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", doc.BookId).One(book, "identify"); err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询关联文档的项目时出错 ->", err)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
} else {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
b.BookIdentify = book.Identify
|
|
|
|
|
b.BookId = doc.BookId
|
|
|
|
|
}
|
2019-03-11 18:09:19 +08:00
|
|
|
|
//处理链接文档存在源文档修改时间的问题
|
|
|
|
|
if content, err := goquery.NewDocumentFromReader(bytes.NewBufferString(b.BlogRelease)); err == nil {
|
|
|
|
|
content.Find(".wiki-bottom").Remove()
|
2021-03-23 21:55:50 +08:00
|
|
|
|
if html, err := content.Html(); err == nil {
|
2019-03-11 18:09:19 +08:00
|
|
|
|
b.BlogRelease = html
|
|
|
|
|
} else {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("处理文章失败 ->", err)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
}
|
2021-03-23 21:55:50 +08:00
|
|
|
|
} else {
|
|
|
|
|
logs.Error("处理文章失败 ->", err)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if b.ModifyAt > 0 {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
member := NewMember()
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id", b.ModifyAt).One(member, "real_name", "account"); err == nil {
|
|
|
|
|
if member.RealName != "" {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
b.ModifyRealName = member.RealName
|
2019-03-11 18:09:19 +08:00
|
|
|
|
} else {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
b.ModifyRealName = member.Account
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if b.MemberId > 0 {
|
|
|
|
|
member := NewMember()
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id", b.MemberId).One(member, "real_name", "account", "avatar"); err == nil {
|
|
|
|
|
if member.RealName != "" {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
b.CreateName = member.RealName
|
2019-03-11 18:09:19 +08:00
|
|
|
|
} else {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
b.CreateName = member.Account
|
|
|
|
|
}
|
|
|
|
|
b.MemberAvatar = member.Avatar
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return b, nil
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 判断指定的文章标识是否存在
|
2018-07-13 19:04:51 +08:00
|
|
|
|
func (b *Blog) IsExist(identify string) bool {
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return o.QueryTable(b.TableNameWithPrefix()).Filter("blog_identify", identify).Exist()
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 保存文章
|
2018-07-17 19:13:11 +08:00
|
|
|
|
func (b *Blog) Save(cols ...string) error {
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-07-18 17:10:24 +08:00
|
|
|
|
if b.OrderIndex <= 0 {
|
2018-07-17 19:13:11 +08:00
|
|
|
|
blog := NewBlog()
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err := o.QueryTable(blog.TableNameWithPrefix()).OrderBy("-blog_id").Limit(1).One(blog, "blog_id"); err == nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
b.OrderIndex = blog.BlogId + 1
|
2019-03-11 18:09:19 +08:00
|
|
|
|
} else {
|
|
|
|
|
c, _ := o.QueryTable(b.TableNameWithPrefix()).Count()
|
2018-07-17 19:13:11 +08:00
|
|
|
|
b.OrderIndex = int(c) + 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var err error
|
2018-09-13 18:19:26 +08:00
|
|
|
|
|
|
|
|
|
b.Processor().Version = time.Now().Unix()
|
|
|
|
|
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if b.BlogId > 0 {
|
|
|
|
|
b.Modified = time.Now()
|
2019-03-11 18:09:19 +08:00
|
|
|
|
_, err = o.Update(b, cols...)
|
|
|
|
|
key := fmt.Sprintf("blog-id-%d", b.BlogId)
|
2024-07-24 09:20:06 +08:00
|
|
|
|
_ = cache.Delete(key)
|
2018-07-23 16:25:39 +08:00
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
} else {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2018-07-17 19:13:11 +08:00
|
|
|
|
b.Created = time.Now()
|
2019-03-11 18:09:19 +08:00
|
|
|
|
_, err = o.Insert(b)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2018-07-24 17:54:24 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 过滤文章的危险标签,处理文章外链以及图片.
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (b *Blog) Processor() *Blog {
|
|
|
|
|
|
|
|
|
|
b.BlogRelease = utils.SafetyProcessor(b.BlogRelease)
|
|
|
|
|
|
|
|
|
|
//解析文档中非本站的链接,并设置为新窗口打开
|
|
|
|
|
if content, err := goquery.NewDocumentFromReader(bytes.NewBufferString(b.BlogRelease)); err == nil {
|
|
|
|
|
|
|
|
|
|
content.Find("a").Each(func(i int, contentSelection *goquery.Selection) {
|
|
|
|
|
if src, ok := contentSelection.Attr("href"); ok {
|
|
|
|
|
if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
//logs.Info(src,conf.BaseUrl,strings.HasPrefix(src,conf.BaseUrl))
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if conf.BaseUrl != "" && !strings.HasPrefix(src, conf.BaseUrl) {
|
|
|
|
|
contentSelection.SetAttr("target", "_blank")
|
|
|
|
|
if html, err := content.Html(); err == nil {
|
|
|
|
|
b.BlogRelease = html
|
|
|
|
|
}
|
2018-07-24 17:54:24 +08:00
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 17:54:24 +08:00
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
})
|
|
|
|
|
//设置图片为CDN地址
|
2021-03-26 11:34:02 +08:00
|
|
|
|
if cdnimg, _ := web.AppConfig.String("cdnimg"); cdnimg != "" {
|
2018-09-13 18:19:26 +08:00
|
|
|
|
content.Find("img").Each(func(i int, contentSelection *goquery.Selection) {
|
|
|
|
|
if src, ok := contentSelection.Attr("src"); ok && strings.HasPrefix(src, "/uploads/") {
|
|
|
|
|
contentSelection.SetAttr("src", utils.JoinURI(cdnimg, src))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-07-24 17:54:24 +08:00
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
|
|
|
|
|
return b
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 分页查询文章列表
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) FindToPager(pageIndex, pageSize int, memberId int, status string) (blogList []*Blog, totalCount int, err error) {
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
|
|
offset := (pageIndex - 1) * pageSize
|
|
|
|
|
|
2018-11-05 18:50:01 +08:00
|
|
|
|
query := o.QueryTable(b.TableNameWithPrefix())
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
if memberId > 0 {
|
2019-03-11 18:09:19 +08:00
|
|
|
|
query = query.Filter("member_id", memberId)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2024-07-24 09:20:06 +08:00
|
|
|
|
if status != "" && status != "all" {
|
2019-03-11 18:09:19 +08:00
|
|
|
|
query = query.Filter("blog_status", status)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
if status == "" {
|
|
|
|
|
query = query.Filter("blog_status__ne", "private")
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
_, err = query.OrderBy("-order_index", "-blog_id").Offset(offset).Limit(pageSize).All(&blogList)
|
2018-07-13 19:04:51 +08:00
|
|
|
|
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if err != nil {
|
2018-07-23 10:13:33 +08:00
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
|
err = nil
|
|
|
|
|
}
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("获取文章列表时出错 ->", err)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-03-11 18:09:19 +08:00
|
|
|
|
count, err := query.Count()
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("获取文章数量时出错 ->", err)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return nil, 0, err
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
totalCount = int(count)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
for _, blog := range blogList {
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if blog.BlogType == 1 {
|
|
|
|
|
blog.Link()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 删除文章
|
2018-07-17 19:13:11 +08:00
|
|
|
|
func (b *Blog) Delete(blogId int) error {
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 删除文章缓存
|
|
|
|
|
key := fmt.Sprintf("blog-id-%d", blogId)
|
|
|
|
|
_ = cache.Delete(key)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
_, err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id", blogId).Delete()
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("删除文章失败 ->", err)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
return err
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 查询下一篇文章
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) QueryNext(blogId int) (*Blog, error) {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
blog := NewBlog()
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id", blogId).One(blog, "order_index"); err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询文章时出错 ->", err)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return b, err
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
err := o.QueryTable(b.TableNameWithPrefix()).Filter("order_index__gte", blog.OrderIndex).Filter("blog_id__gt", blogId).OrderBy("order_index", "blog_id").One(blog)
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err != nil && err != orm.ErrNoRows {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询文章时出错 ->", err)
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return blog, err
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 查询下一篇文章
|
2019-03-11 18:09:19 +08:00
|
|
|
|
func (b *Blog) QueryPrevious(blogId int) (*Blog, error) {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
blog := NewBlog()
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id", blogId).One(blog, "order_index"); err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询文章时出错 ->", err)
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return b, err
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
err := o.QueryTable(b.TableNameWithPrefix()).Filter("order_index__lte", blog.OrderIndex).Filter("blog_id__lt", blogId).OrderBy("-order_index", "-blog_id").One(blog)
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err != nil && err != orm.ErrNoRows {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询文章时出错 ->", err)
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
2019-03-11 18:09:19 +08:00
|
|
|
|
return blog, err
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
// 关联文章附件
|
2018-07-18 17:10:24 +08:00
|
|
|
|
func (b *Blog) LinkAttach() (err error) {
|
|
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
|
|
var attachList []*Attachment
|
|
|
|
|
//当不是关联文章时,用文章ID去查询附件
|
|
|
|
|
if b.BlogType != 1 || b.DocumentId <= 0 {
|
2019-03-11 18:09:19 +08:00
|
|
|
|
_, err = o.QueryTable(NewAttachment().TableNameWithPrefix()).Filter("document_id", b.BlogId).Filter("book_id", 0).All(&attachList)
|
|
|
|
|
if err != nil && err != orm.ErrNoRows {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询文章附件时出错 ->", err)
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
2019-03-11 18:09:19 +08:00
|
|
|
|
} else {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
_, err = o.QueryTable(NewAttachment().TableNameWithPrefix()).Filter("document_id", b.DocumentId).Filter("book_id", b.BookId).All(&attachList)
|
|
|
|
|
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if err != nil && err != orm.ErrNoRows {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询文章附件时出错 ->", err)
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
b.AttachList = attachList
|
|
|
|
|
return
|
2019-03-11 18:09:19 +08:00
|
|
|
|
}
|