package controller import ( "fmt" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "hongze/hongze_yb_en_api/controller/resp" "hongze/hongze_yb_en_api/global" "hongze/hongze_yb_en_api/models" "hongze/hongze_yb_en_api/models/english_report_email" "hongze/hongze_yb_en_api/services" "hongze/hongze_yb_en_api/utils" "time" ) type AuthController struct { } func (a *AuthController) Login(c *gin.Context) { req := new(models.LoginReq) err := c.BindQuery(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } sysUser, err := english_report_email.CheckUser(req.Type, req.Account, req.Password) if err != nil { resp.FailData("登录失败,账号或密码错误", "Err:"+err.Error(), c) return } if sysUser == nil { resp.FailData("登录失败,账号或密码错误", "Err:"+err.Error(), c) return } if sysUser.Enable == 0 { resp.FailData("您的账号已被禁用,如需登录,请联系管理员", "Err:"+err.Error(), c) return } account := utils.MD5(req.Account) token, err := utils.GenToken(account) sysSession := new(models.EnglishYbSession) sysSession.UserId = int(sysUser.AdminId) //现在要求永不过期 sysSession.ExpiredTime = time.Now().AddDate(99, 0, 0) sysSession.CreatedTime = time.Now() sysSession.LastUpdatedTime = time.Now() sysSession.AccessToken = token err = sysSession.AddSession() fmt.Println("id:", sysSession.SessionId) if err != nil { resp.FailData("新增session信息失败", "Err:"+err.Error(), c) return } } func (a *AuthController) Register(c *gin.Context) { req := new(models.LoginReq) err := c.BindQuery(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } } // @Title 修改密码 // @Description 修改密码 // @Param request body models.ModifyPwdReq true "type json string" // @Success 200 {object} models.LoginResp // @router /modifyPwd [post] func (a *AuthController) ModifyPwd(c *gin.Context) { req := new(models.ModifyPwdReq) err := c.BindQuery(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } } // GetSmsCode 获取短信验证码接口 // @Tags 用户模块 // @Summary 获取短信验证码 // @Description 获取短信验证码接口 // @Security ApiKeyAuth // @securityDefinitions.basic BasicAuth // @Param mobile query string true "手机号" // @Param area_num query string true "手机国际区号(中国大陆:86)" // @Accept json // @Product json // @Success 200 {string} string 获取验证码成功 // @Failure 400 {string} string 手机号不能为空,请输入手机号 // @Router /get_sms_code [get] func (a *AuthController) GetSmsCode(c *gin.Context) { mobile := c.DefaultQuery("mobile", "") areaNum := c.DefaultQuery("area_num", "") err, errMsg := services.SendSmsCode(mobile, areaNum) if err != nil { if errMsg != "" { errMsg = "获取验证码失败" } resp.Fail(errMsg, c) return } resp.Ok("获取验证码成功", c) } // GetEmailCode 获取邮箱验证码接口 // @Tags 用户模块 // @Summary 获取邮箱验证码 // @Description 获取邮箱验证码 // @Security ApiKeyAuth // @securityDefinitions.basic BasicAuth // @Param email query string true "电子邮箱账号" // @Accept json // @Product json // @Success 200 {string} string 获取验证码成功 // @Failure 400 {string} string 请输入邮箱地址 // @Router /get_email_code [get] func (a *AuthController) GetEmailCode(c *gin.Context) { email := c.DefaultQuery("email", "") if email == "" { resp.Fail("请输入邮箱地址", c) return } if !utils.ValidateEmailFormatat(email) { resp.Fail("邮箱格式错误,请重新输入", c) return } err, errMsg := services.SendEmailCode(email) if err != nil { if errMsg != "" { errMsg = "获取验证码失败" } resp.Fail(errMsg, c) return } resp.Ok("获取验证码成功", c) }