mirror of
https://github.com/quarkcloudio/quark-go.git
synced 2025-09-26 20:11:11 +08:00
feat: 自定义时间类型
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,6 +1,10 @@
|
||||
# Mac OS X files
|
||||
.DS_Store
|
||||
|
||||
# Editor
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
@@ -31,5 +35,3 @@ examples/*/web/
|
||||
|
||||
# examples web site sqlite files
|
||||
examples/*/data.db
|
||||
|
||||
.idea/
|
||||
|
@@ -1,23 +1,23 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime"
|
||||
)
|
||||
|
||||
// 字段
|
||||
type ActionLog struct {
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
ObjectId int `json:"object_id" gorm:"size:11;not null"`
|
||||
Username string `json:"username" gorm:"<-:false"`
|
||||
Url string `json:"url" gorm:"size:500;not null"`
|
||||
Remark string `json:"remark" gorm:"size:255;not null"`
|
||||
Ip string `json:"ip" gorm:"size:100;not null"`
|
||||
Type string `json:"type" gorm:"size:100;not null"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
ObjectId int `json:"object_id" gorm:"size:11;not null"`
|
||||
Username string `json:"username" gorm:"<-:false"`
|
||||
Url string `json:"url" gorm:"size:500;not null"`
|
||||
Remark string `json:"remark" gorm:"size:255;not null"`
|
||||
Ip string `json:"ip" gorm:"size:100;not null"`
|
||||
Type string `json:"type" gorm:"size:100;not null"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
CreatedAt datetime.Time `json:"created_at"`
|
||||
UpdatedAt datetime.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// 插入数据
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/hash"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -21,10 +22,10 @@ type Admin struct {
|
||||
Password string `json:"password" gorm:"size:255;not null"`
|
||||
Avatar string `json:"avatar" gorm:"size:1000"`
|
||||
LastLoginIp string `json:"last_login_ip" gorm:"size:255"`
|
||||
LastLoginTime time.Time `json:"last_login_time"`
|
||||
LastLoginTime datetime.Time `json:"last_login_time"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CreatedAt datetime.Time `json:"created_at"`
|
||||
UpdatedAt datetime.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at"`
|
||||
}
|
||||
|
||||
@@ -44,7 +45,7 @@ type AdminClaims struct {
|
||||
// 管理员Seeder
|
||||
func (model *Admin) Seeder() {
|
||||
seeders := []Admin{
|
||||
{Username: "administrator", Nickname: "超级管理员", Email: "admin@yourweb.com", Phone: "10086", Password: hash.Make("123456"), Sex: 1, Status: 1, LastLoginTime: time.Now()},
|
||||
{Username: "administrator", Nickname: "超级管理员", Email: "admin@yourweb.com", Phone: "10086", Password: hash.Make("123456"), Sex: 1, Status: 1, LastLoginTime: datetime.Time{Time: time.Now()}},
|
||||
}
|
||||
|
||||
db.Client.Create(&seeders)
|
||||
@@ -123,7 +124,7 @@ func (model *Admin) GetMenuListById(id interface{}) (menuList interface{}, Error
|
||||
}
|
||||
|
||||
// 更新最后一次登录数据
|
||||
func (model *Admin) UpdateLastLogin(uid int, lastLoginIp string, lastLoginTime time.Time) error {
|
||||
func (model *Admin) UpdateLastLogin(uid int, lastLoginIp string, lastLoginTime datetime.Time) error {
|
||||
data := Admin{
|
||||
LastLoginIp: lastLoginIp,
|
||||
LastLoginTime: lastLoginTime,
|
||||
|
@@ -1,24 +1,23 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime"
|
||||
)
|
||||
|
||||
// 字段
|
||||
type Config struct {
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
Title string `json:"title" gorm:"size:255;not null"`
|
||||
Type string `json:"type" gorm:"size:20;not null"`
|
||||
Name string `json:"name" gorm:"size:255;not null"`
|
||||
Sort int `json:"sort" gorm:"size:11;default:0"`
|
||||
GroupName string `json:"group_name" gorm:"size:255;not null"`
|
||||
Value string `json:"value" gorm:"size:2000"`
|
||||
Remark string `json:"remark" gorm:"size:100;not null"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
Title string `json:"title" gorm:"size:255;not null"`
|
||||
Type string `json:"type" gorm:"size:20;not null"`
|
||||
Name string `json:"name" gorm:"size:255;not null"`
|
||||
Sort int `json:"sort" gorm:"size:11;default:0"`
|
||||
GroupName string `json:"group_name" gorm:"size:255;not null"`
|
||||
Value string `json:"value" gorm:"size:2000"`
|
||||
Remark string `json:"remark" gorm:"size:100;not null"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
CreatedAt datetime.Time `json:"created_at"`
|
||||
UpdatedAt datetime.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// 存储配置
|
||||
|
@@ -5,28 +5,28 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime"
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
// 字段
|
||||
type File struct {
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
ObjType string `json:"obj_type" gorm:"size:255"`
|
||||
ObjId int `json:"obj_id" gorm:"size:11;default:0"`
|
||||
FileCategoryId int `json:"file_category_id" gorm:"size:11;default:0"`
|
||||
Sort int `json:"sort" gorm:"size:11;default:0"`
|
||||
Name string `json:"name" gorm:"size:255;not null"`
|
||||
Size int64 `json:"size" gorm:"size:20;default:0"`
|
||||
Ext string `json:"ext" gorm:"size:255"`
|
||||
Path string `json:"path" gorm:"size:255;not null"`
|
||||
Url string `json:"url" gorm:"size:255;not null"`
|
||||
Hash string `json:"hash" gorm:"size:255;not null"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
ObjType string `json:"obj_type" gorm:"size:255"`
|
||||
ObjId int `json:"obj_id" gorm:"size:11;default:0"`
|
||||
FileCategoryId int `json:"file_category_id" gorm:"size:11;default:0"`
|
||||
Sort int `json:"sort" gorm:"size:11;default:0"`
|
||||
Name string `json:"name" gorm:"size:255;not null"`
|
||||
Size int64 `json:"size" gorm:"size:20;default:0"`
|
||||
Ext string `json:"ext" gorm:"size:255"`
|
||||
Path string `json:"path" gorm:"size:255;not null"`
|
||||
Url string `json:"url" gorm:"size:255;not null"`
|
||||
Hash string `json:"hash" gorm:"size:255;not null"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
CreatedAt datetime.Time `json:"created_at"`
|
||||
UpdatedAt datetime.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// 插入数据并返回ID
|
||||
|
@@ -2,35 +2,35 @@ package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-basic/uuid"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/fields/tree"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/fields/treeselect"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/lister"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 字段
|
||||
type Menu struct {
|
||||
Key string `json:"key" gorm:"<-:false"`
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
Name string `json:"name" gorm:"size:100;not null"`
|
||||
GuardName string `json:"group_name" gorm:"size:100;not null"`
|
||||
Icon string `json:"icon" gorm:"size:100;"`
|
||||
Type int `json:"type" gorm:"size:100;not null"` // 菜单类型:1目录,2菜单,3按钮
|
||||
Pid int `json:"pid" gorm:"size:11;default:0"`
|
||||
Sort int `json:"sort" gorm:"size:11;default:0"`
|
||||
Path string `json:"path" gorm:"size:255"`
|
||||
Show int `json:"show" gorm:"size:1;not null;default:1"`
|
||||
IsEngine int `json:"is_engine" gorm:"size:1;not null;default:0"`
|
||||
IsLink int `json:"is_link" gorm:"size:1;not null;default:0"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
Locale string `json:"locale" gorm:"<-:false"`
|
||||
HideInMenu bool `json:"hide_in_menu" gorm:"<-:false"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
Name string `json:"name" gorm:"size:100;not null"`
|
||||
GuardName string `json:"group_name" gorm:"size:100;not null"`
|
||||
Icon string `json:"icon" gorm:"size:100;"`
|
||||
Type int `json:"type" gorm:"size:100;not null"` // 菜单类型:1目录,2菜单,3按钮
|
||||
Pid int `json:"pid" gorm:"size:11;default:0"`
|
||||
Sort int `json:"sort" gorm:"size:11;default:0"`
|
||||
Path string `json:"path" gorm:"size:255"`
|
||||
Show int `json:"show" gorm:"size:1;not null;default:1"`
|
||||
IsEngine int `json:"is_engine" gorm:"size:1;not null;default:0"`
|
||||
IsLink int `json:"is_link" gorm:"size:1;not null;default:0"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
Key string `json:"key" gorm:"<-:false"`
|
||||
Locale string `json:"locale" gorm:"<-:false"`
|
||||
HideInMenu bool `json:"hide_in_menu" gorm:"<-:false"`
|
||||
CreatedAt datetime.Time `json:"created_at"`
|
||||
UpdatedAt datetime.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// 菜单表
|
||||
|
@@ -1,23 +1,22 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/fields/selectfield"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/fields/transfer"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime"
|
||||
)
|
||||
|
||||
// 权限
|
||||
type Permission struct {
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
Name string `json:"name" gorm:"size:500;not null"`
|
||||
GuardName string `json:"group_name" gorm:"size:100;not null"`
|
||||
Path string `json:"path" gorm:"size:500;not null"`
|
||||
Method string `json:"method" gorm:"size:500;not null"`
|
||||
Remark string `json:"remark" gorm:"size:100"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
Name string `json:"name" gorm:"size:500;not null"`
|
||||
GuardName string `json:"group_name" gorm:"size:100;not null"`
|
||||
Path string `json:"path" gorm:"size:500;not null"`
|
||||
Method string `json:"method" gorm:"size:500;not null"`
|
||||
Remark string `json:"remark" gorm:"size:100"`
|
||||
CreatedAt datetime.Time `json:"created_at"`
|
||||
UpdatedAt datetime.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// 获取列表
|
||||
|
@@ -7,26 +7,27 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime"
|
||||
)
|
||||
|
||||
// 字段
|
||||
type Picture struct {
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
ObjType string `json:"obj_type" gorm:"size:255"`
|
||||
ObjId int `json:"obj_id" gorm:"size:11;default:0"`
|
||||
PictureCategoryId int `json:"picture_category_id" gorm:"size:11;default:0"`
|
||||
Sort int `json:"sort" gorm:"size:11;default:0"`
|
||||
Name string `json:"name" gorm:"size:255;not null"`
|
||||
Size int64 `json:"size" gorm:"size:20;default:0"`
|
||||
Width int `json:"width" gorm:"size:11;default:0"`
|
||||
Height int `json:"height" gorm:"size:11;default:0"`
|
||||
Ext string `json:"ext" gorm:"size:255"`
|
||||
Path string `json:"path" gorm:"size:255;not null"`
|
||||
Url string `json:"url" gorm:"size:255;not null"`
|
||||
Hash string `json:"hash" gorm:"size:255;not null"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
ObjType string `json:"obj_type" gorm:"size:255"`
|
||||
ObjId int `json:"obj_id" gorm:"size:11;default:0"`
|
||||
PictureCategoryId int `json:"picture_category_id" gorm:"size:11;default:0"`
|
||||
Sort int `json:"sort" gorm:"size:11;default:0"`
|
||||
Name string `json:"name" gorm:"size:255;not null"`
|
||||
Size int64 `json:"size" gorm:"size:20;default:0"`
|
||||
Width int `json:"width" gorm:"size:11;default:0"`
|
||||
Height int `json:"height" gorm:"size:11;default:0"`
|
||||
Ext string `json:"ext" gorm:"size:255"`
|
||||
Path string `json:"path" gorm:"size:255;not null"`
|
||||
Url string `json:"url" gorm:"size:255;not null"`
|
||||
Hash string `json:"hash" gorm:"size:255;not null"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
CreatedAt datetime.Time `json:"created_at"`
|
||||
UpdatedAt datetime.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// 获取列表
|
||||
|
@@ -1,19 +1,18 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/fields/checkbox"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime"
|
||||
)
|
||||
|
||||
// 角色
|
||||
type Role struct {
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
Name string `json:"name" gorm:"size:255;not null"`
|
||||
GuardName string `json:"guard_name" gorm:"size:100;not null"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Id int `json:"id" gorm:"autoIncrement"`
|
||||
Name string `json:"name" gorm:"size:255;not null"`
|
||||
GuardName string `json:"guard_name" gorm:"size:100;not null"`
|
||||
CreatedAt datetime.Time `json:"created_at"`
|
||||
UpdatedAt datetime.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// 获取角色列表
|
||||
|
@@ -1,52 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
//自定义时间
|
||||
type Time time.Time
|
||||
|
||||
func (t *Time) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == "null" {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
//前端接收的时间字符串
|
||||
str := string(data)
|
||||
//去除接收的str收尾多余的"
|
||||
timeStr := strings.Trim(str, "\"")
|
||||
t1, err := time.Parse("2006-01-02 15:04:05", timeStr)
|
||||
*t = Time(t1)
|
||||
return err
|
||||
}
|
||||
|
||||
func (t Time) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%v\"", time.Time(t).Format("2006-01-02 15:04:05"))
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
|
||||
func (t Time) Value() (driver.Value, error) {
|
||||
// Time 转换成 time.Time 类型
|
||||
tTime := time.Time(t)
|
||||
return tTime.Format("2006-01-02 15:04:05"), nil
|
||||
}
|
||||
|
||||
func (t *Time) Scan(v interface{}) error {
|
||||
switch vt := v.(type) {
|
||||
case time.Time:
|
||||
// 字符串转成 time.Time 类型
|
||||
*t = Time(vt)
|
||||
case string:
|
||||
// 字符串转成 time.Time 类型
|
||||
getTime, _ := time.Parse("2006-01-02 15:04:05", vt)
|
||||
*t = Time(getTime)
|
||||
default:
|
||||
return errors.New("类型处理错误")
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/template/login"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/template/resource"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/builder"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/hash"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -124,7 +125,7 @@ func (p *Index) Handle(ctx *builder.Context) error {
|
||||
}
|
||||
|
||||
// 更新登录信息
|
||||
(&model.Admin{}).UpdateLastLogin(adminInfo.Id, ctx.ClientIP(), time.Now())
|
||||
(&model.Admin{}).UpdateLastLogin(adminInfo.Id, ctx.ClientIP(), datetime.Time{Time: time.Now()})
|
||||
|
||||
// 获取token字符串
|
||||
tokenString, err := ctx.JwtToken((&model.Admin{}).GetClaims(adminInfo))
|
||||
|
@@ -1,8 +1,6 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/model"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/service/actions"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/service/searches"
|
||||
@@ -51,13 +49,7 @@ func (p *ActionLog) Fields(ctx *builder.Context) []interface{} {
|
||||
field.Text("username", "用户"),
|
||||
field.Text("url", "行为").SetEllipsis(true),
|
||||
field.Text("ip", "IP"),
|
||||
field.Datetime("created_at", "发生时间", func() interface{} {
|
||||
if p.Field["created_at"] == nil {
|
||||
return p.Field["created_at"]
|
||||
}
|
||||
|
||||
return p.Field["created_at"].(time.Time).Format("2006-01-02 15:04:05")
|
||||
}),
|
||||
field.Datetime("created_at", "发生时间"),
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,6 @@ package resources
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/fields/radio"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/rule"
|
||||
@@ -136,17 +135,7 @@ func (p *Admin) Fields(ctx *builder.Context) []interface{} {
|
||||
OnlyOnForms().
|
||||
ShowOnImporting(true),
|
||||
|
||||
field.Datetime("last_login_time", "最后登录时间", func() interface{} {
|
||||
if p.Field["last_login_time"] == nil {
|
||||
return p.Field["last_login_time"]
|
||||
}
|
||||
|
||||
if p.Field["last_login_time"].(time.Time).Format("2006-01-02 15:04:05") == "0001-01-01 00:00:00" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return p.Field["last_login_time"].(time.Time).Format("2006-01-02 15:04:05")
|
||||
}).OnlyOnIndex(),
|
||||
field.Datetime("last_login_time", "最后登录时间").OnlyOnIndex(),
|
||||
|
||||
field.Switch("status", "状态").
|
||||
SetRules([]*rule.Rule{
|
||||
|
@@ -1,8 +1,6 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/model"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/service/actions"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/service/searches"
|
||||
@@ -38,13 +36,7 @@ func (p *File) Fields(ctx *builder.Context) []interface{} {
|
||||
field.Text("name", "名称"),
|
||||
field.Text("size", "大小").SetSorter(true),
|
||||
field.Text("ext", "扩展名"),
|
||||
field.Datetime("created_at", "上传时间", func() interface{} {
|
||||
if p.Field["created_at"] == nil {
|
||||
return p.Field["created_at"]
|
||||
}
|
||||
|
||||
return p.Field["created_at"].(time.Time).Format("2006-01-02 15:04:05")
|
||||
}),
|
||||
field.Datetime("created_at", "上传时间"),
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,8 +1,6 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/model"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/service/actions"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/service/searches"
|
||||
@@ -44,13 +42,7 @@ func (p *Picture) Fields(ctx *builder.Context) []interface{} {
|
||||
field.Text("width", "宽度"),
|
||||
field.Text("height", "高度"),
|
||||
field.Text("ext", "扩展名"),
|
||||
field.Datetime("created_at", "上传时间", func() interface{} {
|
||||
if p.Field["created_at"] == nil {
|
||||
return p.Field["created_at"]
|
||||
}
|
||||
|
||||
return p.Field["created_at"].(time.Time).Format("2006-01-02 15:04:05")
|
||||
}),
|
||||
field.Datetime("created_at", "上传时间"),
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,6 @@ package resources
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/rule"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/message"
|
||||
@@ -47,22 +46,18 @@ func (p *Role) Fields(ctx *builder.Context) []interface{} {
|
||||
rule.Required(true, "名称必须填写"),
|
||||
}),
|
||||
|
||||
field.Text("guard_name", "GuardName").SetDefault("admin"),
|
||||
field.Tree("menu_ids", "权限").SetData(treeData).OnlyOnForms(),
|
||||
field.Datetime("created_at", "创建时间", func() interface{} {
|
||||
if p.Field["created_at"] == nil {
|
||||
return p.Field["created_at"]
|
||||
}
|
||||
field.Text("guard_name", "GuardName").
|
||||
SetDefault("admin"),
|
||||
|
||||
return p.Field["created_at"].(time.Time).Format("2006-01-02 15:04:05")
|
||||
}).OnlyOnIndex(),
|
||||
field.Datetime("updated_at", "更新时间", func() interface{} {
|
||||
if p.Field["updated_at"] == nil {
|
||||
return p.Field["updated_at"]
|
||||
}
|
||||
field.Tree("menu_ids", "权限").
|
||||
SetData(treeData).
|
||||
OnlyOnForms(),
|
||||
|
||||
return p.Field["updated_at"].(time.Time).Format("2006-01-02 15:04:05")
|
||||
}).OnlyOnIndex(),
|
||||
field.Datetime("created_at", "创建时间").
|
||||
OnlyOnIndex(),
|
||||
|
||||
field.Datetime("updated_at", "更新时间").
|
||||
OnlyOnIndex(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -2,7 +2,6 @@ package resources
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/fields/radio"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/rule"
|
||||
@@ -115,17 +114,7 @@ func (p *User) Fields(ctx *builder.Context) []interface{} {
|
||||
OnlyOnForms().
|
||||
ShowOnImporting(true),
|
||||
|
||||
field.Datetime("last_login_time", "最后登录时间", func() interface{} {
|
||||
if p.Field["last_login_time"] == nil {
|
||||
return p.Field["last_login_time"]
|
||||
}
|
||||
|
||||
if p.Field["last_login_time"].(time.Time).Format("2006-01-02 15:04:05") == "0001-01-01 00:00:00" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return p.Field["last_login_time"].(time.Time).Format("2006-01-02 15:04:05")
|
||||
}).OnlyOnIndex(),
|
||||
field.Datetime("last_login_time", "最后登录时间").OnlyOnIndex(),
|
||||
|
||||
field.Switch("status", "状态").
|
||||
SetRules([]*rule.Rule{
|
||||
|
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
adminmodel "github.com/quarkcloudio/quark-go/v2/pkg/app/admin/model"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/hash"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -22,12 +23,12 @@ type User struct {
|
||||
Password string `json:"password" gorm:"size:255;not null"`
|
||||
Avatar string `json:"avatar" gorm:"size:1000"`
|
||||
LastLoginIp string `json:"last_login_ip" gorm:"size:255"`
|
||||
LastLoginTime time.Time `json:"last_login_time"`
|
||||
LastLoginTime datetime.Time `json:"last_login_time" gorm:"default:null"`
|
||||
WxOpenid string `json:"wx_openid" gorm:"size:255"`
|
||||
WxUnionid string `json:"wx_unionid" gorm:"size:255"`
|
||||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CreatedAt datetime.Time `json:"created_at"`
|
||||
UpdatedAt datetime.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at"`
|
||||
}
|
||||
|
||||
@@ -60,7 +61,7 @@ func (model *User) Seeder() {
|
||||
db.Client.Create(&menuSeeders)
|
||||
|
||||
seeders := []User{
|
||||
{Username: "tangtanglove", Nickname: "默认用户", Email: "tangtanglove@yourweb.com", Phone: "10086", Password: hash.Make("123456"), Sex: 1, Status: 1, LastLoginTime: time.Now()},
|
||||
{Username: "tangtanglove", Nickname: "默认用户", Email: "tangtanglove@yourweb.com", Phone: "10086", Password: hash.Make("123456"), Sex: 1, Status: 1, LastLoginTime: datetime.Time{Time: time.Now()}},
|
||||
}
|
||||
|
||||
db.Client.Create(&seeders)
|
||||
@@ -133,7 +134,7 @@ func (model *User) GetInfoByUsername(username string) (User *User, Error error)
|
||||
}
|
||||
|
||||
// 更新最后一次登录数据
|
||||
func (model *User) UpdateLastLogin(uid int, lastLoginIp string, lastLoginTime time.Time) error {
|
||||
func (model *User) UpdateLastLogin(uid int, lastLoginIp string, lastLoginTime datetime.Time) error {
|
||||
data := User{
|
||||
LastLoginIp: lastLoginIp,
|
||||
LastLoginTime: lastLoginTime,
|
||||
|
@@ -22,7 +22,7 @@ const (
|
||||
AppName = "QuarkGo"
|
||||
|
||||
// 版本号
|
||||
Version = "2.3.11"
|
||||
Version = "2.4.0"
|
||||
|
||||
// 包名
|
||||
PkgName = "github.com/quarkcloudio/quark-go/v2"
|
||||
|
75
pkg/utils/datetime/date.go
Normal file
75
pkg/utils/datetime/date.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package datetime
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 日期
|
||||
type Date struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
// 编码为自定义的Json格式
|
||||
func (t Date) MarshalJSON() ([]byte, error) {
|
||||
|
||||
// 时间为零返回null
|
||||
if t.IsZero() {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
|
||||
return []byte("\"" + t.Format("2006-01-02") + "\""), nil
|
||||
}
|
||||
|
||||
// 将Json格式解码
|
||||
func (t *Date) UnmarshalJSON(data []byte) error {
|
||||
|
||||
var err error
|
||||
|
||||
if len(data) == 2 || string(data) == "null" {
|
||||
return err
|
||||
}
|
||||
|
||||
var now time.Time
|
||||
|
||||
// 自定义格式解析
|
||||
if now, err = time.ParseInLocation("2006-01-02", string(data), time.Local); err == nil {
|
||||
*t = Date{now}
|
||||
return err
|
||||
}
|
||||
|
||||
// 带引号的自定义格式解析
|
||||
if now, err = time.ParseInLocation("\"2006-01-02\"", string(data), time.Local); err == nil {
|
||||
*t = Date{now}
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// 转换为数据库值
|
||||
func (t Date) Value() (driver.Value, error) {
|
||||
|
||||
if t.IsZero() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return t.Time, nil
|
||||
}
|
||||
|
||||
// 数据库值转换为Time
|
||||
func (t *Date) Scan(i interface{}) error {
|
||||
|
||||
if value, ok := i.(time.Time); ok {
|
||||
*t = Date{Time: value}
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("无法将值转换为时间戳")
|
||||
}
|
||||
|
||||
// 返回字符串
|
||||
func (t *Date) String() string {
|
||||
return t.Format("2006-01-02")
|
||||
}
|
35
pkg/utils/datetime/format.go
Normal file
35
pkg/utils/datetime/format.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package datetime
|
||||
|
||||
import "time"
|
||||
|
||||
// 将字符串解析为日期
|
||||
func ParseDate(value string) (Date, error) {
|
||||
|
||||
date, err := time.ParseInLocation("2006-01-02", value, time.Local)
|
||||
|
||||
return Date{Time: date}, err
|
||||
}
|
||||
|
||||
// 将字符串解析为日期
|
||||
func ParseDateInLocation(value string, location *time.Location) (Date, error) {
|
||||
|
||||
date, err := time.ParseInLocation("2006-01-02", value, location)
|
||||
|
||||
return Date{Time: date}, err
|
||||
}
|
||||
|
||||
// 将字符串解析为时间
|
||||
func ParseTime(value string) (Time, error) {
|
||||
|
||||
time, err := time.ParseInLocation("2006-01-02 15:04:05", value, time.Local)
|
||||
|
||||
return Time{Time: time}, err
|
||||
}
|
||||
|
||||
// 将字符串解析为时间
|
||||
func ParseTimeInLocation(value string, location *time.Location) (Time, error) {
|
||||
|
||||
time, err := time.ParseInLocation("2006-01-02 15:04:05", value, location)
|
||||
|
||||
return Time{Time: time}, err
|
||||
}
|
86
pkg/utils/datetime/time.go
Normal file
86
pkg/utils/datetime/time.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package datetime
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 时间
|
||||
type Time struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
// 编码为自定义的Json格式
|
||||
func (t Time) MarshalJSON() ([]byte, error) {
|
||||
|
||||
// 时间为零返回null
|
||||
if t.IsZero() {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
|
||||
return []byte("\"" + t.Format("2006-01-02 15:04:05") + "\""), nil
|
||||
}
|
||||
|
||||
// 将Json格式解码
|
||||
func (t *Time) UnmarshalJSON(data []byte) error {
|
||||
|
||||
var err error
|
||||
|
||||
if len(data) == 2 || string(data) == "null" {
|
||||
return err
|
||||
}
|
||||
|
||||
var now time.Time
|
||||
|
||||
// 自定义格式解析
|
||||
if now, err = time.ParseInLocation("2006-01-02 15:04:05", string(data), time.Local); err == nil {
|
||||
*t = Time{now}
|
||||
return err
|
||||
}
|
||||
|
||||
// 带引号的自定义格式解析
|
||||
if now, err = time.ParseInLocation("\"2006-01-02 15:04:05\"", string(data), time.Local); err == nil {
|
||||
*t = Time{now}
|
||||
return err
|
||||
}
|
||||
|
||||
// 默认格式解析
|
||||
if now, err = time.ParseInLocation(time.RFC3339, string(data), time.Local); err == nil {
|
||||
*t = Time{now}
|
||||
return err
|
||||
}
|
||||
|
||||
if now, err = time.ParseInLocation("\""+time.RFC3339+"\"", string(data), time.Local); err == nil {
|
||||
*t = Time{now}
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// 转换为数据库值
|
||||
func (t Time) Value() (driver.Value, error) {
|
||||
|
||||
if t.IsZero() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return t.Time, nil
|
||||
}
|
||||
|
||||
// 数据库值转换为Time
|
||||
func (t *Time) Scan(i interface{}) error {
|
||||
|
||||
if value, ok := i.(time.Time); ok {
|
||||
*t = Time{Time: value}
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("无法将值转换为时间戳")
|
||||
}
|
||||
|
||||
// 返回字符串
|
||||
func (t *Time) String() string {
|
||||
return t.Format("2006-01-02 15:04:05")
|
||||
}
|
Reference in New Issue
Block a user