user.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package gateway
  2. import (
  3. "confrontation-training/common"
  4. "confrontation-training/constant"
  5. errors "confrontation-training/err"
  6. "confrontation-training/models"
  7. "confrontation-training/response"
  8. "confrontation-training/service"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. )
  12. type UserService struct {
  13. service.UserService
  14. }
  15. func GetUser() *UserService {
  16. return &UserService{}
  17. }
  18. // UseRegister
  19. // PingExample confrontation-training
  20. // @Summary 用户注册
  21. // @Schemes
  22. // @Description 用户注册
  23. // @Tags 用户管理
  24. // @Param user body string true "userName:用户名;password:密码;role:角色"
  25. // @Accept json
  26. // @Produce json
  27. // @Success 200 {string} string "ok"
  28. // @Router /v1/user/register [post]
  29. func (u *UserService) UseRegister(c *gin.Context) {
  30. var param models.UserRegister
  31. err := c.ShouldBindJSON(&param)
  32. if err != nil {
  33. fmt.Printf("参数格式化异常:%s", err.Error())
  34. response.Failed(errors.ParamInvalid, c)
  35. return
  36. }
  37. if _, count := u.FindUserByPhone(param.Phone); count > 0 {
  38. response.Failed(errors.UserAlreadyExists, c)
  39. return
  40. }
  41. if result := u.CreateUser(param); result.Error == nil {
  42. response.Success(errors.UserRegisterSuccess, result.RowsAffected, c)
  43. return
  44. } else {
  45. response.Failed(errors.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
  46. return
  47. }
  48. }
  49. // UserLogin
  50. // PingExample confrontation-training
  51. // @Summary 用户登录
  52. // @Schemes
  53. // @Description 用户登录
  54. // @Tags 用户管理
  55. // @Param user body string true "username:编号;password:密码;role:角色;"
  56. // @Accept json
  57. // @Produce json
  58. // @Success 200 {string} string "ok"
  59. // @Router /v1/user/login [post]
  60. func (u *UserService) UserLogin(c *gin.Context) {
  61. var param models.UserLogin
  62. err := c.ShouldBindJSON(&param)
  63. if err != nil {
  64. fmt.Printf("参数格式化异常:%s", err.Error())
  65. response.Failed(errors.ParamInvalid, c)
  66. return
  67. }
  68. user := u.Login(param)
  69. if user != nil {
  70. //生成token
  71. toke, _ := common.GenerateToke(*user)
  72. resultMap := map[string]string{
  73. "token": toke,
  74. }
  75. response.Success(errors.UserLoginSuccess, resultMap, c)
  76. } else {
  77. response.Failed(errors.UserLoginFailed, c)
  78. }
  79. }
  80. // ResetPassword
  81. // PingExample confrontation-training
  82. // @Summary 管理员重置普通用户密码
  83. // @Schemes
  84. // @Description 管理员重置普通用户密码
  85. // @Tags 用户管理
  86. // @Param user body string true "username:管理员编号;usernameInit:被重置用户编号"
  87. // @Accept json
  88. // @Produce json
  89. // @Success 200 {string} string "ok"
  90. // @Router /v1/user/reset [post]
  91. func (u *UserService) ResetPassword(c *gin.Context) {
  92. var param models.ResetPassword
  93. err := c.ShouldBindJSON(&param)
  94. if err != nil {
  95. fmt.Printf("参数格式化异常:%s", err.Error())
  96. response.Failed(errors.ParamInvalid, c)
  97. return
  98. }
  99. if admin := u.FindUserByPhoneAndRole(param.Phone, constant.RoleAdmin); admin == nil {
  100. fmt.Printf("管理员"+errors.UserNotExists+"%s", param.Phone)
  101. response.Failed("管理员"+errors.UserNotExists+param.Phone, c)
  102. return
  103. }
  104. if normalUser := u.FindUserByPhoneAndRole(param.PhoneInit, constant.RoleNormal); normalUser == nil {
  105. fmt.Printf(errors.UserNotExists+"%s", param.PhoneInit)
  106. response.Failed(errors.UserNotExists+param.PhoneInit, c)
  107. return
  108. }
  109. result := u.RestPassword(param.PhoneInit)
  110. if result == nil {
  111. response.Failed(errors.UserPasswordResetError, c)
  112. return
  113. }
  114. if result.Error != nil {
  115. response.Failed(errors.UserPasswordResetError+result.Error.Error(), c)
  116. return
  117. }
  118. if result.RowsAffected >= 0 {
  119. response.Success(errors.UserPasswordResetSuccess, result.RowsAffected, c)
  120. return
  121. }
  122. }
  123. // UserList
  124. // PingExample confrontation-training
  125. // @Summary 用户列表查询
  126. // @Schemes
  127. // @Description 用户列表查询
  128. // @Tags 用户管理
  129. // @Param user body string true "role:角色;userName:用户名;pageNum:页数;pageSize:每页记录数"
  130. // @Accept json
  131. // @Produce json
  132. // @Success 200 {string} string "ok"
  133. // @Router /v1/user/find [post]
  134. func (u *UserService) UserList(c *gin.Context) {
  135. var param models.UserListParam
  136. err := c.ShouldBindJSON(&param)
  137. if err != nil {
  138. fmt.Printf(errors.ParamInvalid+"%s", err.Error())
  139. response.Failed(errors.ParamInvalid, c)
  140. return
  141. }
  142. userList, rows := u.GetUserList(param)
  143. response.SuccessPage(errors.FindSuccess, userList, rows, c)
  144. }
  145. // ModePass
  146. // PingExample confrontation-training
  147. // @Summary 用户修改密码
  148. // @Schemes
  149. // @Description 用户修改密码
  150. // @Tags 用户管理
  151. // @Param user body string true "userName:用户名 password:密码 newPassword:新密码"
  152. // @Accept json
  153. // @Produce json
  154. // @Success 200 {string} string "ok"
  155. // @Router /v1/user/change/password [post]
  156. func (u *UserService) ModePass(c *gin.Context) {
  157. var param models.ChangePassword
  158. err := c.ShouldBindJSON(&param)
  159. if err != nil {
  160. response.Failed(errors.ParamInvalid, c)
  161. return
  162. }
  163. user, count := u.FindUserByPhone(param.Phone)
  164. if count <= 0 {
  165. response.Failed(errors.UserNotExists, c)
  166. return
  167. }
  168. if user.Password != param.Password {
  169. response.Failed(errors.UserOldPasswordError, c)
  170. return
  171. }
  172. count = u.ChangePassword(user.ID, param.NewPassword)
  173. if count <= 0 {
  174. response.Failed(errors.UserPasswordChangeError, c)
  175. return
  176. }
  177. response.Success(errors.UserPasswordChangeSuccess, count, c)
  178. }