diff --git a/platformapi/mtunionapi/callback.go b/platformapi/mtunionapi/callback.go new file mode 100644 index 00000000..0194d72d --- /dev/null +++ b/platformapi/mtunionapi/callback.go @@ -0,0 +1,43 @@ +package mtunionapi + +import ( + "git.rosy.net.cn/baseapi/utils" + "io/ioutil" + "net/http" +) + +type CallBack struct { + Errcode string `json:"errcode"` + Errmsg string `json:"errmsg"` +} + +type CallBackResult struct { + Smstitle string `json:"smstitle"` + Quantity string `json:"quantity"` + Orderid string `json:"orderid"` + Dealid string `json:"dealid"` + Paytime string `json:"paytime"` + Type string `json:"type"` + Ordertime string `json:"ordertime"` + Sid string `json:"sid"` + UID string `json:"uid"` + Status string `json:"status"` + Total string `json:"total"` + Direct string `json:"direct"` + Ratio string `json:"ratio"` + Sign string `json:"sign"` +} + +func (a *API) GetCallbackMsg(request *http.Request) (call *CallBackResult, err error) { + data, err := ioutil.ReadAll(request.Body) + if err != nil { + return nil, err + } + values, err := utils.HTTPBody2Values(data, false) + if err != nil { + return nil, err + } + mapData := utils.URLValues2Map(values) + utils.Map2StructByJson(mapData, &call, false) + return call, err +} diff --git a/platformapi/mtunionapi/mtunionapi.go b/platformapi/mtunionapi/mtunionapi.go index 1cab563d..2e281a93 100644 --- a/platformapi/mtunionapi/mtunionapi.go +++ b/platformapi/mtunionapi/mtunionapi.go @@ -98,6 +98,36 @@ func (a *API) AccessAPI(action string, isPost bool, bizParams map[string]interfa return retVal, err } +func (a *API) AccessAPI2(action string, isPost bool, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) { + params := make(map[string]interface{}) + params["key"] = a.appKey + params = utils.MergeMaps(params, bizParams) + signStr := a.signParam(params) + params[sigKey] = signStr + fullURL := utils.GenerateGetURL(prodURL, action, nil) + err = platformapi.AccessPlatformAPIWithRetry(a.client, + func() *http.Request { + var request *http.Request + if !isPost { + request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(fullURL, "", params), nil) + } else { + request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode())) + } + return request + }, + a.config, + func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) { + if jsonResult1 == nil { + return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil") + } + if err == nil { + retVal = jsonResult1 + } + return errLevel, err + }) + return retVal, err +} + func (a *API) AccessStorePage(fullURL string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) { if a.GetCookieCount() == 0 { return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法") @@ -143,27 +173,27 @@ func (a *API) MiniCode(actID int, userID string) (url string, err error) { "sid": strings.ToLower(userID), "actId": actID, }) - if err == nil { + if err == nil { if utils.MustInterface2Int64(result["status"]) != 0 { - return "",fmt.Errorf(result["des"].(string)) - }else { - return result["data"].(string),err + return "", fmt.Errorf(result["des"].(string)) + } else { + return result["data"].(string), err } } return url, err } -func (a *API) GenerateLink(actID,linkType int, userID string) (url string, err error) { +func (a *API) GenerateLink(actID, linkType int, userID string) (url string, err error) { result, err := a.AccessAPI("generateLink", false, map[string]interface{}{ - "sid": strings.ToLower(userID), - "actId": actID, + "sid": strings.ToLower(userID), + "actId": actID, "linkType": linkType, }) - if err == nil { + if err == nil { if utils.MustInterface2Int64(result["status"]) != 0 { - return "",fmt.Errorf(result["des"].(string)) - }else { - return result["data"].(string),err + return "", fmt.Errorf(result["des"].(string)) + } else { + return result["data"].(string), err } } return url, err diff --git a/platformapi/mtunionapi/order.go b/platformapi/mtunionapi/order.go index aa9ab2a1..68c290dd 100644 --- a/platformapi/mtunionapi/order.go +++ b/platformapi/mtunionapi/order.go @@ -1,34 +1,80 @@ package mtunionapi import ( - "fmt" "git.rosy.net.cn/baseapi/utils" "time" ) const ( -MtUnionOrderTypeTG=0 //团购订单 -MtUnionOrderTypeJD=2 //酒店订单 -MtUnionOrderTypeWM=4 //外卖订单 -MtUnionOrderTypeHF=5 //话费订单 -MtUnionOrderTypeSG=6 //闪购订单 + MtUnionOrderTypeTG = 0 //团购订单 + MtUnionOrderTypeJD = 2 //酒店订单 + MtUnionOrderTypeWM = 4 //外卖订单 + MtUnionOrderTypeHF = 5 //话费订单 + MtUnionOrderTypeSG = 6 //闪购订单 + + defaultPageSize = 50 ) -func (a *API) OrderList(orderType int, startTime,endTime int64,page int) (url string, err error) { - result, err := a.AccessAPI("orderList", false, map[string]interface{}{ - "ts": time.Now().Unix(), - "type": orderType, +type OrderListResult struct { + Total int `json:"total"` + DataList []struct { + Smstitle string `json:"smstitle"` + Refundprice string `json:"refundprice"` + Refundtime string `json:"refundtime"` + Orderid string `json:"orderid"` + Refundprofit string `json:"refundprofit"` + Paytime string `json:"paytime"` + Appkey string `json:"appkey"` + Payprice string `json:"payprice"` + Profit string `json:"profit"` + Sid string `json:"sid"` + Status int `json:"status"` + } `json:"dataList"` +} + +func (a *API) OrderList(orderType int, startTime, endTime int64, page int) (orderListResult *OrderListResult, err error) { + result, err := a.AccessAPI2("api/orderList", false, map[string]interface{}{ + "ts": time.Now().Unix(), + "type": orderType, "startTime": startTime, - "endTime":endTime, - "page":page, - "limit":100, + "endTime": endTime, + "page": page, + "limit": defaultPageSize, }) - if err == nil { - if utils.MustInterface2Int64(result["status"]) != 0 { - return "",fmt.Errorf(result["des"].(string)) - }else { - return result["data"].(string),err - } + if err == nil { + utils.Map2StructByJson(result, &orderListResult, false) } - return url, err -} \ No newline at end of file + return orderListResult, err +} + +type CouponListResult struct { + Total int `json:"total"` + DataList []struct { + AppKey string `json:"appKey"` + Sid string `json:"sid"` + CouponTime string `json:"couponTime"` + Money string `json:"money"` + MinUseMoney string `json:"minUseMoney"` + CouponName string `json:"couponName"` + CouponType string `json:"couponType"` + CouponCode string `json:"couponCode"` + BeginTime int `json:"beginTime"` + EndTime int `json:"endTime"` + } `json:"dataList"` +} + +func (a *API) CouponList(orderType int, startTime, endTime int64, page int, sid string) (couponListResult *CouponListResult, err error) { + result, err := a.AccessAPI2("api/couponList", false, map[string]interface{}{ + "ts": time.Now().Unix(), + "type": orderType, + "startTime": startTime, + "endTime": endTime, + "page": page, + "limit": defaultPageSize, + "sid": sid, + }) + if err == nil { + utils.Map2StructByJson(result, &couponListResult, false) + } + return couponListResult, err +} diff --git a/platformapi/mtunionapi/order_test.go b/platformapi/mtunionapi/order_test.go index 35be9d9e..1162533a 100644 --- a/platformapi/mtunionapi/order_test.go +++ b/platformapi/mtunionapi/order_test.go @@ -1 +1,22 @@ package mtunionapi + +import ( + "git.rosy.net.cn/baseapi/utils" + "testing" +) + +func TestOrderList(t *testing.T) { + result, err := api.OrderList(6, utils.Str2Time("2021-01-01").Unix(), utils.Str2Time("2021-04-01").Unix(), 1) + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestCouponList(t *testing.T) { + result, err := api.CouponList(4, utils.Str2Time("2021-03-19").Unix(), utils.Str2Time("2021-03-25").Unix(), 1, "000000001") + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +}