From 7242dd435e8ac56465d96a30fcaca3857cb53fbd Mon Sep 17 00:00:00 2001 From: gazebo Date: Tue, 23 Jul 2019 09:12:33 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E8=BE=BE=E8=BE=BE=E4=B8=8E=E7=BE=8E?= =?UTF-8?q?=E5=9B=A2=E9=85=8D=E9=80=81=E7=9A=84=E6=8E=A5=E5=8F=A3=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E8=BE=83=E6=AD=A3=E5=BC=8F=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platformapi/dadaapi/shop.go | 96 +++++++++++-------- platformapi/dadaapi/shop_test.go | 27 +++++- platformapi/ebaiapi/shop_sku_test.go | 5 + platformapi/mtpsapi/callback.go | 29 +++++- platformapi/mtpsapi/mtpsapi.go | 88 ++++++++++++++--- platformapi/mtpsapi/mtpsapi_test.go | 55 ++++++++--- platformapi/yilianyunapi/yilianyunapi_test.go | 8 +- utils/utils.go | 11 +-- utils/utils_test.go | 17 ++++ 9 files changed, 255 insertions(+), 81 deletions(-) diff --git a/platformapi/dadaapi/shop.go b/platformapi/dadaapi/shop.go index 22abad34..1ea9fb62 100644 --- a/platformapi/dadaapi/shop.go +++ b/platformapi/dadaapi/shop.go @@ -21,58 +21,78 @@ const ( BusinessTypeOther = -5 ) -func (a *API) ShopDetail(originShopID string) (shopDetail map[string]interface{}, err error) { +const ( + ShopStatusOffline = 0 // -门店下线 + ShopStatusOnline = 1 // -门店激活 +) + +type ShopInfo struct { + StationName string `json:"station_name,omitempty"` + Business int `json:"business,omitempty"` + CityName string `json:"city_name,omitempty"` + AreaName string `json:"area_name,omitempty"` + StationAddress string `json:"station_address,omitempty"` + Lng float64 `json:"lng,omitempty"` + Lat float64 `json:"lat,omitempty"` + ContactName string `json:"contact_name,omitempty"` + Phone string `json:"phone,omitempty"` + + OriginShopID string `json:"origin_shop_id,omitempty"` + IDCard string `json:"id_card,omitempty"` + UserName string `json:"username,omitempty"` + Password string `json:"password,omitempty"` + + NewShopID string `json:"new_shop_id,omitempty"` // 修改用 + + BDName string `json:"bd_name,omitempty"` // 查询用 + BDPhone string `json:"bd_phone,omitempty"` // 查询用 + Status int `json:"status,omitempty"` // 查询用 +} + +type AddShopFailInfo struct { + Code int `json:"code"` + ShopNo string `json:"shopNo"` + Msg string `json:"msg"` + ShopName string `json:"shopName"` +} + +type AddShopResult struct { + Success int `json:"success"` + SuccessList []*ShopInfo `json:"successList"` + FailedList []*AddShopFailInfo `json:"failedList"` +} + +func (a *API) ShopDetail(originShopID string) (shopDetail *ShopInfo, err error) { params := map[string]interface{}{ "origin_shop_id": originShopID, } result, err := a.AccessAPI("api/shop/detail", params) - if err != nil { - return nil, err + if err == nil { + err = utils.Map2StructByJson(result.Result, &shopDetail, false) } - return result.Result.(map[string]interface{}), nil + return shopDetail, err } -func (a *API) ShopAdd(originShopID, stationName string, business int, cityName, areaName, stationAddress string, lng, lat float64, contactName, phone string, addParams map[string]interface{}) (outOriginShopID string, err error) { - params := map[string]interface{}{ - "station_name": stationName, - "business": business, - "city_name": cityName, - "area_name": areaName, - "station_address": stationAddress, - "lng": lng, - "lat": lat, - "contact_name": contactName, - "phone": phone, - } - if originShopID != "" { - params["origin_shop_id"] = originShopID - } - if addParams != nil { - params = utils.MergeMaps(params, addParams) - } - successList, failedList, err := a.BatchShopAdd([]map[string]interface{}{params}) - if err != nil { - if len(failedList) == 1 { - return "", utils.NewErrorIntCode(failedList[0]["msg"].(string), int(utils.MustInterface2Int64(failedList[0]["code"]))) - } +func (a *API) ShopAdd(shopInfo *ShopInfo) (outOriginShopID string, err error) { + addResult, err := a.BatchShopAdd([]*ShopInfo{shopInfo}) + if addResult != nil && len(addResult.FailedList) == 1 { + return "", utils.NewErrorIntCode(addResult.FailedList[0].Msg, int(utils.MustInterface2Int64(addResult.FailedList[0].Code))) + } else if err != nil { return "", err } - return utils.Interface2String(successList[0]["originShopId"]), nil + return addResult.SuccessList[0].OriginShopID, nil } -func (a *API) BatchShopAdd(shopInfoList []map[string]interface{}) (successList, failedList []map[string]interface{}, err error) { +func (a *API) BatchShopAdd(shopInfoList []*ShopInfo) (addResult *AddShopResult, err error) { result, err := a.AccessAPI("api/shop/add", shopInfoList) - mapResult := result.Result.(map[string]interface{}) - if successList2 := mapResult["successList"]; successList2 != nil { - successList = utils.Slice2MapSlice(successList2.([]interface{})) + err2 := utils.Map2StructByJson(result.Result, &addResult, false) + if err == nil { + err = err2 } - if failedList2 := mapResult["failedList"]; failedList2 != nil { - failedList = utils.Slice2MapSlice(failedList2.([]interface{})) - } - return successList, failedList, err + return addResult, err } -func (a *API) ShopUpdate(originShopID string, shopInfo map[string]interface{}) (err error) { - _, err = a.AccessAPI("api/shop/update", utils.MergeMaps(utils.Params2Map("origin_shop_id", originShopID), shopInfo)) +func (a *API) ShopUpdate(shopInfo *ShopInfo) (err error) { + _, err = a.AccessAPI("api/shop/update", shopInfo) return err } diff --git a/platformapi/dadaapi/shop_test.go b/platformapi/dadaapi/shop_test.go index b54ea086..0fd80304 100644 --- a/platformapi/dadaapi/shop_test.go +++ b/platformapi/dadaapi/shop_test.go @@ -15,10 +15,33 @@ func TestShopDetail(t *testing.T) { baseapi.SugarLogger.Debug(utils.Format4Output(result, false)) } -func TestShopAddl(t *testing.T) { - result, err := dadaapi.ShopAdd("18180948107", "京西大本营", BusinessTypeFruitVegetable, "成都市", "金牛区", "西南交通大学科技大厦二楼", 104.056844, 30.695151, "徐先生", "18180948107", nil) +func TestShopAdd(t *testing.T) { + shopInfo := &ShopInfo{ + OriginShopID: "181809481071", + StationName: "京西大本营2", + Business: BusinessTypeFruitVegetable, + CityName: "成都市", + AreaName: "金牛区", + StationAddress: "西南交通大学科技大厦二楼", + Lng: 104.056844, + Lat: 30.695151, + ContactName: "徐先生", + Phone: "18180948107", + } + result, err := dadaapi.ShopAdd(shopInfo) if err != nil { t.Fatal(err) } baseapi.SugarLogger.Debug(result) } + +func TestShopUpdate(t *testing.T) { + shopInfo := &ShopInfo{ + OriginShopID: "18180948107", + StationName: "京西大本营234", + } + err := dadaapi.ShopUpdate(shopInfo) + if err != nil { + t.Fatal(err) + } +} diff --git a/platformapi/ebaiapi/shop_sku_test.go b/platformapi/ebaiapi/shop_sku_test.go index c08974e1..ec52a631 100644 --- a/platformapi/ebaiapi/shop_sku_test.go +++ b/platformapi/ebaiapi/shop_sku_test.go @@ -186,3 +186,8 @@ func TestSkuStockUpdateBatch(t *testing.T) { } t.Log(utils.Format4Output(failedSkuIDs, false)) } + +func TestGetEbaiCatIDFromName(t *testing.T) { + ebaiCatID := api.GetEbaiCatIDFromName("300280", "应季水果新鲜美味") + t.Log(ebaiCatID) +} diff --git a/platformapi/mtpsapi/callback.go b/platformapi/mtpsapi/callback.go index 4a5da2dc..062b6e9a 100644 --- a/platformapi/mtpsapi/callback.go +++ b/platformapi/mtpsapi/callback.go @@ -13,9 +13,9 @@ type CallbackResponse struct { } type CallbackCommonMsg struct { - AppKey string - Timestamp int64 - Sign string + AppKey string `json:"appkey"` + Timestamp int64 `json:"timestamp"` + Sign string `json:"sign"` } type CallbackOrderMsg struct { @@ -35,6 +35,17 @@ type CallbackOrderExceptionMsg struct { ExceptionTime int64 } +type CallbackShopStatusMsg struct { + ShopName string `json:"shop_name"` + ShopID string `json:"shop_id"` + Status int `json:"status"` + RejectMessage string `json:"reject_message"` + + AppKey string `json:"appkey"` + Timestamp int64 `json:"timestamp"` + Sign string `json:"sign"` +} + var ( SuccessResponse = &CallbackResponse{Code: 0} SignatureIsNotOk = &CallbackResponse{Code: -1} @@ -117,6 +128,16 @@ func (a *API) GetOrderExceptionCallbackMsg(request *http.Request) (orderMsg *Cal ExceptionDescr: request.FormValue("exception_descr"), ExceptionTime: utils.Str2Int64(request.FormValue("exception_time")), } - return orderMsg, nil } + +func (a *API) GetShopStatusCallbackMsg(request *http.Request) (shopStatusMsg *CallbackShopStatusMsg, callbackResponse *CallbackResponse) { + if callbackResponse = a.CheckCallbackValidation(request); callbackResponse != nil { + return nil, callbackResponse + } + err := utils.Map2StructByJson(utils.URLValues2Map(request.PostForm), &shopStatusMsg, false) + if err != nil { + callbackResponse = Err2CallbackResponse(err, "") + } + return shopStatusMsg, callbackResponse +} diff --git a/platformapi/mtpsapi/mtpsapi.go b/platformapi/mtpsapi/mtpsapi.go index 3266602b..ef01a967 100644 --- a/platformapi/mtpsapi/mtpsapi.go +++ b/platformapi/mtpsapi/mtpsapi.go @@ -57,9 +57,10 @@ var ( ) const ( - DeliveryServiceCodeRapid = 4011 - DeliveryServiceCodeIntime = 4012 - DeliveryServiceCodeTogether = 4013 + DeliveryServiceCodeSuperRapid = 4002 // 飞速达 + DeliveryServiceCodeRapid = 4011 // 快速达 + DeliveryServiceCodeIntime = 4012 // 及时达 + DeliveryServiceCodeTogether = 4013 // 集中送 ) const ( @@ -73,8 +74,8 @@ const ( ) const ( - CoordinateTypeMars = 0 - CoordinateTypeBaidu = 1 + CoordinateTypeMars = 0 // 火星坐标(高德,腾讯地图均采用火星坐标),此为默认项 + CoordinateTypeBaidu = 1 // 百度坐标 ) // 错误码 @@ -97,6 +98,21 @@ const ( CancelReasonRideerOther = 399 ) +const ( + ShopStatusAuditRejected = 10 // 审核驳回 + ShopStatusAuditPassed = 20 // 审核通过 + ShopStatusCreateSuccess = 30 // 创建成功 + ShopStatusOnline = 40 // 上线可发单 +) + +const ( + ShopCategoryMarket = 120 // 生活超市 + ShopCategoryMarketConvenience = 120001 // 便利店 + + ShopCategoryFruit = 150 // 生鲜果蔬 + ShopCategoryFruitFruit = 150001 // 果蔬 +) + type OrderInfoCommon struct { DeliveryID int64 MtPeisongID string @@ -148,10 +164,40 @@ type GoodsItem struct { GoodPrice float64 `json:"goodPrice"` GoodUnit string `json:"goodUnit"` } + type GoodsDetail struct { Goods []*GoodsItem `json:"goods"` } +type BusinessHour struct { + BeginTime string `json:"beginTime"` + EndTime string `json:"endTime"` +} + +type ShopInfo struct { + ShopID string `json:"shop_id,omitempty"` + ShopName string `json:"shop_name,omitempty"` + Category int `json:"category,omitempty"` + SecondCategory int `json:"second_category,omitempty"` + ContactName string `json:"contact_name,omitempty"` + ContactPhone string `json:"contact_phone,omitempty"` + ContactEmail string `json:"contact_email,omitempty"` + ShopAddress string `json:"shop_address,omitempty"` + ShopAddressDetail string `json:"shop_address_detail,omitempty"` + ShopLng int `json:"shop_lng,omitempty"` + ShopLat int `json:"shop_lat,omitempty"` + CoordinateType int `json:"coordinate_type"` + DeliveryServiceCodes string `json:"delivery_service_codes,omitempty"` + BusinessHours string `json:"business_hours,omitempty"` + + // 以下为查询专有的 + City int `json:"city,omitempty"` + DeliveryHours string `json:"delivery_hours,omitempty"` + Prebook int `json:"prebook,omitempty"` + PrebookOutOfBizTime int `json:"prebook_out_of_biz_time,omitempty"` + PrebookPeriod string `json:"prebook_period,omitempty"` +} + type API struct { appKey string secret string @@ -198,14 +244,11 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R panic("params is nil!") } - params2 := make(url.Values) - for k, v := range params { - params2[k] = []string{fmt.Sprint(v)} - } - params2["appkey"] = []string{a.appKey} - params2["timestamp"] = []string{utils.Int64ToStr(utils.GetCurTimestamp())} - params2["version"] = []string{"1.0"} - params2[signKey] = []string{a.signParams(params2)} + params2 := utils.Map2URLValues(params) + params2.Set("appkey", a.appKey) + params2.Set("timestamp", utils.Int64ToStr(utils.GetCurTimestamp())) + params2.Set("version", "1.0") + params2.Set(signKey, a.signParams(params2)) // baseapi.SugarLogger.Debug(params2.Encode()) err = platformapi.AccessPlatformAPIWithRetry(a.client, @@ -286,6 +329,25 @@ func (a *API) CancelOrder(deliveryId int64, mtPeiSongId string, cancelReasonId i } } +func (a *API) ShopCreate(shopInfo *ShopInfo) (status int, err error) { + params := utils.Struct2MapByJson(shopInfo) + result, err := a.AccessAPI("shop/create", params) + if err == nil { + status = int(utils.Interface2Int64WithDefault(result.Data["status"], 0)) + } + return status, err +} + +func (a *API) ShopQuery(shopID string) (shopInfo *ShopInfo, err error) { + result, err := a.AccessAPI("shop/query", map[string]interface{}{ + "shop_id": shopID, + }) + if err == nil { + err = utils.Map2StructByJson(result.Data, &shopInfo, false) + } + return shopInfo, err +} + func (a *API) simulateOrderBehavior(action string, deliveryId int64, mtPeiSongId string) (err error) { params := map[string]interface{}{ "delivery_id": deliveryId, diff --git a/platformapi/mtpsapi/mtpsapi_test.go b/platformapi/mtpsapi/mtpsapi_test.go index f00acc36..df68e511 100644 --- a/platformapi/mtpsapi/mtpsapi_test.go +++ b/platformapi/mtpsapi/mtpsapi_test.go @@ -11,7 +11,7 @@ import ( ) var ( - mtpsapi *API + api *API sugarLogger *zap.SugaredLogger ) @@ -21,9 +21,9 @@ func init() { baseapi.Init(sugarLogger) // sandbox - mtpsapi = New("25e816550bc9484480642f19a95f13fd", "r4$HqrKx9~=7?2Jfo,$Z~a7%~k!Au&pEdI2)oPJvSbH2ao@2N0[8wSIvtuumh_J^") + api = New("25e816550bc9484480642f19a95f13fd", "r4$HqrKx9~=7?2Jfo,$Z~a7%~k!Au&pEdI2)oPJvSbH2ao@2N0[8wSIvtuumh_J^") // prod - // mtpsapi = New("3c0a05d464c247c19d7ec13accc78605", "b1M}9?:sTbsB[OF2gNORnN(|(iy9rB8(`7]|[wGLnbmt`evfM>E:A90DjHAW:UPE") + // api = New("3c0a05d464c247c19d7ec13accc78605", "b1M}9?:sTbsB[OF2gNORnN(|(iy9rB8(`7]|[wGLnbmt`evfM>E:A90DjHAW:UPE") } func handleError(t *testing.T, err error) { @@ -44,7 +44,7 @@ func TestAccessAPI(t *testing.T) { "delivery_id": 123456789, "mt_peisong_id": mtPeiSongId, } - result, err := mtpsapi.AccessAPI("order/status/query", params) + result, err := api.AccessAPI("order/status/query", params) if err != nil { t.Fatalf("Error when accessing AccessAPI result:%v, error:%v", result, err) } else { @@ -73,40 +73,73 @@ func TestCreateOrderByShop(t *testing.T) { OrderType: OrderTypeASAP, } - order, err := mtpsapi.CreateOrderByShop(basicParams, nil) + order, err := api.CreateOrderByShop(basicParams, nil) handleError(t, err) if order != nil { sugarLogger.Debugf("order:%v", order) } } +func TestShopQuery(t *testing.T) { + shopInfo, err := api.ShopQuery("not exist") + if err == nil { + t.Fatal("应该报错找不到门店") + } + shopInfo, err = api.ShopQuery("11726346") + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(shopInfo, false)) +} + +func TestShopCreate(t *testing.T) { + shopInfo := &ShopInfo{ + ShopID: "11726346", + ShopName: "测试门店", + Category: ShopCategoryFruit, + SecondCategory: ShopCategoryFruitFruit, + ContactName: "徐", + ContactPhone: "18180948107", + ShopAddress: "北京市通州区五所南路与玉桥西路南延交叉口北100米梨园农副产品交易中心 水果区", + ShopLng: 116672496, + ShopLat: 39879491, + CoordinateType: CoordinateTypeMars, + DeliveryServiceCodes: utils.Int2Str(DeliveryServiceCodeRapid), + BusinessHours: "[{\"beginTime\":\"09:00\",\"endTime\":\"18:00\"}]", + } + _, err := api.ShopCreate(shopInfo) + if err != nil { + t.Fatal(err) + } +} + func TestSimulateArrange(t *testing.T) { - err := mtpsapi.SimulateArrange(123456789, "1529387562097059") + err := api.SimulateArrange(123456789, "1529387562097059") handleError(t, err) } func TestSimulatePickup(t *testing.T) { - err := mtpsapi.SimulatePickup(123456789, "1529387562097059") + err := api.SimulatePickup(123456789, "1529387562097059") handleError(t, err) } func TestSimulateRearrange(t *testing.T) { - err := mtpsapi.SimulateRearrange(123456789, "1529387562097059") + err := api.SimulateRearrange(123456789, "1529387562097059") handleError(t, err) } func TestSimulateDeliver(t *testing.T) { - err := mtpsapi.SimulateDeliver(123456789, "1529387562097059") + err := api.SimulateDeliver(123456789, "1529387562097059") handleError(t, err) } func TestSimulateReportException(t *testing.T) { - err := mtpsapi.SimulateReportException(123456789, "1529387562097059") + err := api.SimulateReportException(123456789, "1529387562097059") handleError(t, err) } func TestCancelOrder(t *testing.T) { - result, err := mtpsapi.CancelOrder(123456789, "1529387562097059", CancelReasonMerchantOther, "just a test") + result, err := api.CancelOrder(123456789, "1529387562097059", CancelReasonMerchantOther, "just a test") handleError(t, err) sugarLogger.Debug(result) } diff --git a/platformapi/yilianyunapi/yilianyunapi_test.go b/platformapi/yilianyunapi/yilianyunapi_test.go index e323110d..3e3f79f1 100644 --- a/platformapi/yilianyunapi/yilianyunapi_test.go +++ b/platformapi/yilianyunapi/yilianyunapi_test.go @@ -41,7 +41,9 @@ func TestRetrieveToken(t *testing.T) { } func TestAddPrinter(t *testing.T) { - err := api.AddPrinter("4004617180", "381870509796", "测试打印机1") + // err := api.AddPrinter("4004600675", "fem2ukwvduik", "公司测试打印机1") + // 4004617180 公司测试打印机2 + err := api.AddPrinter("4004600675", "fem2ukwvduik", "测试打印机1") handleError(t, err) } @@ -63,7 +65,7 @@ func TestPrintMsgWithToken(t *testing.T) { } func TestGetPrintStatus(t *testing.T) { - state, err := api.GetPrintStatus("4004617683") + state, err := api.GetPrintStatus("4004617180") handleError(t, err) baseapi.SugarLogger.Debug(state) } @@ -86,6 +88,6 @@ func TestCancelAll(t *testing.T) { } func TestPlayText(t *testing.T) { - err := api.PlayText("4004617180", utils.GetUUID(), "大家好", "") + err := api.PlayText("4004617180", utils.GetUUID(), "我们已安排%s配送员%s负责配送。^_^", "") handleError(t, err) } diff --git a/utils/utils.go b/utils/utils.go index 445c7c42..cba4891f 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,9 +2,7 @@ package utils import ( "encoding/base64" - "fmt" "net/http" - "net/url" "reflect" "strings" "time" @@ -172,14 +170,7 @@ func CallFuncRetryAsync(handler func(int) error, duration time.Duration, retryCo func GenerateGetURL(baseURL, apiStr string, params map[string]interface{}) string { queryString := "" if params != nil { - for k, v := range params { - if queryString == "" { - queryString = "?" - } else { - queryString += "&" - } - queryString += k + "=" + url.QueryEscape(fmt.Sprint(v)) - } + queryString = "?" + Map2URLValues(params).Encode() } if apiStr != "" { return baseURL + "/" + apiStr + queryString diff --git a/utils/utils_test.go b/utils/utils_test.go index cc2b553d..ffdab708 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -235,3 +235,20 @@ func TestLimitUTF8StringLen(t *testing.T) { t.Log(LimitUTF8StringLen("a大家好1234567", 8)) t.Log(LimitMixedStringLen("a大家好1234567", 8)) } + +func TestGenerateGetURL(t *testing.T) { + fullURL := GenerateGetURL("http://www.163.com", "api", map[string]interface{}{ + "k1": "v1", + "k2": 2, + }) + t.Log(fullURL) + if fullURL != "http://www.163.com/api?k1=v1&k2=2" { + t.Fatal("GenerateGetURL错误") + } + + fullURL = GenerateGetURL("http://www.163.com", "api", nil) + t.Log(fullURL) + if fullURL != "http://www.163.com/api" { + t.Fatal("GenerateGetURL错误") + } +}