orderModel = new Order; $this->expressModel = new OrderExpress; } /** * 发货操作 * * @param string $action 发货行为(默认:confirm=确认发货, cancel=取消发货, change=修改发货信息) * @param int $order_id 订单id * @param int $order_express_id 发货单id * @param string $method 发货方式(input=手动发货, api=推送运单, upload=上传发货单) * @param array $express 物流信息 * * @return \think\Response */ public function dispatch() { $params = $this->request->param(); $action = $params['action'] ?? 'confirm'; if (!in_array($action, ['confirm', 'cancel', 'change'])) { $this->error('发货参数错误'); } $orderId = $params['order_id'] ?? 0; if (!$orderId) { $this->error('订单ID不能为空'); } $order = $this->orderModel->where([ 'id' => $orderId, 'uniacid' => UNIACID ])->find(); if (!$order) { $this->error('未找到订单'); } if ($order['order_type'] != 'physical') { $this->error('该订单不是实物商品订单'); } switch ($action) { case 'confirm': $result = $this->confirmDispatch($order, $params); $this->success('发货成功', $result); break; case 'cancel': $result = $this->cancelDispatch($order, $params); $this->success('取消发货成功'); break; case 'change': $result = $this->changeDispatch($order, $params); $this->success('修改成功', $result); break; } $this->error('操作失败'); } /** * 确认发货 */ private function confirmDispatch($order, $params) { $method = $params['method'] ?? 'input'; if (!in_array($method, ['input', 'api', 'upload'])) { $this->error('请使用正确的发货方式'); } $orderStatus = \app\common\constant\order\Status::STATUS_UNSEND; if ($order['status'] != $orderStatus) { $this->error("该订单状态不允许发货"); } $express = $params['express'] ?? null; if (empty($express['name']) || empty($express['code']) || empty($express['no'])) { $this->error('请输入正确的快递信息'); } // 获取收货地址信息 $address = OrderAddress::where([ 'order_id' => $order['id'], 'uniacid' => UNIACID ])->find(); Db::startTrans(); try { $orderExpress = $this->expressModel->create([ 'uniacid' => UNIACID, 'user_id' => $order['user_id'], 'order_id' => $order['id'], 'method' => $method, 'driver' => $express['driver'] ?? null, 'express_name' => $express['name'], 'express_code' => $express['code'], 'express_no' => $express['no'], 'sender_mobile' => $address ? $address['mobile'] : '', 'status' => 'transport', 'ext' => isset($express['ext']) ? json_encode($express['ext']) : null, 'createtime' => time(), 'updatetime' => time() ]); $order->status = \app\common\constant\order\Status::STATUS_UNRECEIVE; $order->save(); \app\common\model\order\Log::set($order['order_no'], "订单已发货,快递公司:{$express['name']},运单号:{$express['no']}"); Db::commit(); $this->syncExpressInfo($orderExpress, $order); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } return $express; } /** * 取消发货 */ private function cancelDispatch($order, $params) { $orderExpressId = $params['order_express_id'] ?? 0; if (!$orderExpressId) { $this->error('发货单ID不能为空'); } $orderExpress = $this->expressModel->where([ 'id' => $orderExpressId, 'order_id' => $order['id'], 'uniacid' => UNIACID ])->find(); if (!$orderExpress) { $this->error('未找到发货单'); } Db::startTrans(); try { $orderExpress->delete(); $order->status = \app\common\constant\order\Status::STATUS_UNSEND; $order->save(); \app\common\model\order\Log::set($order['order_no'], "已取消发货"); Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } return true; } /** * 修改发货信息 */ private function changeDispatch($order, $params) { $orderExpressId = $params['order_express_id'] ?? 0; if (!$orderExpressId) { $this->error('发货单ID不能为空'); } $orderExpress = $this->expressModel->where([ 'id' => $orderExpressId, 'order_id' => $order['id'], 'uniacid' => UNIACID ])->find(); if (!$orderExpress) { $this->error('未找到发货单'); } $express = $params['express'] ?? null; if (empty($express['name']) || empty($express['code']) || empty($express['no'])) { $this->error('请输入正确的快递信息'); } Db::startTrans(); try { $orderExpress->save([ 'express_name' => $express['name'], 'express_code' => $express['code'], 'express_no' => $express['no'], 'sender_mobile' => $express['sender_mobile'] ?? '', 'method' => 'input', 'status' => 'noinfo', 'updatetime' => time() ]); // 删除旧的物流轨迹记录 \app\common\model\app\physical\OrderExpressLog::where([ 'order_express_id' => $orderExpress['id'], 'uniacid' => UNIACID ])->delete(); \app\common\model\order\Log::set($order['order_no'], "修改发货信息,快递公司:{$express['name']},运单号:{$express['no']}"); Db::commit(); $this->syncExpressInfo($orderExpress, $order); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } return $express; } /** * 查询物流信息 */ public function query() { $orderExpressId = $this->request->param('order_express_id', 0); if (!$orderExpressId) { $this->error('发货单ID不能为空'); } $orderExpress = $this->expressModel->where([ 'id' => $orderExpressId, 'uniacid' => UNIACID ])->find(); if (!$orderExpress) { $this->error('未找到发货单'); } $expressLib = new Express(); $result = $expressLib->search([ 'order_id' => $orderExpress['order_id'], 'express_code' => $orderExpress['express_code'], 'express_no' => $orderExpress['express_no'] ], $orderExpress); $this->success('查询成功', $result); } /** * 刷新物流信息 */ public function refresh() { $orderExpressId = $this->request->param('order_express_id', 0); if (!$orderExpressId) { $this->error('发货单ID不能为空'); } $orderExpress = $this->expressModel->where([ 'id' => $orderExpressId, 'uniacid' => UNIACID ])->find(); if (!$orderExpress) { $this->error('未找到发货单'); } $order = $this->orderModel->where([ 'id' => $orderExpress['order_id'], 'uniacid' => UNIACID ])->find(); if (!$order) { $this->error('未找到订单'); } try { $this->syncExpressInfo($orderExpress, $order); $orderExpress = $this->expressModel->where([ 'id' => $orderExpressId, 'uniacid' => UNIACID ])->find(); } catch (\Exception $e) { $this->error('刷新失败:' . $e->getMessage()); } $this->success('刷新成功', $orderExpress); } /** * 同步物流信息 */ private function syncExpressInfo($orderExpress, $order) { try { $physicalConfig = \app\common\model\app\Config::getConfig('physical'); $aliyunConfig = []; $aliyunConfig['appcode'] = $physicalConfig['express_app_code'] ?? ''; $expressLib = new Express('aliyun', $aliyunConfig); // 使用 Express 库的更新方法 $expressLib->updateExpress($orderExpress); } catch (\Exception $e) { \think\Log::error('syncExpressInfo.Exception: ' . $e->getMessage()); } } /** * 根据订单号获取收货信息 */ public function getAddress() { $orderNo = $this->request->param('order_no', ''); if (empty($orderNo)) { $this->error('订单号不能为空'); } $order = $this->orderModel->where([ 'order_no' => $orderNo, 'uniacid' => UNIACID ])->find(); if (!$order) { $this->error('未找到订单'); } if ($order['order_type'] != 'physical') { $this->error('该订单不是实物商品订单'); } $address = OrderAddress::where([ 'order_id' => $order['id'], 'uniacid' => UNIACID ])->find(); if (!$address) { $this->error('未找到收货地址'); } $this->success('获取成功', $address); } /** * 根据订单号编辑收货信息 */ public function editAddress() { $orderNo = $this->request->param('order_no', ''); if (empty($orderNo)) { $this->error('订单号不能为空'); } $order = $this->orderModel->where([ 'order_no' => $orderNo, 'uniacid' => UNIACID ])->find(); if (!$order) { $this->error('未找到订单'); } if ($order['order_type'] != 'physical') { $this->error('该订单不是实物商品订单'); } $orderStatus = \app\common\constant\order\Status::STATUS_UNSEND; if ($order['status'] != $orderStatus) { $this->error('该订单状态不允许修改收货信息'); } $consignee = $this->request->param('consignee', ''); $mobile = $this->request->param('mobile', ''); $province = $this->request->param('province', ''); $city = $this->request->param('city', ''); $district = $this->request->param('district', ''); $address = $this->request->param('address', ''); if (empty($consignee)) { $this->error('收货人不能为空'); } if (empty($mobile)) { $this->error('联系电话不能为空'); } if (empty($address)) { $this->error('详细地址不能为空'); } $orderAddress = OrderAddress::where([ 'order_id' => $order['id'], 'uniacid' => UNIACID ])->find(); Db::startTrans(); try { if ($orderAddress) { $orderAddress->save([ 'consignee' => $consignee, 'mobile' => $mobile, 'province_name' => $province, 'city_name' => $city, 'district_name' => $district, 'address' => $address, 'updatetime' => time() ]); } else { OrderAddress::create([ 'uniacid' => UNIACID, 'order_id' => $order['id'], 'user_id' => $order['user_id'], 'consignee' => $consignee, 'mobile' => $mobile, 'province_name' => $province, 'city_name' => $city, 'district_name' => $district, 'address' => $address, 'createtime' => time(), 'updatetime' => time() ]); } \app\common\model\order\Log::set($order['order_no'], "修改收货信息:{$consignee},{$mobile},{$province}{$city}{$district}{$address}"); Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error('修改失败:' . $e->getMessage()); } $this->success('修改成功'); } }