appUser.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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"` // 状态 1:正常 2:禁用
  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"` // 手机号
  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"` // 验证码
  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. // AccountLogin 账号登录
  38. func (a AppUser) AccountLogin(appUser AppUserRegist) (e.Rescode, string) {
  39. //TODO implement me
  40. tableName := "appUser_" + appUser.AppID
  41. tx := global.DBLink.Table(tableName)
  42. if tx.Error != nil {
  43. if strings.Contains(tx.Error.Error(), "Error 1146 (42S02)") {
  44. return e.NOTTABLE, ""
  45. }
  46. return e.LOGINFAIL, ""
  47. }
  48. md5 := utils.MD5(appUser.Password)
  49. tx.Select("username", "id", "password", "nickname", "phone").
  50. Where("username=? and password=?", appUser.Username, md5).
  51. Where("state=?", 1).First(&a)
  52. token, err := utils.CreateToken(a.ID, a.Username, "user")
  53. token = "interior:" + token
  54. if err != nil {
  55. return e.TokenIsFaild, ""
  56. }
  57. if tx.RowsAffected > 0 {
  58. global.DBLink.Table(tableName).Where("id=?", a.ID).Update("last_login_time", utils.Time(time.Now()))
  59. return e.SUCCESS, token
  60. }
  61. return e.ThePasswordIsWrongOrThePhoneNumberIsIncorrect, ""
  62. }
  63. // CodeLogin 验证码登录
  64. func (a AppUser) CodeLogin(appUser AppUserRegist) (e.Rescode, string) {
  65. //TODO implement me
  66. result, err := global.Rdb.Get(context.Background(), appUser.Phone).Result()
  67. if err != nil {
  68. return e.TheVerificationCodeWasNotSent, ""
  69. }
  70. if result != appUser.Code {
  71. return e.CodeIsError, ""
  72. }
  73. tableName := "appUser_" + appUser.AppID
  74. tx := global.DBLink.Table(tableName)
  75. tx.Select("username", "id", "password", "nickname", "phone").
  76. Where("phone=?", appUser.Phone).First(&a)
  77. token, err := utils.CreateToken(a.ID, a.Username, "user")
  78. token = "interior:" + token
  79. if a.Phone != "" {
  80. global.DBLink.Table(tableName).Where("id=?", a.ID).Update("last_login_time", utils.Time(time.Now()))
  81. return e.SUCCESS, token
  82. } else {
  83. return e.NOTREGIST, ""
  84. }
  85. }
  86. // DeleteAppUserByID 删除用户
  87. func (a AppUser) DeleteAppUserByID(id int, tableName string) e.Rescode {
  88. //TODO implement me
  89. tx := global.DBLink.Table(tableName).Where("id=?", id).Delete(&a)
  90. if tx.Error != nil {
  91. return e.DELETEFAIL
  92. }
  93. return e.SUCCESS
  94. }
  95. // UpdateAppUser 更新用户
  96. func (a AppUser) UpdateAppUser(appUser AppUser, tabaleName string) e.Rescode {
  97. //TODO implement me
  98. tx := global.DBLink.Table(tabaleName).Updates(appUser)
  99. if tx.Error != nil {
  100. return e.UPDATEFAIL
  101. }
  102. return e.SUCCESS
  103. }
  104. // GetAppUserList 获取用户列表
  105. func (a AppUser) GetAppUserList(params unity.CapQueryPageParams, tableName string, queryCond string) (result []AppUser, total int64, err error) {
  106. query := global.DBLink.Table(tableName)
  107. if params.Query != "%%" {
  108. query = query.Where(queryCond, params.Query)
  109. }
  110. if params.State != "" {
  111. query = query.Where("state=?", params.State)
  112. }
  113. if err = query.Count(&total).Error; err != nil {
  114. if strings.Contains(err.Error(), "Error 1146 (42S02)") {
  115. return nil, 0, errors.New("当前系统没有该表,请检查appid是否正确")
  116. }
  117. return nil, 0, err
  118. }
  119. offset := (params.Page - 1) * params.Size
  120. if err = query.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  121. return nil, 0, err
  122. }
  123. return result, total, nil
  124. }
  125. // RegistAppUser 注册用户
  126. func (a AppUser) RegistAppUser(appUser AppUserRegist) e.Rescode {
  127. //TODO implement me
  128. tx := global.DBLink.Select("phone").Where("phone = ?", appUser.Phone).First(&a)
  129. ctx := context.Background()
  130. result, err := global.Rdb.Get(ctx, appUser.Phone).Result()
  131. if err != nil {
  132. return e.TheVerificationCodeWasNotSent
  133. } else if result != appUser.Code {
  134. return e.CodeIsError
  135. }
  136. if tx.RowsAffected == 0 {
  137. a.AppID = appUser.AppID
  138. a.State = 1
  139. a.Phone = appUser.Phone
  140. a.Username = appUser.Username
  141. a.Nickname = "游客" + unity.RandomId()
  142. md5 := utils.MD5(appUser.Password)
  143. a.Password = md5
  144. a.LastLoginTime = utils.Time(time.Now())
  145. a.RegistMethod = appUser.RegistMethod
  146. tableName := "appUser_" + appUser.AppID
  147. //验证表是否已经创建,没有则创建
  148. table := global.DBLink.Migrator().HasTable(tableName)
  149. if !table {
  150. global.DBLink.Table(tableName).AutoMigrate(&AppUser{})
  151. }
  152. db := global.DBLink.Table(tableName).Create(&a)
  153. if db.Error != nil {
  154. return e.RegistrationFailed
  155. }
  156. return e.SUCCESS
  157. }
  158. return e.AlreadyExists
  159. }
  160. // AddAppUser 添加用户
  161. func (a AppUser) AddAppUser(appUser AppUser) e.Rescode {
  162. //TODO implement me
  163. //验证表是否已经创建,没有则创建
  164. tableName := "appUser_" + appUser.AppID
  165. table := global.DBLink.Migrator().HasTable(tableName)
  166. if !table {
  167. global.DBLink.Table(tableName).AutoMigrate(&AppUser{})
  168. }
  169. a.AppID = appUser.AppID
  170. a.State = 1
  171. a.Nickname = appUser.Nickname
  172. a.Username = appUser.Username
  173. a.Password = utils.MD5(appUser.Password)
  174. a.LastLoginTime = utils.Time(time.Now())
  175. a.RegistMethod = 3
  176. if err := global.DBLink.Table(tableName).Create(&a).Error; err != nil {
  177. if strings.Contains(err.Error(), "Duplicate entry") {
  178. return e.TheUserAlreadyExists
  179. }
  180. return e.AddAppUserFail
  181. }
  182. return e.SUCCESS
  183. }
  184. // GetAllUserCount 获取用户总数
  185. func GetAllUserCount(tableName string) (e.Rescode, int64) {
  186. var count int64
  187. tx := global.DBLink.Table(tableName).Count(&count)
  188. if tx.Error != nil {
  189. return e.ERROR, 0
  190. }
  191. return e.SUCCESS, count
  192. }