Jelajahi Sumber

组件列表管理

huangyan 1 tahun lalu
induk
melakukan
78ddd6fcfc

+ 24 - 0
Makefile

@@ -0,0 +1,24 @@
+BINARY_NAME := cc-officialweb
+GOOS := linux
+GOARCH := amd64
+
+all: build
+# 运行
+run:
+	@echo "Running $(BINARY_NAME)..."
+	go run main.go
+# 编译为linux下的二进制文件
+build:
+	@echo "Cross-compiling for $(GOOS)/$(GOARCH)..."
+	go build -o $(BINARY_NAME)-$(GOOS)-$(GOARCH) -ldflags="-s -w" -tags netgo -a -installsuffix cgo -x -v -gcflags=all=-trimpath=$(GOPATH) -asmflags=all=-trimpath=$(GOPATH)
+# 安装必要的依赖
+deps:
+	@echo "Installing dependencies..."
+	go mod tidy
+	go mod download
+# 删除编译生成的文件
+clean:
+	@echo "Cleaning..."
+	del /Q $(BINARY_NAME)-*
+
+.PHONY: all build clean

+ 0 - 44
app/controller/component.go

@@ -1,44 +0,0 @@
-package controller
-
-import (
-	"Panel_development/app/e"
-	"Panel_development/app/model"
-	"Panel_development/app/service"
-	"Panel_development/unity"
-	"github.com/gin-gonic/gin"
-	"github.com/go-playground/validator/v10"
-)
-
-var ComponentClassify service.ComponentClassify = &model.ComponentClassify{}
-
-// GetComponent 获取组件分类
-func GetComponent(c *gin.Context) {
-	name := model.ComponentClassify{}.TableName()
-	all, err := unity.QueryAll(name)
-	if err != nil {
-		e.ResponseWithMsg(c, e.ERROR, err.Error())
-	} else {
-		e.ResponseSuccess(c, all)
-	}
-}
-
-// AddComponentClassify 添加组件分类
-func AddComponentClassify(c *gin.Context) {
-	var cc model.ComponentClassify
-	err := c.ShouldBindJSON(&cc)
-	if err != nil {
-		e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
-		return
-	}
-	err = validator.New().Struct(&cc)
-	if err != nil {
-		e.ResponseWithMsg(c, e.ERROR, err.Error())
-	} else {
-		cc, err = ComponentClassify.AddComponentClassify(cc)
-		if err != nil {
-			e.ResponseWithMsg(c, e.ERROR, err.Error())
-		} else {
-			e.ResponseSuccess(c, cc)
-		}
-	}
-}

+ 67 - 0
app/controller/componentClassify.go

@@ -0,0 +1,67 @@
+package controller
+
+import (
+	"Panel_development/app/e"
+	"Panel_development/app/model"
+	"Panel_development/app/service"
+	"Panel_development/unity"
+	"github.com/gin-gonic/gin"
+	"github.com/go-playground/validator/v10"
+	"strconv"
+)
+
+var ComponentClassify service.ComponentClassify = &model.ComponentClassify{}
+
+// GetComponentClassify 获取组件分类
+func GetComponentClassify(c *gin.Context) {
+	classify, err := ComponentClassify.GetComponentClassify()
+	if err != nil {
+		e.ResponseWithMsg(c, e.ERROR, err.Error())
+	} else {
+		e.ResponseSuccess(c, classify)
+	}
+
+}
+
+// AddComponentClassify 添加组件分类
+func AddComponentClassify(c *gin.Context) {
+	var cclass model.ComponentClassify
+	err := c.ShouldBindJSON(&cclass)
+	if err != nil {
+		e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
+		return
+	}
+	err = validator.New().Struct(&cclass)
+	if err != nil {
+		e.ResponseWithMsg(c, e.ERROR, err.Error())
+	} else {
+		cc, err := ComponentClassify.AddComponentClassify(cclass)
+		if err != nil {
+			e.ResponseWithMsg(c, e.ERROR, err.Error())
+		} else {
+			e.ResponseSuccess(c, cc)
+		}
+	}
+}
+
+// DeleteComponentClassify 删除组件分类
+func DeleteComponentClassify(c *gin.Context) {
+	s := c.Query("id")
+	validate := validator.New()
+	err := validate.Var(s, "required,numeric")
+	if err != nil {
+		e.ResponseWithMsg(c, e.ERROR, "id不能为空")
+	}
+	atoi, _ := strconv.Atoi(s)
+	_, err = unity.DeleteById(atoi, model.ComponentClassify{})
+	if err != nil {
+		e.ResponseWithMsg(c, e.DELETEFAIL, e.DELETEFAIL.GetMsg())
+	} else {
+		e.ResponseSuccess(c, nil)
+	}
+}
+
+// UpdateComponentClassify 更新组件分类
+func UpdateComponentClassify(c *gin.Context) {
+
+}

