package controllers import ( "encoding/base64" "encoding/json" "eta/eta_forum_hub/models" "eta/eta_forum_hub/models/system" "eta/eta_forum_hub/utils" "fmt" ) type AuthController struct { BaseAuthController } // CreateAuthCode // @Title 生成编码 // @Description 生成编码 // @Param request body models.CreateAuthCodeReq true "type json string" // @Success 200 {object} data_manage.AddChartInfoResp // @router /auth_code [post] func (this *AuthController) CreateAuthCode() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req system.CreateAuthCodeReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } prefix := map[int]string{ 1: utils.CACHE_FORUM_AUTH_CODE_PREFIX, 2: utils.CACHE_CRM_AUTH_CODE_PREFIX, } str := base64.URLEncoding.EncodeToString([]byte(req.AdminName)) key := fmt.Sprint(prefix[req.Source], str) // 是否已生成但未消费 exist, _ := utils.Rc.RedisString(key) var data system.CreateAuthCodeResp data.AuthCode = str if exist != "" { br.Ret = 200 br.Success = true br.Msg = "保存成功" br.Data = data return } if !utils.Rc.SetNX(key, req.AdminName, utils.GetTodayLastSecond()) { br.Msg = "授权码生成失败" return } br.Ret = 200 br.Success = true br.Msg = "保存成功" br.Data = data }