调试frontend
This commit is contained in:
+1087
-1037
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,154 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"server/models"
|
||||
"server/pkg/jwtutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
type BackendMenuFrontController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *BackendMenuFrontController) checkAuth() (uint64, bool) {
|
||||
authHeader := c.Ctx.Request.Header.Get("Authorization")
|
||||
if authHeader == "" {
|
||||
c.Data["json"] = map[string]interface{}{"code": 401, "msg": "未提供认证信息"}
|
||||
_ = c.ServeJSON()
|
||||
return 0, false
|
||||
}
|
||||
authParts := strings.SplitN(authHeader, " ", 2)
|
||||
if len(authParts) != 2 || authParts[0] != "Bearer" {
|
||||
c.Data["json"] = map[string]interface{}{"code": 401, "msg": "认证信息格式错误"}
|
||||
_ = c.ServeJSON()
|
||||
return 0, false
|
||||
}
|
||||
claims, err := jwtutil.ParseToken(authParts[1])
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 401, "msg": "token无效"}
|
||||
_ = c.ServeJSON()
|
||||
return 0, false
|
||||
}
|
||||
return uint64(claims.TenantId), true
|
||||
}
|
||||
|
||||
func (c *BackendMenuFrontController) List() {
|
||||
tid, ok := c.checkAuth()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var menus []models.BackendMenuFront
|
||||
_, err := models.Orm.QueryTable("yz_backend_menu_front").
|
||||
Filter("tenant_id", tid).
|
||||
Filter("delete_time__isnull", true).
|
||||
OrderBy("sort").
|
||||
All(&menus)
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "查询失败"}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": menus}
|
||||
}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *BackendMenuFrontController) Create() {
|
||||
tid, ok := c.checkAuth()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var menu models.BackendMenuFront
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &menu); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
menu.TenantID = tid
|
||||
menu.CreateTime = nil // 由orm自动处理
|
||||
_, err := models.Orm.Insert(&menu)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "创建失败"}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "创建成功"}
|
||||
}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *BackendMenuFrontController) Update() {
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
tid, ok := c.checkAuth()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var menu models.BackendMenuFront
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &menu); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
num, err := models.Orm.QueryTable("yz_backend_menu_front").
|
||||
Filter("id", id).
|
||||
Filter("tenant_id", tid).
|
||||
Update(map[string]interface{}{
|
||||
"pid": menu.Pid,
|
||||
"title": menu.Title,
|
||||
"path": menu.Path,
|
||||
"component_path": menu.ComponentPath,
|
||||
"icon": menu.Icon,
|
||||
"sort": menu.Sort,
|
||||
"status": menu.Status,
|
||||
"is_visible": menu.IsVisible,
|
||||
"type": menu.Type,
|
||||
"permission": menu.Permission,
|
||||
"update_time": time.Now(),
|
||||
})
|
||||
|
||||
if err != nil || num == 0 {
|
||||
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "更新失败"}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "更新成功"}
|
||||
}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *BackendMenuFrontController) Delete() {
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
tid, ok := c.checkAuth()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
num, err := models.Orm.QueryTable("yz_backend_menu_front").
|
||||
Filter("id", id).
|
||||
Filter("tenant_id", tid).
|
||||
Update(map[string]interface{}{"delete_time": time.Now()})
|
||||
|
||||
if err != nil || num == 0 {
|
||||
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "删除失败"}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
|
||||
}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -37,8 +39,54 @@ func (c *IndexPortalController) getTidByHost() uint64 {
|
||||
return 1 // 默认租户
|
||||
}
|
||||
|
||||
// GetHeadMenu GET /index/headmenu
|
||||
// 获取后端菜单数据(支持开发环境租户切换)
|
||||
func (c *IndexPortalController) GetHeadMenu() {
|
||||
// DEV 环境下支持通过 X-Tenant-ID 切换租户
|
||||
runmode, _ := beego.AppConfig.String("runmode")
|
||||
tenantName := c.Ctx.Request.Header.Get("X-Tenant-ID")
|
||||
|
||||
// 解码
|
||||
if decoded, err := url.QueryUnescape(tenantName); err == nil {
|
||||
tenantName = decoded
|
||||
}
|
||||
|
||||
// 强制打印,确保你能从终端看到真实值
|
||||
fmt.Printf("[DEBUG] runmode: %s, Decoded TenantName: %s\n", runmode, tenantName)
|
||||
|
||||
if runmode == "dev" && tenantName != "" {
|
||||
// 查找租户 ID - 支持 name 或 short_name
|
||||
var tenant models.SystemTenant
|
||||
// 使用 OR 逻辑查询 name 或 short_name
|
||||
cond := orm.NewCondition()
|
||||
cond = cond.Or("tenant_name", tenantName).Or("tenant_short_name", tenantName)
|
||||
|
||||
err := models.Orm.QueryTable(new(models.SystemTenant)).
|
||||
SetCond(cond).
|
||||
One(&tenant)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("[DEBUG] QueryTenant Error: %v, Looking for: %s\n", err, tenantName)
|
||||
} else {
|
||||
fmt.Printf("[DEBUG] Found Tenant ID: %d\n", tenant.ID)
|
||||
// 查询该租户的自定义菜单
|
||||
var menus []models.BackendMenuFront
|
||||
_, err = models.Orm.QueryTable("yz_backend_menu_front").
|
||||
Filter("tenant_id", tenant.ID).
|
||||
Filter("delete_time__isnull", true).
|
||||
OrderBy("sort").
|
||||
All(&menus)
|
||||
if err == nil && len(menus) > 0 {
|
||||
// 转换数据格式供前端使用
|
||||
menuData := buildFrontMenuTree(menus, 0)
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": menuData}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
} else {
|
||||
fmt.Printf("[DEBUG] No menus found for tenant: %d\n", tenant.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 前台的静态导航菜单结构
|
||||
menuData := []map[string]interface{}{
|
||||
{
|
||||
@@ -103,6 +151,27 @@ func (c *IndexPortalController) GetHeadMenu() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// 辅助构建树结构
|
||||
func buildFrontMenuTree(menus []models.BackendMenuFront, pid int64) []map[string]interface{} {
|
||||
var tree []map[string]interface{}
|
||||
for _, m := range menus {
|
||||
if int64(m.Pid) == pid {
|
||||
node := map[string]interface{}{
|
||||
"id": m.ID,
|
||||
"title": m.Title,
|
||||
"path": m.Path,
|
||||
"type": m.Type,
|
||||
}
|
||||
children := buildFrontMenuTree(menus, int64(m.ID))
|
||||
if len(children) > 0 {
|
||||
node["children"] = children
|
||||
}
|
||||
tree = append(tree, node)
|
||||
}
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
||||
// GetFooterData GET /index/footerdata
|
||||
func (c *IndexPortalController) GetFooterData() {
|
||||
tid := c.getTidByHost()
|
||||
@@ -296,7 +365,7 @@ func (c *IndexPortalController) GetCompanyNewsDetail() {
|
||||
c.getArticleDetail()
|
||||
}
|
||||
|
||||
// GetKingdeeNewsDetail GET /index/kingdeenews/detail/:id
|
||||
// GetKingdeeNewsDetail GET /index/kingdeeNews/detail/:id
|
||||
func (c *IndexPortalController) GetKingdeeNewsDetail() {
|
||||
c.getArticleDetail()
|
||||
}
|
||||
@@ -393,4 +462,4 @@ func (c *IndexPortalController) GetOnePageByPath() {
|
||||
"data": nil,
|
||||
}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
type BackendMenuFront struct {
|
||||
ID uint64 `orm:"column(id);pk;auto" json:"id"`
|
||||
TenantID uint64 `orm:"column(tenant_id)" json:"tenant_id"`
|
||||
Pid int64 `orm:"column(pid)" json:"pid"`
|
||||
Title string `orm:"column(title)" json:"title"`
|
||||
Path string `orm:"column(path)" json:"path"`
|
||||
ComponentPath string `orm:"column(component_path)" json:"component_path"`
|
||||
Icon string `orm:"column(icon)" json:"icon"`
|
||||
Sort int `orm:"column(sort)" json:"sort"`
|
||||
Status int8 `orm:"column(status)" json:"status"`
|
||||
IsVisible int8 `orm:"column(is_visible)" json:"is_visible"`
|
||||
Type int8 `orm:"column(type)" json:"type"`
|
||||
Permission string `orm:"column(permission)" json:"permission"`
|
||||
CreateTime *time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"create_time"`
|
||||
UpdateTime *time.Time `orm:"column(update_time);auto_now;type(datetime)" json:"update_time"`
|
||||
DeleteTime *time.Time `orm:"column(delete_time);null;type(datetime)" json:"delete_time"`
|
||||
}
|
||||
|
||||
func (m *BackendMenuFront) TableName() string {
|
||||
return "yz_backend_menu_front"
|
||||
}
|
||||
|
||||
func init() {
|
||||
orm.RegisterModel(new(BackendMenuFront))
|
||||
}
|
||||
+200
-159
@@ -1,159 +1,200 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
// CmsArticleCategory CMS 文章分类 yz_cms_article_category
|
||||
type CmsArticleCategory struct {
|
||||
ID uint64 `orm:"column(id);pk;auto" json:"id"`
|
||||
Tid uint64 `orm:"column(tid);default(0)" json:"tid"`
|
||||
Cid uint64 `orm:"column(cid);default(0)" json:"cid"`
|
||||
Name string `orm:"column(name);size(100)" json:"name"`
|
||||
Image string `orm:"column(image);size(500);default()" json:"image"`
|
||||
Desc string `orm:"column(desc);size(500);default()" json:"desc"`
|
||||
Sort int `orm:"column(sort);default(0)" json:"sort"`
|
||||
Status int8 `orm:"column(status);default(1)" json:"status"`
|
||||
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"create_time"`
|
||||
UpdateTime *time.Time `orm:"column(update_time);type(datetime);null" json:"update_time"`
|
||||
DeleteTime *time.Time `orm:"column(delete_time);type(datetime);null" json:"delete_time"`
|
||||
}
|
||||
|
||||
func (m *CmsArticleCategory) TableName() string {
|
||||
return "yz_cms_article_category"
|
||||
}
|
||||
|
||||
// CmsArticle CMS 文章 yz_cms_article
|
||||
type CmsArticle struct {
|
||||
ID uint64 `orm:"column(id);pk;auto" json:"id"`
|
||||
Tid uint64 `orm:"column(tid);default(0)" json:"tid"`
|
||||
Title string `orm:"column(title);size(255)" json:"title"`
|
||||
Author string `orm:"column(author);size(100);default()" json:"author"`
|
||||
CateID uint64 `orm:"column(cate_id);default(0)" json:"cate_id"`
|
||||
Content string `orm:"column(content);type(mediumtext);null" json:"content"`
|
||||
Desc string `orm:"column(desc);size(500);default()" json:"desc"`
|
||||
Image string `orm:"column(image);size(500);default()" json:"image"`
|
||||
IsTrans int8 `orm:"column(is_trans);default(0)" json:"is_trans"`
|
||||
TransURL *string `orm:"column(transurl);size(500);null" json:"transurl"`
|
||||
Status int8 `orm:"column(status);default(0)" json:"status"`
|
||||
Top int8 `orm:"column(top);default(0)" json:"top"`
|
||||
Recommend int8 `orm:"column(recommend);default(0)" json:"recommend"`
|
||||
Views int `orm:"column(views);default(0)" json:"views"`
|
||||
Likes int `orm:"column(likes);default(0)" json:"likes"`
|
||||
PublisherID *uint64 `orm:"column(publisher_id);null" json:"publisher_id"`
|
||||
PublishTime *time.Time `orm:"column(publish_time);type(datetime);null" json:"publish_time"`
|
||||
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"create_time"`
|
||||
UpdateTime *time.Time `orm:"column(update_time);type(datetime);null" json:"update_time"`
|
||||
DeleteTime *time.Time `orm:"column(delete_time);type(datetime);null" json:"delete_time"`
|
||||
}
|
||||
|
||||
func (m *CmsArticle) TableName() string {
|
||||
return "yz_cms_article"
|
||||
}
|
||||
|
||||
var cmsArticleTablesOnce sync.Once
|
||||
|
||||
// EnsureCmsArticleTables 首次使用时自动建表(若不存在)。
|
||||
func EnsureCmsArticleTables() error {
|
||||
var err error
|
||||
cmsArticleTablesOnce.Do(func() {
|
||||
_, err = Orm.Raw(`
|
||||
CREATE TABLE IF NOT EXISTS yz_cms_article_category (
|
||||
id bigint unsigned NOT NULL AUTO_INCREMENT,
|
||||
tid bigint unsigned NOT NULL DEFAULT 0,
|
||||
cid bigint unsigned NOT NULL DEFAULT 0,
|
||||
name varchar(100) NOT NULL DEFAULT '',
|
||||
image varchar(500) NOT NULL DEFAULT '',
|
||||
` + "`desc`" + ` varchar(500) NOT NULL DEFAULT '',
|
||||
sort int NOT NULL DEFAULT 0,
|
||||
status tinyint NOT NULL DEFAULT 1,
|
||||
create_time datetime NOT NULL,
|
||||
update_time datetime DEFAULT NULL,
|
||||
delete_time datetime DEFAULT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_tid_cid (tid, cid)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`).Exec()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = Orm.Raw(`
|
||||
CREATE TABLE IF NOT EXISTS yz_cms_article (
|
||||
id bigint unsigned NOT NULL AUTO_INCREMENT,
|
||||
tid bigint unsigned NOT NULL DEFAULT 0,
|
||||
title varchar(255) NOT NULL DEFAULT '',
|
||||
author varchar(100) NOT NULL DEFAULT '',
|
||||
cate_id bigint unsigned NOT NULL DEFAULT 0,
|
||||
content mediumtext,
|
||||
` + "`desc`" + ` varchar(500) NOT NULL DEFAULT '',
|
||||
image varchar(500) NOT NULL DEFAULT '',
|
||||
is_trans tinyint NOT NULL DEFAULT 0,
|
||||
transurl varchar(500) DEFAULT NULL,
|
||||
status tinyint NOT NULL DEFAULT 0,
|
||||
top tinyint NOT NULL DEFAULT 0,
|
||||
recommend tinyint NOT NULL DEFAULT 0,
|
||||
views int NOT NULL DEFAULT 0,
|
||||
likes int NOT NULL DEFAULT 0,
|
||||
publisher_id bigint unsigned DEFAULT NULL,
|
||||
publish_time datetime DEFAULT NULL,
|
||||
create_time datetime NOT NULL,
|
||||
update_time datetime DEFAULT NULL,
|
||||
delete_time datetime DEFAULT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_tid_status (tid, status),
|
||||
KEY idx_cate_id (cate_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`).Exec()
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func CmsCategoryNameMap(tid uint64, ids []uint64) map[uint64]string {
|
||||
out := make(map[uint64]string)
|
||||
if len(ids) == 0 {
|
||||
return out
|
||||
}
|
||||
var rows []CmsArticleCategory
|
||||
_, _ = Orm.QueryTable(new(CmsArticleCategory)).
|
||||
Filter("tid", tid).
|
||||
Filter("id__in", ids).
|
||||
Filter("delete_time__isnull", true).
|
||||
All(&rows, "ID", "Name")
|
||||
for _, r := range rows {
|
||||
out[r.ID] = r.Name
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func CmsFormatTime(t *time.Time) string {
|
||||
if t == nil {
|
||||
return ""
|
||||
}
|
||||
return t.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
func CmsSimilarArticles(tid uint64, title string, limit int) ([]orm.Params, error) {
|
||||
if limit <= 0 {
|
||||
limit = 5
|
||||
}
|
||||
var rows []CmsArticle
|
||||
_, err := Orm.QueryTable(new(CmsArticle)).
|
||||
Filter("tid", tid).
|
||||
Filter("delete_time__isnull", true).
|
||||
Filter("title__icontains", title).
|
||||
Limit(limit).
|
||||
All(&rows, "ID", "Title")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]orm.Params, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, orm.Params{
|
||||
"id": r.ID,
|
||||
"title": r.Title,
|
||||
"similarity": 80,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
package models
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
// CmsArticleCategory CMS 文章分类 yz_cms_article_category
|
||||
type CmsArticleCategory struct {
|
||||
ID uint64 `orm:"column(id);pk;auto" json:"id"`
|
||||
Tid uint64 `orm:"column(tid);default(0)" json:"tid"`
|
||||
Cid uint64 `orm:"column(cid);default(0)" json:"cid"`
|
||||
Name string `orm:"column(name);size(100)" json:"name"`
|
||||
Image string `orm:"column(image);size(500);default()" json:"image"`
|
||||
Desc string `orm:"column(desc);size(500);default()" json:"desc"`
|
||||
Sort int `orm:"column(sort);default(0)" json:"sort"`
|
||||
Status int8 `orm:"column(status);default(1)" json:"status"`
|
||||
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"create_time"`
|
||||
UpdateTime *time.Time `orm:"column(update_time);type(datetime);null" json:"update_time"`
|
||||
DeleteTime *time.Time `orm:"column(delete_time);type(datetime);null" json:"delete_time"`
|
||||
}
|
||||
|
||||
func (m *CmsArticleCategory) TableName() string {
|
||||
return "yz_cms_article_category"
|
||||
}
|
||||
|
||||
// CmsArticle CMS 文章 yz_cms_article
|
||||
type CmsArticle struct {
|
||||
ID uint64 `orm:"column(id);pk;auto" json:"id"`
|
||||
Tid uint64 `orm:"column(tid);default(0)" json:"tid"`
|
||||
Title string `orm:"column(title);size(255)" json:"title"`
|
||||
Author string `orm:"column(author);size(100);default()" json:"author"`
|
||||
CateID uint64 `orm:"column(cate_id);default(0)" json:"cate_id"`
|
||||
Content string `orm:"column(content);type(mediumtext);null" json:"content"`
|
||||
Desc string `orm:"column(desc);size(500);default()" json:"desc"`
|
||||
Image string `orm:"column(image);size(500);default()" json:"image"`
|
||||
IsTrans int8 `orm:"column(is_trans);default(0)" json:"is_trans"`
|
||||
TransURL *string `orm:"column(transurl);size(500);null" json:"transurl"`
|
||||
Status int8 `orm:"column(status);default(0)" json:"status"`
|
||||
Top int8 `orm:"column(top);default(0)" json:"top"`
|
||||
Recommend int8 `orm:"column(recommend);default(0)" json:"recommend"`
|
||||
Views int `orm:"column(views);default(0)" json:"views"`
|
||||
Likes int `orm:"column(likes);default(0)" json:"likes"`
|
||||
PublisherID *uint64 `orm:"column(publisher_id);null" json:"publisher_id"`
|
||||
PublishTime *time.Time `orm:"column(publish_time);type(datetime);null" json:"publish_time"`
|
||||
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"create_time"`
|
||||
UpdateTime *time.Time `orm:"column(update_time);type(datetime);null" json:"update_time"`
|
||||
DeleteTime *time.Time `orm:"column(delete_time);type(datetime);null" json:"delete_time"`
|
||||
}
|
||||
|
||||
func (m *CmsArticle) TableName() string {
|
||||
return "yz_cms_article"
|
||||
}
|
||||
|
||||
var cmsArticleTablesOnce sync.Once
|
||||
|
||||
// EnsureCmsArticleTables 首次使用时自动建表(若不存在)。
|
||||
func EnsureCmsArticleTables() error {
|
||||
var err error
|
||||
cmsArticleTablesOnce.Do(func() {
|
||||
_, err = Orm.Raw(`
|
||||
CREATE TABLE IF NOT EXISTS yz_cms_article_category (
|
||||
id bigint unsigned NOT NULL AUTO_INCREMENT,
|
||||
tid bigint unsigned NOT NULL DEFAULT 0,
|
||||
cid bigint unsigned NOT NULL DEFAULT 0,
|
||||
name varchar(100) NOT NULL DEFAULT '',
|
||||
image varchar(500) NOT NULL DEFAULT '',
|
||||
` + "`desc`" + ` varchar(500) NOT NULL DEFAULT '',
|
||||
sort int NOT NULL DEFAULT 0,
|
||||
status tinyint NOT NULL DEFAULT 1,
|
||||
create_time datetime NOT NULL,
|
||||
update_time datetime DEFAULT NULL,
|
||||
delete_time datetime DEFAULT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_tid_cid (tid, cid)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`).Exec()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = Orm.Raw(`
|
||||
CREATE TABLE IF NOT EXISTS yz_cms_article (
|
||||
id bigint unsigned NOT NULL AUTO_INCREMENT,
|
||||
tid bigint unsigned NOT NULL DEFAULT 0,
|
||||
title varchar(255) NOT NULL DEFAULT '',
|
||||
author varchar(100) NOT NULL DEFAULT '',
|
||||
cate_id bigint unsigned NOT NULL DEFAULT 0,
|
||||
content mediumtext,
|
||||
` + "`desc`" + ` varchar(500) NOT NULL DEFAULT '',
|
||||
image varchar(500) NOT NULL DEFAULT '',
|
||||
is_trans tinyint NOT NULL DEFAULT 0,
|
||||
transurl varchar(500) DEFAULT NULL,
|
||||
status tinyint NOT NULL DEFAULT 0,
|
||||
top tinyint NOT NULL DEFAULT 0,
|
||||
recommend tinyint NOT NULL DEFAULT 0,
|
||||
views int NOT NULL DEFAULT 0,
|
||||
likes int NOT NULL DEFAULT 0,
|
||||
publisher_id bigint unsigned DEFAULT NULL,
|
||||
publish_time datetime DEFAULT NULL,
|
||||
create_time datetime NOT NULL,
|
||||
update_time datetime DEFAULT NULL,
|
||||
delete_time datetime DEFAULT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_tid_status (tid, status),
|
||||
KEY idx_cate_id (cate_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`).Exec()
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func EnsureCmsArticleDefaultCategories() error {
|
||||
// 只初始化两级全局分类:文章中心(顶级)和新闻中心(文章中心的子分类)。
|
||||
// 不在启动时创建其它业务分类,后续分类由管理员按需新增。
|
||||
defaults := []struct {
|
||||
name string
|
||||
cid uint64
|
||||
sort int
|
||||
}{
|
||||
{name: "文章中心", cid: 0, sort: 1},
|
||||
{name: "新闻中心", cid: 1, sort: 2},
|
||||
}
|
||||
|
||||
for _, item := range defaults {
|
||||
count, err := Orm.QueryTable(new(CmsArticleCategory)).
|
||||
Filter("tid", 0).
|
||||
Filter("name", item.name).
|
||||
Filter("cid", item.cid).
|
||||
Filter("delete_time__isnull", true).
|
||||
Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
continue
|
||||
}
|
||||
now := time.Now()
|
||||
_, err = Orm.Insert(&CmsArticleCategory{
|
||||
Tid: 0,
|
||||
Cid: item.cid,
|
||||
Name: item.name,
|
||||
Sort: item.sort,
|
||||
Status: 1,
|
||||
CreateTime: now,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CmsCategoryNameMap(tid uint64, ids []uint64) map[uint64]string {
|
||||
out := make(map[uint64]string)
|
||||
if len(ids) == 0 {
|
||||
return out
|
||||
}
|
||||
var rows []CmsArticleCategory
|
||||
qs := Orm.QueryTable(new(CmsArticleCategory)).
|
||||
Filter("id__in", ids).
|
||||
Filter("delete_time__isnull", true)
|
||||
cond := orm.NewCondition().Or("tid", 0).Or("tid", tid)
|
||||
_, _ = qs.SetCond(cond).All(&rows, "ID", "Name")
|
||||
for _, r := range rows {
|
||||
out[r.ID] = r.Name
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func CmsFormatTime(t *time.Time) string {
|
||||
if t == nil {
|
||||
return ""
|
||||
}
|
||||
return t.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
func CmsSimilarArticles(tid uint64, title string, limit int) ([]orm.Params, error) {
|
||||
if limit <= 0 {
|
||||
limit = 5
|
||||
}
|
||||
var rows []CmsArticle
|
||||
_, err := Orm.QueryTable(new(CmsArticle)).
|
||||
Filter("tid", tid).
|
||||
Filter("delete_time__isnull", true).
|
||||
Filter("title__icontains", title).
|
||||
Limit(limit).
|
||||
All(&rows, "ID", "Title")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]orm.Params, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, orm.Params{
|
||||
"id": r.ID,
|
||||
"title": r.Title,
|
||||
"similarity": 80,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -31,7 +31,9 @@ func RegisterAuthRoutes() {
|
||||
|
||||
// 菜单接口
|
||||
beego.Router("/backend/menu/:id", &controllers.BackendMenuController{}, "get:GetBackendMenu")
|
||||
beego.Router("/backend/allmenu", &controllers.BackendMenuController{}, "get:GetAllBackendMenus")
|
||||
// 前端菜单接口
|
||||
beego.Router("/backend/frontmenus", &controllers.BackendMenuFrontController{}, "get:List;post:Create")
|
||||
beego.Router("/backend/frontmenus/:id", &controllers.BackendMenuFrontController{}, "post:Update;delete:Delete")
|
||||
|
||||
// 操作日志(yz_system_operation_log)
|
||||
beego.Router("/backend/operationLogs", &controllers.BackendOperationLogController{}, "get:List")
|
||||
|
||||
Reference in New Issue
Block a user