appUser.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package model
  2. import (
  3. "context"
  4. "project_management/app/e"
  5. "project_management/global"
  6. "project_management/unity"
  7. "project_management/utils"
  8. "time"
  9. )
  10. type AppUser struct {
  11. //gorm.Model
  12. utils.BaseModel
  13. Username string `gorm:"type:varchar(50);index:username_name,unique" json:"username" validate:"required" min:"3" max:"50"` // 用户名
  14. Phone string `gorm:"type:varchar(50);" json:"phone" min:"11" max:"11"` // 手机号
  15. Nickname string `gorm:"type:varchar(50);" json:"nickname"` // 昵称
  16. Password string `gorm:"type:varchar(50);" json:"password" validate:"required"` // 密码
  17. LastLoginTime string `gorm:"type:datetime;" json:"last_login_time"` // 最后登录时间
  18. State int `gorm:"type:int;" json:"state"` // 状态 0:正常 1:禁用
  19. AppID string `gorm:"type:varchar(50);" json:"app_id" validate:"required"` // 应用id
  20. RegistMethod int `gorm:"type:int;" json:"regist_method"` // 注册方式 1 短信登录 2 微信登录 3 系统添加
  21. }
  22. type AppUserRegist struct {
  23. Username string `gorm:"type:varchar(50);index:username_name,unique" json:"username" validate:"required" min:"3" max:"50"` // 用户名
  24. Phone string `gorm:"type:varchar(50);" json:"phone" validate:"required" min:"11" max:"11"` // 手机号
  25. Nickname string `gorm:"type:varchar(50);" json:"nickname"` // 昵称
  26. Password string `gorm:"type:varchar(50);" json:"password" validate:"required" min:"6" max:"20"` // 密码
  27. AppID string `gorm:"type:varchar(50);not null;unique" json:"app_id" validate:"required"` // 应用id
  28. Code string `json:"code" validate:"required" min:"6" max:"6"` // 验证码
  29. RegistMethod int `gorm:"type:int;" json:"regist_method"` // 注册方式 1 短信登录 2 微信登录 3 系统添加
  30. }
  31. // RegistAppUser 注册用户
  32. func (a AppUser) RegistAppUser(appUser AppUserRegist) e.Rescode {
  33. //TODO implement me
  34. tx := global.DBLink.Select("phone").Where("phone = ?", appUser.Phone).First(&a)
  35. ctx := context.Background()
  36. result, err := global.Rdb.Get(ctx, appUser.Phone).Result()
  37. if err != nil {
  38. return e.TheVerificationCodeWasNotSent
  39. } else if result != appUser.Code {
  40. return e.CodeIsError
  41. }
  42. if tx.RowsAffected == 0 {
  43. a.AppID = appUser.AppID
  44. a.State = 0
  45. a.Phone = appUser.Phone
  46. a.Username = appUser.Username
  47. a.Nickname = "游客" + unity.RandomId()
  48. md5 := utils.MD5(appUser.Password)
  49. a.Password = md5
  50. a.LastLoginTime = time.Now().Format("2006-01-02 15:04:05")
  51. a.RegistMethod = appUser.RegistMethod
  52. tableName := "appUser_" + appUser.AppID
  53. //验证表是否已经创建,没有则创建
  54. table := global.DBLink.Migrator().HasTable(tableName)
  55. if !table {
  56. global.DBLink.Table(tableName).AutoMigrate(&AppUser{})
  57. }
  58. db := global.DBLink.Table(tableName).Create(&a)
  59. if db.Error != nil {
  60. return e.RegistrationFailed
  61. }
  62. return e.SUCCESS
  63. }
  64. return e.AlreadyExists
  65. }
  66. // AddAppUser 添加用户
  67. func (a AppUser) AddAppUser(appUser AppUser) e.Rescode {
  68. //TODO implement me
  69. //验证表是否已经创建,没有则创建
  70. tableName := "appUser_" + appUser.AppID
  71. table := global.DBLink.Migrator().HasTable(tableName)
  72. if !table {
  73. global.DBLink.Table(tableName).AutoMigrate(&AppUser{})
  74. }
  75. a.AppID = appUser.AppID
  76. a.State = 0
  77. a.Nickname = appUser.Nickname
  78. a.Username = appUser.Username
  79. a.Password = utils.MD5(appUser.Password)
  80. a.LastLoginTime = time.Now().Format("2006-01-02 15:04:05")
  81. a.RegistMethod = 3
  82. if err := global.DBLink.Table(tableName).Create(&a).Error; err != nil {
  83. return e.AddAppUserFail
  84. }
  85. return e.SUCCESS
  86. }