auth.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package controllers
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "eta/eta_forum_hub/models"
  6. "eta/eta_forum_hub/models/system"
  7. "eta/eta_forum_hub/utils"
  8. "fmt"
  9. )
  10. type AuthController struct {
  11. BaseAuthController
  12. }
  13. // CreateAuthCode
  14. // @Title 生成编码
  15. // @Description 生成编码
  16. // @Param request body models.CreateAuthCodeReq true "type json string"
  17. // @Success 200 {object} data_manage.AddChartInfoResp
  18. // @router /auth_code [post]
  19. func (this *AuthController) CreateAuthCode() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. var req system.CreateAuthCodeReq
  26. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  27. if err != nil {
  28. br.Msg = "参数解析异常!"
  29. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  30. return
  31. }
  32. prefix := map[int]string{
  33. 1: utils.CACHE_FORUM_AUTH_CODE_PREFIX,
  34. 2: utils.CACHE_CRM_AUTH_CODE_PREFIX,
  35. }
  36. str := base64.URLEncoding.EncodeToString([]byte(req.AdminName))
  37. key := fmt.Sprint(prefix[req.Source], str)
  38. // 是否已生成但未消费
  39. exist, _ := utils.Rc.RedisString(key)
  40. var data system.CreateAuthCodeResp
  41. data.AuthCode = str
  42. if exist != "" {
  43. br.Ret = 200
  44. br.Success = true
  45. br.Msg = "保存成功"
  46. br.Data = data
  47. return
  48. }
  49. if !utils.Rc.SetNX(key, req.AdminName, utils.GetTodayLastSecond()) {
  50. br.Msg = "授权码生成失败"
  51. return
  52. }
  53. br.Ret = 200
  54. br.Success = true
  55. br.Msg = "保存成功"
  56. br.Data = data
  57. }