+ 114 - 0
app/controller/componentlist.go

@@ -0,0 +1,114 @@
+package controller
+
+import (
+	"Panel_development/app/e"
+	"Panel_development/app/model"
+	"Panel_development/app/service"
+	"Panel_development/unity"
+	"github.com/gin-gonic/gin"
+	"github.com/go-playground/validator/v10"
+	"strconv"
+)
+
+var ComponentList service.ComponentList = &model.ComponentList{}
+
+// GetComponentList 根据ID获取组件列表
+func GetComponentList(c *gin.Context) {
+	value := c.Query("id")
+	s := c.Query("id")
+	validate := validator.New()
+	err := validate.Var(s, "required,numeric")
+	if err != nil {
+		e.ResponseWithMsg(c, e.ERROR, "id不能为空")
+		return
+	}
+	atoi, _ := strconv.Atoi(value)
+	id, err := unity.GetById(atoi, model.ComponentList{})
+	if err != nil {
+		e.ResponseWithMsg(c, e.ERROR, "获取失败当前数据不存在")
+		return
+	}
+	e.ResponseSuccess(c, id)
+}
+
+// ComponentListAll 获取组件列表
+func ComponentListAll(c *gin.Context) {
+	var page unity.QueryPageParams
+	if err := c.ShouldBindJSON(&page); err != nil {
+		e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
+		return
+	}
+	// 查询条件:查询name
+	queryCond := "name like ?"
+	page.Query = "%" + page.Query + "%"
+	result, total, err := unity.PaginateWithCondition(page, model.ComponentList{}, queryCond)
+	if err != nil {
+		e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
+		return
+	}
+	e.ResPonsePage(c, result, total, page)
+}
+
+// AddComponentList 添加组件列表
+func AddComponentList(c *gin.Context) {
+	var componentList model.ComponentList
+	if err := c.ShouldBindJSON(&componentList); err != nil {
+		e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
+		return
+	}
+	err := validator.New().Struct(&componentList)
+	if err != nil {
+		e.ResponseWithMsg(c, e.ERROR, err.Error())
+		return
+	}
+	add, err := unity.Add(&componentList)
+	if err != nil {
+		e.ResponseWithMsg(c, e.ERROR, err.Error())
+		return
+	}
+	e.ResponseSuccess(c, add)
+
+}
+
+// UpdateComponentList 根据ID更新组件列表
+func UpdateComponentList(c *gin.Context) {
+	var componentList model.ComponentList
+	s := c.Query("id")
+	err := c.ShouldBindJSON(&componentList)
+	if err != nil {
+		e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
+		return
+	}
+	validate := validator.New()
+	err = validate.Var(s, "required,numeric")
+	if err != nil {
+		e.ResponseWithMsg(c, e.ERROR, "id不能为空")
+		return
+	}
+	atoi, _ := strconv.Atoi(s)
+	componentList, err = unity.UpdateById(atoi, componentList)
+	if err != nil {
+		e.ResponseWithMsg(c, e.ERROR, err.Error())
+		return
+	}
+	e.ResponseSuccess(c, componentList)
+}
+
+// DeleteComponentList 根据ID删除组件列表
+func DeleteComponentList(c *gin.Context) {
+	s := c.Query("id")
+	validate := validator.New()
+	err := validate.Var(s, "required,numeric")
+	if err != nil {
+		e.ResponseWithMsg(c, e.ERROR, "id不能为空")
+		return
+	}
+	atoi, _ := strconv.Atoi(s)
+	_, err = unity.DeleteById(atoi, model.ComponentList{})
+	if err != nil {
+		e.ResponseWithMsg(c, e.DELETEFAIL, e.DELETEFAIL.GetMsg())
+		return
+	} else {
+		e.ResponseSuccess(c, nil)
+	}
+}

