|
@@ -3,11 +3,17 @@
|
|
|
namespace app\api\controller\inspection;
|
|
|
|
|
|
use app\api\controller\inspection\Base;
|
|
|
+use app\common\Enum\PayEnum;
|
|
|
use app\common\Service\OrderService;
|
|
|
use app\common\Enum\OrderEnum;
|
|
|
use app\common\Service\OrderActionService;
|
|
|
use app\common\Enum\OrderActionEnum;
|
|
|
+use app\common\Enum\OrderGoodsEnum;
|
|
|
+use app\common\model\OrderGoods;
|
|
|
use app\common\Service\Order\OrderShipService;
|
|
|
+use think\Db;
|
|
|
+use app\common\library\easywechatPlus\WechatMiniProgramShop;
|
|
|
+use app\common\facade\Wechat;
|
|
|
/**
|
|
|
* 验货端订单接口
|
|
|
*
|
|
@@ -96,70 +102,138 @@ class Order extends Base
|
|
|
//确认发货
|
|
|
public function send()
|
|
|
{
|
|
|
- // 验证请求参数
|
|
|
- $userId = 0;
|
|
|
- $validate = new \app\api\validate\inspection\Order();
|
|
|
- $params = [
|
|
|
- 'order_id' => $this->request->post('order_id'),
|
|
|
- 'order_goods_id' => $this->request->post('order_goods_id'),
|
|
|
- 'express_name' => $this->request->post('express_name'),
|
|
|
- 'express_no' => $this->request->post('express_no'),
|
|
|
- 'express_image' => $this->request->post('express_image/a', [])
|
|
|
- ];
|
|
|
- if (!$validate->scene('send')->check($params)) {
|
|
|
- $this->error($validate->getError());
|
|
|
- }
|
|
|
-
|
|
|
- $orderId = intval($params['order_id'] ?? 0);
|
|
|
- $orderGoodsId = intval($params['order_goods_id'] ?? 0);
|
|
|
-
|
|
|
- if (empty($orderId)) {
|
|
|
- $this->error('订单ID不能为空');
|
|
|
- }
|
|
|
-
|
|
|
- if (empty($orderGoodsId)) {
|
|
|
- $this->error('订单商品ID不能为空');
|
|
|
- }
|
|
|
- // 验证订单商品是否可以发货
|
|
|
- $orderData = OrderService::validateOrderGoodsForDelivery($orderId, $orderGoodsId);
|
|
|
- if (!$orderData) {
|
|
|
- $this->error('订单或订单商品不存在,或不允许发货');
|
|
|
- }
|
|
|
-
|
|
|
- $order = $orderData['order'];
|
|
|
- $orderGoods = $orderData['order_goods'];
|
|
|
-
|
|
|
- $expressName = trim($params['express_name'] ?? '');
|
|
|
- $expressNo = trim($params['express_no'] ?? '');
|
|
|
- $expressImage = $params['express_image'] ?? [];
|
|
|
-
|
|
|
- // 事务处理
|
|
|
- // 1. 更新订单商品发货状态
|
|
|
- $result = OrderService::updateOrderGoodsDeliveryStatus($orderGoodsId, $expressName, $expressNo, $expressImage);
|
|
|
- if (!$result) {
|
|
|
- $this->error('更新订单商品发货状态失败');
|
|
|
- }
|
|
|
-
|
|
|
- // 2. 检查并更新订单状态
|
|
|
- OrderService::updateOrderStatusByDeliveryResult($orderId);
|
|
|
-
|
|
|
- // 3. 记录操作日志
|
|
|
- $orderActionService = new OrderActionService();
|
|
|
- $orderActionService->addInspectionAction(
|
|
|
- $order->order_sn,
|
|
|
- OrderActionEnum::ACTION_SHIP,
|
|
|
- $userId,
|
|
|
- '订单商品发货,商品:' . $orderGoods->goods_title . ',快递公司:' . $expressName . ',快递单号:' . $expressNo,
|
|
|
- $userId,
|
|
|
- [
|
|
|
- 'order_goods_id' => $orderGoodsId,
|
|
|
- 'goods_title' => $orderGoods->goods_title,
|
|
|
- 'express_name' => $expressName,
|
|
|
- 'express_no' => $expressNo,
|
|
|
- ]
|
|
|
- );
|
|
|
-
|
|
|
- $this->success('发货成功');
|
|
|
+ // 验证请求参数
|
|
|
+ $userId = 0;
|
|
|
+ $validate = new \app\api\validate\inspection\Order();
|
|
|
+ $params = [
|
|
|
+ 'order_id' => $this->request->post('order_id'),
|
|
|
+ 'order_goods_id' => $this->request->post('order_goods_id'),
|
|
|
+ 'express_id' => $this->request->post('express_id'),
|
|
|
+ 'express_no' => $this->request->post('express_no'),
|
|
|
+ 'express_image' => $this->request->post('express_image/a', [])
|
|
|
+ ];
|
|
|
+ if (!$validate->scene('send')->check($params)) {
|
|
|
+ $this->error($validate->getError());
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据ID 查询快递公司
|
|
|
+ $expressId = intval($params['express_id'] ?? 0);
|
|
|
+ $express = OrderShipService::getExpressById($expressId);
|
|
|
+ if (empty($express)) {
|
|
|
+ $this->error('快递公司不存在');
|
|
|
+ }
|
|
|
+ $orderId = intval($params['order_id'] ?? 0);
|
|
|
+ $orderGoodsId = intval($params['order_goods_id'] ?? 0);
|
|
|
+
|
|
|
+ if (empty($orderId)) {
|
|
|
+ $this->error('订单ID不能为空');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($orderGoodsId)) {
|
|
|
+ $this->error('订单商品ID不能为空');
|
|
|
+ }
|
|
|
+ // 验证订单商品是否可以发货
|
|
|
+ $orderData = OrderService::validateOrderGoodsForDelivery($orderId, $orderGoodsId);
|
|
|
+ if (!$orderData) {
|
|
|
+ $this->error('订单或订单商品不存在,或不允许发货');
|
|
|
+ }
|
|
|
+
|
|
|
+ $order = $orderData['order'];
|
|
|
+ $orderGoods = $orderData['order_goods'];
|
|
|
+ $expressName = $express->name;
|
|
|
+ $expressCode = $express->code;
|
|
|
+ $expressNo = trim($params['express_no'] ?? '');
|
|
|
+ $expressImage = $params['express_image'] ?? [];
|
|
|
+ // 事务处理
|
|
|
+ Db::startTrans();
|
|
|
+ try{
|
|
|
+ // 创建包裹
|
|
|
+ $orderExpress = OrderShipService::createOrderExpress($orderId, $order['user_id'], $expressName, $expressCode, $expressNo, 'input', '', [], $expressImage);
|
|
|
+ if (!$orderExpress) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error('创建包裹失败');
|
|
|
+ }
|
|
|
+ // 1. 更新订单商品发货状态
|
|
|
+ $result = OrderService::updateOrderGoodsDeliveryStatus($orderGoodsId, $expressName, $expressNo, $expressImage,$orderExpress->id);
|
|
|
+ if (!$result) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error('更新订单商品发货状态失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 检查并更新订单状态
|
|
|
+ OrderService::updateOrderStatusByDeliveryResult($orderId);
|
|
|
+
|
|
|
+ // 3. 记录操作日志
|
|
|
+ $orderActionService = new OrderActionService();
|
|
|
+ $orderActionService->addInspectionAction(
|
|
|
+ $order->order_sn,
|
|
|
+ OrderActionEnum::ACTION_SHIP,
|
|
|
+ $userId,
|
|
|
+ '订单商品发货,商品:' . $orderGoods->goods_title . ',快递公司:' . $expressName . ',快递单号:' . $expressNo,
|
|
|
+ $userId,
|
|
|
+ [
|
|
|
+ 'order_goods_id' => $orderGoodsId,
|
|
|
+ 'goods_title' => $orderGoods->goods_title,
|
|
|
+ 'express_name' => $expressName,
|
|
|
+ 'express_no' => $expressNo,
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ // 提交事务
|
|
|
+ Db::commit();
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ // 回滚事务
|
|
|
+ Db::rollback();
|
|
|
+ $this->error('发货失败'.$e->getMessage());
|
|
|
+ }
|
|
|
+ // 4. 同步微信小程序订单发货状态 todo 队列处理
|
|
|
+ $data = [
|
|
|
+ 'order' => $order,
|
|
|
+ 'items' => $orderGoods,
|
|
|
+ 'express' => $orderExpress,
|
|
|
+ 'delivery_type' => 'express',
|
|
|
+ ];
|
|
|
+ $this->syncWechatMiniProgramOrderDispatch($data,'send');
|
|
|
+ $this->success('发货成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ private function syncWechatMiniProgramOrderDispatch($params =[],$type='send'){
|
|
|
+
|
|
|
+ $order = $params['order'];
|
|
|
+ $items = $params['items'];
|
|
|
+ $express = $params['express'];
|
|
|
+ $dispatch_type = $params['delivery_type'];
|
|
|
+ $uploadshoppingInfo = new WechatMiniProgramShop(Wechat::miniProgram());
|
|
|
+ // 微信小程序,并且存在微信发货管理权限时,才推送发货消息
|
|
|
+ if ($order['platform'] == 'WechatMiniProgram' && $uploadshoppingInfo->isTradeManaged()) {
|
|
|
+ $hasNosend = OrderGoods::where('order_id', $order['id'])
|
|
|
+ // ->where('refund_status', OrderGoodsEnum::REFUND_STATUS_NOREFUND)
|
|
|
+ ->where('delivery_status', OrderGoodsEnum::DELIVERY_STATUS_NOSEND)->count();
|
|
|
+ if ($type == 'send') {
|
|
|
+ // if (!$hasNosend && in_array('wechat', $order->pay_type)) {
|
|
|
+ if (!$hasNosend && $order->pay_type == PayEnum::METHOD_WECHAT) {
|
|
|
+ // 所有 items 都已经发货,将交易信息上传微信
|
|
|
+ $uploadshoppingInfo->uploadShippingInfos($order);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 修改物流单
|
|
|
+ // if (!$hasNosend && in_array('wechat', $order->pay_types)) {
|
|
|
+ if (!$hasNosend && $order->pay_type == PayEnum::METHOD_WECHAT) {
|
|
|
+ // 所有 items 都已经发货,将交易信息上传微信
|
|
|
+ $uploadshoppingInfo->uploadShippingInfos($order, $express, 'change');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加自动确认收货队列,这个队列只自动确认 本次发货的 items
|
|
|
+ // $confirm_days = Config::getConfigField('shop.order.auto_confirm');
|
|
|
+ // $confirm_days = $confirm_days > 0 ? $confirm_days : 0;
|
|
|
+ // if ($confirm_days) {
|
|
|
+ // // 小于等于0, 不自动确认收货
|
|
|
+ // \think\Queue::later(($confirm_days * 86400), '\addons\shopro\job\OrderAutoOper@autoConfirm', $params, 'shopro');
|
|
|
+ // }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
// 统计订单
|