auth.go 1.5 KB

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