1:前端传递的数据结构:
{
"page_type": 10,
"page_name": "商城首页",
"page_data": {
"page": {
"params": {
"name": "商城首页",
"title": "萤火商城2.0",
"shareTitle": "分享标题"
},
"style": {
"titleTextColor": "black",
"titleBackgroundColor": "#ffffff"
},
"name": "页面设置",
"type": "page"
},
"items": [
{
"name": "搜索框",
"type": "search",
"params": {
"placeholder": "请输入关键字进行搜索"
},
"style": {
"textAlign": "left",
"searchStyle": "square"
}
},
{
"name": "店铺公告",
"type": "notice",
"params": {
"text": "萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]",
"link": null,
"showIcon": true,
"scrollable": true
},
"style": {
"paddingTop": 0,
"background": "#fffbe8",
"textColor": "#de8c17"
}
},
{
"name": "图片轮播",
"type": "banner",
"style": {
"btnColor": "#ffffff",
"btnShape": "round",
"interval": 2.5
},
"data": [
{
"imgUrl": "http://www.yoshop.com/assets/store/img/diy/banner/01.png",
"link": null
},
{
"imgUrl": "http://www.yoshop.com/assets/store/img/diy/banner/01.png",
"link": null
}
]
},
{
"name": "导航组",
"type": "navBar",
"style": {
"rowsNum": 4,
"background": "#ffffff",
"paddingTop": 0,
"textColor": "#666666"
},
"data": [
{
"imgUrl": "http://www.yoshop.com/assets/store/img/diy/navbar/01.png",
"imgName": "icon-1.png",
"link": null,
"text": "按钮文字1"
},
{
"imgUrl": "http://www.yoshop.com/assets/store/img/diy/navbar/01.png",
"imgName": "icon-2.jpg",
"link": null,
"text": "按钮文字2"
},
{
"imgUrl": "http://www.yoshop.com/assets/store/img/diy/navbar/01.png",
"imgName": "icon-3.jpg",
"link": null,
"text": "按钮文字3"
},
{
"imgUrl": "http://www.yoshop.com/assets/store/img/diy/navbar/01.png",
"imgName": "icon-4.jpg",
"link": null,
"text": "按钮文字4"
}
]
}
]
},
"store_id": 10001,
"is_delete": 0
}
后端的代码:
package models
import (
"database/sql/driver"
"fmt"
"github.com/goccy/go-json"
)
type SysPage struct {
ID uint `gorm:"primary_key;auto_increment" json:"id"`
PageType uint8 `gorm:"not_null;default:10" json:"page_type"` // 页面类型 (10 首页, 20 自定义页)
PageName string `gorm:"type:varchar(255);not_null;default:''" json:"page_name"`
PageData PageData `gorm:"type:longtext;not_null" json:"page_data"`
StoreID uint `gorm:"not_null;default:0" json:"store_id"`
IsDelete uint8 `gorm:"not_null;default:0" json:"is_delete"`
CreateTime uint `gorm:"not_null;default:0" json:"create_time"`
UpdateTime uint `gorm:"not_null;default:0" json:"update_time"`
}
// PageData 是一个自定义类型,用于处理 JSON 数据
type PageData map[string]interface{}
// Scan 实现 sql.Scanner 接口,用于从数据库读取 JSON 数据
func (pd *PageData) Scan(value interface{}) error {
if value == nil {
*pd = PageData{}
return nil
}
bytes, ok := value.([]byte)
if !ok {
return fmt.Errorf("failed to unmarshal PageData: %v", value)
}
return json.Unmarshal(bytes, pd)
}
// Value 实现 driver.Valuer 接口,用于将 JSON 数据写入数据库
func (pd PageData) Value() (driver.Value, error) {
return json.Marshal(pd)
}
func (SysPage) TableName() string {
return "sys_page"
}
package page
import (
"admin/common/database"
"admin/models"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func Add(c *gin.Context) {
var pageReq models.SysPage
if c.Request.ContentLength == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "No data provided"})
return
}
if err := c.ShouldBindJSON(&pageReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
page := models.SysPage{
ID: pageReq.ID,
PageType: pageReq.PageType,
PageName: pageReq.PageName,
PageData: pageReq.PageData,
StoreID: pageReq.StoreID,
IsDelete: pageReq.IsDelete,
CreateTime: uint(time.Now().Unix()),
UpdateTime: uint(time.Now().Unix()),
}
if err := database.DB.Debug().Create(&page).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save the good"})
return
}
c.JSON(http.StatusOK, gin.H{"msg": "create suceessfully!"})
}