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" model2 "code.fnuoos.com/zhimeng/model.git/src/model" implement2 "code.fnuoos.com/zhimeng/model.git/src/super/implement" "code.fnuoos.com/zhimeng/model.git/src/super/model" "fmt" "github.com/gin-gonic/gin" "time" ) // 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 /api/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 } // LoginPhone 手机号登陆 // @Summary 手机号登陆 // @Tags ADMIN // @Description 手机号登入 // @Accept json // @Produce json // @Param req body md.LoginPhoneReq true "用户名、验证码" // @Success 200 {object} md.LoginResponse "token" // @Failure 400 {object} md.Response "具体错误" // @Router /api/login/phone [post] func LoginPhone(c *gin.Context) { var req md.LoginPhoneReq 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)] check, err := svc.SmsCheck(c, req.UserName, req.Captcha) if check == false { e.OutErr(c, 400, e.NewErr(400, err.Error())) return } 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 } 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 } // Register 手机号注册 // @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 /api/login/register [post] func Register(c *gin.Context) { var req md.RegisterReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } masterId := svc.GetMasterId(c) engine := db.DBs[svc.GetMasterId(c)] check, err := svc.SmsCheck(c, req.UserName, req.Captcha) if check == false { e.OutErr(c, 400, e.NewErr(400, err.Error())) return } now := time.Now() //1、判断当前账号是否已存在 agentDb := implement.NewAgentDb(engine) agent, err := agentDb.GetAgentByUsername(req.UserName) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err) return } if agent != nil { e.OutErr(c, e.ERR_NO_DATA, "账号已存在!") return } //2、生成 agent_list 记录 agentListDb := implement2.NewAgentListDb(db.Db) agentId := utils.GenerateUniqueRandomNumbers(8) agentList := model.AgentList{ Uuid: utils.StrToInt(masterId), AgentId: utils.StrToInt(agentId), Kind: 1, CompanyName: "", CompanyAbbreviation: "", UnifiedSocialCreditCode: "", CertificateType: 1, BusinessLicenseImgUrl: "", LegalRepresentative: "", CountryRegionId: 1, CountryRegion: "", RegisteredAddressProvinceId: 0, RegisteredAddressCityId: 0, RegisteredAddressCountyId: 0, RegisteredAddress: "", BusinessLicenseAddress: "", CertificateValidity: "", State: 0, CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), } insertAffected, err := agentListDb.AgentListInsert(&agentList) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if insertAffected <= 0 { e.OutErr(c, e.ERR_DB_ORM, "生成 记录失败") return } //3、新增 agent 记录 agentModel := model2.Agent{ AgentId: utils.StrToInt(agentId), Username: req.UserName, Password: utils.Md5(req.PassWord), State: 1, IsSuperAdministrator: 1, Memo: "", CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), } insertAffected, err = agentDb.AgentInsert(&agentModel) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if insertAffected <= 0 { e.OutErr(c, e.ERR_DB_ORM, "新增 记录失败") return } ip := utils.GetIP(c.Request) key := fmt.Sprintf(md.JwtTokenKey, ip, utils.AnyToString(agentModel.Id)) token, err := svc.HandleLoginToken(key, &agentModel) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } e.OutSuc(c, md.LoginResponse{ Token: token, }, nil) return }