tripartiteCommon.go 3.3 KB

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