123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package service
- import (
- "ElectronicInvoice/internal/consts"
- "context"
- "fmt"
- "github.com/gogf/gf/v2/errors/gerror"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/net/gclient"
- "github.com/gogf/gf/v2/util/gconv"
- "strings"
- "time"
- )
- type (
- TripartiteAuth struct {
- token string
- effectiveTime time.Time
- }
- )
- func createTripartite() *TripartiteAuth {
- t := &TripartiteAuth{}
- return t
- _, err := t.GetToken(true)
- if err != nil {
- g.Log().Errorf(context.Background(), "刷新token异常%v", err)
- }
- return t
- }
- // GetFormHeaderWithToken 携带token的form请求头
- func (t *TripartiteAuth) GetFormHeaderWithToken() map[string]string {
- token, err := t.GetToken()
- if err != nil {
- g.Log().Panic(context.Background(), gerror.Wrap(err, "GetFormHeaderWithToken 获取token异常"))
- }
- return g.MapStrStr{
- "Content-Type": "application/x-www-form-urlencoded",
- "API-AuthToken": token,
- }
- }
- // GetJsonHeaderWithToken 携带token的json请求头
- func (t *TripartiteAuth) GetJsonHeaderWithToken() map[string]string {
- token, err := t.GetToken()
- if err != nil {
- g.Log().Panic(context.Background(), gerror.Wrap(err, "GetJsonHeaderWithToken 获取token异常"))
- }
- return g.MapStrStr{
- "Content-Type": "application/json",
- "API-AuthToken": token,
- }
- }
- func CommonDoPost(url string, header map[string]string, param map[string]interface{}, success func(interface{}) error) (err error) {
- var (
- res *gclient.Response
- ctx = context.Background()
- )
- type CommonRes struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Data interface{} `json:"data"`
- }
- res, err = g.Client().Header(header).Post(ctx, fmt.Sprintf("%s%s", consts.ServiceAddress, url), param)
- if err != nil {
- return
- }
- defer res.Close()
- respByte := res.ReadAll()
- //打印请求三方开票系统日志
- if g.Cfg().MustGet(ctx, "reqDebug", false).Bool() {
- g.Log().Infof(ctx, "%s\nrequest: %s\nheader: %+v\nparam: %+v\nresp: %s", strings.Repeat("=", 50), url, header, param, string(respByte))
- }
- var commonRes CommonRes
- if err := gconv.Struct(respByte, &commonRes); err != nil {
- return err
- }
- if commonRes.Code == 200 {
- if success != nil {
- return success(commonRes.Data)
- }
- return nil
- }
- switch commonRes.Code {
- case 202: //需要重新登录
- JyInvoiceManager.Login = false
- return consts.LoginOutErr
- }
- return fmt.Errorf("CommonDoPost:%s \nErrorCode:%d Msg:%s", url, commonRes.Code, commonRes.Msg)
- }
- // GetToken 获取token
- func (t *TripartiteAuth) GetToken(reload ...bool) (string, error) {
- var (
- ctx = context.Background()
- )
- type (
- tokenResData struct {
- Token string `json:"token"`
- ExpiresIn int `json:"expiresIn"` //一天有效期
- }
- )
- if time.Now().Before(t.effectiveTime) && t.token != "" && len(reload) == 0 {
- return t.token, nil
- }
- err := CommonDoPost("/authority_token/getToken",
- g.MapStrStr{"Content-Type": "application/x-www-form-urlencoded"},
- g.MapStrAny{
- "client_id": g.Cfg().MustGet(ctx, "tripartite.clientId"),
- "client_secret": g.Cfg().MustGet(ctx, "tripartite.clientSecret"),
- },
- func(i interface{}) error {
- var d tokenResData
- if err := gconv.Struct(i, &d); err != nil {
- return err
- }
- t.token = d.Token
- t.effectiveTime = time.Now().Add(time.Second * time.Duration(d.ExpiresIn))
- return nil
- })
- if err != nil {
- return "", err
- }
- return t.token, nil
- }
|