package physical import ( "context" "encoding/json" "fmt" "trasfer_middleware/cmd/rpc/etc" "trasfer_middleware/cmd/rpc/internal/logic/po" "trasfer_middleware/cmd/rpc/internal/logic/po/physical/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 Physical struct { Conf *types.PhysicalConf AppId string } type PhysicalRequest struct { ctx context.Context *Physical *po.RequestStructWithJson } func NewPhysical(conf types.PhysicalConf) *Physical { return &Physical{ Conf: &conf, } } func (r *Physical) SetData(c context.Context, appId string, data map[string]interface{}, config *etc.RockerMqConfig) *PhysicalRequest { r.AppId = appId return &PhysicalRequest{ ctx: c, Physical: r, RequestStructWithJson: &po.RequestStructWithJson{ Config: *config, RequestBody: data, }, } } func (r *PhysicalRequest) request(url string) (*request.Response, error) { if len(r.Physical.AppId) == 0 { return nil, fmt.Errorf("未获取到app_id") } reqUrl := fmt.Sprintf("%s%s", r.Conf.Host, url) req := request.Request{ Method: "POST", Url: reqUrl, Json: r.RequestBody, Headers: map[string]string{"AppId": r.Physical.AppId}, } 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.Physical.Name, po.SetMqSendDataPhysical(r.RequestStructWithJson, &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 *PhysicalRequest) GoodsDetail() (*transfer.GoodsStructWithChild, error) { var res transfer.GoodsStructWithChild req, err := r.request(vo.PHYSICAL_GOODS_DETAIL) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) GoodsList() (*transfer.PhysicalGoodsListRes, error) { var res transfer.PhysicalGoodsListRes req, err := r.request(vo.PHYSICAL_GOODS_LIST) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) GoodsStock() (*transfer.PhysicalGoodsStockRes, error) { var res transfer.PhysicalGoodsStockRes req, err := r.request(vo.PHYSICAL_GOODS_STOCK) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) CusBalanceHis() (*transfer.PhysicalCusLogsRes, error) { var res transfer.PhysicalCusLogsRes req, err := r.request(vo.PHYSICAL_CUS_BALANCE_HIS) if err != nil { return nil, err } _ = json.Unmarshal(req.Content, &res) return &res, nil } func (r *PhysicalRequest) CusBalance() (*transfer.PhysicalCusBalanceRes, error) { var res transfer.PhysicalCusBalanceRes req, err := r.request(vo.PHYSICAL_CUS_BALANCE) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) ExpressList() (*transfer.PhysicalExpressListRes, error) { var res transfer.PhysicalExpressListRes req, err := r.request(vo.PHYSICAL_EXPRESS_LIST) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) AddressList() (*transfer.PhysicalAddressListRes, error) { var res transfer.PhysicalAddressListRes req, err := r.request(vo.PHYSICAL_ADDRESS_LIST) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) FinanceBill() (*transfer.PhysicalFinanceBillRes, error) { var res transfer.PhysicalFinanceBillRes req, err := r.request(vo.PHYSICAL_FINANCE_SEARCH) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) OrderInfo() (*transfer.PhysicalOrderInfoRes, error) { var res transfer.PhysicalOrderInfoRes req, err := r.request(vo.PHYSICAL_ORDER_INFO) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) OrderList() (*transfer.PhysicalOrderListRes, error) { var res transfer.PhysicalOrderListRes req, err := r.request(vo.PHYSICAL_ORDER_LIST) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) OrderSubmit() (*transfer.PhysicalOrderSubRes, error) { var res transfer.PhysicalOrderSubRes req, err := r.request(vo.PHYSICAL_ORDER_SUBMIT) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) OrderClose() (*transfer.PhysicalOrderCloseRes, error) { var res transfer.PhysicalOrderCloseRes req, err := r.request(vo.PHYSICAL_ORDER_CLOSE) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) OrderLogisticsLogs() (*transfer.PhysicalOrderLogisticsLogsRes, error) { var res transfer.PhysicalOrderLogisticsLogsRes req, err := r.request(vo.PHYSICAL_ORDER_LOGIC) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) OrderAfterApply() (*transfer.PhysicalOrderAfterApplyRes, error) { var res transfer.PhysicalOrderAfterApplyRes req, err := r.request(vo.PHYSICAL_ORDEAFTER_APPLY) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil } func (r *PhysicalRequest) OrderAfterReturn() (*transfer.PhysicalOrderAfterReturnRes, error) { var res transfer.PhysicalOrderAfterReturnRes req, err := r.request(vo.PHYSICAL_ORDEAFTER_RETRUN) if err != nil { return nil, err } _ = json.Unmarshal([]byte(req.Text), &res) return &res, nil }