cms前端页面

This commit is contained in:
yxh
2020-10-30 15:07:25 +08:00
parent 509b2aaf1e
commit 1b4a5a3b93
75 changed files with 19758 additions and 466 deletions
+83
View File
@@ -0,0 +1,83 @@
package home
import (
"gfast/app/model/admin/cms_news"
"gfast/app/service/admin/cms_service"
"gfast/library/response/home"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
type Index struct{}
// 首页
func (c *Index) Index(r *ghttp.Request) {
home.Response(r, "index.html")
}
// 列表页
func (c *Index) List(r *ghttp.Request) {
c.listInfo(r)
}
func (c *Index) listInfo(r *ghttp.Request, args ...interface{}) {
keyWords := r.GetString("keyWords")
cateId := r.GetInt("cateId")
pageNum := r.GetInt("page")
var pageSize int = 6
total, _, list, err := cms_service.NewsListByPage(&cms_news.ReqListSearchParams{
CateId: []int{cateId},
NewsStatus: "1",
PageSize: pageSize,
PageNum: pageNum,
KeyWords: keyWords,
})
if err != nil {
g.Log().Error(err)
}
// 获取当前栏目
menu, _ := cms_service.GetMenuInfoById(cateId)
tmp := ""
if len(args) == 0 {
tmp = menu.ListTemplate
} else {
tmp = "list/list.html"
}
home.Response(r, tmp, g.Map{
"list": list,
"pageStyle": r.GetPage(total, pageSize).GetContent(4),
"menu": menu,
"showPage": total > pageSize,
"keyWords": keyWords,
})
}
// 内容页
func (c *Index) Show(r *ghttp.Request) {
//获取栏目ID
cateIds := r.GetInts("cateIds")
//文章id
newsId := r.GetInt64("newsId")
// 查询文章内容
res, err := cms_service.GetModelFieldsByCateIds(r, cateIds, newsId) //文章附加字段
if err != nil {
g.Log().Error(err)
}
// 查询文章信息
newsInfo, _ := cms_service.GetNewsById(int(newsId))
newsInfo.NewsHits++
newsInfo.Save()
cateId := cateIds[0]
// 获取当前栏目
menu, _ := cms_service.GetMenuInfoById(cateId)
home.Response(r, menu.ContentTemplate, g.Map{
"content": res[0],
"author": res[1],
"newsInfo": newsInfo,
"menu": menu,
})
}
func (c *Index) Search(r *ghttp.Request) {
c.listInfo(r, "search")
}
@@ -3,6 +3,7 @@ package cms_category_news
import (
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
// Fill with you ideas below.
@@ -16,3 +17,16 @@ func GetCategoriesByNewsId(newsId uint64) (cateNews []*Entity, err error) {
}
return
}
//通过文章id获取关联的栏目id
func GetCheckedCategoryIdByNewsId(newsId uint64) (catIds []int, err error) {
categories, err := GetCategoriesByNewsId(newsId)
if err != nil {
return
}
catIds = make([]int, len(categories))
for k, v := range categories {
catIds[k] = gconv.Int(v.CategoryId)
}
return
}
+52 -15
View File
@@ -9,6 +9,7 @@ import (
"gfast/app/model/admin/user"
"gfast/library/service"
"gfast/library/utils"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g"
@@ -16,21 +17,26 @@ import (
"github.com/gogf/gf/util/gconv"
)
// Fill with you ideas below.
//文章列表
type NewsList struct {
Entity
UserNickname string `json:"user_nickname"`
CateList *gvar.Var `json:"cateList"`
}
//添加文章参数
type ReqAddParams struct {
NewsStatus uint `p:"status" v:"in:0,1#状态只能为0或1"` // 状态;1:已发布;0:未发布;
Attr []int `p:attr` //文章标记 置顶 推荐
PublishedTime string `p:"published_time"` // 发布时间
NewsTitle string `p:"title" v:"required#标题不能为空"` // post标题
NewsKeywords string `p:"keywords"` // seo keywords
NewsExcerpt string `p:"excerpt"` // post摘要
NewsSource string `p:"source" ` // 转载文章的来源
Thumbnail string `p:"thumbnail" ` // 缩略图
IsJump uint `p:"IsJump" v:"in:0,1#跳转类型只能为0或1"` // 是否跳转地址
JumpUrl string `p:"JumpUrl" v:"required-if:IsJump,1|url#跳转地址不能为空|跳转地址格式不正确"` // 跳转地址
ModelForm map[string]interface{} `p:"modelForm"`
NewsStatus uint `p:"status" v:"in:0,1#状态只能为0或1"` // 状态;1:已发布;0:未发布;
Attr []int `p:"attr"` //文章标记 置顶 推荐
PublishedTime string `p:"published_time"` // 发布时间
NewsTitle string `p:"title" v:"required#标题不能为空"` // post标题
NewsKeywords string `p:"keywords"` // seo keywords
NewsExcerpt string `p:"excerpt"` // post摘要
NewsSource string `p:"source" ` // 转载文章的来源
Thumbnail string `p:"thumbnail" ` // 缩略图
IsJump uint `p:"IsJump" v:"in:0,1#跳转类型只能为0或1"` // 是否跳转地址
JumpUrl string `p:"JumpUrl" v:"required-if:IsJump,1|url#跳转地址不能为空|跳转地址格式不正确"` // 跳转地址
ModelForm g.Map `p:"modelForm"`
}
//文章搜索参数
@@ -39,8 +45,13 @@ type ReqListSearchParams struct {
PublishedTimeStart string `p:"pubTimeStart"`
PublishedTimeEnd string `p:"pubTimeEnd"`
KeyWords string `p:"keyWords"`
IsSlide string `p:"isSlide"`
IsTop string `p:"IsTop"`
Recommended string `p:"Recommended"`
NewsStatus string `p:"NewsStatus"`
PageNum int `p:"page"` //当前页码
PageSize int `p:"pageSize"` //每页数
OrderBy string //排序字段
}
type ReqEditParams struct {
@@ -75,6 +86,8 @@ func AddNews(req *ReqAddParams, cateIds []int, userId uint64, tx *gdb.TX) (insId
entity.IsTop = 1
} else if v == 2 {
entity.Recommended = 1
} else if v == 3 {
entity.IsSlide = 1
}
}
res, e := Model.TX(tx).Insert(entity)
@@ -128,11 +141,16 @@ func EditNews(req *ReqEditParams, cateIds []int, tx *gdb.TX) (err error) {
entity.Thumbnail = req.Thumbnail
entity.IsJump = req.IsJump
entity.JumpUrl = req.JumpUrl
entity.IsTop = 0
entity.Recommended = 0
entity.IsSlide = 0
for _, v := range req.Attr {
if v == 1 {
entity.IsTop = 1
} else if v == 2 {
entity.Recommended = 1
} else if v == 3 {
entity.IsSlide = 1
}
}
_, err = Model.TX(tx).Replace(entity)
@@ -170,7 +188,7 @@ func EditNews(req *ReqEditParams, cateIds []int, tx *gdb.TX) (err error) {
}
//文章列表查询
func ListByPage(req *ReqListSearchParams) (total, page int, list gdb.Result, err error) {
func ListByPage(req *ReqListSearchParams) (total, page int, list []*NewsList, err error) {
model := g.DB().Table(Table + " news")
if req != nil {
if len(req.CateId) > 0 {
@@ -186,6 +204,18 @@ func ListByPage(req *ReqListSearchParams) (total, page int, list gdb.Result, err
if req.PublishedTimeEnd != "" {
model = model.Where("news.published_time <=?", utils.StrToTimestamp(req.PublishedTimeEnd))
}
if req.IsSlide != "" {
model = model.Where("news.is_slide", gconv.Int(req.IsSlide))
}
if req.IsTop != "" {
model = model.Where("news.is_top", gconv.Int(req.IsTop))
}
if req.Recommended != "" {
model = model.Where("news.recommended", gconv.Int(req.Recommended))
}
if req.NewsStatus != "" {
model = model.Where("news.news_status", gconv.Int(req.NewsStatus))
}
}
model = model.LeftJoin(user.Table+" user", "news.user_id=user.id")
total, err = model.Count()
@@ -201,13 +231,20 @@ func ListByPage(req *ReqListSearchParams) (total, page int, list gdb.Result, err
if req.PageSize == 0 {
req.PageSize = service.AdminPageNum
}
list, err = model.Page(page, req.PageSize).Fields("news.*,user.user_nickname").Order("published_time desc,news.id desc").All()
var datas gdb.Result
order := "published_time desc,news.id desc"
if req.OrderBy != "" {
order = req.OrderBy
}
datas, err = model.Page(page, req.PageSize).Fields("news.*,user.user_nickname").
Order(order).All()
if err != nil {
g.Log().Error(err)
err = gerror.New("获取数据失败")
return
}
list = make([]*NewsList, len(datas))
err = datas.Structs(&list)
return
}
@@ -16,6 +16,7 @@ type Entity struct {
NewsStatus uint `orm:"news_status" json:"news_status"` // 状态;1:已发布;0:未发布;
IsTop uint `orm:"is_top" json:"is_top"` // 是否置顶;1:置顶;0:不置顶
Recommended uint `orm:"recommended" json:"recommended"` // 是否推荐;1:推荐;0:不推荐
IsSlide uint `orm:"is_slide" json:"is_slide"` // 是否幻灯;1:是;0:否
NewsHits uint64 `orm:"news_hits" json:"news_hits"` // 查看数
NewsLike uint64 `orm:"news_like" json:"news_like"` // 点赞数
CreateTime uint `orm:"create_time" json:"create_time"` // 创建时间
@@ -29,6 +29,7 @@ var (
NewsStatus string // 状态;1:已发布;0:未发布;
IsTop string // 是否置顶;1:置顶;0:不置顶
Recommended string // 是否推荐;1:推荐;0:不推荐
IsSlide string //是否幻灯 1是 0 否
NewsHits string // 查看数
NewsLike string // 点赞数
CreateTime string // 创建时间
@@ -48,6 +49,7 @@ var (
NewsStatus: "news_status",
IsTop: "is_top",
Recommended: "recommended",
IsSlide: "is_slide",
NewsHits: "news_hits",
NewsLike: "news_like",
CreateTime: "create_time",
+3 -3
View File
@@ -102,7 +102,7 @@ func getPubCateIds(cateIds []int) ([]int, error) {
}
//文章列表查询
func NewsListByPage(req *cms_news.ReqListSearchParams) (total, page int, list gdb.Result, err error) {
func NewsListByPage(req *cms_news.ReqListSearchParams) (total, page int, list []*cms_news.NewsList, err error) {
var menuList []*cms_category.Entity
//获取所有栏目
menuList, err = GetMenuList()
@@ -131,7 +131,7 @@ func NewsListByPage(req *cms_news.ReqListSearchParams) (total, page int, list gd
//匹配文章所属栏目
var cateIds []int
for _, v := range list {
cateIds, err = GetCheckedCategoryIdByNewsId(gconv.Uint64(v["id"]))
cateIds, err = GetCheckedCategoryIdByNewsId(gconv.Uint64(v.Id))
if err != nil {
return
}
@@ -147,7 +147,7 @@ func NewsListByPage(req *cms_news.ReqListSearchParams) (total, page int, list gd
if len(cateNameList) > 0 {
cateVal.Set(cateNameList)
}
v["cateList"] = cateVal
v.CateList = cateVal
}
return
}
+103
View File
@@ -0,0 +1,103 @@
package menu_service
import (
"fmt"
"gfast/app/model/admin/cms_category"
"gfast/library/utils"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
type Menus struct {
*cms_category.Entity
Children []*Menus `json:"children"`
}
//获取导出菜单
func GetNav(activeId uint64) (nav string, err error) {
var menuList []*cms_category.Entity
menuList, err = cms_category.GetList()
if err != nil {
return
}
if menuList != nil {
//剔除隐藏的菜单
menus := make([]*cms_category.Entity, 0, len(menuList))
for _, v := range menuList {
if v.Status == 1 {
menus = append(menus, v)
}
}
topIds := GetTopPidList(menus)
topIds.Iterator(func(k int, v int) bool {
nav += SetTreeMenu(menus, v, activeId, "sf-menu sf-arrows")
return true
})
}
return
}
//获取菜单树形结构
func SetTreeMenu(menuList []*cms_category.Entity, pid int, activeId uint64, class string) string {
parentId := gconv.Int64(pid)
html := fmt.Sprintf(`<ul class="%s">`, class)
//获取最顶级
var topMenus g.Map
menuListMap := make(g.List, len(menuList))
for k, m := range menuList {
menuListMap[k] = gconv.Map(m)
}
if activeId != 0 {
topMenus = utils.FindTopParent(menuListMap, gconv.Int64(activeId), "parent_id", "id")
}
for k, v := range menuList {
if v.ParentId == parentId {
class := ""
if topMenus != nil && topMenus["id"] == v.Id {
class = "active"
} else if activeId == v.Id {
class = "active"
} else if activeId == 0 && k == 0 {
class = "active"
}
for _, vv := range menuList {
if gconv.Uint64(vv.ParentId) == v.Id {
class += " sf-with-ul"
break
}
}
url := fmt.Sprintf("/cms/list/%d", v.Id)
if v.CateType == 3 {
url = v.CateAddress
}
html += fmt.Sprintf(`<li><a href="%s" class="%s">%s</a>`, url, class, v.Name)
html += SetTreeMenu(menuList, gconv.Int(v.Id), activeId, "sub-menu")
html += "</li>"
}
}
if html == "<ul>" {
return ""
}
html += "</ul>"
return html
}
//获取顶级菜单ID
func GetTopPidList(menuList []*cms_category.Entity) (ids *garray.IntArray) {
ids = garray.NewIntArray()
for _, v1 := range menuList {
tag := true
for _, v2 := range menuList {
if gconv.Uint64(v1.ParentId) == v2.Id {
tag = false
break
}
}
if tag {
ids.PushRight(gconv.Int(v1.ParentId))
}
}
ids = ids.Unique()
return
}
+28
View File
@@ -0,0 +1,28 @@
package news_service
import (
"gfast/app/model/admin/cms_news"
"gfast/app/service/admin/cms_service"
"github.com/gogf/gf/frame/g"
)
func GetNewsList(cateId int, pageSize int, attr ...*cms_news.ReqListSearchParams) (list []*cms_news.NewsList, err error) {
var cateIds []int
if cateId != 0 {
cateIds = append(cateIds, cateId)
}
var req *cms_news.ReqListSearchParams
if len(attr) > 0 {
req = attr[0]
} else {
req = &cms_news.ReqListSearchParams{}
}
req.CateId = cateIds
req.PageSize = pageSize
req.NewsStatus = "1"
_, _, list, err = cms_service.NewsListByPage(req)
if err != nil {
g.Log().Error(err)
}
return list, err
}