Compare commits

..

No commits in common. 'bf7064f9920e88b05bb6ce68838876fac8d426b7' and '07b3a61facbbd349fb43f7423865be1dba355945' have entirely different histories.

  1. 46
      app/db.go
  2. 7
      app/net.go
  3. 8
      main.go

@ -17,67 +17,43 @@ func ConfigGormDB() {
if err != nil {
panic(err)
}
if err = DB.AutoMigrate(&User{}, &UserToken{}, &Goods{}, &Price{}); err != nil {
panic(err)
}
hash, err := HashPassword("123456")
if err != nil {
panic(err)
}
admin := User{
ID: 1,
Name: "超级管理员",
PhoneNumber: "18982052224",
Password: hash,
Admin: true,
}
err = DB.Save(&admin).Error
if err != nil {
if err = DB.AutoMigrate(&User{}, &Goods{}, &Price{}); err != nil {
panic(err)
}
}
// User 用户
type User struct {
ID uint `json:"id" gorm:"primarykey"`
gorm.Model
Name string `json:"name"` // 用户名称
PhoneNumber string `json:"phone_number"` // 用户手机
Password string `json:"-"` // 登录密码
Admin bool `json:"admin"` // 是不是管理员
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
Admin bool `json:"-"` // 是不是管理员
}
// UserToken 用户令牌
type UserToken struct {
Code string `json:"code" gorm:"primarykey"` // 主键
UserID uint `json:"user_id"` // 用户ID
User *User `json:"user,omitempty"` // 关联用户
AccessToken string `json:"access_token"` // 授权令牌
RefreshToken string `json:"refresh_token"` // 刷新令牌
CreatedAt time.Time `json:"-"` // 创建时间
Code string `gorm:"primarykey"` // 主键
UserID uint // 用户ID
User *User // 关联用户
AccessToken string // 授权令牌
RefreshToken string // 刷新令牌
CreatedAt time.Time // 创建时间
}
// Goods 商品
type Goods struct {
ID uint `json:"id" gorm:"primarykey"`
gorm.Model
Name string `json:"name"` // 商品名称
Price float32 `json:"price"` // 商品当前价格
Prices []Price `json:"prices,omitempty"` // 商品价格列表
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
// Price 商品价格
type Price struct {
ID uint `json:"id" gorm:"primarykey"`
gorm.Model
GoodsID uint `json:"goods_id"` // 管理商品
Price float32 `json:"price"` // 商品价格
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
// Paginate 分页查询作用域

@ -248,18 +248,13 @@ func (w *ResponseWriter) Error(err error) {
if len(message) == 0 {
message = http.StatusText(status)
}
w.Send(status, map[string]any{
"code": code,
"message": message,
"success": code == 0,
})
w.Send(status, map[string]any{"code": code, "message": message})
}
func (w *ResponseWriter) Ok(data any, message ...string) {
info := map[string]any{
"code": 0,
"message": "ok",
"success": true,
"data": data,
}
if len(message) > 0 {

@ -12,7 +12,7 @@ import (
)
func main() {
app.ConfigLogger("./tmp/debug.log", app.LogWhenMinute)
app.ConfigLogger("debug.log", app.LogWhenMinute)
app.ConfigGormDB()
r := chi.NewRouter()
@ -38,6 +38,9 @@ func main() {
// Please see _example/main.go for other more, or read the library code.
r.Use(httprate.LimitByIP(100, 1*time.Minute))
// mounting net/http/pprof
r.Mount("/debug", middleware.Profiler())
// 允许跨域
// see: https://developer.github.com/v3/#cross-origin-resource-sharing
r.Use(cors.Handler(cors.Options{
@ -51,9 +54,6 @@ func main() {
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
// mounting net/http/pprof
r.Mount("/debug", middleware.Profiler())
app.RegisterRoutes(r)
if err := http.ListenAndServe(":3000", r); err != nil {

Loading…
Cancel
Save