tripartiteCommon.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package service
  2. import (
  3. "ElectronicInvoice/internal/consts"
  4. "context"
  5. "fmt"
  6. "github.com/gogf/gf/v2/errors/gerror"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/net/gclient"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "strings"
  11. "time"
  12. )
  13. type (
  14. TripartiteAuth struct {
  15. token string
  16. effectiveTime time.Time
  17. }
  18. )
  19. func createTripartite() *TripartiteAuth {
  20. t := &TripartiteAuth{}
  21. //return &TripartiteAuth{
  22. // token: "R4OU/sSVy3Kmjw6eYLUCMYjf5/B1Gu46q6l/YdCmZ/hSsjQ3oXtJ18ihYzOYQSgcdFclCJELs13XyH1NbPu3bxFvcwUegyVH99mVK4u/7NmebEUBTg0T4tCniuKnEh3W0JMbE+hlJPJJsIWPNm2CXKBW/q4p/+KzDv2GnUG/tF6rMApjuj+rND6eJBpEwnE/",
  23. // effectiveTime: time.Now().AddDate(0, 0, 1),
  24. //}
  25. _, err := t.GetToken(true)
  26. if err != nil {
  27. g.Log().Errorf(context.Background(), "刷新token异常%v", err)
  28. }
  29. return t
  30. }
  31. // GetFormHeaderWithToken 携带token的form请求头
  32. func (t *TripartiteAuth) GetFormHeaderWithToken() map[string]string {
  33. token, err := t.GetToken()
  34. if err != nil {
  35. g.Log().Panic(context.Background(), gerror.Wrap(err, "GetFormHeaderWithToken 获取token异常"))
  36. }
  37. return g.MapStrStr{
  38. "Content-Type": "application/x-www-form-urlencoded",
  39. "API-AuthToken": token,
  40. }
  41. }
  42. // GetJsonHeaderWithToken 携带token的json请求头
  43. func (t *TripartiteAuth) GetJsonHeaderWithToken() map[string]string {
  44. token, err := t.GetToken()
  45. if err != nil {
  46. g.Log().Panic(context.Background(), gerror.Wrap(err, "GetJsonHeaderWithToken 获取token异常"))
  47. }
  48. return g.MapStrStr{
  49. "Content-Type": "application/json",
  50. "API-AuthToken": token,
  51. }
  52. }
  53. func CommonDoPost(url string, header map[string]string, param map[string]interface{}, success func(interface{}) error) (err error) {
  54. var (
  55. res *gclient.Response
  56. ctx = context.Background()
  57. )
  58. type CommonRes struct {
  59. Code int `json:"code"`
  60. Msg string `json:"msg"`
  61. Data interface{} `json:"data"`
  62. }
  63. res, err = g.Client().Header(header).Post(ctx, fmt.Sprintf("%s%s", consts.ServiceAddress, url), param)
  64. if err != nil {
  65. return
  66. }
  67. defer res.Close()
  68. respByte := res.ReadAll()
  69. //打印请求三方开票系统日志
  70. if g.Cfg().MustGet(ctx, "reqDebug", false).Bool() {
  71. g.Log().Infof(ctx, "%s\nrequest: %s\nheader: %+v\nparam: %+v\nresp: %s", strings.Repeat("=", 50), url, header, param, string(respByte))
  72. }
  73. var commonRes CommonRes
  74. if err := gconv.Struct(respByte, &commonRes); err != nil {
  75. return err
  76. }
  77. if commonRes.Code == 200 {
  78. if success != nil {
  79. return success(commonRes.Data)
  80. }
  81. return nil
  82. }
  83. switch commonRes.Code {
  84. case 202: //需要重新登录
  85. JyInvoiceManager.Login = false
  86. return consts.LoginOutErr
  87. }
  88. return fmt.Errorf("CommonDoPost:%s \nErrorCode:%d Msg:%s", url, commonRes.Code, commonRes.Msg)
  89. }
  90. // GetToken 获取token
  91. func (t *TripartiteAuth) GetToken(reload ...bool) (string, error) {
  92. var (
  93. ctx = context.Background()
  94. )
  95. type (
  96. tokenResData struct {
  97. Token string `json:"token"`
  98. ExpiresIn int `json:"expiresIn"` //一天有效期
  99. }
  100. )
  101. if time.Now().Before(t.effectiveTime) && t.token != "" && len(reload) == 0 {
  102. return t.token, nil
  103. }
  104. err := CommonDoPost("/authority_token/getToken",
  105. g.MapStrStr{"Content-Type": "application/x-www-form-urlencoded"},
  106. g.MapStrAny{
  107. "client_id": g.Cfg().MustGet(ctx, "tripartite.clientId"),
  108. "client_secret": g.Cfg().MustGet(ctx, "tripartite.clientSecret"),
  109. },
  110. func(i interface{}) error {
  111. var d tokenResData
  112. if err := gconv.Struct(i, &d); err != nil {
  113. return err
  114. }
  115. t.token = d.Token
  116. t.effectiveTime = time.Now().Add(time.Second * time.Duration(d.ExpiresIn))
  117. return nil
  118. })
  119. if err != nil {
  120. return "", err
  121. }
  122. return t.token, nil
  123. }