Pārlūkot izejas kodu

feat(alibaba): 添加1688快速创建订单功能并集成到店铺订单系统

- 新增 fastCreateOrder 方法实现1688快速创建订单API调用
- 新增 formatCreateOrderResult 方法格式化订单创建返回结果
- 在店铺订单仓库中集成1688运费计算逻辑
- 为商品属性值实体添加specId字段支持阿里巴巴规格ID
- 为商品实体添加isAlibaba字段标识是否为阿里商品
- 实现1688商品预览订单获取运费功能
shichen 3 mēneši atpakaļ
vecāks
revīzija
290d70684b

+ 24 - 0
app/common/repositories/store/order/StoreGroupOrderRepository.php

@@ -42,6 +42,7 @@ use app\entity\data\user\UserEntity;
42 42
 use app\entity\monomer\store\FzonePayTypeEntity;
43 43
 use app\entity\monomer\store\StoreOrderPensionJsonEntity;
44 44
 use app\entity\request\api\store\order\StoreOrderCreateRequestEntity;
45
+use app\services\ThirdParty\AlibabaAgent\OrderService;
45 46
 use think\db\exception\DbException;
46 47
 use think\exception\ValidateException;
47 48
 use think\facade\Db;
@@ -741,6 +742,29 @@ class StoreGroupOrderRepository extends BaseRepository
741 742
             //     $addressEntity->getCityId()
742 743
             // );
743 744
             $postage = 0.00; // 默认全都包邮s
745
+            // 1688商品要获取运费
746
+            if ($productEntity->getIsAlibaba() == 1) {
747
+                // 地址数据
748
+                $addressParam = [
749
+                    'fullName' => $addressEntity->getRealName(),
750
+                    'mobile' => $addressEntity->getPhone(),
751
+                    'provinceText' => $addressEntity->getProvince(),
752
+                    'cityText' => $addressEntity->getCity(),
753
+                    'areaText' => $addressEntity->getDistrict(),
754
+                    'address' => $addressEntity->getDetail(),
755
+                ];
756
+                // 商品数据
757
+                $cargoParamList = [
758
+                    'offerId' => $productEntity->getSpuId(),
759
+                    'specId' => $productAttrValueEntity->getSpecId(),
760
+                    'quantity' => $cartEntity->getCartNum()
761
+                ];
762
+                $orderService = new OrderService();
763
+                $alibabaOrder = $orderService->previewOrder(['cargoParamList' => $cargoParamList, 'addressParam' => $addressParam]);
764
+                $postage = $alibabaOrder['preview']['sumCarriage'] / 100;
765
+            }
766
+
767
+
744 768
 
745 769
             // 汇付手续费 (订单价格 + 邮费) * 汇付手续费比例
746 770
             $commissionRate = bcmul(bcadd($price, $postage, CommonEnum::DECIMAL_PLACES_OF_THE_AMOUNT), CommonEnum::HUIFU_COMMISSION_CHARGE, CommonEnum::DECIMAL_PLACES_OF_THE_AMOUNT);

+ 13 - 0
app/entity/data/store/StoreProductAttrValueEntity.php

@@ -34,6 +34,8 @@ class StoreProductAttrValueEntity extends DataEntity
34 34
     public $stockNum = null;  // 股权%
35 35
     public $sharesNum = null;  // 股票
36 36
 
37
+    public $specId = null; // 阿里巴巴规格ID
38
+
37 39
     /**
38 40
      * @return mixed
39 41
      */
@@ -519,4 +521,15 @@ class StoreProductAttrValueEntity extends DataEntity
519 521
         $this->sharesNum = $sharesNum;
520 522
         return $this;
521 523
     }
524
+
525
+    public function getSpecId()
526
+    {
527
+        return $this->specId;
528
+    }
529
+
530
+    public function setSpecId($specId): StoreProductAttrValueEntity
531
+    {
532
+        $this->specId = $specId;
533
+        return $this;
534
+    }
522 535
 }

+ 13 - 0
app/entity/data/store/StoreProductEntity.php

@@ -69,6 +69,8 @@ class StoreProductEntity extends DataEntity
69 69
     public $argument = null;  // 下架说明理由
70 70
     public $isStatus = null;  // 下架状态
71 71
 
72
+    public $isAlibaba = null; // 是否是阿里商品
73
+
72 74
     /**
73 75
      * @return mixed
74 76
      */
