request.go
This commit is contained in:
parent
e15325fac9
commit
0de049a20d
|
@ -48,7 +48,7 @@ func alipayOrderRedPack() {
|
||||||
Config: getAlpayRedConf(),
|
Config: getAlpayRedConf(),
|
||||||
Order: &proto.OrderRequest_Order{
|
Order: &proto.OrderRequest_Order{
|
||||||
OrderNo: "lsxd202406071545141534",
|
OrderNo: "lsxd202406071545141534",
|
||||||
Account: "",
|
Account: "123",
|
||||||
Quantity: 1,
|
Quantity: 1,
|
||||||
Amount: 0.01,
|
Amount: 0.01,
|
||||||
Extra: []byte(`{"name":"李1子铭"}`),
|
Extra: []byte(`{"name":"李1子铭"}`),
|
||||||
|
|
|
@ -2,12 +2,10 @@ package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
||||||
"github.com/carlmjohnson/requests"
|
"github.com/carlmjohnson/requests"
|
||||||
"plugins/alipay_cpn/internal/po"
|
"plugins/alipay_cpn/internal/po"
|
||||||
"plugins/utils/helper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 插件通信信息,若不对应则会报错panic
|
// 插件通信信息,若不对应则会报错panic
|
||||||
|
@ -45,15 +43,9 @@ func (s *AlipayCpnService) Order(ctx context.Context, request *proto.OrderReques
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var sp string
|
|
||||||
err = requests.URL(baseUri).Post().BodyForm(uv).ToString(&sp).Fetch(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("请求异常,msg:" + err.Error())
|
|
||||||
}
|
|
||||||
var response *po.OrderResp
|
var response *po.OrderResp
|
||||||
sp = helper.ToChinese(sp)
|
if err = requests.URL(baseUri).Post().Params(uv).ToJSON(&response).Fetch(ctx); err != nil {
|
||||||
if err = json.Unmarshal([]byte(sp), &response); err != nil {
|
return nil, err
|
||||||
return nil, fmt.Errorf("请求解析异常,msg:" + err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return orderResp(request, response), nil
|
return orderResp(request, response), nil
|
||||||
|
@ -77,16 +69,9 @@ func (s *AlipayCpnService) Query(ctx context.Context, request *proto.QueryReques
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var sp string
|
|
||||||
err = requests.URL(baseUri).Post().BodyForm(uv).ToString(&sp).Fetch(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("请求异常,msg:" + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
var response po.QueryResp
|
var response po.QueryResp
|
||||||
sp = helper.ToChinese(sp)
|
if err = requests.URL(baseUri).Post().BodyForm(uv).ToJSON(&response).Fetch(ctx); err != nil {
|
||||||
if err = json.Unmarshal([]byte(sp), &response); err != nil {
|
return nil, fmt.Errorf("请求异常,msg:" + err.Error())
|
||||||
return nil, fmt.Errorf("请求解析异常,msg:" + err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return queryResp(request, response), nil
|
return queryResp(request, response), nil
|
||||||
|
|
|
@ -2,13 +2,11 @@ package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
||||||
"github.com/carlmjohnson/requests"
|
"github.com/carlmjohnson/requests"
|
||||||
"plugins/alipay_redpack/internal/po"
|
"plugins/alipay_redpack/internal/po"
|
||||||
"plugins/utils/alipay"
|
"plugins/utils/alipay"
|
||||||
"plugins/utils/helper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 插件通信信息,若不对应则会报错panic
|
// 插件通信信息,若不对应则会报错panic
|
||||||
|
@ -47,13 +45,9 @@ func (s *AlipayRedPackService) Order(ctx context.Context, request *proto.OrderRe
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var sp string
|
|
||||||
err = requests.URL(baseUri).Post().Params(uv).ToString(&sp).Fetch(ctx)
|
|
||||||
|
|
||||||
var response *po.OrderResp
|
var response *po.OrderResp
|
||||||
sp = helper.ToChinese(sp)
|
if err = requests.URL(baseUri).Post().Params(uv).ToJSON(&response).Fetch(ctx); err != nil {
|
||||||
if err = json.Unmarshal([]byte(sp), &response); err != nil {
|
return nil, err
|
||||||
return nil, fmt.Errorf("请求解析异常,msg:" + err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return orderResp(request, response), nil
|
return orderResp(request, response), nil
|
||||||
|
@ -77,16 +71,9 @@ func (s *AlipayRedPackService) Query(ctx context.Context, request *proto.QueryRe
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var sp string
|
|
||||||
err = requests.URL(baseUri).Post().BodyForm(uv).ToString(&sp).Fetch(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("请求异常,msg:" + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
var response po.QueryResp
|
var response po.QueryResp
|
||||||
sp = helper.ToChinese(sp)
|
if err = requests.URL(baseUri).Post().BodyForm(uv).ToJSON(&response).Fetch(ctx); err != nil {
|
||||||
if err = json.Unmarshal([]byte(sp), &response); err != nil {
|
return nil, fmt.Errorf("请求异常,msg:" + err.Error())
|
||||||
return nil, fmt.Errorf("请求解析异常,msg:" + err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return queryResp(request, response), nil
|
return queryResp(request, response), nil
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package request
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Request(_ context.Context, method, url string, body []byte) ([]byte, http.Header, error) {
|
||||||
|
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient := &http.Client{
|
||||||
|
Timeout: 15 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
bodyBytes, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, nil, fmt.Errorf("请求异常:%s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
return bodyBytes, resp.Header, nil
|
||||||
|
}
|
Loading…
Reference in New Issue