优化企业网站功能

This commit is contained in:
2026-08-20 16:21:07 +08:00
parent 112d60b993
commit 3f0fcb88e7
45 changed files with 8758 additions and 5058 deletions
-6
View File
@@ -88,12 +88,6 @@ func cmsEnsureTables(c *beego.Controller) bool {
_ = c.ServeJSON()
return false
}
if err := models.EnsureCmsArticleDefaultCategories(); err != nil {
c.Ctx.Output.SetStatus(500)
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "初始化文章分类失败: " + err.Error()}
_ = c.ServeJSON()
return false
}
return true
}
+13 -3
View File
@@ -34,6 +34,12 @@ func (c *BackendMenuFrontController) checkAuth() (uint64, bool) {
_ = c.ServeJSON()
return 0, false
}
// tenant_id=0 为全局默认菜单保留值,不允许任何接口以租户身份操作
if claims.TenantId <= 0 {
c.Data["json"] = map[string]interface{}{"code": 401, "msg": "租户ID缺失"}
_ = c.ServeJSON()
return 0, false
}
return uint64(claims.TenantId), true
}
@@ -43,11 +49,15 @@ func (c *BackendMenuFrontController) List() {
return
}
// 保证全局默认导航(tenant_id=0)齐全,缺失自动补齐
models.EnsureGlobalDefaultFrontMenus()
// 全局默认菜单(tenant_id=0,不可删改)+ 租户自定义菜单
var menus []models.BackendMenuFront
_, err := models.Orm.QueryTable("yz_backend_menu_front").
Filter("tenant_id", tid).
Filter("tenant_id__in", []uint64{0, tid}).
Filter("delete_time__isnull", true).
OrderBy("sort").
OrderBy("sort", "id").
All(&menus)
if err != nil {
@@ -151,4 +161,4 @@ func (c *BackendMenuFrontController) Delete() {
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
}
_ = c.ServeJSON()
}
}
+528
View File
@@ -0,0 +1,528 @@
package controllers
import (
"encoding/json"
"io"
"strings"
"time"
"server/models"
"server/pkg/jwtutil"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
)
// BackendProductController CMS 产品管理
type BackendProductController struct {
beego.Controller
}
// BackendProductCategoryController CMS 产品分类管理
type BackendProductCategoryController struct {
beego.Controller
}
func (c *BackendProductController) cmsClaims() (*jwtutil.Claims, error) {
return cmsBackendClaims(&c.Controller)
}
func (c *BackendProductCategoryController) cmsClaims() (*jwtutil.Claims, error) {
return cmsBackendClaims(&c.Controller)
}
func (c *BackendProductController) cmsJSONErr(httpStatus, bizCode int, msg string) {
c.Ctx.Output.SetStatus(httpStatus)
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
_ = c.ServeJSON()
}
func (c *BackendProductCategoryController) cmsJSONErr(httpStatus, bizCode int, msg string) {
c.Ctx.Output.SetStatus(httpStatus)
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
_ = c.ServeJSON()
}
func cmsEnsureProductTables(c *beego.Controller) bool {
if err := models.EnsureCmsProductTables(); err != nil {
c.Ctx.Output.SetStatus(500)
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "初始化产品表失败: " + err.Error()}
_ = c.ServeJSON()
return false
}
return true
}
func cmsProductToMap(row models.CmsProduct) map[string]interface{} {
return map[string]interface{}{
"id": row.ID,
"tid": row.Tid,
"title": row.Title,
"thumb": row.Thumb,
"desc": row.Desc,
"content": row.Content,
"url": row.URL,
"sort": row.Sort,
"status": row.Status,
"create_time": row.CreateTime.Format("2006-01-02 15:04:05"),
"update_time": models.CmsFormatTime(row.UpdateTime),
}
}
func cmsProductCateToMap(row models.CmsProductCategory) map[string]interface{} {
return map[string]interface{}{
"id": row.ID,
"tid": row.Tid,
"title": row.Title,
"pid": row.Pid,
"desc": row.Desc,
"sort": row.Sort,
"status": row.Status,
"create_time": row.CreateTime.Format("2006-01-02 15:04:05"),
"update_time": models.CmsFormatTime(row.UpdateTime),
}
}
// List GET /backend/productsList
func (c *BackendProductController) List() {
if !cmsEnsureProductTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
if tid == 0 {
c.cmsJSONErr(400, 400, "tid不能为空")
return
}
page, _ := c.GetInt("page", 1)
limit, _ := c.GetInt("limit", 10)
if page < 1 {
page = 1
}
if limit < 1 || limit > 100 {
limit = 10
}
keyword := strings.TrimSpace(c.GetString("keyword"))
qs := models.Orm.QueryTable(new(models.CmsProduct)).
Filter("tid", tid).
Filter("delete_time__isnull", true)
if keyword != "" {
qs = qs.Filter("title__icontains", keyword)
}
if status, err := c.GetInt("status", -1); err == nil && status >= 0 {
qs = qs.Filter("status", status)
}
total, _ := qs.Count()
var rows []models.CmsProduct
offset := (page - 1) * limit
_, err = qs.OrderBy("sort", "-id").Limit(limit, offset).All(&rows)
if err != nil && err != orm.ErrNoRows {
c.cmsJSONErr(500, 500, "获取产品列表失败")
return
}
list := make([]map[string]interface{}, 0, len(rows))
for _, r := range rows {
list = append(list, cmsProductToMap(r))
}
c.Data["json"] = map[string]interface{}{
"code": 200,
"msg": "success",
"data": map[string]interface{}{"list": list, "total": total},
}
_ = c.ServeJSON()
}
type cmsProductPayload struct {
Title string `json:"title"`
Thumb string `json:"thumb"`
Desc string `json:"desc"`
Content string `json:"content"`
URL string `json:"url"`
Sort int `json:"sort"`
Status int8 `json:"status"`
}
// Create POST /backend/addProducts
func (c *BackendProductController) Create() {
if !cmsEnsureProductTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
if tid == 0 {
c.cmsJSONErr(400, 400, "tid不能为空")
return
}
raw, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
var p cmsProductPayload
if err := json.Unmarshal(raw, &p); err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
title := strings.TrimSpace(p.Title)
if title == "" {
c.cmsJSONErr(400, 400, "产品名称不能为空")
return
}
now := time.Now()
row := models.CmsProduct{
Tid: tid,
Title: title,
Thumb: strings.TrimSpace(p.Thumb),
Desc: strings.TrimSpace(p.Desc),
Content: p.Content,
URL: strings.TrimSpace(p.URL),
Sort: p.Sort,
Status: p.Status,
CreateTime: now,
UpdateTime: &now,
}
id, err := models.Orm.Insert(&row)
if err != nil {
c.cmsJSONErr(500, 500, "添加失败")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "添加成功", "data": map[string]interface{}{"id": id}}
_ = c.ServeJSON()
}
// Update PUT /backend/editProducts/:id
func (c *BackendProductController) Update() {
if !cmsEnsureProductTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
id, _ := c.GetUint64(":id")
if id == 0 {
c.cmsJSONErr(400, 400, "无效ID")
return
}
raw, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
var p cmsProductPayload
if err := json.Unmarshal(raw, &p); err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
title := strings.TrimSpace(p.Title)
if title == "" {
c.cmsJSONErr(400, 400, "产品名称不能为空")
return
}
now := time.Now()
n, err := models.Orm.QueryTable(new(models.CmsProduct)).
Filter("id", id).
Filter("tid", tid).
Filter("delete_time__isnull", true).
Update(orm.Params{
"title": title,
"thumb": strings.TrimSpace(p.Thumb),
"desc": strings.TrimSpace(p.Desc),
"content": p.Content,
"url": strings.TrimSpace(p.URL),
"sort": p.Sort,
"status": p.Status,
"update_time": now,
})
if err != nil {
c.cmsJSONErr(500, 500, "更新失败")
return
}
if n == 0 {
c.cmsJSONErr(404, 404, "产品不存在")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "更新成功"}
_ = c.ServeJSON()
}
// Delete DELETE /backend/deleteProducts/:id
func (c *BackendProductController) Delete() {
if !cmsEnsureProductTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
id, _ := c.GetUint64(":id")
if id == 0 {
c.cmsJSONErr(400, 400, "无效ID")
return
}
now := time.Now()
n, err := models.Orm.QueryTable(new(models.CmsProduct)).
Filter("id", id).
Filter("tid", tid).
Filter("delete_time__isnull", true).
Update(orm.Params{"delete_time": now})
if err != nil {
c.cmsJSONErr(500, 500, "删除失败")
return
}
if n == 0 {
c.cmsJSONErr(404, 404, "产品不存在")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
_ = c.ServeJSON()
}
// List GET /backend/productsTypesList
func (c *BackendProductCategoryController) List() {
if !cmsEnsureProductTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
if tid == 0 {
c.cmsJSONErr(400, 400, "tid不能为空")
return
}
page, _ := c.GetInt("page", 1)
limit, _ := c.GetInt("limit", 10)
if page < 1 {
page = 1
}
if limit < 1 || limit > 1000 {
limit = 10
}
keyword := strings.TrimSpace(c.GetString("keyword"))
qs := models.Orm.QueryTable(new(models.CmsProductCategory)).
Filter("tid", tid).
Filter("delete_time__isnull", true)
if keyword != "" {
qs = qs.Filter("title__icontains", keyword)
}
total, _ := qs.Count()
var rows []models.CmsProductCategory
offset := (page - 1) * limit
_, err = qs.OrderBy("sort", "id").Limit(limit, offset).All(&rows)
if err != nil && err != orm.ErrNoRows {
c.cmsJSONErr(500, 500, "获取产品分类列表失败")
return
}
list := make([]map[string]interface{}, 0, len(rows))
for _, r := range rows {
list = append(list, cmsProductCateToMap(r))
}
c.Data["json"] = map[string]interface{}{
"code": 200,
"msg": "success",
"data": map[string]interface{}{"list": list, "total": total},
}
_ = c.ServeJSON()
}
type cmsProductCategoryPayload struct {
Title string `json:"title"`
Pid uint64 `json:"pid"`
Desc string `json:"desc"`
Sort int `json:"sort"`
}
// Create POST /backend/addProductsTypes
func (c *BackendProductCategoryController) Create() {
if !cmsEnsureProductTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
if tid == 0 {
c.cmsJSONErr(400, 400, "tid不能为空")
return
}
raw, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
var p cmsProductCategoryPayload
if err := json.Unmarshal(raw, &p); err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
title := strings.TrimSpace(p.Title)
if title == "" {
c.cmsJSONErr(400, 400, "分类名称不能为空")
return
}
now := time.Now()
row := models.CmsProductCategory{
Tid: tid,
Title: title,
Pid: p.Pid,
Desc: strings.TrimSpace(p.Desc),
Sort: p.Sort,
Status: 1,
CreateTime: now,
UpdateTime: &now,
}
id, err := models.Orm.Insert(&row)
if err != nil {
c.cmsJSONErr(500, 500, "添加失败")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "添加成功", "data": map[string]interface{}{"id": id}}
_ = c.ServeJSON()
}
// Update PUT /backend/editProductsTypes/:id
func (c *BackendProductCategoryController) Update() {
if !cmsEnsureProductTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
id, _ := c.GetUint64(":id")
if id == 0 {
c.cmsJSONErr(400, 400, "无效ID")
return
}
raw, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
var p cmsProductCategoryPayload
if err := json.Unmarshal(raw, &p); err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
title := strings.TrimSpace(p.Title)
if title == "" {
c.cmsJSONErr(400, 400, "分类名称不能为空")
return
}
if p.Pid == id {
c.cmsJSONErr(400, 400, "父级分类不能是自己")
return
}
now := time.Now()
n, err := models.Orm.QueryTable(new(models.CmsProductCategory)).
Filter("id", id).
Filter("tid", tid).
Filter("delete_time__isnull", true).
Update(orm.Params{
"title": title,
"pid": p.Pid,
"desc": strings.TrimSpace(p.Desc),
"sort": p.Sort,
"update_time": now,
})
if err != nil {
c.cmsJSONErr(500, 500, "更新失败")
return
}
if n == 0 {
c.cmsJSONErr(404, 404, "分类不存在")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "更新成功"}
_ = c.ServeJSON()
}
// Delete DELETE /backend/deleteProductsTypes/:id
func (c *BackendProductCategoryController) Delete() {
if !cmsEnsureProductTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
id, _ := c.GetUint64(":id")
if id == 0 {
c.cmsJSONErr(400, 400, "无效ID")
return
}
// 存在子分类时不允许删除
childCnt, _ := models.Orm.QueryTable(new(models.CmsProductCategory)).
Filter("tid", tid).
Filter("pid", id).
Filter("delete_time__isnull", true).
Count()
if childCnt > 0 {
c.cmsJSONErr(400, 400, "该分类下存在子分类,请先删除子分类")
return
}
now := time.Now()
n, err := models.Orm.QueryTable(new(models.CmsProductCategory)).
Filter("id", id).
Filter("tid", tid).
Filter("delete_time__isnull", true).
Update(orm.Params{"delete_time": now})
if err != nil {
c.cmsJSONErr(500, 500, "删除失败")
return
}
if n == 0 {
c.cmsJSONErr(404, 404, "分类不存在")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
_ = c.ServeJSON()
}
+524
View File
@@ -0,0 +1,524 @@
package controllers
import (
"encoding/json"
"io"
"strings"
"time"
"server/models"
"server/pkg/jwtutil"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
)
// BackendSolutionController CMS 解决方案(特色服务)管理
type BackendSolutionController struct {
beego.Controller
}
// BackendSolutionCategoryController CMS 解决方案分类管理
type BackendSolutionCategoryController struct {
beego.Controller
}
func (c *BackendSolutionController) cmsClaims() (*jwtutil.Claims, error) {
return cmsBackendClaims(&c.Controller)
}
func (c *BackendSolutionCategoryController) cmsClaims() (*jwtutil.Claims, error) {
return cmsBackendClaims(&c.Controller)
}
func (c *BackendSolutionController) cmsJSONErr(httpStatus, bizCode int, msg string) {
c.Ctx.Output.SetStatus(httpStatus)
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
_ = c.ServeJSON()
}
func (c *BackendSolutionCategoryController) cmsJSONErr(httpStatus, bizCode int, msg string) {
c.Ctx.Output.SetStatus(httpStatus)
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
_ = c.ServeJSON()
}
func cmsEnsureSolutionTables(c *beego.Controller) bool {
if err := models.EnsureCmsSolutionTables(); err != nil {
c.Ctx.Output.SetStatus(500)
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "初始化解决方案表失败: " + err.Error()}
_ = c.ServeJSON()
return false
}
return true
}
func cmsSolutionToMap(row models.CmsSolution) map[string]interface{} {
return map[string]interface{}{
"id": row.ID,
"tid": row.Tid,
"title": row.Title,
"thumb": row.Thumb,
"desc": row.Desc,
"url": row.URL,
"sort": row.Sort,
"status": row.Status,
"create_time": row.CreateTime.Format("2006-01-02 15:04:05"),
"update_time": models.CmsFormatTime(row.UpdateTime),
}
}
func cmsSolutionCateToMap(row models.CmsSolutionCategory) map[string]interface{} {
return map[string]interface{}{
"id": row.ID,
"tid": row.Tid,
"title": row.Title,
"pid": row.Pid,
"desc": row.Desc,
"sort": row.Sort,
"status": row.Status,
"create_time": row.CreateTime.Format("2006-01-02 15:04:05"),
"update_time": models.CmsFormatTime(row.UpdateTime),
}
}
// List GET /backend/servicesList
func (c *BackendSolutionController) List() {
if !cmsEnsureSolutionTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
if tid == 0 {
c.cmsJSONErr(400, 400, "tid不能为空")
return
}
page, _ := c.GetInt("page", 1)
limit, _ := c.GetInt("limit", 10)
if page < 1 {
page = 1
}
if limit < 1 || limit > 100 {
limit = 10
}
keyword := strings.TrimSpace(c.GetString("keyword"))
qs := models.Orm.QueryTable(new(models.CmsSolution)).
Filter("tid", tid).
Filter("delete_time__isnull", true)
if keyword != "" {
qs = qs.Filter("title__icontains", keyword)
}
if status, err := c.GetInt("status", -1); err == nil && status >= 0 {
qs = qs.Filter("status", status)
}
total, _ := qs.Count()
var rows []models.CmsSolution
offset := (page - 1) * limit
_, err = qs.OrderBy("sort", "-id").Limit(limit, offset).All(&rows)
if err != nil && err != orm.ErrNoRows {
c.cmsJSONErr(500, 500, "获取解决方案列表失败")
return
}
list := make([]map[string]interface{}, 0, len(rows))
for _, r := range rows {
list = append(list, cmsSolutionToMap(r))
}
c.Data["json"] = map[string]interface{}{
"code": 200,
"msg": "success",
"data": map[string]interface{}{"list": list, "total": total},
}
_ = c.ServeJSON()
}
type cmsSolutionPayload struct {
Title string `json:"title"`
Thumb string `json:"thumb"`
Desc string `json:"desc"`
URL string `json:"url"`
Sort int `json:"sort"`
Status int8 `json:"status"`
}
// Create POST /backend/addServices
func (c *BackendSolutionController) Create() {
if !cmsEnsureSolutionTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
if tid == 0 {
c.cmsJSONErr(400, 400, "tid不能为空")
return
}
raw, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
var p cmsSolutionPayload
if err := json.Unmarshal(raw, &p); err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
title := strings.TrimSpace(p.Title)
if title == "" {
c.cmsJSONErr(400, 400, "名称不能为空")
return
}
now := time.Now()
row := models.CmsSolution{
Tid: tid,
Title: title,
Thumb: strings.TrimSpace(p.Thumb),
Desc: strings.TrimSpace(p.Desc),
URL: strings.TrimSpace(p.URL),
Sort: p.Sort,
Status: p.Status,
CreateTime: now,
UpdateTime: &now,
}
id, err := models.Orm.Insert(&row)
if err != nil {
c.cmsJSONErr(500, 500, "添加失败")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "添加成功", "data": map[string]interface{}{"id": id}}
_ = c.ServeJSON()
}
// Update PUT /backend/editServices/:id
func (c *BackendSolutionController) Update() {
if !cmsEnsureSolutionTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
id, _ := c.GetUint64(":id")
if id == 0 {
c.cmsJSONErr(400, 400, "无效ID")
return
}
raw, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
var p cmsSolutionPayload
if err := json.Unmarshal(raw, &p); err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
title := strings.TrimSpace(p.Title)
if title == "" {
c.cmsJSONErr(400, 400, "名称不能为空")
return
}
now := time.Now()
n, err := models.Orm.QueryTable(new(models.CmsSolution)).
Filter("id", id).
Filter("tid", tid).
Filter("delete_time__isnull", true).
Update(orm.Params{
"title": title,
"thumb": strings.TrimSpace(p.Thumb),
"desc": strings.TrimSpace(p.Desc),
"url": strings.TrimSpace(p.URL),
"sort": p.Sort,
"status": p.Status,
"update_time": now,
})
if err != nil {
c.cmsJSONErr(500, 500, "更新失败")
return
}
if n == 0 {
c.cmsJSONErr(404, 404, "记录不存在")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "更新成功"}
_ = c.ServeJSON()
}
// Delete DELETE /backend/deleteServices/:id
func (c *BackendSolutionController) Delete() {
if !cmsEnsureSolutionTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
id, _ := c.GetUint64(":id")
if id == 0 {
c.cmsJSONErr(400, 400, "无效ID")
return
}
now := time.Now()
n, err := models.Orm.QueryTable(new(models.CmsSolution)).
Filter("id", id).
Filter("tid", tid).
Filter("delete_time__isnull", true).
Update(orm.Params{"delete_time": now})
if err != nil {
c.cmsJSONErr(500, 500, "删除失败")
return
}
if n == 0 {
c.cmsJSONErr(404, 404, "记录不存在")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
_ = c.ServeJSON()
}
// List GET /backend/servicesTypesList
func (c *BackendSolutionCategoryController) List() {
if !cmsEnsureSolutionTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
if tid == 0 {
c.cmsJSONErr(400, 400, "tid不能为空")
return
}
page, _ := c.GetInt("page", 1)
limit, _ := c.GetInt("limit", 10)
if page < 1 {
page = 1
}
if limit < 1 || limit > 1000 {
limit = 10
}
keyword := strings.TrimSpace(c.GetString("keyword"))
qs := models.Orm.QueryTable(new(models.CmsSolutionCategory)).
Filter("tid", tid).
Filter("delete_time__isnull", true)
if keyword != "" {
qs = qs.Filter("title__icontains", keyword)
}
total, _ := qs.Count()
var rows []models.CmsSolutionCategory
offset := (page - 1) * limit
_, err = qs.OrderBy("sort", "id").Limit(limit, offset).All(&rows)
if err != nil && err != orm.ErrNoRows {
c.cmsJSONErr(500, 500, "获取解决方案分类列表失败")
return
}
list := make([]map[string]interface{}, 0, len(rows))
for _, r := range rows {
list = append(list, cmsSolutionCateToMap(r))
}
c.Data["json"] = map[string]interface{}{
"code": 200,
"msg": "success",
"data": map[string]interface{}{"list": list, "total": total},
}
_ = c.ServeJSON()
}
type cmsSolutionCategoryPayload struct {
Title string `json:"title"`
Pid uint64 `json:"pid"`
Desc string `json:"desc"`
Sort int `json:"sort"`
}
// Create POST /backend/addServicesTypes
func (c *BackendSolutionCategoryController) Create() {
if !cmsEnsureSolutionTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
if tid == 0 {
c.cmsJSONErr(400, 400, "tid不能为空")
return
}
raw, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
var p cmsSolutionCategoryPayload
if err := json.Unmarshal(raw, &p); err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
title := strings.TrimSpace(p.Title)
if title == "" {
c.cmsJSONErr(400, 400, "分类名称不能为空")
return
}
now := time.Now()
row := models.CmsSolutionCategory{
Tid: tid,
Title: title,
Pid: p.Pid,
Desc: strings.TrimSpace(p.Desc),
Sort: p.Sort,
Status: 1,
CreateTime: now,
UpdateTime: &now,
}
id, err := models.Orm.Insert(&row)
if err != nil {
c.cmsJSONErr(500, 500, "添加失败")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "添加成功", "data": map[string]interface{}{"id": id}}
_ = c.ServeJSON()
}
// Update PUT /backend/editServicesTypes/:id
func (c *BackendSolutionCategoryController) Update() {
if !cmsEnsureSolutionTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
id, _ := c.GetUint64(":id")
if id == 0 {
c.cmsJSONErr(400, 400, "无效ID")
return
}
raw, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
var p cmsSolutionCategoryPayload
if err := json.Unmarshal(raw, &p); err != nil {
c.cmsJSONErr(400, 400, "参数错误")
return
}
title := strings.TrimSpace(p.Title)
if title == "" {
c.cmsJSONErr(400, 400, "分类名称不能为空")
return
}
if p.Pid == id {
c.cmsJSONErr(400, 400, "父级分类不能是自己")
return
}
now := time.Now()
n, err := models.Orm.QueryTable(new(models.CmsSolutionCategory)).
Filter("id", id).
Filter("tid", tid).
Filter("delete_time__isnull", true).
Update(orm.Params{
"title": title,
"pid": p.Pid,
"desc": strings.TrimSpace(p.Desc),
"sort": p.Sort,
"update_time": now,
})
if err != nil {
c.cmsJSONErr(500, 500, "更新失败")
return
}
if n == 0 {
c.cmsJSONErr(404, 404, "分类不存在")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "更新成功"}
_ = c.ServeJSON()
}
// Delete DELETE /backend/deleteServicesTypes/:id
func (c *BackendSolutionCategoryController) Delete() {
if !cmsEnsureSolutionTables(&c.Controller) {
return
}
claims, err := c.cmsClaims()
if err != nil {
c.cmsJSONErr(401, 401, err.Error())
return
}
tid := cmsEffectiveTid(&c.Controller, claims)
id, _ := c.GetUint64(":id")
if id == 0 {
c.cmsJSONErr(400, 400, "无效ID")
return
}
// 存在子分类时不允许删除
childCnt, _ := models.Orm.QueryTable(new(models.CmsSolutionCategory)).
Filter("tid", tid).
Filter("pid", id).
Filter("delete_time__isnull", true).
Count()
if childCnt > 0 {
c.cmsJSONErr(400, 400, "该分类下存在子分类,请先删除子分类")
return
}
now := time.Now()
n, err := models.Orm.QueryTable(new(models.CmsSolutionCategory)).
Filter("id", id).
Filter("tid", tid).
Filter("delete_time__isnull", true).
Update(orm.Params{"delete_time": now})
if err != nil {
c.cmsJSONErr(500, 500, "删除失败")
return
}
if n == 0 {
c.cmsJSONErr(404, 404, "分类不存在")
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
_ = c.ServeJSON()
}
+41 -19
View File
@@ -120,32 +120,43 @@ func (c *TenantSiteController) resolveTemplateCode(tid uint64) string {
func (c *TenantSiteController) writeHTML(status int, html string) {
c.Ctx.Output.SetStatus(status)
c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
c.Ctx.WriteString(c.forceHTTPSStorageURLs(html))
c.Ctx.WriteString(c.rewriteStorageURLs(html))
}
// forceHTTPSStorageURLs 访客以 HTTPS 访问站点时,把页面中存储(CDN)域名的
// http:// 资源升级为 https://,避免混合内容(Mixed Content)被浏览器拦截。
// 仅替换存储配置里那个域名,不动正文里的其他外部链接。
func (c *TenantSiteController) forceHTTPSStorageURLs(html string) string {
proto := c.Ctx.Input.Header("X-Forwarded-Proto")
if proto == "" && c.Ctx.Input.IsSecure() {
proto = "https"
}
if proto != "https" {
return html
}
var legacyQiniuDomains = []string{
"7colud.yunzer.cn",
"7cloud.yunzer.cn",
}
// rewriteStorageURLs 将历史七牛域名替换为当前存储配置的访问域名。
// 这样已存入数据库的旧完整 URL 会跟随新的 CDN 或 /qiniu/ 反向代理入口,
// 不需要批量更新业务数据。
func (c *TenantSiteController) rewriteStorageURLs(html string) string {
cfg, err := models.GetStorageConfig()
if err != nil || strings.TrimSpace(cfg.QiniuDomain) == "" {
return html
}
host := strings.TrimSpace(cfg.QiniuDomain)
host = strings.TrimPrefix(host, "http://")
host = strings.TrimPrefix(host, "https://")
host = strings.TrimRight(host, "/")
if host == "" {
return rewriteLegacyQiniuURLs(html, storagePublicBaseURL(cfg.QiniuDomain))
}
func storagePublicBaseURL(domain string) string {
base := strings.TrimRight(strings.TrimSpace(domain), "/")
if base == "" || strings.HasPrefix(base, "/") || strings.HasPrefix(base, "http://") || strings.HasPrefix(base, "https://") {
return base
}
return "https://" + base
}
func rewriteLegacyQiniuURLs(html, targetBase string) string {
if targetBase == "" {
return html
}
return strings.ReplaceAll(html, "http://"+host, "https://"+host)
for _, domain := range legacyQiniuDomains {
for _, scheme := range []string{"http://", "https://"} {
html = strings.ReplaceAll(html, scheme+domain+"/", targetBase+"/")
}
}
return html
}
// rewriteThemeAssets 将模板内书写的相对资源路径(href/src/url() 等)
@@ -211,6 +222,11 @@ func rewriteThemeAssets(html, code string) string {
// render 解析并输出模板文件
func (c *TenantSiteController) render(file string, mod func(ctx *tagengine.RenderCtx)) {
c.renderFile(file, "", mod)
}
// renderFile 解析并输出模板;preferred 不存在时尝试 fallback(fallback 为空则直接 404)
func (c *TenantSiteController) renderFile(preferred, fallback string, mod func(ctx *tagengine.RenderCtx)) {
tid, ok := c.resolveTid()
if !ok {
c.writeHTML(404, siteNotFoundPage)
@@ -219,6 +235,12 @@ func (c *TenantSiteController) render(file string, mod func(ctx *tagengine.Rende
code := c.resolveTemplateCode(tid)
dir := filepath.Join(cmsThemesRoot(), code)
file := preferred
if _, err := os.Stat(filepath.Join(dir, file)); err != nil {
if fallback != "" {
file = fallback
}
}
if _, err := os.Stat(filepath.Join(dir, file)); err != nil {
c.writeHTML(404, fmt.Sprintf(renderErrorPage, "模板页面不存在"))
return
@@ -271,7 +293,7 @@ func (c *TenantSiteController) Page() {
c.writeHTML(404, fmt.Sprintf(renderErrorPage, "页面不存在"))
return
}
c.render("page.html", func(ctx *tagengine.RenderCtx) {
c.renderFile(path+".html", "page.html", func(ctx *tagengine.RenderCtx) {
ctx.PagePath = path
})
}