@@ -1184,4 +1186,15 @@ class StoreProductEntity extends DataEntity
1184 1186
         $this->isStatus = $isStatus;
1185 1187
         return $this;
1186 1188
     }
1189
+
1190
+    public function getIsAlibaba()
1191
+    {
1192
+        return $this->isAlibaba;
1193
+    }
1194
+
1195
+    public function setIsAlibaba($isAlibaba): StoreProductEntity
1196
+    {
1197
+        $this->isAlibaba = $isAlibaba;
1198
+        return $this;
1199
+    }
1187 1200
 }

+ 75 - 0
app/services/ThirdParty/AlibabaAgent/OrderService.php

@@ -147,6 +147,81 @@ class OrderService extends AlibabaAgentBaseService
147 147
     }
148 148
 
149 149
     /**
150
+     * 快速创建订单
151
+     *
152
+     * API: com.alibaba.trade:alibaba.trade.fastCreateOrder-1
153
+     *
154
+     * 接口文档: https://open.1688.com/api/apidocdetail.htm?id=com.alibaba.trade%3Aalibaba.trade.fastCreateOrder-1
155
+     *
156
+     * 请求参数:
157
+     *   - flow: 流程标识(saleproxy=分销采购, general=普通下单)
158
+     *   - cargoParamList: 商品列表JSON数组(alibaba.trade.fast.cargo[]),每个商品包含 offerId, quantity, specId
159
+     *   - addressParam: 地址参数JSON对象,包含 fullName, mobile, provinceText, cityText, areaText, address
160
+     *   - totalAmount: 总金额(单位:分,可选)
161
+     *   - message: 买家留言(可选)
162
+     *   - orderGroup: 订单分组标识(从预下单返回的 orderGroup 字段获取,可选)
163
+     *
164
+     * @param array $params 创建订单参数
165
+     * @return array|false
166
+     */
167
+    public function fastCreateOrder(array $params = [])
168
+    {
169
+        $businessParams = [];
170
+
171
+        // --- flow(流程标识) ---
172
+        $businessParams['flow'] = $params['flow'] ?? 'general';
173
+
174
+        // --- cargoParamList(商品列表,JSON字符串) ---
175
+        if (!empty($params['cargoParamList'])) {
176
+            $businessParams['cargoParamList'] = is_array($params['cargoParamList'])
177
+                ? json_encode($params['cargoParamList'], JSON_UNESCAPED_UNICODE)
178
+                : $params['cargoParamList'];
179
+        }
180
+
181
+        // --- addressParam(地址参数,JSON字符串) ---
182
+        if (!empty($params['addressParam'])) {
183
+            $businessParams['addressParam'] = is_array($params['addressParam'])
184
+                ? json_encode($params['addressParam'], JSON_UNESCAPED_UNICODE)
185
+                : $params['addressParam'];
186
+        }
187
+
188
+        // --- 调用API ---
189
+        $result = $this->executeParam2(
190
+            self::NAMESPACE,
191
+            'alibaba.trade.fastCreateOrder',
192
+            1,
193
+            $businessParams
194
+        );
195
+
196
+        if ($result === false) {
197
+            Log::channel('alibaba')->error('[创建订单] API请求失败', $businessParams);
198
+            return false;
199
+        }
200
+
201
+        return $this->formatCreateOrderResult($result);
202
+    }
203
+
204
+    /**
205
+     * 格式化创建订单返回结果
206
+     *
207
+     * @param array $result API原始返回数据
208
+     * @return array
209
+     */
210
+    protected function formatCreateOrderResult(array $result): array
211
+    {
212
+        // 创建订单成功返回: { "result": { "orderId": "123456", "totalAmount": 370 }, "success": true }
213
+        $data = $result['result'] ?? $result;
214
+        $success = $result['success'] ?? false;
215
+
216
+        return [
217
+            'success'     => $success,
218
+            'orderId'     => $data['orderId'] ?? 0,
219
+            'totalAmount' => $data['totalAmount'] ?? 0,
220
+            'rawData'     => $data,
221
+        ];
222
+    }
223
+
224
+    /**
150 225
      * 构建商品列表参数(便捷方法)
151 226
      *
152 227
      * @param array $items 商品列表 [['offerId' => 123, 'quantity' => 2, 'skuId' => 456], ...]