|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- package hdl
-
- import (
- implement3 "applet/app/db/implement"
- "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"
- )
-
- func LoginBase(c *gin.Context) {
- NewSysCfgDb := implement3.NewSysCfgDb(svc.MasterDb(c), c.GetString("mid"))
- logo := NewSysCfgDb.SysCfgGetWithDb("agent_logo")
- re := NewSysCfgDb.SysCfgFindWithDb("agent_login_logo", "seo_agent_logo", "seo_agent_title")
-
- res := map[string]string{
- "logo": logo,
- "login_logo": re["agent_login_logo"],
- "seo_logo": re["seo_agent_logo"],
- "seo_title": re["seo_agent_title"],
- }
- e.OutSuc(c, res, nil)
- return
- }
-
- // 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,
- SettlementType: 3,
- 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
- }
|