transfer_middleware/cmd/rpc/internal/logic/po/zltx/zltxOrder.go

174 lines
4.4 KiB
Go
Raw Normal View History

2024-06-07 11:11:04 +08:00
package zltx
import (
2024-06-18 16:34:14 +08:00
"context"
2024-06-07 11:11:04 +08:00
"encoding/json"
"fmt"
2024-06-19 10:01:42 +08:00
"time"
2024-06-18 16:34:14 +08:00
"trasfer_middleware/cmd/rpc/etc"
"trasfer_middleware/cmd/rpc/internal/logic/po"
2024-06-07 11:11:04 +08:00
"trasfer_middleware/cmd/rpc/internal/logic/po/zltx/types"
"trasfer_middleware/cmd/rpc/internal/logic/vo"
"trasfer_middleware/cmd/rpc/pb/transfer"
2024-06-18 16:34:14 +08:00
"trasfer_middleware/cmd/rpc/pkg/mq"
2024-06-19 10:01:42 +08:00
"trasfer_middleware/until/common"
2024-06-12 13:46:14 +08:00
"trasfer_middleware/until/request"
2024-06-18 16:34:14 +08:00
"trasfer_middleware/until/sysLog"
2024-06-07 11:11:04 +08:00
)
type ZltxOrder struct {
2024-06-18 16:34:14 +08:00
Conf *types.ZLTXConf
2024-06-07 11:11:04 +08:00
}
type ZltxOrderRequest struct {
2024-06-18 16:34:14 +08:00
ctx context.Context
2024-06-07 11:11:04 +08:00
*ZltxOrder
2024-06-18 16:34:14 +08:00
*po.RequestStruct
2024-06-07 11:11:04 +08:00
}
func NewZltxOrder(conf types.ZLTXConf) *ZltxOrder {
return &ZltxOrder{
Conf: &conf,
}
}
2024-06-18 16:34:14 +08:00
func (r *ZltxOrder) SetData(c context.Context, data map[string]interface{}, config *etc.RockerMqConfig) *ZltxOrderRequest {
2024-06-19 10:01:42 +08:00
data["timeStamp"] = time.Now().Unix()
2024-06-07 11:11:04 +08:00
a := data
delete(a, "extendParameter")
delete(a, "sign")
2024-06-19 10:01:42 +08:00
data["sign"] = common.ZltxSign(a, "8db16e8cc8363ed4eb4c14f9520bcc32")
//data := common.MergeMaps(common.TToMap(p), common.ToMap(r), common.ToMap(e))
2024-06-07 11:11:04 +08:00
requestBody := make(map[string]string, len(data))
for key, value := range data {
requestBody[key] = fmt.Sprintf("%v", value)
}
return &ZltxOrderRequest{
2024-06-18 16:34:14 +08:00
ctx: c,
ZltxOrder: r,
RequestStruct: &po.RequestStruct{
Config: *config,
RequestBody: requestBody,
},
2024-06-07 11:11:04 +08:00
}
}
func (r *ZltxOrderRequest) request(url string) (*request.Response, error) {
2024-06-18 16:34:14 +08:00
reqUrl := fmt.Sprintf("%s%s", r.Conf.Host, url)
2024-06-07 11:11:04 +08:00
req := request.Request{
Method: "POST",
2024-06-18 16:34:14 +08:00
Url: reqUrl,
2024-06-07 11:11:04 +08:00
Data: r.RequestBody,
}
resp, _ := req.Send()
2024-06-18 16:34:14 +08:00
//异步存入请求记录
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.SetMqSendData(r.RequestStruct, &resp, reqUrl))
2024-06-19 10:01:42 +08:00
handlerResCode(&resp.Text)
2024-06-18 16:34:14 +08:00
if err != nil {
sysLog.LogSendMq(r.ctx, err)
}
return &resp, err
2024-06-07 11:11:04 +08:00
}
2024-06-19 10:01:42 +08:00
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)
}
}
2024-06-07 11:11:04 +08:00
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
}
2024-06-18 16:34:14 +08:00
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
}