CartValidate.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\logic\CommonPresellLogic;
  21. use app\common\model\Cart;
  22. use app\common\model\Goods;
  23. use app\common\validate\BaseValidate;
  24. /**
  25. * 购物车验证
  26. * Class CartValidate
  27. * @package app\shopapi\validate
  28. */
  29. class CartValidate extends BaseValidate
  30. {
  31. protected $rule = [
  32. 'cart_id' => 'require|checkCart',
  33. 'cart_ids' => 'require|array',
  34. 'goods_num' => 'require|integer|gt:0',
  35. 'item_id' => 'require|checkGoods',
  36. 'selected' => 'require|in:0,1',
  37. ];
  38. protected $message = [
  39. 'item_id' => '请选择商品',
  40. 'goods_num.require' => '商品数量不能为0',
  41. 'goods_num.gt' => '商品数量需大于0',
  42. 'goods_num.integer' => '商品数量需为整数',
  43. 'cart_id.require' => '参数错误',
  44. 'selected.require' => '参数错误',
  45. 'selected.in' => '参数错误',
  46. ];
  47. protected $scene = [
  48. 'del' => ['cart_id'],
  49. 'delIds' => ['cart_ids'],
  50. 'editSku' => [ 'cart_id', 'item_id', 'goods_num' ],
  51. 'add' => ['item_id', 'goods_num'],
  52. 'selected' => ['cart_id', 'selected'],
  53. 'change' => ['cart_id', 'goods_num'],
  54. ];
  55. /**
  56. * @notes 验证购物车是否存在
  57. * @param $value
  58. * @param $rule
  59. * @param $data
  60. * @return bool|string
  61. * @author 段誉
  62. * @date 2021/7/16 18:32
  63. */
  64. protected function checkCart($value, $rule, $data)
  65. {
  66. $cart = (new Cart())->getCartById($value, $data['user_id']);
  67. request()->_api_validate_cart_detail = $cart;
  68. if ($cart->isEmpty()) {
  69. return '购物车不存在';
  70. }
  71. return true;
  72. }
  73. /**
  74. * @notes 验证商品
  75. * @param $value
  76. * @param $rule
  77. * @param $data
  78. * @return bool|string
  79. * @author 段誉
  80. * @date 2021/7/16 18:31
  81. */
  82. protected function checkGoods($value, $rule, $data)
  83. {
  84. $goods = (new Goods())->alias('g')
  85. ->field(['g.status', 'g.delete_time', 'gi.stock', 'g.id as goods_id'])
  86. ->join('goods_item gi', 'gi.goods_id = g.id')
  87. ->where(['gi.id' => $value])
  88. ->find();
  89. if (empty($goods) || $goods['delete_time'] > 0) {
  90. return '找不到商品';
  91. }
  92. if ($this->currentScene == 'editSku') {
  93. $cart = request()->_api_validate_cart_detail;
  94. if ($cart['goods_id'] != $goods['goods_id']) {
  95. return '找不到商品';
  96. }
  97. }
  98. if ($goods['status'] == 0) {
  99. return '商品不能购买';
  100. }
  101. if (CommonPresellLogic::checkGoodsHas($goods['id'])) {
  102. return '预售活动商品不能加入购物车';
  103. }
  104. $cart = (new Cart())->getCartByItem($data['item_id'], $data['user_id']);
  105. $cartNum = $data['goods_num'] + $cart['goods_num'] ?? 0;
  106. if ($goods['stock'] < $cartNum) {
  107. return '商品库存不足';
  108. }
  109. return true;
  110. }
  111. }