|
|
@ -7,6 +7,7 @@ import ( |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DB 用户数据操作
|
|
|
|
var DB *gorm.DB |
|
|
|
var DB *gorm.DB |
|
|
|
|
|
|
|
|
|
|
|
func ConfigGormDB() { |
|
|
|
func ConfigGormDB() { |
|
|
@ -20,6 +21,7 @@ func ConfigGormDB() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// User 用户
|
|
|
|
type User struct { |
|
|
|
type User struct { |
|
|
|
gorm.Model |
|
|
|
gorm.Model |
|
|
|
Name string `json:"name"` // 用户名称
|
|
|
|
Name string `json:"name"` // 用户名称
|
|
|
@ -42,6 +44,7 @@ type Price struct { |
|
|
|
Price float32 `json:"price"` // 商品价格
|
|
|
|
Price float32 `json:"price"` // 商品价格
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Paginate 分页查询作用域
|
|
|
|
func Paginate(r *Request) func(db *gorm.DB) *gorm.DB { |
|
|
|
func Paginate(r *Request) func(db *gorm.DB) *gorm.DB { |
|
|
|
return func(db *gorm.DB) *gorm.DB { |
|
|
|
return func(db *gorm.DB) *gorm.DB { |
|
|
|
page := r.Int("page", 1, func(p int) int { |
|
|
|
page := r.Int("page", 1, func(p int) int { |
|
|
@ -55,6 +58,7 @@ func Paginate(r *Request) func(db *gorm.DB) *gorm.DB { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Search 搜索作用域
|
|
|
|
func Search(r *Request, key, query string) func(db *gorm.DB) *gorm.DB { |
|
|
|
func Search(r *Request, key, query string) func(db *gorm.DB) *gorm.DB { |
|
|
|
return func(db *gorm.DB) *gorm.DB { |
|
|
|
return func(db *gorm.DB) *gorm.DB { |
|
|
|
if keyword, ok := r.Get(key); ok { |
|
|
|
if keyword, ok := r.Get(key); ok { |
|
|
@ -65,6 +69,7 @@ func Search(r *Request, key, query string) func(db *gorm.DB) *gorm.DB { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TimeRange 时间范围作用域
|
|
|
|
func TimeRange(r *Request, column string) func(db *gorm.DB) *gorm.DB { |
|
|
|
func TimeRange(r *Request, column string) func(db *gorm.DB) *gorm.DB { |
|
|
|
return func(db *gorm.DB) *gorm.DB { |
|
|
|
return func(db *gorm.DB) *gorm.DB { |
|
|
|
var queries []string |
|
|
|
var queries []string |
|
|
|