DeliveryQuoteUtil 是一个统一的配送报价工具类,用于处理跑腿平台报价、免费配送规则等逻辑。
use common\components\delivery\helpers\DeliveryQuoteUtil;
try {
$quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
'productList' => $productList, // 商品列表
'deliveryPlatform' => $platform, // 配送平台
'ghsInfo' => $ghsInfo, // 供货商信息
'custom' => $custom, // 客户信息对象
'order' => [ // 订单信息
'orderSn' => $orderSn,
'itemTotalAmount' => $totalAmount,
'remark' => $remark,
],
'mainId' => $this->mainId, // 当前操作者 mainId
'productCount' => $productCount, // 商品总数量
]);
// 获取结果
$sendCost = $quoteResult['sendCost']; // 配送费用(元)
$sendDistance = $quoteResult['sendDistance']; // 配送距离(米)
$deliveryList = $quoteResult['deliveryList']; // 可选配送方式列表
} catch (\Exception $e) {
util::fail($e->getMessage());
}
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| productList | array | 是 | 商品列表,每项包含 bigNum(数量)和 weight(重量) |
| deliveryPlatform | string | 是 | 配送平台:shansong/huolala/fengniao/shunfeng/dada |
| ghsInfo | array | 是 | 供货商信息,包含 mainId 和 shopId |
| custom | object | 是 | 客户信息对象,包含 name, mobile, fullAddress, lat, long 等 |
| order | array | 是 | 订单信息,包含 orderSn, itemTotalAmount, remark |
| mainId | int | 是 | 当前操作者的 mainId |
| productCount | int | 否 | 商品总数量(用于免费配送规则判断) |
| 字段名 | 类型 | 说明 |
|---|---|---|
| sendCost | float | 配送费用(元),已应用免费配送规则 |
| sendDistance | int | 配送距离(米) |
| deliveryList | array | 可选配送方式列表 |
| platformQuotes | array | 原始平台报价数据 |
// 生成随机订单号,不要跟原来的混起来,跑腿-销售单-
$prefix = 'PT-XSD-' . $this->mainId . '-';
$orderSn = $prefix . round(microtime(true) * 1000);
// 获取配送报价
try {
$quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
'productList' => $productList,
'deliveryPlatform' => $post['deliveryPlatform'],
'ghsInfo' => $ghsInfo,
'custom' => $custom,
'order' => [
'orderSn' => $orderSn,
'itemTotalAmount' => $post['itemTotalAmount'] ?? 0,
'remark' => $post['remark'] ?? '',
],
'mainId' => $this->mainId,
'productCount' => $productCount,
]);
$sendCost = $quoteResult['sendCost'];
$sendDistance = $quoteResult['sendDistance'];
} catch (\Exception $e) {
util::fail($e->getMessage());
}
// 准备商品列表
$productList = [];
foreach ($orderItems as $item) {
$productList[] = [
'bigNum' => $item['num'],
'weight' => $item['weight'],
];
}
// 准备客户信息对象
$custom = (object)[
'name' => $customName,
'mobile' => $customMobile,
'fullAddress' => $fullAddress,
'floor' => $floor,
'dist' => $dist,
'lat' => $lat,
'long' => $long,
'address' => $address,
'city' => $city,
];
// 获取配送报价
try {
$quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
'productList' => $productList,
'deliveryPlatform' => 'shansong',
'ghsInfo' => [
'mainId' => $shop->mainId,
'shopId' => $shop->id,
],
'custom' => $custom,
'order' => [
'orderSn' => $orderSn,
'itemTotalAmount' => $totalAmount,
'remark' => $remark,
],
'mainId' => $this->mainId,
'productCount' => $totalCount,
]);
$sendCost = $quoteResult['sendCost'];
$sendDistance = $quoteResult['sendDistance'];
} catch (\Exception $e) {
util::fail('获取配送报价失败:' . $e->getMessage());
}
工具类会自动应用以下免费配送规则:
hcFreeKm 范围内,免收配送费这些规则从 xh_shop_ext 表的 hcFreeKm 和 hcMap 字段中读取。
工具类会在以下情况抛出异常:
建议使用 try-catch 捕获异常并给用户友好的提示。
工具类会自动记录以下日志:
日志通过 Yii::error() 和 noticeUtil::push() 记录。
custom 对象包含完整的地址和经纬度信息productList 中每个商品必须包含 bigNum 和 weight 字段shansong/huolala/fengniao/shunfeng/dadasendCost 单位是元,sendDistance 单位是米