package admin import ( "encoding/json" "sws_xcx/controllers" "sws_xcx/enums" "sws_xcx/models" "sws_xcx/service" "sws_xcx/utils" ) type LoginApiController struct { controllers.BaseApiController } // @Title Admin Login // @Description 管理员登录 // @Param body body models.AdminLoginReq true "用户登录参数" // @Success 200 {object} models.SysAdmin // @Failure 500 error // @router /login [post] func (c *LoginApiController) Login() { dataBody := models.AdminLoginReq{} json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody) if dataBody.UserName == "" || dataBody.Password == "" { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong) return } admin, err := service.NewSysAdminService().GetByUserName(dataBody.UserName) if err != nil { c.ServeDynamicFailJsonSend(err.Error()) return } if admin.ID == 0 { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist) return } //hash, _ := utils.PasswordHash(dataBody.Password) if !utils.PasswordVerify(dataBody.Password, admin.Password) { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong) return } sysAdmin := models.SysAdminVO{ID: admin.ID, UserName: admin.UserName, NickName: admin.NickName, UserType: admin.UserType, DeptID: admin.DeptID} c.SetSession("admin_info", sysAdmin) c.ServeSuccessJSON(sysAdmin) }