package hdl import ( "applet/app/db" "applet/app/db/implement" "applet/app/e" "applet/app/lib/validate" "applet/app/md" "applet/app/svc" "applet/app/utils" "fmt" "github.com/gin-gonic/gin" ) // Login 登陆 // @Summary 登陆 // @Tags ADMIN // @Description 登入 // @Accept json // @Produce json // @Param req body md2.LoginReq true "用户名密码" // @Success 200 {object} md2.LoginResponse "token" // @Failure 400 {object} md2.Response "具体错误" // @Router /admin/login [post] func Login(c *gin.Context) { var req md.LoginReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } engine := db.DBs[svc.GetMasterId(c)] adminDb := implement.NewAdminDb(engine) admin, err := adminDb.GetAdminByUserName(req.UserName) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err) return } if admin == nil { e.OutErr(c, e.ERR_NO_DATA, "账号不存在!") return } if utils.Md5(req.PassWord) != admin.Password { e.OutErr(c, e.ERR_INVALID_ARGS, "密码错误") return } ip := utils.GetIP(c.Request) key := fmt.Sprintf(md.JwtTokenKey, ip, utils.AnyToString(admin.AdmId)) token, err := svc.HandleLoginToken(key, admin) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } e.OutSuc(c, md.LoginResponse{ Token: token, }, nil) return }