user.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "icloudapp.cn/tools/entity"
  6. _ "icloudapp.cn/tools/entity"
  7. "icloudapp.cn/tools/repository/mysql"
  8. _ "icloudapp.cn/tools/repository/mysql"
  9. "icloudapp.cn/tools/util"
  10. )
  11. type User struct {
  12. ctx context.Context
  13. Role string
  14. }
  15. func NewUser(ctx context.Context) *User {
  16. return &User{ctx, ""}
  17. }
  18. // SetRole 设置用户权限
  19. func (u *User) SetRole(role string) {
  20. u.Role = role
  21. }
  22. // TableName 设置表名称
  23. func (u *User) TableName() string {
  24. return "user"
  25. }
  26. func (u *User) UserInfoByID(uid int64) (entity.UserInfo, error) {
  27. var userInfo entity.UserInfo
  28. var user entity.User
  29. result := mysql.DBConn.Select(
  30. "uid",
  31. "nickname",
  32. "password",
  33. "salt",
  34. "avatar",
  35. "create_at",
  36. "update_at",
  37. ).Where("uid = ?", uid).Table(u.TableName()).Find(&user)
  38. if result.Error != nil {
  39. userInfo.Code = entity.CodeServerBusy
  40. userInfo.Msg = result.Error.Error()
  41. return userInfo, result.Error
  42. }
  43. if result.RowsAffected == 0 {
  44. user.Uid = 0
  45. userInfo.Code = entity.CodeDataDoesNotExist
  46. }
  47. userInfo.Body = user
  48. return userInfo, nil
  49. }
  50. // UserInfoByUsername 通过nickname获取用户信息
  51. func (u *User) UserInfoByUsername(username string) (userInfo *entity.User, err error) {
  52. result := mysql.DBConn.Select(
  53. "uid",
  54. "nickname",
  55. "password",
  56. "salt",
  57. "avatar",
  58. "create_at",
  59. "update_at",
  60. ).Where("nickname = ?", username).Table(u.TableName()).Find(&userInfo)
  61. if result.Error != nil {
  62. return userInfo, util.NewError(result.Error.Error())
  63. }
  64. if result.RowsAffected == 0 {
  65. userInfo.Uid = 0
  66. }
  67. return userInfo, nil
  68. }
  69. // UserInfoByEmail 通过邮箱获取用户信息
  70. func (u *User) UserInfoByEmail(email string) (userInfo *entity.User, err error) {
  71. result := mysql.DBConn.Select(
  72. "uid",
  73. "nickname",
  74. "password",
  75. "salt",
  76. "avatar",
  77. "create_at",
  78. "update_at",
  79. ).Where("email = ?", email).Table(u.TableName()).Find(&userInfo)
  80. if result.Error != nil {
  81. return nil, util.NewError(result.Error.Error())
  82. }
  83. if result.RowsAffected == 0 {
  84. userInfo.Email = ""
  85. }
  86. return userInfo, nil
  87. }
  88. // Register 用户注册
  89. func (u *User) Register(register entity.User) (userInfo entity.UserInfo, err error) {
  90. //check nickname, email 是否已注册
  91. existUsername, errInfo := u.UserInfoByUsername(register.Username)
  92. if errInfo != nil {
  93. userInfo.Msg = entity.CodeServerBusy.Msg()
  94. userInfo.Code = entity.CodeServerBusy
  95. return userInfo, errors.New(errInfo.Error())
  96. }
  97. if existUsername.Uid > 0 {
  98. userInfo.Msg = "用户名已经被注册"
  99. userInfo.Code = entity.CodeUserExist
  100. userInfo.Body = *existUsername
  101. return userInfo, entity.CodeUserExist.Error()
  102. }
  103. existEmail, err := u.UserInfoByEmail(register.Email)
  104. if err != nil {
  105. userInfo.Msg = entity.CodeServerBusy.Msg()
  106. userInfo.Code = entity.CodeServerBusy
  107. return userInfo, errors.New(errInfo.Error())
  108. }
  109. if existEmail.Uid > 0 {
  110. userInfo.Msg = entity.CodeEmailExist.Msg()
  111. userInfo.Code = entity.CodeEmailExist
  112. userInfo.Body = *existEmail
  113. return userInfo, entity.CodeEmailExist.Error()
  114. }
  115. dbErr := mysql.DBConn.Table(u.TableName()).Create(&register)
  116. if dbErr.Error != nil {
  117. return userInfo, errors.New(dbErr.Error.Error())
  118. }
  119. userInfo.Body = register
  120. return userInfo, nil
  121. }
  122. // ChangePassword 通过用户ID更改密码
  123. func (u *User) ChangePassword(uid int64, password, slat string) (userInfo entity.UserInfo, err error) {
  124. userInfo, errInfo := u.UserInfoByID(uid)
  125. if err != nil {
  126. userInfo.Msg = entity.CodeServerBusy.Msg()
  127. userInfo.Code = entity.CodeServerBusy
  128. return userInfo, errors.New(errInfo.Error())
  129. }
  130. var user entity.User
  131. user.Uid = uid
  132. user.Salt = slat
  133. user.Status = userInfo.Body.Status
  134. user.Password = password
  135. result := mysql.DBConn.Table(u.TableName()).Where("uid = ?", uid).UpdateColumns(&user)
  136. if result.Error != nil {
  137. return userInfo, result.Error
  138. }
  139. return userInfo, nil
  140. }
  141. // ChangeEmail 更换邮件地址,邮件地址必须是唯一的
  142. func (u *User) ChangeEmail(uid int64, email string) (userInfo entity.UserInfo, err error) {
  143. userInfo, errInfo := u.UserInfoByID(uid)
  144. if err != nil {
  145. userInfo.Msg = entity.CodeServerBusy.Msg()
  146. userInfo.Code = entity.CodeServerBusy
  147. return userInfo, errors.New(errInfo.Error())
  148. }
  149. existEmail, err := u.UserInfoByEmail(email)
  150. if err != nil {
  151. userInfo.Msg = entity.CodeServerBusy.Msg()
  152. userInfo.Code = entity.CodeServerBusy
  153. return userInfo, errors.New(errInfo.Error())
  154. }
  155. if existEmail.Uid > 0 {
  156. userInfo.Msg = entity.CodeEmailExist.Msg()
  157. userInfo.Code = entity.CodeEmailExist
  158. userInfo.Body = *existEmail
  159. return userInfo, entity.CodeEmailExist.Error()
  160. }
  161. var user entity.User
  162. user.Uid = uid
  163. user.Email = email
  164. result := mysql.DBConn.Table(u.TableName()).Where("uid = ?", uid).UpdateColumns(&user)
  165. if result.Error != nil {
  166. return userInfo, result.Error
  167. }
  168. return userInfo, nil
  169. }