✨ init project
This commit is contained in:
43
model/common/basetypes.go
Normal file
43
model/common/basetypes.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type JSONMap map[string]interface{}
|
||||
|
||||
func (m JSONMap) Value() (driver.Value, error) {
|
||||
if m == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
func (m *JSONMap) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
*m = make(map[string]interface{})
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
switch value.(type) {
|
||||
case []byte:
|
||||
err = json.Unmarshal(value.([]byte), m)
|
||||
case string:
|
||||
err = json.Unmarshal([]byte(value.(string)), m)
|
||||
default:
|
||||
err = errors.New("basetypes.JSONMap.Scan: invalid value type")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type TreeNode[T any] interface {
|
||||
GetChildren() []T
|
||||
SetChildren(children T)
|
||||
GetID() int
|
||||
GetParentID() int
|
||||
}
|
7
model/common/clearDB.go
Normal file
7
model/common/clearDB.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package common
|
||||
|
||||
type ClearDB struct {
|
||||
TableName string
|
||||
CompareField string
|
||||
Interval string
|
||||
}
|
48
model/common/request/common.go
Normal file
48
model/common/request/common.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PageInfo Paging common input parameter structure
|
||||
type PageInfo struct {
|
||||
Page int `json:"page" form:"page"` // 页码
|
||||
PageSize int `json:"pageSize" form:"pageSize"` // 每页大小
|
||||
Keyword string `json:"keyword" form:"keyword"` // 关键字
|
||||
}
|
||||
|
||||
func (r *PageInfo) Paginate() func(db *gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
if r.Page <= 0 {
|
||||
r.Page = 1
|
||||
}
|
||||
switch {
|
||||
case r.PageSize > 100:
|
||||
r.PageSize = 100
|
||||
case r.PageSize <= 0:
|
||||
r.PageSize = 10
|
||||
}
|
||||
offset := (r.Page - 1) * r.PageSize
|
||||
return db.Offset(offset).Limit(r.PageSize)
|
||||
}
|
||||
}
|
||||
|
||||
// GetById Find by id structure
|
||||
type GetById struct {
|
||||
ID int `json:"id" form:"id"` // 主键ID
|
||||
}
|
||||
|
||||
func (r *GetById) Uint() uint {
|
||||
return uint(r.ID)
|
||||
}
|
||||
|
||||
type IdsReq struct {
|
||||
Ids []int `json:"ids" form:"ids"`
|
||||
}
|
||||
|
||||
// GetAuthorityId Get role by id structure
|
||||
type GetAuthorityId struct {
|
||||
AuthorityId uint `json:"authorityId" form:"authorityId"` // 角色ID
|
||||
}
|
||||
|
||||
type Empty struct{}
|
8
model/common/response/common.go
Normal file
8
model/common/response/common.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package response
|
||||
|
||||
type PageResult struct {
|
||||
List interface{} `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
63
model/common/response/response.go
Normal file
63
model/common/response/response.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Data interface{} `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
const (
|
||||
ERROR = 7
|
||||
SUCCESS = 0
|
||||
)
|
||||
|
||||
func Result(code int, data interface{}, msg string, c *gin.Context) {
|
||||
// 开始时间
|
||||
c.JSON(http.StatusOK, Response{
|
||||
code,
|
||||
data,
|
||||
msg,
|
||||
})
|
||||
}
|
||||
|
||||
func Ok(c *gin.Context) {
|
||||
Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
|
||||
}
|
||||
|
||||
func OkWithMessage(message string, c *gin.Context) {
|
||||
Result(SUCCESS, map[string]interface{}{}, message, c)
|
||||
}
|
||||
|
||||
func OkWithData(data interface{}, c *gin.Context) {
|
||||
Result(SUCCESS, data, "成功", c)
|
||||
}
|
||||
|
||||
func OkWithDetailed(data interface{}, message string, c *gin.Context) {
|
||||
Result(SUCCESS, data, message, c)
|
||||
}
|
||||
|
||||
func Fail(c *gin.Context) {
|
||||
Result(ERROR, map[string]interface{}{}, "操作失败", c)
|
||||
}
|
||||
|
||||
func FailWithMessage(message string, c *gin.Context) {
|
||||
Result(ERROR, map[string]interface{}{}, message, c)
|
||||
}
|
||||
|
||||
func NoAuth(message string, c *gin.Context) {
|
||||
c.JSON(http.StatusUnauthorized, Response{
|
||||
7,
|
||||
nil,
|
||||
message,
|
||||
})
|
||||
}
|
||||
|
||||
func FailWithDetailed(data interface{}, message string, c *gin.Context) {
|
||||
Result(ERROR, data, message, c)
|
||||
}
|
Reference in New Issue
Block a user