IntegralPlaceOrderValidate.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\shopapi\validate;
  3. use app\common\enum\IntegralGoodsEnum;
  4. use app\common\model\IntegralGoods;
  5. use app\common\model\UserAddress;
  6. use app\common\validate\BaseValidate;
  7. /**
  8. * 积分订单下单验证
  9. * Class IntegralOrderValidate
  10. * @package app\api\validate
  11. */
  12. class IntegralPlaceOrderValidate extends BaseValidate
  13. {
  14. protected $rule = [
  15. 'num' => 'require|number|gt:0',
  16. 'id' => 'require|number|checkGoods',
  17. 'address_id' => 'require|checkAddress',
  18. ];
  19. protected $message = [
  20. 'id.require' => '参数缺失',
  21. 'id.number' => '参数类型错误',
  22. 'num.require' => '请选择商品数量',
  23. 'num.number' => '商品数量参数类型错误',
  24. 'num.gt' => '请选择商品数量',
  25. 'address_id.require' => '请选择地址',
  26. ];
  27. /**
  28. * @notes 订单结算场景
  29. * @return IntegralPlaceOrderValidate
  30. * @author 段誉
  31. * @date 2022/3/31 11:35
  32. */
  33. public function sceneSettlement()
  34. {
  35. return $this->only(['id','num']);
  36. }
  37. /**
  38. * @notes 提交订单场景
  39. * @return IntegralPlaceOrderValidate
  40. * @author 段誉
  41. * @date 2022/3/31 11:36
  42. */
  43. public function sceneSubmit()
  44. {
  45. return $this->only(['id', 'num', 'address_id']);
  46. }
  47. /**
  48. * @notes 验证商品
  49. * @param $value
  50. * @param $rule
  51. * @param $data
  52. * @return bool|string
  53. * @author 段誉
  54. * @date 2022/3/31 11:37
  55. */
  56. protected function checkGoods($value, $rule, $data)
  57. {
  58. $conditon = [
  59. 'id' => $value,
  60. 'status' => IntegralGoodsEnum::STATUS_SHELVES
  61. ];
  62. $goods = IntegralGoods::where($conditon)->findOrEmpty();
  63. if ($goods->isEmpty()) {
  64. return '积分商品不存在';
  65. }
  66. if ($goods['stock'] < intval($data['num'])) {
  67. return '积分商品库存不足';
  68. }
  69. return true;
  70. }
  71. /**
  72. * @notes 验证地址信息
  73. * @param $value
  74. * @param $rule
  75. * @param $data
  76. * @return bool|string
  77. * @author 段誉
  78. * @date 2022/3/31 11:38
  79. */
  80. protected function checkAddress($value, $rule, $data)
  81. {
  82. $condition = [
  83. 'id' => (int)$value,
  84. 'user_id' => $data['user_id'],
  85. ];
  86. $address = UserAddress::where($condition)->findOrEmpty();
  87. if ($address->isEmpty()) {
  88. return '收货地址信息不存在';
  89. }
  90. return true;
  91. }
  92. }