diff --git a/platformapi/weimobapi/callback.go b/platformapi/weimobapi/callback.go new file mode 100644 index 00000000..388d3cb4 --- /dev/null +++ b/platformapi/weimobapi/callback.go @@ -0,0 +1,84 @@ +package weimobapi + +import ( + "time" + + "git.rosy.net.cn/baseapi/utils" +) + +const ( + MsgEventCreateOrder = "createOrder" + MsgEventOrderStatusChange = "orderStatusChange" +) + +const ( + ChannelTypeWeixinOpen = 0 + ChannelTypeWeixinMini = 1 + ChannelTypeH5 = 2 + ChannelTypeQQ = 3 + ChannelTypeWeibo = 4 + ChannelTypeToutiao = 5 + ChannelTypeAliPay = 6 + ChannelTypeOffline = 7 +) + +type CallbackMsg struct { + ID string `json:"id"` + MsgEvent string `json:"msgEvent"` + PublicAccountID int64 `json:"public_account_id"` + OrderNo int64 `json:"orderNo"` + StatusTime time.Time `json:"statusTime"` + ChannelType int `json:"channelType"` +} + +type ErrorInfo struct { + ErrCode int `json:"errcode"` + ErrMsg string `json:"errmsg"` +} +type CallbackResponse struct { + Code ErrorInfo `json:"code"` +} + +var ( + SuccessResponse = &CallbackResponse{ + Code: ErrorInfo{}, + } +) + +func Err2CallbackResponse(err error, data string) *CallbackResponse { + if err == nil { + return SuccessResponse + } + if data == "" { + data = err.Error() + } + return &CallbackResponse{ + Code: ErrorInfo{ + ErrCode: -1, + ErrMsg: data, + }, + } +} + +func (a *API) GetCallbackMsg(body []byte) (msg *CallbackMsg, callbackResponse *CallbackResponse) { + var mapMsg map[string]interface{} + err := utils.UnmarshalUseNumber(body, &mapMsg) + if err != nil { + return nil, Err2CallbackResponse(err, "") + } + msgBody := mapMsg["msg_body"].(map[string]interface{}) + msg = &CallbackMsg{ + ID: utils.Interface2String(mapMsg["id"]), + MsgEvent: utils.Interface2String(mapMsg["event"]), + PublicAccountID: utils.MustInterface2Int64(mapMsg["public_account_id"]), + OrderNo: utils.MustInterface2Int64(msgBody["orderNo"]), + } + + if msg.MsgEvent == MsgEventCreateOrder { + // msg.StatusTime = msgBody["createTime"] + msg.ChannelType = int(utils.MustInterface2Int64(msgBody["channelType"])) + } else { + msg.StatusTime = time.Now() + } + return nil, SuccessResponse +} diff --git a/platformapi/weimobapi/goods.go b/platformapi/weimobapi/goods.go index 4457a666..195cf656 100644 --- a/platformapi/weimobapi/goods.go +++ b/platformapi/weimobapi/goods.go @@ -19,6 +19,14 @@ const ( GoodsTypeOversea = 1 ) +const ( + OrderStatusWait4Pay = 0 // 待支付 + OrderStatusPayed = 1 // 已付款,待发货 + OrderStatusDelivering = 2 // 已发货 + OrderStatusFinished = 3 // 已完成 + OrderStatusCanceled = 4 // 已取消 +) + type PendingSaveB2CGoodsVo struct { FreightTemplateId int64 `json:"freightTemplateId"` DeliveryTypeIdList []int64 `json:"deliveryTypeIdList"` @@ -68,10 +76,10 @@ func (a *API) QueryGoodsList(pageNum, pageSize int) (retVal []map[string]interfa "pageSize": pageSize, }) if err == nil { - if pageList, ok := result["pageList"].([]interface{}); ok { + if pageList, ok := result.(map[string]interface{})["pageList"].([]interface{}); ok { retVal = utils.Slice2MapSlice(pageList) } - totalCount = int(utils.MustInterface2Int64(result["totalCount"])) + totalCount = int(utils.MustInterface2Int64(result.(map[string]interface{})["totalCount"])) return retVal, totalCount, nil } return nil, 0, err @@ -80,7 +88,7 @@ func (a *API) QueryGoodsList(pageNum, pageSize int) (retVal []map[string]interfa func (a *API) QueryCategoryTree() (retVal []*Category, err error) { result, err := a.AccessAPI("category/queryCategoryTree", nil) if err == nil { - categoryList := result["categoryList"].([]interface{}) + categoryList := result.(map[string]interface{})["categoryList"].([]interface{}) retVal = make([]*Category, len(categoryList)) for k, v := range categoryList { retVal[k] = map2Category(1, 0, v.(map[string]interface{})) @@ -104,7 +112,7 @@ func (a *API) QueryChildrenCategory(categoryId int64) (retVal []*Category, err e "categoryId": categoryId, }) if err == nil { - categoryList := result["categoryList"].([]interface{}) + categoryList := result.(map[string]interface{})["categoryList"].([]interface{}) retVal = make([]*Category, len(categoryList)) for k, v := range categoryList { retVal[k] = map2Category(2, categoryId, v.(map[string]interface{})) @@ -117,7 +125,7 @@ func (a *API) QueryChildrenCategory(categoryId int64) (retVal []*Category, err e func (a *API) QueryClassifyInfoList() (retVal []*GoodsClassify, err error) { result, err := a.AccessAPI("goodsClassify/queryClassifyInfoList", nil) if err == nil { - goodsClassifyList := interface2ClassifyList(result["goodsClassifyList"], nil) + goodsClassifyList := interface2ClassifyList(result.(map[string]interface{})["goodsClassifyList"], nil) return goodsClassifyList, nil } return nil, err @@ -163,7 +171,7 @@ func (a *API) AddClassify(title string, parentID int64, imageURL string) (goodsC } result, err := a.AccessAPI("goodsClassify/addClassify", apiParams) if err == nil { - return utils.MustInterface2Int64(result["goodsClassifyId"]), nil + return utils.MustInterface2Int64(result.(map[string]interface{})["goodsClassifyId"]), nil } return 0, err } @@ -204,12 +212,12 @@ func (a *API) AddGoods(outerGoodsCode, title string, isMultiSku bool, goodsImage }) if err == nil { skuMap := make(map[string]int64) - skuList := result["skuList"].([]interface{}) + skuList := result.(map[string]interface{})["skuList"].([]interface{}) for _, v := range skuList { sku := v.(map[string]interface{}) skuMap[utils.Interface2String(sku[KeyOuterSkuCode])] = utils.MustInterface2Int64(sku[KeySkuID]) } - return utils.MustInterface2Int64(result["goodsId"]), skuMap, nil + return utils.MustInterface2Int64(result.(map[string]interface{})["goodsId"]), skuMap, nil } return 0, nil, err } @@ -237,12 +245,12 @@ func (a *API) UpdateGoods(goodsID int64, title string, isMultiSku bool, goodsIma }) if err == nil { skuMap := make(map[string]int64) - skuList := result["skuList"].([]interface{}) + skuList := result.(map[string]interface{})["skuList"].([]interface{}) for _, v := range skuList { sku := v.(map[string]interface{}) skuMap[utils.Interface2String(sku[KeyOuterSkuCode])] = utils.MustInterface2Int64(sku[KeySkuID]) } - return utils.MustInterface2Int64(result["goodsId"]), skuMap, nil + return utils.MustInterface2Int64(result.(map[string]interface{})["goodsId"]), skuMap, nil } return 0, nil, err } @@ -270,7 +278,7 @@ func (a *API) FindDeliveryTypeList(goodsID int64) (retVal []*DeliveryType, err e } result, err := a.AccessAPI("goods/findDeliveryTypeList", apiParams) if err == nil { - deliveryTypeList := result["deliveryTypeList"].([]interface{}) + deliveryTypeList := result.(map[string]interface{})["deliveryTypeList"].([]interface{}) retVal = make([]*DeliveryType, len(deliveryTypeList)) for k, v := range deliveryTypeList { mapData := v.(map[string]interface{}) @@ -295,7 +303,7 @@ func (a *API) uploadImg(imgData []byte, name string) (imgURL string, err error) } result, err := a.AccessAPI("goodsImage/uploadImg", apiParams) if err == nil { - urlInfo := result["urlInfo"].([]interface{}) + urlInfo := result.(map[string]interface{})["urlInfo"].([]interface{}) if len(urlInfo) > 0 { urlInfo0 := urlInfo[0].(map[string]interface{}) if utils.MustInterface2Int64(urlInfo0["legalStatus"]) == 0 { @@ -333,7 +341,7 @@ func (a *API) FindFreightTemplateList(goodsID int64) (retVal map[string]interfac } result, err := a.AccessAPI("goods/findFreightTemplateList", apiParams) if err == nil { - return result, nil + return result.(map[string]interface{}), nil } return nil, err } diff --git a/platformapi/weimobapi/logistics.go b/platformapi/weimobapi/logistics.go new file mode 100644 index 00000000..7ab8f008 --- /dev/null +++ b/platformapi/weimobapi/logistics.go @@ -0,0 +1,13 @@ +package weimobapi + +import ( + "git.rosy.net.cn/baseapi/utils" +) + +func (a *API) QueryLogisticsCompany() (retVal []map[string]interface{}, err error) { + result, err := a.AccessAPI("logistics/queryLogisticsCompany", nil) + if err == nil { + return utils.Slice2MapSlice(result.([]interface{})), nil + } + return nil, err +} diff --git a/platformapi/weimobapi/logistics_test.go b/platformapi/weimobapi/logistics_test.go new file mode 100644 index 00000000..cda52447 --- /dev/null +++ b/platformapi/weimobapi/logistics_test.go @@ -0,0 +1,16 @@ +package weimobapi + +import ( + "testing" + + "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/utils" +) + +func TestQueryLogisticsCompany(t *testing.T) { + result, err := api.QueryLogisticsCompany() + if err != nil { + t.Fatal(err) + } + baseapi.SugarLogger.Debug(utils.Format4Output(result, false)) +} diff --git a/platformapi/weimobapi/order.go b/platformapi/weimobapi/order.go new file mode 100644 index 00000000..f9a620f4 --- /dev/null +++ b/platformapi/weimobapi/order.go @@ -0,0 +1,53 @@ +package weimobapi + +import ( + "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/utils" + "github.com/fatih/structs" +) + +type DeliveryOrderItem struct { + ItemId int64 `json:"itemId"` + SkuId int64 `json:"skuId"` + SkuNum int `json:"skuNum"` +} + +type DeliveryOrder struct { + OrderNo int64 `json:"orderNo"` + DeliveryNo string `json:"deliveryNo"` + DeliveryCompanyCode string `json:"deliveryCompanyCode"` + DeliveryCompanyName string `json:"deliveryCompanyName"` + IsNeedLogistics bool `json:"isNeedLogistics"` + IsSplitPackage bool `json:"isSplitPackage"` + DeliveryRemark string `json:"deliveryRemark"` + DeliveryOrderItemList []*DeliveryOrderItem `json:"deliveryOrderItemList"` +} + +func (a *API) QueryOrderDetail(orderNo int64, needInvoiceInfo bool) (retVal map[string]interface{}, err error) { + result, err := a.AccessAPI("order/queryOrderDetail", map[string]interface{}{ + "orderNo": orderNo, + "needInvoiceInfo": needInvoiceInfo, + }) + if err == nil { + return result.(map[string]interface{}), nil + } + return nil, err +} + +func (a *API) CancelOrder(orderNo int64, specificCancelReason string) (err error) { + apiParams := map[string]interface{}{ + "orderNo": orderNo, + } + if specificCancelReason != "" { + apiParams["specificCancelReason"] = specificCancelReason + } + _, err = a.AccessAPI("order/cancelOrder", apiParams) + return err +} + +func (a *API) DeliveryOrder(orderDeliveryInfo *DeliveryOrder) (err error) { + apiParams := structs.Map(orderDeliveryInfo) + baseapi.SugarLogger.Debug(utils.Format4Output(apiParams, false)) + _, err = a.AccessAPI("order/deliveryOrder", apiParams) + return err +} diff --git a/platformapi/weimobapi/order_test.go b/platformapi/weimobapi/order_test.go new file mode 100644 index 00000000..a0b7d7b4 --- /dev/null +++ b/platformapi/weimobapi/order_test.go @@ -0,0 +1,26 @@ +package weimobapi + +import ( + "testing" + + "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/utils" +) + +func TestQueryOrderDetail(t *testing.T) { + result, err := api.QueryOrderDetail(5287873015048, false) + if err != nil { + t.Fatal(err) + } + baseapi.SugarLogger.Debug(utils.Format4Output(result, false)) +} + +func TestBatchDeliveryOrder(t *testing.T) { + deliveryOrder := &DeliveryOrder{ + OrderNo: 5287873015048, + } + err := api.DeliveryOrder(deliveryOrder) + if err != nil { + t.Fatal(err) + } +} diff --git a/platformapi/weimobapi/weimobapi.go b/platformapi/weimobapi/weimobapi.go index 5b631266..d2453dea 100644 --- a/platformapi/weimobapi/weimobapi.go +++ b/platformapi/weimobapi/weimobapi.go @@ -105,7 +105,7 @@ func (a *API) MustGetToken() *TokenInfo { return token } -func (a *API) AccessAPI(apiStr string, apiParams map[string]interface{}) (retVal map[string]interface{}, err error) { +func (a *API) AccessAPI(apiStr string, apiParams map[string]interface{}) (retVal interface{}, err error) { err = platformapi.AccessPlatformAPIWithRetry(a.client, func() *http.Request { var request *http.Request @@ -171,7 +171,7 @@ func (a *API) AccessAPI(apiStr string, apiParams map[string]interface{}) (retVal errMsg = errMap["errmsg"].(string) } if code == ResponseCodeSuccess { - retVal = jsonResult1["data"].(map[string]interface{}) + retVal = jsonResult1["data"] return platformapi.ErrLevelSuccess, nil } } @@ -203,7 +203,7 @@ func (a *API) RefreshTokenByCode(code, redirectURL string) (retVal *TokenInfo, e "redirect_uri": redirectURL, }) if err == nil { - retVal = map2TokenInfo(result) + retVal = map2TokenInfo(result.(map[string]interface{})) a.SetToken(retVal) return retVal, nil } @@ -218,7 +218,7 @@ func (a *API) RefreshTokenByRefreshToken() (retVal *TokenInfo, err error) { "refresh_token": curToken.RefreshToken, }) if err == nil { - retVal = map2TokenInfo(result) + retVal = map2TokenInfo(result.(map[string]interface{})) a.SetToken(retVal) return retVal, nil