189 lines
5.6 KiB
PHP
189 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Admin\System;
|
|
|
|
use App\Http\Requests\BaseRequest;
|
|
|
|
/**
|
|
* 系统角色请求验证
|
|
*/
|
|
class SystemRoleRequest extends BaseRequest
|
|
{
|
|
/**
|
|
* 获取特定验证规则
|
|
*/
|
|
protected function getSpecificRules(): array
|
|
{
|
|
$action = $this->route()->getActionMethod();
|
|
|
|
return match($action) {
|
|
'list' => $this->listRules(),
|
|
'detail' => $this->detailRules(),
|
|
'create' => $this->createRules(),
|
|
'update' => $this->updateRules(),
|
|
'delete' => $this->deleteRules(),
|
|
'batchDelete' => $this->batchDeleteRules(),
|
|
'batchUpdateStatus' => $this->batchUpdateStatusRules(),
|
|
'checkCode' => $this->checkCodeRules(),
|
|
'simpleList' => [],
|
|
default => []
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取分页验证规则
|
|
*/
|
|
private function getPaginationRules(): array
|
|
{
|
|
return [
|
|
'page' => ['sometimes', 'integer', 'min:1'],
|
|
'page_size' => ['sometimes', 'integer', 'min:1', 'max:100'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 列表查询验证规则
|
|
*/
|
|
private function listRules(): array
|
|
{
|
|
return array_merge($this->getPaginationRules(), [
|
|
'keyword' => ['sometimes', 'string', 'max:50'],
|
|
'status' => ['sometimes', 'integer', 'in:0,1'],
|
|
'type' => ['sometimes', 'integer', 'min:0'],
|
|
'data_scope' => ['sometimes', 'integer', 'in:1,2,3,4'],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 详情查询验证规则
|
|
*/
|
|
private function detailRules(): array
|
|
{
|
|
return [
|
|
'id' => ['required', 'integer', 'exists:system_role,id'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 创建验证规则
|
|
*/
|
|
private function createRules(): array
|
|
{
|
|
return [
|
|
'name' => ['required', 'string', 'max:30', 'unique:system_role,name'],
|
|
'code' => ['required', 'string', 'max:100', 'unique:system_role,code'],
|
|
'sort' => ['sometimes', 'integer', 'min:0'],
|
|
'data_scope' => ['sometimes', 'integer', 'in:1,2,3,4'],
|
|
'data_scope_dept_ids' => ['sometimes', 'array'],
|
|
'data_scope_dept_ids.*' => ['integer'],
|
|
'status' => ['sometimes', 'integer', 'in:0,1'],
|
|
'type' => ['sometimes', 'integer', 'min:0'],
|
|
'remark' => ['sometimes', 'string', 'max:500'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 更新验证规则
|
|
*/
|
|
private function updateRules(): array
|
|
{
|
|
return [
|
|
'id' => ['required', 'integer', 'exists:system_role,id'],
|
|
'name' => ['required', 'string', 'max:30', 'unique:system_role,name,' . $this->input('id')],
|
|
'code' => ['required', 'string', 'max:100', 'unique:system_role,code,' . $this->input('id')],
|
|
'sort' => ['sometimes', 'integer', 'min:0'],
|
|
'data_scope' => ['sometimes', 'integer', 'in:1,2,3,4'],
|
|
'data_scope_dept_ids' => ['sometimes', 'array'],
|
|
'data_scope_dept_ids.*' => ['integer'],
|
|
'status' => ['sometimes', 'integer', 'in:0,1'],
|
|
'type' => ['sometimes', 'integer', 'min:0'],
|
|
'remark' => ['sometimes', 'string', 'max:500'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 删除验证规则
|
|
*/
|
|
private function deleteRules(): array
|
|
{
|
|
return [
|
|
'id' => ['required', 'integer', 'exists:system_role,id'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 批量删除验证规则
|
|
*/
|
|
private function batchDeleteRules(): array
|
|
{
|
|
return [
|
|
'ids' => ['required', 'array', 'min:1'],
|
|
'ids.*' => ['integer', 'exists:system_role,id'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 批量更新状态验证规则
|
|
*/
|
|
private function batchUpdateStatusRules(): array
|
|
{
|
|
return [
|
|
'ids' => ['required', 'array', 'min:1'],
|
|
'ids.*' => ['integer', 'exists:system_role,id'],
|
|
'status' => ['required', 'integer', 'in:0,1'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 检查编码验证规则
|
|
*/
|
|
private function checkCodeRules(): array
|
|
{
|
|
return [
|
|
'code' => ['required', 'string', 'max:100'],
|
|
'id' => ['sometimes', 'integer', 'exists:system_role,id'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取特定验证错误消息
|
|
*/
|
|
protected function getSpecificMessages(): array
|
|
{
|
|
return [
|
|
'name.required' => '角色名称不能为空',
|
|
'name.max' => '角色名称不能超过30个字符',
|
|
'name.unique' => '角色名称已存在',
|
|
'code.required' => '角色编码不能为空',
|
|
'code.max' => '角色编码不能超过100个字符',
|
|
'code.unique' => '角色编码已存在',
|
|
'sort.min' => '排序值不能小于0',
|
|
'data_scope.in' => '数据范围值无效',
|
|
'status.in' => '状态值无效',
|
|
'type.min' => '角色类型值无效',
|
|
'remark.max' => '备注不能超过500个字符',
|
|
'ids.required' => '请选择要操作的角色',
|
|
'ids.min' => '至少选择一个角色进行操作',
|
|
'data_scope_dept_ids.array' => '数据范围部门必须为数组格式',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取字段名称
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'name' => '角色名称',
|
|
'code' => '角色编码',
|
|
'sort' => '排序',
|
|
'data_scope' => '数据范围',
|
|
'data_scope_dept_ids' => '数据范围部门',
|
|
'status' => '状态',
|
|
'type' => '角色类型',
|
|
'remark' => '备注',
|
|
'ids' => '角色ID',
|
|
];
|
|
}
|
|
}
|