user.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package controller
  2. import (
  3. "Ic_ouath/app/e"
  4. "Ic_ouath/app/service"
  5. "Ic_ouath/app/service/imp"
  6. "Ic_ouath/global"
  7. "Ic_ouath/models"
  8. "Ic_ouath/page"
  9. "Ic_ouath/simple_zap"
  10. "Ic_ouath/utils"
  11. "context"
  12. "github.com/astaxie/beego/validation"
  13. "github.com/gin-gonic/gin"
  14. "github.com/go-playground/validator/v10"
  15. )
  16. var users service.User = &imp.User{}
  17. // PhoneRegist 手机号注册
  18. // @Tags 用户管理
  19. // @Summary 手机号注册
  20. // @Success 200 {object} e.R
  21. // @Fail 400 {object} e.R
  22. // @Param req body models.UserRegist true "注册信息"
  23. // @Accept application/json
  24. // @Router /user [post]
  25. func PhoneRegist(c *gin.Context) {
  26. var userRegist models.UserRegist
  27. err := c.ShouldBindJSON(&userRegist)
  28. if err != nil {
  29. e.ResponseError(c, e.JSONParsingFailed)
  30. return
  31. }
  32. rescode := users.PhoneRegist(userRegist)
  33. e.ResponseError(c, rescode)
  34. }
  35. // SendCode 发送验证码
  36. // @Tags 用户管理
  37. // @Summary 发送验证码
  38. // @Success 200 {object} e.R
  39. // @Fail 400 {object} e.R
  40. // @Param phone query string true "手机号"
  41. // @Accept application/json
  42. // @Router /sendcode [post]
  43. func SendCode(c *gin.Context) {
  44. phone := c.PostForm("phone")
  45. utils.SendModel(phone)
  46. v := validation.Validation{}
  47. v.Mobile(phone, "phone")
  48. v.Required(phone, "phone")
  49. if v.HasErrors() {
  50. e.ResponseWithMsg(c, e.ThePhoneNumberIsWrong, "手机号码格式不正确")
  51. } else {
  52. rescode := utils.SendModel(phone)
  53. if rescode != e.SUCCESS {
  54. e.ResponseWithMsg(c, rescode, rescode.GetMsg())
  55. } else {
  56. e.ResponseSuccess(c, "发送成功")
  57. }
  58. }
  59. }
  60. // Login 账号登录
  61. // @Tags 用户管理
  62. // @Summary 账号密码登录
  63. // @Success 200 {object} e.R
  64. // @Fail 400 {object} e.R
  65. // @Param req body models.UserRegist true "账号信息"
  66. // @Accept application/json
  67. // @Router /user [post]
  68. func Login(c *gin.Context) {
  69. var userRegist models.UserRegist
  70. err := c.ShouldBindJSON(&userRegist)
  71. if err != nil {
  72. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  73. return
  74. } else {
  75. login, rescode := users.Login(userRegist)
  76. if rescode == e.SUCCESS {
  77. e.ResponseSuccess(c, login)
  78. } else {
  79. e.ResponseWithMsg(c, rescode, rescode.GetMsg())
  80. }
  81. }
  82. }
  83. // CodeLogin 验证码登录
  84. // @Tags 用户管理
  85. // @Summary 验证码登录
  86. // @Success 200 {object} e.R
  87. // @Fail 400 {object} e.R
  88. // @Param req body models.UserRegist true "账号信息"
  89. // @Accept application/json
  90. // @Router /codelogin [post]
  91. func CodeLogin(c *gin.Context) {
  92. var userRegist models.UserRegist
  93. err := c.ShouldBindJSON(&userRegist)
  94. if err != nil {
  95. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  96. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, e.JSONParsingFailed.GetMsg())
  97. return
  98. }
  99. login, rescode := users.CodeLogin(userRegist)
  100. if rescode == e.SUCCESS {
  101. e.ResponseSuccess(c, login)
  102. } else {
  103. e.ResponseWithMsg(c, rescode, rescode.GetMsg())
  104. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, rescode.GetMsg())
  105. }
  106. }
  107. // GetUserAll 获取所有用户
  108. func GetUserAll(c *gin.Context) {
  109. user, rescode := users.GetUserAll()
  110. if rescode == e.SUCCESS {
  111. e.ResponseSuccess(c, user)
  112. } else {
  113. e.ResponseWithMsg(c, rescode, rescode.GetMsg())
  114. }
  115. }
  116. // GetUserAlls 获取所有用户
  117. // @Tags 用户管理
  118. // @Summary 获取所有用户
  119. // @Success 200 {object} e.R
  120. // @Fail 400 {object} e.R
  121. // @Param req body page.PageParams true "分页信息"
  122. // @Accept application/json
  123. // @Router /admin/getalluser [post]
  124. func GetUserAlls(c *gin.Context) {
  125. var pages page.PageParams
  126. err := c.ShouldBindJSON(&pages)
  127. if err != nil {
  128. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  129. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, e.JSONParsingFailed.GetMsg())
  130. return
  131. }
  132. result, total, err := page.Paginate(global.DBLink, pages, models.User{})
  133. if err != nil {
  134. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  135. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, e.PaginationFailed.GetMsg())
  136. return
  137. }
  138. e.ResPonsePage(c, result, total, pages)
  139. }
  140. // UpdateUserById 修改用户信息
  141. // @Tags 用户管理
  142. // @Summary 获取所有用户
  143. // @Success 200 {object} e.R
  144. // @Fail 400 {object} e.R
  145. // @Param req body page.PageParams true "分页信息"
  146. // @Accept application/json
  147. // @Router /admin/user [post]
  148. func UpdateUserById(c *gin.Context) {
  149. var userVo models.UserVo
  150. userId := c.MustGet("user_id")
  151. err := c.ShouldBindJSON(&userVo)
  152. if err != nil {
  153. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  154. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, e.JSONParsingFailed.GetMsg())
  155. return
  156. }
  157. recode := users.UpdateUser(userId, userVo)
  158. if recode == e.SUCCESS {
  159. e.ResponseSuccess(c, "修改成功")
  160. } else {
  161. e.ResponseWithMsg(c, recode, recode.GetMsg())
  162. simple_zap.WithCtx(context.Background()).Sugar().Info(err, recode.GetMsg())
  163. }
  164. }
  165. // DeleteUserById 删除用户信息
  166. // @Tags 用户管理
  167. // @Summary 删除用户
  168. // @Success 200 {object} e.R
  169. // @Fail 400 {object} e.R
  170. // @Param id query string true "id"
  171. // @Accept application/json
  172. // @Router /admin/user [delete]
  173. func DeleteUserById(c *gin.Context) {
  174. ids := c.QueryArray("ids")
  175. recode := users.DeleteUser(ids)
  176. if recode == e.SUCCESS {
  177. e.ResponseSuccess(c, "删除成功")
  178. } else {
  179. e.ResponseWithMsg(c, recode, recode.GetMsg())
  180. simple_zap.WithCtx(context.Background()).Sugar().Info(recode.GetMsg())
  181. }
  182. }
  183. // UpdatePassword 修改账号密码
  184. // @Tags 用户管理
  185. // @Summary 手机号注册
  186. // @Success 200 {object} e.R
  187. // @Fail 400 {object} e.R
  188. // @Param req body models.UserRegist true "注册信息"
  189. // @Accept application/json
  190. // @Router /user [post]
  191. func UpdatePassword(c *gin.Context) {
  192. var userRegist models.UserUpdate
  193. err := c.ShouldBindJSON(&userRegist)
  194. if err != nil {
  195. e.ResponseError(c, e.JSONParsingFailed)
  196. return
  197. }
  198. validate := validator.New()
  199. err = validate.Struct(userRegist)
  200. if err != nil {
  201. e.ResponseWithMsg(c, e.ERROR, err.Error())
  202. return
  203. }
  204. rescode := users.UpdatePassword(userRegist)
  205. if rescode == e.SUCCESS {
  206. e.ResponseSuccess(c, nil)
  207. return
  208. } else {
  209. e.ResponseWithMsg(c, rescode, rescode.GetMsg())
  210. }
  211. }