user.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package api
  2. import (
  3. "AIT/common"
  4. "AIT/constant"
  5. "AIT/exception/errmsg"
  6. "AIT/models"
  7. "AIT/response"
  8. "AIT/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 kjb-doc
  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(errmsg.ParamInvalid, c)
  35. return
  36. }
  37. if user, count := u.FindUserByUsername(param.UserName); user.Id != "" || count > 0 {
  38. response.Failed(errmsg.UserAlreadyExists, c)
  39. return
  40. }
  41. if result := u.CreateUser(param); result.Error == nil {
  42. response.Success(errmsg.UserRegisterSuccess, result.RowsAffected, c)
  43. return
  44. } else {
  45. response.Failed(errmsg.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
  46. return
  47. }
  48. }
  49. // UserLogin
  50. // PingExample kjb-doc
  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(errmsg.ParamInvalid, c)
  66. return
  67. }
  68. if user := u.Login(param); user != nil {
  69. //生成token
  70. toke, _ := common.GenerateToke(*user)
  71. //resultMap := make(map[string]string)
  72. //resultMap["token"] = toke
  73. resultMap := map[string]string{
  74. "token": toke,
  75. }
  76. response.Success(errmsg.UserLoginSuccess, resultMap, c)
  77. }
  78. }
  79. // ResetPassword
  80. // PingExample kjb-doc
  81. // @Summary 管理员重置普通用户密码
  82. // @Schemes
  83. // @Description 管理员重置普通用户密码
  84. // @Tags 用户管理
  85. // @Param user body string true "username:管理员编号;usernameInit:被重置用户编号"
  86. // @Accept json
  87. // @Produce json
  88. // @Success 200 {string} string "ok"
  89. // @Router /v1/user/reset [post]
  90. func (u *UserService) ResetPassword(c *gin.Context) {
  91. var param models.ResetPassword
  92. err := c.ShouldBindJSON(&param)
  93. if err != nil {
  94. fmt.Printf("参数格式化异常:%s", err.Error())
  95. response.Failed(errmsg.ParamInvalid, c)
  96. return
  97. }
  98. if admin := u.FindUserByUsernameAndRole(param.Username, constant.RoleAdmin); admin == nil {
  99. fmt.Printf("管理员"+errmsg.UserNotExists+"%s", param.Username)
  100. response.Failed("管理员"+errmsg.UserNotExists+param.Username, c)
  101. return
  102. }
  103. if normalUser := u.FindUserByUsernameAndRole(param.UsernameInit, constant.RoleNormal); normalUser == nil {
  104. fmt.Printf(errmsg.UserNotExists+"%s", param.UsernameInit)
  105. response.Failed(errmsg.UserNotExists+param.UsernameInit, c)
  106. return
  107. }
  108. result := u.RestPassword(param.UsernameInit)
  109. if result == nil {
  110. response.Failed(errmsg.UserPasswordResetError, c)
  111. return
  112. }
  113. if result.Error != nil {
  114. response.Failed(errmsg.UserPasswordResetError+result.Error.Error(), c)
  115. return
  116. }
  117. if result.RowsAffected >= 0 {
  118. response.Success(errmsg.UserPasswordResetSuccess, result.RowsAffected, c)
  119. return
  120. }
  121. }
  122. // UserList
  123. // PingExample kjb-doc
  124. // @Summary 用户列表查询
  125. // @Schemes
  126. // @Description 用户列表查询
  127. // @Tags 用户管理
  128. // @Param user body string true "role:角色;userName:用户名;pageNum:页数;pageSize:每页记录数"
  129. // @Accept json
  130. // @Produce json
  131. // @Success 200 {string} string "ok"
  132. // @Router /v1/user/find [post]
  133. func (u *UserService) UserList(c *gin.Context) {
  134. var param models.UserListParam
  135. err := c.ShouldBindJSON(&param)
  136. if err != nil {
  137. fmt.Printf(errmsg.ParamInvalid+"%s", err.Error())
  138. response.Failed(errmsg.ParamInvalid, c)
  139. return
  140. }
  141. userList, rows := u.GetUserList(param)
  142. response.SuccessPage(errmsg.FindSuccess, userList, rows, c)
  143. }
  144. // ModePass
  145. // PingExample kjb-doc
  146. // @Summary 用户修改密码
  147. // @Schemes
  148. // @Description 用户修改密码
  149. // @Tags 用户管理
  150. // @Param user body string true "userName:用户名 password:密码 newPassword:新密码"
  151. // @Accept json
  152. // @Produce json
  153. // @Success 200 {string} string "ok"
  154. // @Router /v1/user/change/password [post]
  155. func (u *UserService) ModePass(c *gin.Context) {
  156. var param models.ChangePassword
  157. err := c.ShouldBindJSON(&param)
  158. if err != nil {
  159. response.Failed(errmsg.ParamInvalid, c)
  160. return
  161. }
  162. user, count := u.FindUserByUsername(param.UserName)
  163. if count <= 0 {
  164. response.Failed(errmsg.UserNotExists, c)
  165. return
  166. }
  167. if user.Password != param.Password {
  168. response.Failed(errmsg.UserOldPasswordError, c)
  169. return
  170. }
  171. count = u.ChangePassword(user.Id, param.NewPassword)
  172. if count <= 0 {
  173. response.Failed(errmsg.UserPasswordChangeError, c)
  174. return
  175. }
  176. response.Success(errmsg.UserPasswordChangeSuccess, count, c)
  177. }