tripartiteCommon.go 3.5 KB

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