user.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package model
  2. import (
  3. "database/sql/driver"
  4. "fmt"
  5. "time"
  6. )
  7. /* code 结构体 */
  8. type CodeData struct {
  9. Code string `form:"code" binding:"required"`
  10. Gid string `form:"gid" binding:"required"`
  11. Pf string `form:"pf" binding:"required"`
  12. Secret string `form:"secret"`
  13. }
  14. type Users struct {
  15. ID int `json:"id" gorm:"not null;"`
  16. OpenId string `json:"openId" gorm:"not null;column:openId;"`
  17. Pf string `json:"pf" gorm:"not null;"`
  18. UserId int `json:"userId" gorm:"not null;column:userId;"`
  19. CreatedAt time.Time `json:"createdAt" gorm:"column:createdAt;"`
  20. UpdatedAt time.Time `json:"updatedAt" gorm:"column:updatedAt;"`
  21. }
  22. type User struct {
  23. ID int `json:"id" gorm:"not null;"`
  24. Pf string `json:"pf" gorm:"not null;"`
  25. Gid string `json:"gid" gorm:"not null;"`
  26. UserId int `json:"userId" gorm:"not null;column:userId;"`
  27. CreatedAt time.Time `json:"createdAt" gorm:"column:createdAt;"`
  28. }
  29. type UserLogin struct {
  30. ID int `json:"id" gorm:"not null;"`
  31. Pf string `json:"pf" gorm:"not null;"`
  32. Gid string `json:"gid" gorm:"not null;"`
  33. UserId int `json:"userId" gorm:"not null;column:userId;"`
  34. LoginTime time.Time `json:"loginTime" gorm:"column:loginTime;"`
  35. }
  36. type UserOnline struct {
  37. ID int `json:"id" gorm:"not null;"`
  38. Pf string `json:"pf" gorm:"not null;"`
  39. Gid string `json:"gid" gorm:"not null;"`
  40. Type int `json:"type" gorm:"not null;"`
  41. UserId int `json:"userId" gorm:"not null;column:userId;"`
  42. LogTime time.Time `json:"logTime" gorm:"column:logTime;"`
  43. }
  44. type Admin struct {
  45. ID int `json:"id" gorm:"not null;"`
  46. Account string `json:"account" gorm:"not null;"`
  47. Password string `json:"password" gorm:"not null;"`
  48. Name string `json:"name" gorm:"not null;"`
  49. CreatedAt time.Time `json:"createdAt" gorm:"column:createdAt;"`
  50. UpdatedAt time.Time `json:"updatedAt" gorm:"column:updatedAt;"`
  51. }
  52. // 1. 创建 time.Time 类型的副本 XTime;
  53. type XTime struct {
  54. time.Time
  55. }
  56. // 2. 为 Xtime 重写 MarshaJSON 方法,在此方法中实现自定义格式的转换;
  57. func (t XTime) MarshalJSON() ([]byte, error) {
  58. output := fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05"))
  59. return []byte(output), nil
  60. }
  61. // 3. 为 Xtime 实现 Value 方法,写入数据库时会调用该方法将自定义时间类型转换并写入数据库;
  62. func (t XTime) Value() (driver.Value, error) {
  63. var zeroTime time.Time
  64. if t.Time.UnixNano() == zeroTime.UnixNano() {
  65. return nil, nil
  66. }
  67. return t.Time, nil
  68. }
  69. // 4. 为 Xtime 实现 Scan 方法,读取数据库时会调用该方法将时间数据转换成自定义时间类型;
  70. func (t *XTime) Scan(v interface{}) error {
  71. value, ok := v.(time.Time)
  72. if ok {
  73. *t = XTime{Time: value}
  74. return nil
  75. }
  76. return fmt.Errorf("can not convert %v to timestamp", v)
  77. }
  78. type GameAction struct {
  79. ID int `json:"id" gorm:"not null;"`
  80. Gid string `json:"gid" gorm:"not null;"`
  81. ActionId string `json:"actionId" gorm:"not null;column:actionId;"`
  82. ActionName string `json:"actionName" gorm:"not null;column:actionName;"`
  83. Status int `json:"status" gorm:"not null;column:status;"`
  84. Remark string `json:"remark" gorm:"not null;column:remark;"`
  85. CreatedAt XTime `json:"createdAt" gorm:"column:createdAt;type:date;"`
  86. UpdatedAt XTime `json:"updatedAt" gorm:"column:updatedAt;type:date;"`
  87. }
  88. type GameActionOption struct {
  89. ID int `json:"id" gorm:"not null;"`
  90. ActionId int `json:"actionId" gorm:"not null;column:actionId;"`
  91. OptionId string `json:"optionId" gorm:"not null;column:optionId;"`
  92. OptionName string `json:"optionName" gorm:"not null;column:optionName;"`
  93. OptionType string `json:"optionType" gorm:"not null;column:optionType;"`
  94. Status int `json:"status" gorm:"not null;column:status;"`
  95. CreatedAt XTime `json:"createdAt" gorm:"column:createdAt;"`
  96. UpdatedAt XTime `json:"updatedAt" gorm:"column:updatedAt;"`
  97. }
  98. type UserAction struct {
  99. ID int `json:"id" gorm:"not null;"`
  100. Pf string `json:"pf" gorm:"not null;"`
  101. Gid string `json:"gid" gorm:"not null;"`
  102. ActionId string `json:"actionId" gorm:"not null;column:actionId;"`
  103. UserId int `json:"userId" gorm:"not null;column:userId;"`
  104. CreatedAt XTime `json:"createdAt" gorm:"column:createdAt;type:date;"`
  105. Data string `json:"data" gorm:"not null;column:data;"`
  106. }