user.go 5.5 KB

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