重命名包为 core

This commit is contained in:
2025-05-07 17:38:27 +08:00
parent 60bbe47368
commit 0a16f9923c
43 changed files with 349 additions and 347 deletions

19
web/core/errors.go Normal file
View File

@@ -0,0 +1,19 @@
package core
type AuthUnAuthorizedErr string
func (e AuthUnAuthorizedErr) Error() string {
return string(e)
}
type AuthForbiddenErr string
func (e AuthForbiddenErr) Error() string {
return string(e)
}
type DataErr string
func (e DataErr) Error() string {
return string(e)
}

180
web/core/types.go Normal file
View File

@@ -0,0 +1,180 @@
package core
import (
"database/sql"
"database/sql/driver"
"time"
)
// region req
type PageReqInter interface {
GetPage() int
GetSize() int
GetOffset() int
GetLimit() int
}
type PageReq struct {
RawPage int `json:"page"`
RawSize int `json:"size"`
}
func (p *PageReq) GetPage() int {
if p.RawPage < 1 {
return 1
}
return p.RawPage
}
func (p *PageReq) GetSize() int {
if p.RawSize < 1 {
return 10
}
if p.RawSize > 100 {
return 100
}
return p.RawSize
}
func (p *PageReq) GetOffset() int {
return (p.GetPage() - 1) * p.GetSize()
}
func (p *PageReq) GetLimit() int {
return p.GetSize()
}
// endregion
// region resp
type ErrResp struct {
Message string `json:"message"`
Error bool `json:"error"`
}
type PageResp struct {
Total int `json:"total"`
Page int `json:"page"`
Size int `json:"size"`
List any `json:"list"`
}
// endregion
// region LocalDateTime
type LocalDateTime time.Time
var formats = []string{
"2006-01-02 15:04:05.999999999-07:00",
"2006-01-02T15:04:05.999999999-07:00",
"2006-01-02 15:04:05.999999999",
"2006-01-02T15:04:05.999999999",
"2006-01-02 15:04:05",
"2006-01-02T15:04:05",
"2006-01-02 15:04",
"2006-01-02T15:04",
"2006-01-02",
}
//goland:noinspection GoMixedReceiverTypes
func (ldt *LocalDateTime) Scan(value interface{}) (err error) {
var t time.Time
if strValue, ok := value.(string); ok {
var timeValue time.Time
for _, format := range formats {
timeValue, err = time.Parse(format, strValue)
if err == nil {
t = timeValue
break
}
}
t = timeValue
} else {
nullTime := &sql.NullTime{}
err = nullTime.Scan(value)
if err != nil {
return err
}
if nullTime == nil {
return nil
}
t = nullTime.Time
}
*ldt = LocalDateTime(time.Date(
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.Local,
))
return
}
//goland:noinspection GoMixedReceiverTypes
func (ldt LocalDateTime) Value() (driver.Value, error) {
return time.Time(ldt).Local(), nil
}
// GormDataType gorm common data type
//
//goland:noinspection GoMixedReceiverTy
//goland:noinspection GoMixedReceiverTypes
func (ldt LocalDateTime) GormDataType() string {
return "ldt"
}
//goland:noinspection GoMixedReceiverTypes
func (ldt LocalDateTime) GobEncode() ([]byte, error) {
return time.Time(ldt).GobEncode()
}
//goland:noinspection GoMixedReceiverTypes
func (ldt *LocalDateTime) GobDecode(b []byte) error {
return (*time.Time)(ldt).GobDecode(b)
}
//goland:noinspection GoMixedReceiverTypes
func (ldt LocalDateTime) MarshalJSON() ([]byte, error) {
return time.Time(ldt).MarshalJSON()
}
//goland:noinspection GoMixedReceiverTypes
func (ldt *LocalDateTime) UnmarshalJSON(b []byte) error {
return (*time.Time)(ldt).UnmarshalJSON(b)
}
// endregion
// region err
type ServiceErr struct {
code int
name string
msg string
}
func (e ServiceErr) Code() int {
return e.code
}
func (e ServiceErr) Name() string {
return e.name
}
func (e ServiceErr) Error() string {
return e.msg
}
func NewErr(name, msg string, code ...int) *ServiceErr {
_code := 400
if len(code) > 0 {
_code = code[0]
}
return &ServiceErr{
name: name,
msg: msg,
code: _code,
}
}
// endregion