user_imp.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package imp
  2. import (
  3. "Ic_ouath/app/e"
  4. "Ic_ouath/global"
  5. "Ic_ouath/models"
  6. "Ic_ouath/utils"
  7. "context"
  8. "fmt"
  9. "strconv"
  10. )
  11. type User struct{}
  12. func (u *User) UpdatePassword(user models.UserUpdate) e.Rescode {
  13. //TODO implement me
  14. var users models.User
  15. tx := global.DBLink.Where("phone=?", user.Phone).Find(&users)
  16. ctx := context.Background()
  17. result, err := global.Rdb.Get(ctx, user.Phone).Result()
  18. if err != nil {
  19. return e.CodeIsError
  20. } else if result != user.Code {
  21. return e.CodeIsError
  22. }
  23. if tx.RowsAffected > 0 {
  24. user.Password = utils.MD5(user.Password)
  25. tx = global.DBLink.Model(&users).Where("phone=?", user.Phone).Update("password", user.Password)
  26. if tx.RowsAffected > 0 {
  27. return e.SUCCESS
  28. }
  29. return e.ERROR
  30. }
  31. return e.TheSystemIsAbnormal
  32. }
  33. func (u *User) PhoneRegist(userRegist models.UserRegist) e.Rescode {
  34. //查找账号是否注册或者手机号已经注册,一个手机号只能注册一个账号
  35. tx := global.DBLink.Where("account = ?", userRegist.Account).Find(&models.User{})
  36. row := global.DBLink.Where("phone = ?", userRegist.Phone).Find(&models.User{})
  37. if row.RowsAffected > 0 {
  38. return e.AlreadyExists
  39. }
  40. if tx.RowsAffected > 0 {
  41. return e.AlreadyExists
  42. } else {
  43. ctx := context.Background()
  44. result, err := global.Rdb.Get(ctx, userRegist.Phone).Result()
  45. if err != nil {
  46. return e.TheVerificationCodeWasNotSent
  47. } else if result != userRegist.Code {
  48. return e.CodeIsError
  49. }
  50. var user = models.User{
  51. Phone: userRegist.Phone,
  52. Account: userRegist.Account,
  53. Password: utils.MD5(userRegist.Password),
  54. Username: userRegist.Username,
  55. State: true,
  56. }
  57. tx = global.DBLink.Create(&user)
  58. if tx.RowsAffected > 0 {
  59. return e.SUCCESS
  60. }
  61. }
  62. return e.ERROR
  63. }
  64. // Login 账号密码登录
  65. func (u *User) Login(userRegist models.UserRegist) (models.UserDto, e.Rescode) {
  66. var count int64
  67. var userdto models.UserDto
  68. var user models.User
  69. md5 := utils.MD5(userRegist.Password)
  70. global.DBLink.Where("account=?", userRegist.Account).Where("password=?", md5).Find(&user).Count(&count)
  71. if count > 0 {
  72. token, err := utils.CreateToken(user.ID, user.Username, user.Role)
  73. token = "manage:" + token
  74. if err != nil {
  75. return userdto, e.TokenIsFaild
  76. } else {
  77. userdto.Token = token
  78. userdto.Username = user.Username
  79. userdto.Account = user.Account
  80. userdto.Avatar = user.Avatar
  81. userdto.ID = user.ID
  82. userdto.Role = user.Role
  83. return userdto, e.SUCCESS
  84. }
  85. } else {
  86. return userdto, e.ThePasswordIsWrongOrThePhoneNumberIsIncorrect
  87. }
  88. }
  89. // CodeLogin 验证码登录
  90. func (u *User) CodeLogin(userRegist models.UserRegist) (models.UserDto, e.Rescode) {
  91. var user models.User
  92. var userdto models.UserDto
  93. tx := global.DBLink.Where("phone=?", userRegist.Phone).Find(&user)
  94. ctx := context.Background()
  95. result, err := global.Rdb.Get(ctx, userRegist.Phone).Result()
  96. if err != nil {
  97. return userdto, e.CodeIsError
  98. } else if result != userRegist.Code {
  99. return userdto, e.CodeIsError
  100. }
  101. token, err := utils.CreateToken(user.ID, user.Username, user.Role)
  102. token = "manage:" + token
  103. userdto.Token = token
  104. if tx.RowsAffected > 0 {
  105. userdto.Username = user.Username
  106. userdto.Account = user.Account
  107. userdto.Avatar = user.Avatar
  108. userdto.ID = user.ID
  109. userdto.Role = user.Role
  110. return userdto, e.SUCCESS
  111. } else {
  112. return models.UserDto{}, e.GoToRegister
  113. }
  114. }
  115. // GetUserAll 获取所有用户
  116. func (u *User) GetUserAll() ([]models.User, e.Rescode) {
  117. //TODO implement me
  118. var users []models.User
  119. tx := global.DBLink.Find(&users)
  120. if tx.RowsAffected > 0 {
  121. return users, e.SUCCESS
  122. }
  123. return users, e.TheUserIsEmpty
  124. }
  125. // UpdateUser 更新用户
  126. func (u *User) UpdateUser(id any, uservo models.UserVo) e.Rescode {
  127. var user models.User
  128. atoi, _ := strconv.Atoi(fmt.Sprint(id))
  129. tx := global.DBLink.Model(&user).
  130. Where("id=?", uservo.Id).
  131. Updates(models.User{
  132. Username: uservo.Username,
  133. State: uservo.State,
  134. Avatar: uservo.Avatar,
  135. Account: uservo.Account,
  136. Phone: uservo.Phone,
  137. Password: utils.MD5(uservo.Password),
  138. UpdateBy: atoi,
  139. })
  140. if tx.RowsAffected > 0 {
  141. return e.SUCCESS
  142. }
  143. return e.UPDATEFAIL
  144. }
  145. // DeleteUser 删除用户
  146. func (u *User) DeleteUser(ids []string) e.Rescode {
  147. for _, id := range ids {
  148. tx := global.DBLink.Where("id=?", id).Delete(&models.User{})
  149. if tx.RowsAffected > 0 {
  150. return e.SUCCESS
  151. }
  152. }
  153. return e.DELETEFAIL
  154. }