appUser.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "strings"
  9. "time"
  10. )
  11. type AppUser struct {
  12. //gorm.Model
  13. utils.BaseModel
  14. Username string `gorm:"type:varchar(50);index:username_name,unique" json:"username" min:"3" max:"50"` // 用户名
  15. Phone string `gorm:"type:varchar(50);" json:"phone" min:"11" max:"11"` // 手机号
  16. Nickname string `gorm:"type:varchar(50);" json:"nickname"` // 昵称
  17. Password string `gorm:"type:varchar(50);" json:"password"` // 密码
  18. LastLoginTime string `gorm:"type:datetime;" json:"last_login_time"` // 最后登录时间
  19. State int `gorm:"type:int;" json:"state"` // 状态 0:正常 1:禁用
  20. AppID string `gorm:"type:varchar(50);" json:"app_id" ` // 应用id
  21. RegistMethod int `gorm:"type:int;" json:"regist_method"` // 注册方式 1 短信登录 2 微信登录 3 系统添加
  22. }
  23. type AppUserRegist struct {
  24. Username string `gorm:"type:varchar(50);index:username_name,unique" json:"username" validate:"required" min:"3" max:"50"` // 用户名
  25. Phone string `gorm:"type:varchar(50);" json:"phone" validate:"required" min:"11" max:"11"` // 手机号
  26. Nickname string `gorm:"type:varchar(50);" json:"nickname"` // 昵称
  27. Password string `gorm:"type:varchar(50);" json:"password" validate:"required" min:"6" max:"20"` // 密码
  28. AppID string `gorm:"type:varchar(50);not null;unique" json:"app_id" validate:"required"` // 应用id
  29. Code string `json:"code" validate:"required" min:"6" max:"6"` // 验证码
  30. RegistMethod int `gorm:"type:int;" json:"regist_method"` // 注册方式 1 短信登录 2 微信登录 3 系统添加
  31. }
  32. func (a AppUser) DeleteAppUserByID(id int, tableName string) e.Rescode {
  33. //TODO implement me
  34. tx := global.DBLink.Table(tableName).Where("id=?", id).Delete(&a)
  35. if tx.Error != nil {
  36. return e.DELETEFAIL
  37. }
  38. return e.SUCCESS
  39. }
  40. func (a AppUser) UpdateAppUser(appUser AppUser, tabaleName string) e.Rescode {
  41. //TODO implement me
  42. tx := global.DBLink.Table(tabaleName).Updates(appUser)
  43. if tx.Error != nil {
  44. return e.UPDATEFAIL
  45. }
  46. return e.SUCCESS
  47. }
  48. func (a AppUser) GetAppUserList(params unity.QueryPageParams, tableName string, queryCond string) (result []AppUser, total int64, err error) {
  49. query := global.DBLink.Table(tableName)
  50. if params.Query != "%%" {
  51. query = query.Where(queryCond, params.Query)
  52. }
  53. if params.State != "" {
  54. query = query.Where("state=?", params.State)
  55. }
  56. if err = query.Count(&total).Error; err != nil {
  57. return nil, 0, err
  58. }
  59. offset := (params.Page - 1) * params.Size
  60. if err = query.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  61. return nil, 0, err
  62. }
  63. return result, total, nil
  64. }
  65. // RegistAppUser 注册用户
  66. func (a AppUser) RegistAppUser(appUser AppUserRegist) e.Rescode {
  67. //TODO implement me
  68. tx := global.DBLink.Select("phone").Where("phone = ?", appUser.Phone).First(&a)
  69. ctx := context.Background()
  70. result, err := global.Rdb.Get(ctx, appUser.Phone).Result()
  71. if err != nil {
  72. return e.TheVerificationCodeWasNotSent
  73. } else if result != appUser.Code {
  74. return e.CodeIsError
  75. }
  76. if tx.RowsAffected == 0 {
  77. a.AppID = appUser.AppID
  78. a.State = 0
  79. a.Phone = appUser.Phone
  80. a.Username = appUser.Username
  81. a.Nickname = "游客" + unity.RandomId()
  82. md5 := utils.MD5(appUser.Password)
  83. a.Password = md5
  84. a.LastLoginTime = time.Now().Format("2006-01-02 15:04:05")
  85. a.RegistMethod = appUser.RegistMethod
  86. tableName := "appUser_" + appUser.AppID
  87. //验证表是否已经创建,没有则创建
  88. table := global.DBLink.Migrator().HasTable(tableName)
  89. if !table {
  90. global.DBLink.Table(tableName).AutoMigrate(&AppUser{})
  91. }
  92. db := global.DBLink.Table(tableName).Create(&a)
  93. if db.Error != nil {
  94. return e.RegistrationFailed
  95. }
  96. return e.SUCCESS
  97. }
  98. return e.AlreadyExists
  99. }
  100. // AddAppUser 添加用户
  101. func (a AppUser) AddAppUser(appUser AppUser) e.Rescode {
  102. //TODO implement me
  103. //验证表是否已经创建,没有则创建
  104. tableName := "appUser_" + appUser.AppID
  105. table := global.DBLink.Migrator().HasTable(tableName)
  106. if !table {
  107. global.DBLink.Table(tableName).AutoMigrate(&AppUser{})
  108. }
  109. a.AppID = appUser.AppID
  110. a.State = 0
  111. a.Nickname = appUser.Nickname
  112. a.Username = appUser.Username
  113. a.Password = utils.MD5(appUser.Password)
  114. a.LastLoginTime = time.Now().Format("2006-01-02 15:04:05")
  115. a.RegistMethod = 3
  116. if err := global.DBLink.Table(tableName).Create(&a).Error; err != nil {
  117. if strings.Contains(err.Error(), "Duplicate entry") {
  118. return e.TheUserAlreadyExists
  119. }
  120. return e.AddAppUserFail
  121. }
  122. return e.SUCCESS
  123. }
  124. // GetAllUserCount 获取用户总数
  125. func GetAllUserCount(tableName string) (e.Rescode, int64) {
  126. var count int64
  127. tx := global.DBLink.Table(tableName).Count(&count)
  128. if tx.Error != nil {
  129. return e.ERROR, 0
  130. }
  131. return e.SUCCESS, count
  132. }