| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- /**
- * 1688分销严选采购解决方案 - 订单服务类
- *
- * 提供预下单、下单、订单查询等功能
- *
- * @author: yourname
- * @day: 2026/04/28
- */
- namespace app\services\ThirdParty\AlibabaAgent;
- use think\facade\Db;
- use think\facade\Log;
- class OrderService extends AlibabaAgentBaseService
- {
- /**
- * API命名空间
- */
- const NAMESPACE = 'com.alibaba.trade';
- /**
- * 预下单
- *
- * API: com.alibaba.trade:alibaba.createOrder.preview-1
- *
- * 接口文档: https://open.1688.com/api/apidocdetail.htm?id=com.alibaba.trade%3Aalibaba.createOrder.preview-1
- *
- * 请求参数格式:
- * - flow: 流程标识(saleproxy=分销采购, general=普通下单)
- * - productList: 商品列表JSON数组,每个商品包含 offerId, quantity, specId
- * - addressParam: 地址参数JSON对象,包含 fullName, mobile, provinceText, cityText, areaText, address
- * - totalAmount: 总金额(单位:分,可选)
- * - message: 买家留言(可选)
- *
- * @param array $params 预下单参数
- * @return array|false
- */
- public function previewOrder(array $params = [])
- {
- $businessParams = [];
- // --- productList(商品列表,JSON字符串) ---
- // 直接传入已格式化的数组,由方法转为JSON
- if (!empty($params['cargoParamList'])) {
- $businessParams['cargoParamList'] = is_array($params['cargoParamList'])
- ? json_encode($params['cargoParamList'], JSON_UNESCAPED_UNICODE)
- : $params['cargoParamList'];
- }
- // --- addressParam(地址参数,JSON字符串) ---
- // 直接传入已格式化的数组,由方法转为JSON
- if (!empty($params['addressParam'])) {
- $businessParams['addressParam'] = is_array($params['addressParam'])
- ? json_encode($params['addressParam'], JSON_UNESCAPED_UNICODE)
- : $params['addressParam'];
- }
- // --- 调用API ---
- $result = $this->executeParam2(
- self::NAMESPACE,
- 'alibaba.createOrder.preview',
- 1,
- $businessParams
- );
- if ($result === false) {
- Log::channel('alibaba')->error('[预下单] API请求失败', $businessParams);
- return false;
- }
- return $this->formatPreviewResult($result);
- }
- /**
- * 格式化预下单返回结果
- *
- * 实际返回结构:
- * {
- * "orderPreviewResuslt": [{
- * "tradeModeNameList": ["assureTrade"],
- * "status": true,
- * "sumPayment": 370, // 总支付金额(单位:分)
- * "sumCarriage": 300, // 总运费(单位:分)
- * "sumPaymentNoCarriage": 70, // 商品总金额(不含运费,单位:分)
- * "flowFlag": "fenxiaonew",
- * "cargoList": [{ // 商品列表
- * "amount": 0.7,
- * "finalUnitPrice": 0.7,
- * "specId": "...",
- * "skuId": 6148579878582,
- * "offerId": 678279922176,
- * "openOfferId": "...",
- * "cargoPromotionList": []
- * }],
- * "tradeModelList": [{"name":"担保交易","tradeType":"assureTrade","opSupport":true}],
- * "payChannelInfos": [{"name":"alipay"}],
- * "orderGroup": "79d4f82d...",
- * "canUseOfficialSolution": false
- * }],
- * "success": true,
- * "unsupportedCrossBorderPayOfferList": []
- * }
- *
- * @param array $result API原始返回数据
- * @return array
- */
- protected function formatPreviewResult(array $result): array
- {
- $success = $result['success'] ?? false;
- $previewList = $result['orderPreviewResuslt'] ?? [];
- if (empty($previewList) || !is_array($previewList)) {
- return [
- 'success' => $success,
- 'preview' => null,
- 'list' => [],
- ];
- }
- $formattedList = [];
- foreach ($previewList as $item) {
- $formattedList[] = [
- 'sumPayment' => $item['sumPayment'] ?? 0,
- 'sumCarriage' => $item['sumCarriage'] ?? 0,
- 'sumPaymentNoCarriage' => $item['sumPaymentNoCarriage'] ?? 0,
- 'flowFlag' => $item['flowFlag'] ?? '',
- // 'orderGroup' => $item['orderGroup'] ?? '',
- // 'status' => $item['status'] ?? false,
- // 'cargoList' => $item['cargoList'] ?? [],
- // 'tradeModeNameList' => $item['tradeModeNameList'] ?? [],
- // 'tradeModelList' => $item['tradeModelList'] ?? [],
- // 'payChannelInfos' => $item['payChannelInfos'] ?? [],
- // 'shopPromotionList' => $item['shopPromotionList'] ?? [],
- // 'tradeServiceList' => $item['tradeServiceList'] ?? [],
- // 'taoSampleSinglePromotion' => $item['taoSampleSinglePromotion'] ?? false,
- // 'canUseOfficialSolution' => $item['canUseOfficialSolution'] ?? false,
- ];
- }
- return [
- // 'success' => $success,
- 'preview' => $formattedList[0] ?? null,
- // 'list' => $formattedList,
- // 'unsupportedCrossBorderPayOfferList' => $result['unsupportedCrossBorderPayOfferList'] ?? [],
- ];
- }
- /**
- * 构建商品列表参数(便捷方法)
- *
- * @param array $items 商品列表 [['offerId' => 123, 'quantity' => 2, 'skuId' => 456], ...]
- * @return array
- */
- public function buildProductList(array $items): array
- {
- $productList = [];
- foreach ($items as $item) {
- $product = [
- 'offerId' => (int)($item['offerId'] ?? $item['productId'] ?? 0),
- 'quantity' => (int)($item['quantity'] ?? 1),
- ];
- if (!empty($item['skuId'])) {
- $product['skuId'] = (int)$item['skuId'];
- }
- if (!empty($item['price'])) {
- $product['price'] = (int)$item['price']; // 单位:分
- }
- if (!empty($item['productUnit'])) {
- $product['productUnit'] = $item['productUnit'];
- }
- $productList[] = $product;
- }
- return $productList;
- }
- /**
- * 构建地址参数(便捷方法)
- *
- * @param int $addressId 地址ID
- * @return array
- */
- public function buildAddressParam(int $addressId): array
- {
- return [
- 'addressId' => $addressId,
- ];
- }
- /**
- * 构建完整地址参数(使用系统地址数据格式)
- *
- * 适配系统 user_address 表字段:
- * address_id, real_name, phone, province, city, district, detail, post_code, code_list
- *
- * @param array $address 地址信息(系统user_address表数据)
- * @return array
- */
- public function buildFullAddressParam(array $address): array
- {
- // 解析地区编码列表: "1,67449,35,493"
- $codeList = $address['code_list'] ?? '';
- $codes = explode(',', $codeList);
- $districtCode = $codes[1] ?? $codes[0] ?? '';
- return [
- 'fullName' => $address['real_name'] ?? $address['fullName'] ?? '',
- 'mobile' => $address['phone'] ?? $address['mobile'] ?? '',
- 'phone' => $address['phone'] ?? $address['phone'] ?? '',
- 'postCode' => $address['post_code'] ?? $address['postCode'] ?? '',
- 'cityText' => $address['city'] ?? $address['cityText'] ?? '',
- 'provinceText' => $address['province'] ?? $address['provinceText'] ?? '',
- 'areaText' => $address['district'] ?? $address['areaText'] ?? '',
- 'townText' => $address['village'] ?? $address['townText'] ?? '',
- 'address' => $address['detail'] ?? $address['address'] ?? '',
- 'districtCode' => $districtCode,
- ];
- }
- }
|