| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace App\Module\Shop\Validations;
- use App\Module\Shop\Validators\ShopItemValidator;
- use App\Module\Shop\Validators\ShopBuyLimitValidator;
- use App\Module\Shop\Validators\ShopFundValidator;
- use UCore\ValidationCore;
- /**
- * 商店购买验证类
- *
- * 用于验证商店购买操作的输入数据,包括用户ID、商品ID和购买数量
- */
- class ShopBuyValidation extends ValidationCore
- {
- /** @var \App\Module\Shop\Models\ShopItem|null 商品对象,由 ShopItemValidator 设置 */
- public ?\App\Module\Shop\Models\ShopItem $shop_item = null;
- /**
- * 验证规则
- *
- * @param array $rules 自定义规则
- * @return array
- */
- public function rules($rules = []): array
- {
- return [
- [
- 'user_id,good_id,number', 'required'
- ],
- [
- 'user_id,good_id,number', 'integer', 'min' => 1,
- 'msg' => '{attr}必须是大于0的整数'
- ],
- // 验证商品是否存在且可购买
- [
- 'good_id', new ShopItemValidator($this, ['shop_item']),
- 'msg' => '商品验证失败'
- ],
- // 验证购买限制
- [
- 'good_id', new ShopBuyLimitValidator($this, ['user_id', 'number', 'shop_item']),
- 'msg' => '购买限制验证失败'
- ],
- // 验证用户资金是否充足
- [
- 'good_id', new ShopFundValidator($this, ['user_id', 'number', 'shop_item']),
- 'msg' => '资金不足'
- ]
- ];
- }
- /**
- * 设置默认值
- *
- * @return array
- */
- public function default(): array
- {
- return [];
- }
- }
|