+ 2 - 2
app/e/R.go

@@ -33,14 +33,14 @@ func ResponseWithMsg(c *gin.Context, code Rescode, msg any) {
 		Data:    nil,
 	})
 }
-func ResPonsePage(c *gin.Context, data interface{}, total int64, params unity.PageParams) {
+func ResPonsePage(c *gin.Context, data interface{}, total int64, params unity.QueryPageParams) {
 	c.JSON(http.StatusOK, &R{
 		Code:    SUCCESS,
 		Message: SUCCESS.GetMsg(),
 		Data: gin.H{
 			"result":    data,
 			"total":     total,
-			"unity":     params.Page,
+			"current":   params.Page,
 			"page_size": params.Size,
 		},
 	})

+ 0 - 39
app/model/component.go

@@ -1,39 +0,0 @@
-package model
-
-import (
-	"Panel_development/global"
-	"errors"
-	"gorm.io/gorm"
-)
-
-type ComponentList struct {
-	gorm.Model
-	Name       string `gorm:"type:varchar(255);not null;unique;comment:组件名称"`
-	Image      string `gorm:"type:varchar(255);not null;comment:组件图片"`
-	UserId     uint   `gorm:"type:int;not null;comment:用户id"`
-	Data       string `gorm:"type:longtext;comment:组件数据"`
-	ClassifyId uint   `gorm:"type:int;not null;comment:组件分类id"`
-}
-
-func (c ComponentList) TableName() string {
-	return "component_list"
-}
-
-type ComponentClassify struct {
-	gorm.Model
-	Name           string              `gorm:"type:varchar(255);not null;unique;comment:组件分类名称" json:"name" validate:"required"`
-	ParentId       uint                `gorm:"type:int;not null;comment:父组件id" json:"parentId"`
-	ComponentClass []ComponentClassify `gorm:"foreignKey:ParentId;references:ID" json:"componentClass"`
-}
-
-func (cc ComponentClassify) TableName() string {
-	return "component_classify"
-}
-func (cc ComponentClassify) AddComponentClassify(ComponentClassify) (ComponentClassify, error) {
-	//TODO implement me
-	tx := global.DBLink.Create(&ComponentClassify{})
-	if tx.RowsAffected > 0 {
-		return ComponentClassify{}, nil
-	}
-	return ComponentClassify{}, errors.New("添加失败")
-}

+ 40 - 0
app/model/componentClassify.go

@@ -0,0 +1,40 @@
+package model
+
+import (
+	"Panel_development/global"
+	"errors"
+	"gorm.io/gorm"
+)
+
+type ComponentClassify struct {
+	gorm.Model
+	Name     string              `gorm:"type:varchar(255);not null;unique;comment:组件分类名称" json:"name" validate:"required"`
+	ParentId uint                `gorm:"type:int;not null;comment:父组件id" json:"parentId"`
+	Child    []ComponentClassify `gorm:"-" json:"child"` // 此字段将不会被GORM迁移至数据库
+}
+
+func (cc ComponentClassify) TableName() string {
+	return "component_classify"
+}
+
+// AddComponentClassify 添加组件分类
+func (cc ComponentClassify) AddComponentClassify(cclass ComponentClassify) (ComponentClassify, error) {
+	//TODO implement me
+	tx := global.DBLink.Create(&cclass)
+	if tx.RowsAffected > 0 {
+		return ComponentClassify{}, nil
+	}
+	return ComponentClassify{}, errors.New("添加失败")
+}
+
+// GetComponentClassify 获取组件分类
+func (cc ComponentClassify) GetComponentClassify() ([]ComponentClassify, error) {
+	//TODO implement me
+	var parent []ComponentClassify
+	var child []ComponentClassify
+	global.DBLink.Where("parent_id = 0").Find(&parent)
+	for _, classify := range parent {
+		global.DBLink.Where("parent_id =?", classify.ID).Find(&child)
+	}
+	return parent, nil
+}

+ 14 - 0
app/model/componentList.go

@@ -0,0 +1,14 @@
+package model
+
+import "gorm.io/gorm"
+
+type ComponentList struct {
+	gorm.Model
+	Name  string `gorm:"type:varchar(255);not null;unique;comment:组件名称" json:"name" validate:"required"`
+	Image string `gorm:"type:varchar(255);not null;comment:组件图片" json:"image" validate:"required"`
+	Data  string `gorm:"type:longtext;comment:组件数据" json:"data" validate:"required"`
+}
+
+func (c ComponentList) TableName() string {
+	return "component_list"
+}

+ 1 - 0
app/router.go

@@ -11,5 +11,6 @@ func InitRouter() error {
 
 	gin.SetMode(global.ServerSetting.Mode)
 	routers.ComponentClassRouter(engine)
+	routers.ComponentListRouter(engine)
 	return engine.Run(global.ServerSetting.Port)
 }

+ 4 - 3
app/routers/componentclass.go

@@ -6,7 +6,8 @@ import (
 )
 
 func ComponentClassRouter(r *gin.Engine) {
-	group := r.Group("/api/componentclass")
-	group.GET("/list", controller.GetComponent)
-	group.POST("/add", controller.AddComponentClassify)
+	group := r.Group("/api")
+	group.GET("/componentclass", controller.GetComponentClassify)
+	group.POST("/addcomponentclass", controller.AddComponentClassify)
+	group.DELETE("/componentclass", controller.DeleteComponentClassify)
 }

+ 15 - 0
app/routers/componentlist.go

@@ -0,0 +1,15 @@
+package routers
+
+import (
+	"Panel_development/app/controller"
+	"github.com/gin-gonic/gin"
+)
+
+func ComponentListRouter(r *gin.Engine) {
+	group := r.Group("/api")
+	group.GET("/componentList", controller.GetComponentList)
+	group.POST("/componentall", controller.ComponentListAll)
+	group.POST("/componentList", controller.AddComponentList)
+	group.PUT("/componentList", controller.UpdateComponentList)
+	group.DELETE("/componentList", controller.DeleteComponentList)
+}

+ 2 - 1
app/service/componentclassify.go

@@ -3,5 +3,6 @@ package service
 import "Panel_development/app/model"
 
 type ComponentClassify interface {
-	AddComponentClassify(model.ComponentClassify) (ComponentClassify, error)
+	AddComponentClassify(classify model.ComponentClassify) (model.ComponentClassify, error)
+	GetComponentClassify() ([]model.ComponentClassify, error)
 }

+ 4 - 0
app/service/componentlist.go

@@ -0,0 +1,4 @@
+package service
+
+type ComponentList interface {
+}

+ 1 - 0
go.mod

@@ -22,6 +22,7 @@ require (
 	github.com/go-openapi/swag v0.19.15 // indirect
 	github.com/go-playground/locales v0.14.1 // indirect
 	github.com/go-playground/universal-translator v0.18.1 // indirect
+	github.com/go-playground/validator v9.31.0+incompatible // indirect
 	github.com/go-playground/validator/v10 v10.20.0 // indirect
 	github.com/go-redis/redis/v8 v8.11.5 // indirect
 	github.com/go-sql-driver/mysql v1.7.0 // indirect

+ 2 - 0
go.sum

@@ -41,6 +41,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
 github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
 github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
 github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
+github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA=
+github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
 github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
 github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
 github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=

+ 5 - 0
main.go

@@ -5,6 +5,7 @@ import (
 	"Panel_development/database"
 	"Panel_development/global"
 	"Panel_development/simple_zap"
+	"Panel_development/unity"
 	"context"
 )
 
@@ -19,6 +20,10 @@ func init() {
 	}
 	// 数据库迁移
 	database.Migrate(global.DBLink)
+	//本地化
+	if err = unity.Translator("zh"); err != nil {
+		simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "注册本地化失败")
+	}
 }
 
 // @title 面板开发

+ 14 - 14
unity/unity.go

@@ -6,10 +6,16 @@ import (
 )
 
 type PageParams struct {
-	Page int    `json:"unity" form:"unity"`
+	Page int    `json:"page" form:"page"`
 	Size int    `json:"size" form:"size"`
 	Desc string `json:"desc" form:"desc"`
 }
+type QueryPageParams struct {
+	Page  int    `json:"page" form:"page"`
+	Size  int    `json:"size" form:"size"`
+	Query string `json:"query" form:"query"`
+	Desc  string `json:"desc" form:"desc"`
+}
 
 // Paginate 使用给定的DB连接执行分页查询
 func Paginate[T any](params PageParams, model T) (result []T, total int64, current int, err error) {
@@ -29,29 +35,23 @@ func Paginate[T any](params PageParams, model T) (result []T, total int64, curre
 	return result, count, params.Page, nil
 }
 
-func PaginateWithCondition[T any](params PageParams, model T, queryCond interface{}, args ...interface{}) (result []T, total int64, err error) {
+func PaginateWithCondition[T any](params QueryPageParams, model T, queryCond interface{}) (result []T, total int64, err error) {
 	var count int64
-	// 根据模型和额外查询条件查询数据库中符合条件的数据总量
-	//if err = global.DBLink.Model(model).Where(queryCond, args...).Count(&count).Error; err != nil {
-	//	return nil, 0, err
-	//}
 	if queryCond != nil {
-		if err = global.DBLink.Model(result).Where(queryCond, args...).Count(&count).Error; err != nil {
+		if err = global.DBLink.Model(model).Where(queryCond, params.Query).Count(&count).Error; err != nil {
 			return nil, 0, err
 		}
 	} else {
-		if err = global.DBLink.Model(result).Count(&count).Error; err != nil {
+		if err = global.DBLink.Model(model).Count(&count).Error; err != nil {
 			return nil, 0, err
 		}
 	}
 	// 计算查询的偏移量,并设置每次查询的记录数量
 	offset := (params.Page - 1) * params.Size
-
 	// 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
-	if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Where(queryCond, args...).Find(&result).Error; err != nil {
+	if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Where(queryCond, params.Query).Find(&result).Error; err != nil {
 		return nil, 0, err
 	}
-
 	return result, count, nil
 }
 
@@ -66,7 +66,7 @@ func AddGenericItem[T any](item T, mapper func(T) interface{}) error {
 // GetById 根据id查询
 func GetById[T any](id int, model T) (T, error) {
 	tx := global.DBLink.Where("id = ?", id).First(&model)
-	if tx.Error != nil {
+	if tx.RowsAffected <= 0 {
 		return model, errors.New("查询失败")
 	} else {
 		return model, nil
@@ -74,7 +74,7 @@ func GetById[T any](id int, model T) (T, error) {
 }
 
 // UpdateById 根据id进行修改
-func UpdateById[T any](id uint, model T) (T, error) {
+func UpdateById[T any](id int, model T) (T, error) {
 	tx := global.DBLink.Where("id = ?", id).Updates(&model)
 	if tx.RowsAffected > 0 {
 		return model, nil
@@ -89,7 +89,7 @@ func Add[T any](model T) (T, error) {
 	if tx.RowsAffected > 0 {
 		return model, nil
 	} else {
-		return model, errors.New("添加失败当前id不存在")
+		return model, errors.New("添加失败")
 	}
 }
 

+ 39 - 0
unity/zh_cn.go

@@ -0,0 +1,39 @@
+package unity
+
+import (
+	"fmt"
+	"github.com/gin-gonic/gin/binding"
+	"github.com/go-playground/locales/en"
+	"github.com/go-playground/locales/zh"
+	ut "github.com/go-playground/universal-translator"
+	"github.com/go-playground/validator/v10"
+	enTranslations "github.com/go-playground/validator/v10/translations/en"
+	chTranslations "github.com/go-playground/validator/v10/translations/zh"
+)
+
+var Trans ut.Translator
+
+func Translator(local string) (err error) {
+	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
+		zhT := zh.New()
+		enT := en.New()
+		uni := ut.New(enT, zhT, enT)
+		var o bool
+		Trans, o = uni.GetTranslator(local)
+		if !o {
+			return fmt.Errorf("uni.GetTranslator(%s) failed", local)
+		}
+		//register translate
+		// 注册翻译器
+		switch local {
+		case "en":
+			err = enTranslations.RegisterDefaultTranslations(v, Trans)
+		case "zh":
+			err = chTranslations.RegisterDefaultTranslations(v, Trans)
+		default:
+			err = enTranslations.RegisterDefaultTranslations(v, Trans)
+		}
+		return
+	}
+	return
+}