Files
chatroom/app/Http/Requests/StoreShopItemRequest.php
T

76 lines
2.1 KiB
PHP
Raw Normal View History

2026-04-30 09:40:50 +08:00
<?php
/**
* 文件功能:后台新增商店商品请求验证。
*
* 统一校验商店商品字段,包含座驾欢迎语字段。
*/
namespace App\Http\Requests;
use App\Models\ShopItem;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
/**
* 后台新增商店商品请求
* 负责新增商品时的权限与字段校验。
*/
class StoreShopItemRequest extends FormRequest
{
/**
* 判断当前用户是否允许新增商品。
*/
public function authorize(): bool
{
return $this->user()?->id === 1;
}
/**
* 获取新增商品验证规则。
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:100'],
'slug' => ['required', 'string', 'max:100', Rule::unique('shop_items', 'slug')],
'icon' => ['required', 'string', 'max:20'],
'description' => ['nullable', 'string', 'max:500'],
'price' => ['required', 'integer', 'min:0'],
'type' => ['required', Rule::in($this->allowedTypes())],
'duration_days' => ['nullable', 'integer', 'min:0'],
'duration_minutes' => ['nullable', 'integer', 'min:0'],
'intimacy_bonus' => ['nullable', 'integer', 'min:0'],
'charm_bonus' => ['nullable', 'integer', 'min:0'],
'welcome_message' => ['nullable', 'string', 'max:255'],
'sort_order' => ['required', 'integer', 'min:0'],
'is_active' => ['boolean'],
];
}
/**
* 获取允许后台配置的商品类型。
*
* @return array<int, string>
*/
protected function allowedTypes(): array
{
return [
'instant',
'duration',
'one_time',
'ring',
'auto_fishing',
ShopItem::TYPE_SIGN_REPAIR,
'msg_bubble',
'msg_name_color',
'msg_text_color',
'avatar_frame',
ShopItem::TYPE_RIDE,
];
}
}