|
- package hdl
-
- import (
- "applet/app/e"
- "applet/app/lib/validate"
- "applet/app/md"
- "applet/app/svc"
- "applet/app/utils"
- db "code.fnuoos.com/zhimeng/model.git/src"
- "code.fnuoos.com/zhimeng/model.git/src/implement"
- "fmt"
- "github.com/gin-gonic/gin"
- )
-
- // Login 登陆
- // @Summary 登陆
- // @Tags ADMIN
- // @Description 登入
- // @Accept json
- // @Produce json
- // @Param req body md.LoginReq true "用户名密码"
- // @Success 200 {object} md.LoginResponse "token"
- // @Failure 400 {object} md.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.NewAgentDb(engine)
- admin, err := adminDb.GetAgentByUsername(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.Id))
- 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
- }
|