csloginlogic.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package logic
  2. import (
  3. "context"
  4. "github.com/golang-jwt/jwt/v4"
  5. "github.com/pkg/errors"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "time"
  8. "ylink/bff/authbff/api/internal/svc"
  9. "ylink/bff/authbff/api/internal/types"
  10. "ylink/comm/globalkey"
  11. "ylink/comm/jwtkey"
  12. "ylink/comm/result"
  13. )
  14. type CsLoginLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewCsLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CsLoginLogic {
  20. return &CsLoginLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. }
  25. }
  26. func (l *CsLoginLogic) CsLogin(req *types.CsAuthReq) (resp *types.AuthResp, err error) {
  27. now := time.Now().Unix()
  28. token, err := l.generateCsToken(now, req.CsId)
  29. if err != nil {
  30. return nil, errors.Wrap(result.NewErrCode(result.TokenGenerateError), "")
  31. }
  32. return &types.AuthResp{
  33. AccessToken: token,
  34. }, nil
  35. }
  36. //
  37. // generateCsToken
  38. // @Description: 客服token签发
  39. // @receiver l
  40. // @param iat
  41. // @param csId
  42. // @return string
  43. // @return error
  44. //
  45. func (l *CsLoginLogic) generateCsToken(iat int64, csId string) (string, error) {
  46. secret := l.svcCtx.Config.JwtAuth.AccessSecret
  47. expire := l.svcCtx.Config.JwtAuth.AccessExpire
  48. claims := make(jwt.MapClaims)
  49. claims["iat"] = iat
  50. claims["exp"] = iat + expire
  51. claims[jwtkey.CsId] = csId
  52. claims[jwtkey.Type] = globalkey.ConnectTypeCs
  53. token := jwt.New(jwt.SigningMethodHS256)
  54. token.Claims = claims
  55. return token.SignedString([]byte(secret))
  56. }