OrderValidate.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\validate;
  20. use app\common\enum\AfterSaleEnum;
  21. use app\common\enum\DeliveryEnum;
  22. use app\common\enum\OrderEnum;
  23. use app\common\enum\YesNoEnum;
  24. use app\common\model\AfterSale;
  25. use app\common\model\Order;
  26. use app\common\service\ConfigService;
  27. use app\common\validate\BaseValidate;
  28. /**
  29. * 订单验证
  30. * Class OrderValidate
  31. * @package app\shopapi\validate
  32. */
  33. class OrderValidate extends BaseValidate
  34. {
  35. protected $rule = [
  36. 'id' => 'require|checkOrder'
  37. ];
  38. protected $message = [
  39. 'id.require' => '参数缺失',
  40. ];
  41. public function sceneDetail()
  42. {
  43. return $this->only(['id']);
  44. }
  45. public function sceneCancel()
  46. {
  47. return $this->only(['id'])->append('id', 'checkCancel');
  48. }
  49. public function sceneConfirm()
  50. {
  51. return $this->only(['id'])->append('id', 'checkConfirm');
  52. }
  53. public function sceneOrderTraces()
  54. {
  55. return $this->only(['id'])->append('id','checkTraces');
  56. }
  57. public function sceneDel()
  58. {
  59. return $this->only(['id'])->append('id','checkDel');
  60. }
  61. //验证订单
  62. public function checkOrder($value, $rule, $data)
  63. {
  64. $order = (new Order())->getUserOrderById($value, $data['user_id']);
  65. if ($order->isEmpty()) {
  66. return '订单不存在';
  67. }
  68. return true;
  69. }
  70. /**
  71. * @notes 验证订单能否取消
  72. * @param $value
  73. * @param $rule
  74. * @param $data
  75. * @return bool|string
  76. * @author 段誉
  77. * @date 2021/8/2 15:48
  78. */
  79. public function checkCancel($value, $rule, $data)
  80. {
  81. $order = (new Order())->getUserOrderById($value, $data['user_id']);
  82. //已发货订单不可取消
  83. if (($order->isEmpty() || $order['order_status'] > OrderEnum::STATUS_WAIT_DELIVERY) && $order['delivery_type'] != DeliveryEnum::SELF_DELIVERY) {
  84. return '很抱歉!订单无法取消';
  85. }
  86. // 用户未支付时,允许任意取消;已支付时,在允许取消的时间内订单未发货则允许取消;
  87. if ($order['order_status'] == OrderEnum::STATUS_WAIT_DELIVERY) {
  88. $ableCancelOrder = ConfigService::get('transaction', 'cancel_unshipped_orders');
  89. if ($ableCancelOrder == YesNoEnum::NO) {
  90. return '订单不可取消';
  91. }
  92. $configTime = ConfigService::get('transaction', 'cancel_unshipped_orders_times');
  93. $ableCancelTime = strtotime($order['pay_time']) + ($configTime * 60);
  94. if (time() > $ableCancelTime) {
  95. return '订单不可取消';
  96. }
  97. }
  98. return true;
  99. }
  100. /**
  101. * @notes 验证能否确认收货
  102. * @param $value
  103. * @param $rule
  104. * @param $data
  105. * @return bool|string
  106. * @author 段誉
  107. * @date 2021/8/2 15:26
  108. */
  109. public function checkConfirm($value, $rule, $data)
  110. {
  111. $order = (new Order())->getUserOrderById($value, $data['user_id']);
  112. if ($order->isEmpty()) {
  113. return '订单不存在';
  114. }
  115. if ($order['order_status'] < OrderEnum::STATUS_WAIT_RECEIVE) {
  116. return '订单未发货';
  117. }
  118. if ($order['order_status'] == OrderEnum::STATUS_FINISH) {
  119. return '订单已完成';
  120. }
  121. return true;
  122. }
  123. /**
  124. * @notes 检查订单是否有物流信息
  125. * @param $value
  126. * @param $rule
  127. * @param $data
  128. * @return bool|string
  129. * @author ljj
  130. * @date 2021/8/13 3:56 下午
  131. */
  132. public function checkTraces($value, $rule, $data)
  133. {
  134. $order = (new Order())->getUserOrderById($value, $data['user_id']);
  135. if ($order->isEmpty()) {
  136. return '订单不存在';
  137. }
  138. if ($order['express_status'] == DeliveryEnum::NOT_SHIPPED) {
  139. return '订单未发货';
  140. }
  141. return true;
  142. }
  143. /**
  144. * @notes 检查订单是否可以删除
  145. * @param $value
  146. * @param $rule
  147. * @param $data
  148. * @return bool|string
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\DbException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. * @author ljj
  153. * @date 2021/8/31 2:36 下午
  154. */
  155. public function checkDel($value, $rule, $data)
  156. {
  157. $order = Order::where('id',$value)->find()->toArray();
  158. if ($order['order_status'] != OrderEnum::STATUS_CLOSE) {
  159. return '订单无法删除';
  160. }
  161. //如果在售后中无法删除
  162. if(AfterSale::where(['order_id'=>$value,'status'=>AfterSaleEnum::STATUS_ING])->find()){
  163. return '订单正在售后中,无法删除';
  164. }
  165. return true;
  166. }
  167. }