CartValidate.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\validate\merchant;
  12. use think\Validate;
  13. class CartValidate extends Validate
  14. {
  15. protected $rule = [
  16. 'uid|用户id' => 'require|number',
  17. 'product_id|商品id' => 'require|number',
  18. 'cart_num|数量' => 'require|number|gt:0',
  19. 'product_attr_unique|规格编码' => 'max:32',
  20. 'cart_ids' => 'require|array',
  21. 'type|类型' => 'require|number|between:0,2',
  22. 'old_price|应付金额' => 'require|float|egt:0|elt:999999.99',
  23. 'new_price|一口价金额' => 'float|egt:0|elt:999999.99|requireIf:type,0',
  24. 'reduce_price|减价金额' => 'float|egt:0|elt:999999.99|requireIf:type,1|requireIf:change_fee_type,1',
  25. 'discount_rate|折扣率' => 'number|egt:0|elt:100|requireIf:type,2|requireIf:change_fee_type,2',
  26. 'change_fee_type|批量改价类型' => 'require|number|between:0,2',
  27. 'old_pay_price|应付金额' => 'require|float|egt:0|elt:999999.99',
  28. 'new_pay_price|一口价金额' => 'float|egt:0|elt:999999.99|requireIf:change_fee_type,0'
  29. ];
  30. public $message = [
  31. 'discount_rate.number' => '折扣率必须是整数'
  32. ];
  33. protected $scene = [
  34. 'create' => ['uid', 'product_id', 'cart_num', 'product_attr_unique'],
  35. 'change' => ['uid', 'cart_num', 'product_attr_unique'],
  36. 'list' => ['uid'],
  37. 'updatePrice' => ['type', 'old_price', 'new_price', 'reduce_price', 'discount_rate'],
  38. 'batchUpdatePrice' => ['uid', 'change_fee_type', 'old_pay_price', 'new_pay_price', 'reduce_price', 'discount_rate', 'cart_ids']
  39. ];
  40. public function createCheck(array $data): bool
  41. {
  42. if (!$this->scene('create')->check($data)) {
  43. return false;
  44. }
  45. if($data['uid'] == 0 && empty($data['tourist_unique_key'])) {
  46. $this->error = '请传入游客唯一标识';
  47. return false;
  48. }
  49. return true;
  50. }
  51. public function changeCheck(array $data): bool
  52. {
  53. if (!$this->scene('change')->check($data)) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. public function listCheck(array $data): bool
  59. {
  60. if (!$this->scene('list')->check($data)) {
  61. return false;
  62. }
  63. if($data['uid'] == 0 && empty($data['tourist_unique_key'])) {
  64. $this->error = '请传入游客唯一标识';
  65. return false;
  66. }
  67. return true;
  68. }
  69. public function updatePriceCheck(array $data): bool
  70. {
  71. if (!$this->scene('updatePrice')->check($data)) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. public function batchUpdatePriceCheck(array $data): bool
  77. {
  78. if (!$this->scene('batchUpdatePrice')->check($data)) {
  79. return false;
  80. }
  81. return true;
  82. }
  83. }