|
@@ -0,0 +1,131 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+namespace app\common\Traits;
|
|
|
|
+
|
|
|
|
+use app\common\Enum\OrderEnum;
|
|
|
|
+use Exception;
|
|
|
|
+// 移除 think\helper\Str,使用原生PHP函数
|
|
|
|
+use ReflectionClass;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Trait OrderStatusTrait
|
|
|
|
+ * @package App\Models\Order
|
|
|
|
+ * @method bool canCancelHandle()
|
|
|
|
+ * @method bool canDeleteHandle()
|
|
|
|
+ * @method bool canPayHandle()
|
|
|
|
+ * @method bool canCommentHandle()
|
|
|
|
+ * @method bool canConfirmHandle()
|
|
|
|
+ * @method bool canRefundHandle()
|
|
|
|
+ * @method bool canRebuyHandle()
|
|
|
|
+ * @method bool canAftersaleHandle()
|
|
|
|
+ * @method bool isCreateStatus()
|
|
|
|
+ * @method bool isPayStatus()
|
|
|
|
+ * @method bool isShipStatus()
|
|
|
|
+ * @method bool isConfirmStatus()
|
|
|
|
+ * @method bool isCancelStatus()
|
|
|
|
+ * @method bool isAutoCancelStatus()
|
|
|
|
+ * @method bool isRefundStatus()
|
|
|
|
+ * @method bool isRefundConfirmStatus()
|
|
|
|
+ * @method bool isAutoConfirmStatus()
|
|
|
|
+ */
|
|
|
|
+trait OrderStatusTrait
|
|
|
|
+{
|
|
|
|
+ private $canHandleMap = [
|
|
|
|
+ // 取消操作
|
|
|
|
+ 'cancel' => [
|
|
|
|
+ OrderEnum::STATUS_CREATE
|
|
|
|
+ ],
|
|
|
|
+ // 删除操作
|
|
|
|
+ 'delete' => [
|
|
|
|
+ OrderEnum::STATUS_CANCEL,
|
|
|
|
+ OrderEnum::STATUS_AUTO_CANCEL,
|
|
|
|
+ OrderEnum::STATUS_ADMIN_CANCEL,
|
|
|
|
+ OrderEnum::STATUS_REFUND_CONFIRM,
|
|
|
|
+ OrderEnum::STATUS_CONFIRM,
|
|
|
|
+ OrderEnum::STATUS_AUTO_CONFIRM
|
|
|
|
+ ],
|
|
|
|
+ // 支付操作
|
|
|
|
+ 'pay' => [
|
|
|
|
+ OrderEnum::STATUS_CREATE
|
|
|
|
+ ],
|
|
|
|
+ // 发货
|
|
|
|
+ 'ship' => [
|
|
|
|
+ OrderEnum::STATUS_PAY
|
|
|
|
+ ],
|
|
|
|
+ // 评论操作
|
|
|
|
+ 'comment' => [
|
|
|
|
+ OrderEnum::STATUS_CONFIRM,
|
|
|
|
+ OrderEnum::STATUS_AUTO_CONFIRM
|
|
|
|
+ ],
|
|
|
|
+ // 确认收货操作
|
|
|
|
+ 'confirm' => [OrderEnum::STATUS_SHIP],
|
|
|
|
+ // 取消订单并退款操作
|
|
|
|
+ 'refund' => [OrderEnum::STATUS_PAY],
|
|
|
|
+ // 再次购买
|
|
|
|
+ 'rebuy' => [
|
|
|
|
+ OrderEnum::STATUS_CONFIRM,
|
|
|
|
+ OrderEnum::STATUS_AUTO_CONFIRM
|
|
|
|
+ ],
|
|
|
|
+ // 售后操作
|
|
|
|
+ 'aftersale' => [
|
|
|
|
+ OrderEnum::STATUS_CONFIRM,
|
|
|
|
+ OrderEnum::STATUS_AUTO_CONFIRM
|
|
|
|
+ ],
|
|
|
|
+ // 同意退款
|
|
|
|
+ 'agreerefund' => [
|
|
|
|
+ OrderEnum::STATUS_REFUND
|
|
|
|
+ ],
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ public function __call($name, $arguments)
|
|
|
|
+ {
|
|
|
|
+ // 检查是否是can*Handle格式的方法
|
|
|
|
+ $canHandleMatches = [];
|
|
|
|
+ if (preg_match('/^can(.+)Handle$/', $name, $canHandleMatches)) {
|
|
|
|
+ if (is_null($this->order_status)) {
|
|
|
|
+ throw new Exception("order status is null when call method[$name]!");
|
|
|
|
+ }
|
|
|
|
+ $key = strtolower($canHandleMatches[1]);
|
|
|
|
+ return in_array($this->order_status, $this->canHandleMap[$key] ?? []);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 检查是否是is*Status格式的方法
|
|
|
|
+ $statusMatches = [];
|
|
|
|
+ if (preg_match('/^is(.+)Status$/', $name, $statusMatches)) {
|
|
|
|
+ if (is_null($this->order_status)) {
|
|
|
|
+ throw new Exception("order status is null when call method[$name]!");
|
|
|
|
+ }
|
|
|
|
+ // 将驼峰转为下划线并转大写,然后加上STATUS_前缀
|
|
|
|
+ $statusName = strtoupper(preg_replace('/([a-z])([A-Z])/', '$1_$2', $statusMatches[1]));
|
|
|
|
+ $constantName = 'STATUS_' . $statusName;
|
|
|
|
+ $status = (new ReflectionClass(OrderEnum::class))->getConstant($constantName);
|
|
|
|
+ return $this->order_status == $status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return parent::__call($name, $arguments);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function getCanHandelOptions()
|
|
|
|
+ {
|
|
|
|
+ return [
|
|
|
|
+ 'cancel' => $this->canCancelHandle(),
|
|
|
|
+ 'delete' => $this->canDeleteHandle(),
|
|
|
|
+ 'pay' => $this->canPayHandle(),
|
|
|
|
+ 'comment' => $this->canCommentHandle(),
|
|
|
|
+ 'confirm' => $this->canConfirmHandle(),
|
|
|
|
+ 'refund' => $this->canRefundHandle(),
|
|
|
|
+ 'rebuy' => $this->canRebuyHandle(),
|
|
|
|
+ 'aftersale' => $this->canAftersaleHandle(),
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function isHadPaid()
|
|
|
|
+ {
|
|
|
|
+ return !in_array($this->order_status, [
|
|
|
|
+ OrderEnum::STATUS_CREATE,
|
|
|
|
+ OrderEnum::STATUS_ADMIN_CANCEL,
|
|
|
|
+ OrderEnum::STATUS_AUTO_CANCEL,
|
|
|
|
+ OrderEnum::STATUS_CANCEL
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+}
|