123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?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
- ]);
- }
- }
|