login_api_controllor.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package admin
  2. import (
  3. "encoding/json"
  4. "sws_xcx/controllers"
  5. "sws_xcx/enums"
  6. "sws_xcx/models"
  7. "sws_xcx/service"
  8. "sws_xcx/utils"
  9. )
  10. type LoginApiController struct {
  11. controllers.BaseApiController
  12. }
  13. // @Title Admin Login
  14. // @Description 管理员登录
  15. // @Param body body models.AdminLoginReq true "用户登录参数"
  16. // @Success 200 {object} models.SysAdmin
  17. // @Failure 500 error
  18. // @router /login [post]
  19. func (c *LoginApiController) Login() {
  20. dataBody := models.AdminLoginReq{}
  21. json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  22. if dataBody.UserName == "" || dataBody.Password == "" {
  23. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  24. return
  25. }
  26. admin, err := service.NewSysAdminService().GetByUserName(dataBody.UserName)
  27. if err != nil {
  28. c.ServeDynamicFailJsonSend(err.Error())
  29. return
  30. }
  31. if admin.ID == 0 {
  32. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  33. return
  34. }
  35. //hash, _ := utils.PasswordHash(dataBody.Password)
  36. if !utils.PasswordVerify(dataBody.Password, admin.Password) {
  37. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  38. return
  39. }
  40. sysAdmin := models.SysAdminVO{ID: admin.ID, UserName: admin.UserName, NickName: admin.NickName, UserType: admin.UserType, DeptID: admin.DeptID}
  41. c.SetSession("admin_info", sysAdmin)
  42. c.ServeSuccessJSON(sysAdmin)
  43. }