package ebaiapi import ( "fmt" "net/http" "time" "git.rosy.net.cn/baseapi" "git.rosy.net.cn/baseapi/platformapi" "git.rosy.net.cn/baseapi/utils" ) const ( storeURL = "https://be.ele.me" ) const ( ReplyStatusAll = -1 ReplyStatusNotReplied = 0 ReplyStatusReplied = 1 CommentLevelAll = -1 CommentLevel1 = 1 CommentLevel2 = 2 CommentLevel3 = 3 CommentLevel4 = 4 CommentLevel5 = 5 CommentContentAll = -1 CommentContentHaveContent = 1 CommentContentNoContent = 0 ) func (a *API) SetStoreCookie(key, value string) { a.locker.Lock() defer a.locker.Unlock() a.storeCookies[key] = value } func (a *API) GetStoreCookie(key string) string { a.locker.RLock() defer a.locker.RUnlock() return a.storeCookies[key] } func (a *API) AccessStorePage(subURL string) (retVal map[string]interface{}, err error) { a.locker.RLock() storeCookieLen := len(a.storeCookies) a.locker.RUnlock() if storeCookieLen == 0 { return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法") } err = platformapi.AccessPlatformAPIWithRetry(a.client, func() *http.Request { fullURL := utils.GenerateGetURL(storeURL, subURL, nil) // baseapi.SugarLogger.Debug(fullURL) request, _ := http.NewRequest(http.MethodGet, fullURL, nil) if err != nil { return nil } a.locker.RLock() for k, v := range a.storeCookies { request.AddCookie(&http.Cookie{ Name: k, Value: v, }) } a.locker.RUnlock() return request }, a.config, func(jsonResult1 map[string]interface{}) (errLevel string, err error) { retVal = jsonResult1 code := int(utils.MustInterface2Int64(jsonResult1["errno"])) if code == ResponseCodeSuccess { retVal = jsonResult1["data"].(map[string]interface{}) return platformapi.ErrLevelSuccess, nil } newErr := utils.NewErrorIntCode(jsonResult1["errmsg"].(string), code) if code == ResponseCodeCallElmFailed { return platformapi.ErrLevelRecoverableErr, newErr } baseapi.SugarLogger.Debugf("ebai AccessStorePage failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true)) return platformapi.ErrLevelCodeIsNotOK, newErr }) return retVal, err } func (a *API) GetRealMobile4Order(orderId string) (mobile string, err error) { retVal, err := a.GetStoreOrderInfo(orderId) if err == nil { return retVal["order_basic"].(map[string]interface{})["user_phone_call"].(string), nil } return "", err } func (a *API) GetStoreOrderInfo(orderId string) (storeOrderInfo map[string]interface{}, err error) { retVal, err := a.AccessStorePage(fmt.Sprintf("crm/orderlist?keyword=%s", orderId)) // baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false)) if err == nil { resultList := retVal["order_list"].([]interface{}) if len(resultList) > 0 { return resultList[0].(map[string]interface{}), nil } return nil, fmt.Errorf("不能找到订单:%s的相关信息", orderId) } return nil, err } func (a *API) GetStoreOrderInfoList(fromTime, toTime string, shopID string, orderStatus int) (storeOrderList []map[string]interface{}, err error) { // pageSize := 20 pageNo := 1 urlTemplate := "crm/orderlist?start_timestamp=%d&end_timestamp=%d&shop_id=%s" params := []interface{}{ utils.Str2Time(fromTime).Unix(), utils.Str2Time(toTime).Unix(), shopID, } if orderStatus >= 0 { urlTemplate += "&order_status=%d" params = append(params, orderStatus) } fixedURL := fmt.Sprintf(urlTemplate, params...) for { retVal, err2 := a.AccessStorePage(fixedURL + "&page=" + utils.Int2Str(pageNo)) // baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false)) if err = err2; err == nil { resultList := retVal["order_list"].([]interface{}) storeOrderList = append(storeOrderList, utils.Slice2MapSlice(resultList)...) if len(storeOrderList) >= int(utils.MustInterface2Int64(retVal["order_count"])) { return storeOrderList, nil } pageNo++ } else { return nil, err } } return nil, err } func (a *API) getCommentList(isElm bool, fromTime, toTime time.Time, shopID, supplierID string, replyStatus, level, nonEmpty int) (commentList []map[string]interface{}, err error) { pageSize := 200 pageNo := 1 urlTemplate := "crm/%s?start_time=%s&end_time=%s&page_count=%d" params := []interface{}{ []string{ "getelecommentlist", "getcommentlist", }[1-utils.Bool2Int(isElm)], utils.Time2Str(fromTime), utils.Time2Str(toTime), pageSize, } if shopID != "" { urlTemplate += "&shop_id=%s" params = append(params, shopID) } else if supplierID != "" { urlTemplate += "&supplier_id=%s" params = append(params, supplierID) } if replyStatus != ReplyStatusAll { urlTemplate += "&reply_status=%d" params = append(params, replyStatus) } if level != CommentLevelAll { urlTemplate += "&level=%d" params = append(params, level) } if nonEmpty != CommentContentAll { urlTemplate += "&nonempty=%d" params = append(params, replyStatus) } fixedURL := fmt.Sprintf(urlTemplate, params...) for { retVal, err2 := a.AccessStorePage(fixedURL + "&page_num=" + utils.Int2Str(pageNo)) if err = err2; err == nil { for _, comment := range retVal["comment_list"].([]interface{}) { commentMap := comment.(map[string]interface{}) for _, orderComment := range commentMap["order_commentList"].([]interface{}) { orderCommentMap := orderComment.(map[string]interface{}) orderCommentMap["order_id"] = commentMap["order_id"] orderCommentMap["shop_id"] = commentMap["shop_id"] commentList = append(commentList, orderComment.(map[string]interface{})) } } if len(commentList) >= int(utils.MustInterface2Int64(retVal["total"])) { return commentList, nil } pageNo++ } else { return nil, err } } return nil, err } func (a *API) GetEleCommentList(fromTime, toTime time.Time, shopID, supplierID string, replyStatus, level, nonEmpty int) (commentList []map[string]interface{}, err error) { return a.getCommentList(true, fromTime, toTime, shopID, supplierID, replyStatus, level, nonEmpty) } func (a *API) GetCommentList(fromTime, toTime time.Time, shopID, supplierID string, replyStatus, level, nonEmpty int) (commentList []map[string]interface{}, err error) { return a.getCommentList(false, fromTime, toTime, shopID, supplierID, replyStatus, level, nonEmpty) } func (a *API) PageGetSkuList(baiduShopID int64) (skuList []map[string]interface{}, err error) { pageSize := 200 pageNo := 1 urlTemplate := "commodity/getskulist?wid=%d&perpage=%d&upc_type=2&weight=2" params := []interface{}{ baiduShopID, pageSize, } fixedURL := fmt.Sprintf(urlTemplate, params...) for { retVal, err2 := a.AccessStorePage(fixedURL + "&curpage=" + utils.Int2Str(pageNo)) if err = err2; err == nil { for _, sku := range retVal["sku_list"].([]interface{}) { skuList = append(skuList, sku.(map[string]interface{})) } if len(skuList) >= int(utils.Str2Int64(utils.Interface2String(retVal["total"]))) { return skuList, nil } pageNo++ } else { return nil, err } } return nil, err } func (a *API) PageGetCustomSkuList(baiduShopID int64, customCatID int64) (skuList []map[string]interface{}, err error) { urlTemplate := "commodity/getCustomSkuList?wid=%d&custom_cat_id=%d" params := []interface{}{ baiduShopID, customCatID, } fixedURL := fmt.Sprintf(urlTemplate, params...) retVal, err := a.AccessStorePage(fixedURL) if err == nil { return utils.Slice2MapSlice(retVal["sku_list"].([]interface{})), nil } return nil, err } func (a *API) PageGetCustomCatList(baiduShopID int64) (catList []map[string]interface{}, err error) { urlTemplate := "commodity/GetCustomCatList?wid=%d" params := []interface{}{ baiduShopID, } fixedURL := fmt.Sprintf(urlTemplate, params...) retVal, err := a.AccessStorePage(fixedURL) if err == nil { return utils.Slice2MapSlice(retVal["cat_list"].([]interface{})), nil } return nil, err }