175 lines
4.3 KiB
Go
175 lines
4.3 KiB
Go
package zltx
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"trasfer_middleware/cmd/rpc/etc"
|
|
"trasfer_middleware/cmd/rpc/internal/logic/po"
|
|
"trasfer_middleware/cmd/rpc/internal/logic/po/zltx/types"
|
|
"trasfer_middleware/cmd/rpc/internal/logic/vo"
|
|
"trasfer_middleware/cmd/rpc/pb/transfer"
|
|
"trasfer_middleware/cmd/rpc/pkg/mq"
|
|
"trasfer_middleware/until/request"
|
|
"trasfer_middleware/until/sysLog"
|
|
)
|
|
|
|
type ZltxOrder struct {
|
|
Conf *types.ZLTXConf
|
|
}
|
|
|
|
type ZltxOrderRequest struct {
|
|
ctx context.Context
|
|
*ZltxOrder
|
|
*po.RequestStruct
|
|
}
|
|
|
|
func NewZltxOrder(conf types.ZLTXConf) *ZltxOrder {
|
|
|
|
return &ZltxOrder{
|
|
Conf: &conf,
|
|
}
|
|
}
|
|
|
|
func (r *ZltxOrder) SetData(c context.Context, data map[string]interface{}, config *etc.RockerMqConfig) *ZltxOrderRequest {
|
|
//data["timeStamp"] = time.Now().Unix()
|
|
//a := data
|
|
//delete(a, "extendParameter")
|
|
//delete(a, "sign")
|
|
//data["sign"] = common.ZltxSign(a, "8db16e8cc8363ed4eb4c14f9520bcc32")
|
|
//
|
|
requestBody := make(map[string]string, len(data))
|
|
for key, value := range data {
|
|
requestBody[key] = fmt.Sprintf("%v", value)
|
|
}
|
|
return &ZltxOrderRequest{
|
|
ctx: c,
|
|
ZltxOrder: r,
|
|
RequestStruct: &po.RequestStruct{
|
|
Config: *config,
|
|
RequestBody: requestBody,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *ZltxOrderRequest) request(url string) (*request.Response, error) {
|
|
reqUrl := fmt.Sprintf("%s%s", r.Conf.Host, url)
|
|
req := request.Request{
|
|
Method: "POST",
|
|
Url: reqUrl,
|
|
Data: r.RequestBody,
|
|
}
|
|
resp, _ := req.Send()
|
|
//异步存入请求记录
|
|
sendMq := mq.AliyunRocketMq{
|
|
AccessKey: r.Config.AccessKey,
|
|
SecretKey: r.Config.SecretKey,
|
|
SecurityToken: r.Config.SecurityToken,
|
|
ServerAddress: r.Config.Host,
|
|
}
|
|
err := sendMq.Produce(r.ctx, r.Config.TopicPrefix+r.Config.Topic.ZLTX.Name, po.SetMqSendDataZLTX(r.RequestStruct, &resp, url))
|
|
handlerResCode(&resp.Text)
|
|
if err != nil {
|
|
sysLog.LogSendMq(r.ctx, err)
|
|
}
|
|
return &resp, err
|
|
}
|
|
|
|
func handlerResCode(respText *string) {
|
|
var res map[string]interface{}
|
|
_ = json.Unmarshal([]byte(*respText), &res)
|
|
if _, ok := res["code"].(string); !ok {
|
|
if _, ok := res["code"].(float64); ok {
|
|
res["code"] = fmt.Sprintf("%d", int(res["code"].(float64)))
|
|
}
|
|
if _, ok := res["code"].(int); ok {
|
|
res["code"] = fmt.Sprintf("%d", res["code"].(int))
|
|
}
|
|
respByte, _ := json.Marshal(res)
|
|
*respText = string(respByte)
|
|
}
|
|
}
|
|
|
|
func (r *ZltxOrderRequest) RechargeOrder() (*transfer.DefaultRes, error) {
|
|
var res transfer.DefaultRes
|
|
req, err := r.request(vo.ZLTX_RECHARGE)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_ = json.Unmarshal([]byte(req.Text), &res)
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *ZltxOrderRequest) RechargeQuery() (*transfer.ZltxOrderCardQueryRes, error) {
|
|
var res transfer.ZltxOrderCardQueryRes
|
|
req, err := r.request(vo.ZLTX_RECHARGE_QUERY)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_ = json.Unmarshal([]byte(req.Text), &res)
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *ZltxOrderRequest) CardOrder() (*transfer.DefaultRes, error) {
|
|
var res transfer.DefaultRes
|
|
req, err := r.request(vo.ZLTX_CARD)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_ = json.Unmarshal([]byte(req.Text), &res)
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *ZltxOrderRequest) CardQuery() (*transfer.ZltxOrderCardQueryRes, error) {
|
|
var res transfer.ZltxOrderCardQueryRes
|
|
req, err := r.request(vo.ZLTX_CARD_QUERY)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_ = json.Unmarshal([]byte(req.Text), &res)
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *ZltxOrderRequest) OrderSendSms() (*transfer.ZltxOrderSmsRes, error) {
|
|
var res transfer.ZltxOrderSmsRes
|
|
req, err := r.request(vo.ZLTX_CARD_ISSUE)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_ = json.Unmarshal([]byte(req.Text), &res)
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *ZltxOrderRequest) RechargeProduct() (*types.RechargeProductRes, error) {
|
|
var res types.RechargeProductRes
|
|
req, err := r.request(vo.ZLTX_RECHARGE_PRODUCT)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_ = json.Unmarshal([]byte(req.Text), &res)
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *ZltxOrderRequest) RechargeInfo() (*transfer.ZltxRechargeInfoRes, error) {
|
|
var res transfer.ZltxRechargeInfoRes
|
|
req, err := r.request(vo.ZLTX_RECHARGE_INFO)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_ = json.Unmarshal([]byte(req.Text), &res)
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *ZltxOrderRequest) RsMiXueCouponGrant() (*transfer.RsCouponGrantRes, error) {
|
|
var (
|
|
res transfer.RsCouponGrantRes
|
|
)
|
|
req, err := r.request(vo.ZLTX_RS_MIXUE_COUPON_GRANT)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_ = json.Unmarshal([]byte(req.Text), &res)
|
|
|
|
return &res, nil
|
|
}
|