123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package service
- import (
- "ElectronicInvoice/internal/consts"
- "context"
- "fmt"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/net/gclient"
- "github.com/gogf/gf/v2/util/gconv"
- "strings"
- )
- func getDefaultFormHeader() map[string]string {
- return g.MapStrStr{"Content-Type": "application/x-www-form-urlencoded"}
- }
- func getFormHeaderWithToken() map[string]string {
- return g.MapStrStr{
- "Content-Type": "application/x-www-form-urlencoded",
- "API-AuthToken": JyElectronicTripartite.token,
- }
- }
- func getJsonHeaderWithToken() map[string]string {
- return g.MapStrStr{
- "Content-Type": "application/json",
- "API-AuthToken": JyElectronicTripartite.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
- }
- return fmt.Errorf("CommonDoPost:%s \nErrorCode:%d Msg:%s", url, commonRes.Code, commonRes.Msg)
- }
|