appUser.go 5.6 KB

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