OrderStatusTrait.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\common\Traits;
  3. use app\common\Enum\OrderEnum;
  4. use Exception;
  5. // 移除 think\helper\Str,使用原生PHP函数
  6. use ReflectionClass;
  7. /**
  8. * Trait OrderStatusTrait
  9. * @package App\Models\Order
  10. * @method bool canCancelHandle()
  11. * @method bool canDeleteHandle()
  12. * @method bool canPayHandle()
  13. * @method bool canCommentHandle()
  14. * @method bool canConfirmHandle()
  15. * @method bool canRefundHandle()
  16. * @method bool canRebuyHandle()
  17. * @method bool canAftersaleHandle()
  18. * @method bool isCreateStatus()
  19. * @method bool isPayStatus()
  20. * @method bool isShipStatus()
  21. * @method bool isConfirmStatus()
  22. * @method bool isCancelStatus()
  23. * @method bool isAutoCancelStatus()
  24. * @method bool isRefundStatus()
  25. * @method bool isRefundConfirmStatus()
  26. * @method bool isAutoConfirmStatus()
  27. */
  28. trait OrderStatusTrait
  29. {
  30. private $canHandleMap = [
  31. // 取消操作
  32. 'cancel' => [
  33. OrderEnum::STATUS_CREATE
  34. ],
  35. // 删除操作
  36. 'delete' => [
  37. OrderEnum::STATUS_CANCEL,
  38. OrderEnum::STATUS_AUTO_CANCEL,
  39. OrderEnum::STATUS_ADMIN_CANCEL,
  40. OrderEnum::STATUS_REFUND_CONFIRM,
  41. OrderEnum::STATUS_CONFIRM,
  42. OrderEnum::STATUS_AUTO_CONFIRM
  43. ],
  44. // 支付操作
  45. 'pay' => [
  46. OrderEnum::STATUS_CREATE
  47. ],
  48. // 发货
  49. 'ship' => [
  50. OrderEnum::STATUS_PAY
  51. ],
  52. // 评论操作
  53. 'comment' => [
  54. OrderEnum::STATUS_CONFIRM,
  55. OrderEnum::STATUS_AUTO_CONFIRM
  56. ],
  57. // 确认收货操作
  58. 'confirm' => [OrderEnum::STATUS_SHIP],
  59. // 取消订单并退款操作
  60. 'refund' => [OrderEnum::STATUS_PAY],
  61. // 再次购买
  62. 'rebuy' => [
  63. OrderEnum::STATUS_CONFIRM,
  64. OrderEnum::STATUS_AUTO_CONFIRM
  65. ],
  66. // 售后操作
  67. 'aftersale' => [
  68. OrderEnum::STATUS_CONFIRM,
  69. OrderEnum::STATUS_AUTO_CONFIRM
  70. ],
  71. // 同意退款
  72. 'agreerefund' => [
  73. OrderEnum::STATUS_REFUND
  74. ],
  75. ];
  76. public function __call($name, $arguments)
  77. {
  78. // 检查是否是can*Handle格式的方法
  79. $canHandleMatches = [];
  80. if (preg_match('/^can(.+)Handle$/', $name, $canHandleMatches)) {
  81. if (is_null($this->order_status)) {
  82. throw new Exception("order status is null when call method[$name]!");
  83. }
  84. $key = strtolower($canHandleMatches[1]);
  85. return in_array($this->order_status, $this->canHandleMap[$key] ?? []);
  86. }
  87. // 检查是否是is*Status格式的方法
  88. $statusMatches = [];
  89. if (preg_match('/^is(.+)Status$/', $name, $statusMatches)) {
  90. if (is_null($this->order_status)) {
  91. throw new Exception("order status is null when call method[$name]!");
  92. }
  93. // 将驼峰转为下划线并转大写,然后加上STATUS_前缀
  94. $statusName = strtoupper(preg_replace('/([a-z])([A-Z])/', '$1_$2', $statusMatches[1]));
  95. $constantName = 'STATUS_' . $statusName;
  96. $status = (new ReflectionClass(OrderEnum::class))->getConstant($constantName);
  97. return $this->order_status == $status;
  98. }
  99. return parent::__call($name, $arguments);
  100. }
  101. public function getCanHandelOptions()
  102. {
  103. return [
  104. 'cancel' => $this->canCancelHandle(),
  105. 'delete' => $this->canDeleteHandle(),
  106. 'pay' => $this->canPayHandle(),
  107. 'comment' => $this->canCommentHandle(),
  108. 'confirm' => $this->canConfirmHandle(),
  109. 'refund' => $this->canRefundHandle(),
  110. 'rebuy' => $this->canRebuyHandle(),
  111. 'aftersale' => $this->canAftersaleHandle(),
  112. ];
  113. }
  114. public function isHadPaid()
  115. {
  116. return !in_array($this->order_status, [
  117. OrderEnum::STATUS_CREATE,
  118. OrderEnum::STATUS_ADMIN_CANCEL,
  119. OrderEnum::STATUS_AUTO_CANCEL,
  120. OrderEnum::STATUS_CANCEL
  121. ]);
  122. }
  123. }