|
- 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.NewMediumDb(engine)
- admin, err := adminDb.GetMediumByUsername(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.NewMediumDb(engine)
- admin, err := adminDb.GetMediumByUsername(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/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
- }
- 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、判断当前账号是否已存在
- mediumDb := implement.NewMediumDb(engine)
- medium, err := mediumDb.GetMediumByUsername(req.UserName)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err)
- return
- }
- if medium != nil {
- e.OutErr(c, e.ERR_NO_DATA, "账号已存在!")
- return
- }
- //2、生成 medium_list 记录
- mediumListDb := implement2.NewMediumListDb(db.Db)
- mediumId := utils.GenerateUniqueRandomNumbers(8)
- mediumList := model.MediumList{
- Uuid: utils.StrToInt(c.GetString("mid")),
- MediumId: utils.StrToInt(mediumId),
- Kind: 1,
- CompanyName: "",
- SettlementType: 1,
- CompanyAbbreviation: "",
- UnifiedSocialCreditCode: "",
- 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 := mediumListDb.MediumListInsert(&mediumList)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if insertAffected <= 0 {
- e.OutErr(c, e.ERR_DB_ORM, "生成 medium_list 记录失败")
- return
- }
- //3、新增 medium 记录
- mediumModel := model2.Medium{
- MediumId: utils.StrToInt(mediumId),
- 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 = mediumDb.MediumInsert(&mediumModel)
- if err != nil {
- return
- }
- if insertAffected <= 0 {
- e.OutErr(c, e.ERR_DB_ORM, "新增 medium 记录失败")
- return
- }
- //4、新增 MediumDivisionStrategy 记录
- MediumDivisionStrategyModel := model.MediumDivisionStrategy{
- MediumId: utils.StrToInt(mediumId),
- Uuid: utils.StrToInt(c.GetString("mid")),
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- }
- insertAffected, err = db.Db.Insert(&MediumDivisionStrategyModel)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if insertAffected <= 0 {
- e.OutErr(c, e.ERR_DB_ORM, "新增 记录失败")
- return
- }
-
- //5、判断是否为渠道代理邀请
- if req.AgentId != "" {
- agentDb := implement.NewAgentDb(engine)
- agent, err1 := agentDb.GetAgent(utils.StrToInt(req.AgentId))
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- if agent != nil {
- //5.1 新增agent_with_medium记录
- agentWithMediumDb := implement2.NewAgentWithMediumDb(engine)
- insertAffected, err = agentWithMediumDb.AgentWithMediumInsert(&model.AgentWithMedium{
- AgentId: utils.StrToInt(req.AgentId),
- MediumId: utils.StrToInt(mediumId),
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if insertAffected <= 0 {
- e.OutErr(c, e.ERR_DB_ORM, "新增 agent_with_medium 记录失败")
- return
- }
- }
- }
- ip := utils.GetIP(c.Request)
- key := fmt.Sprintf(md.JwtTokenKey, ip, utils.AnyToString(mediumModel.Id))
- token, err := svc.HandleLoginToken(key, &mediumModel)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- e.OutSuc(c, md.LoginResponse{
- Token: token,
- }, nil)
- return
- }
|