2022-08-19 19:48:37 +08:00
|
|
|
<?php
|
2023-07-03 10:08:34 +08:00
|
|
|
declare (strict_types=1);
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
namespace app\common\validate\system;
|
|
|
|
|
|
2023-07-03 10:08:34 +08:00
|
|
|
use app\admin\service\AuthService;
|
2022-08-19 19:48:37 +08:00
|
|
|
use think\Validate;
|
2023-07-03 10:08:34 +08:00
|
|
|
use app\common\model\system\Admin as AdminModel;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
class Admin extends Validate
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 定义验证规则
|
2023-07-03 10:08:34 +08:00
|
|
|
* 格式:'字段名' => ['规则1','规则2'...]
|
2022-08-19 19:48:37 +08:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
protected $rule = [
|
|
|
|
|
'name' => 'require|min:2|max:12|chsAlphaNum',
|
|
|
|
|
'pwd|密码' => 'require|min:6|max:64',
|
|
|
|
|
'group_id' => 'require|checkGroup',
|
2022-08-19 19:48:37 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 定义错误信息
|
2023-07-03 10:08:34 +08:00
|
|
|
* 格式:'字段名.规则名' => '错误信息'
|
2022-08-19 19:48:37 +08:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
protected $message = [
|
|
|
|
|
'name.require' => '用户名不能为空',
|
|
|
|
|
'name.min' => '用户名不能少于2个字符',
|
|
|
|
|
'name.max' => '用户名不能超过12个字符',
|
|
|
|
|
'name.filters' => '用户名包含禁止注册字符',
|
|
|
|
|
'name.chsAlphaNum' => '用户名只能是汉字、字母和数字',
|
|
|
|
|
'pwd.require' => '密码不能为空',
|
|
|
|
|
'pwd.min' => '密码不能少于6个字符',
|
|
|
|
|
'pwd.max' => '密码不能超过64个字符',
|
|
|
|
|
'group_id.require' => '请选择用户组',
|
|
|
|
|
'group_id.checkGroup' => '无权限操作',
|
2022-08-19 19:48:37 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 测试验证场景
|
|
|
|
|
protected $scene = [
|
2023-07-03 10:08:34 +08:00
|
|
|
'add' => ['name', 'pwd', 'group_id'],
|
|
|
|
|
'edit' => ['name', 'group_id'],
|
|
|
|
|
'login' => ['name', 'pwd'],
|
2022-08-19 19:48:37 +08:00
|
|
|
];
|
2023-07-03 10:08:34 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 验证用户组权限
|
|
|
|
|
* @param $value
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function checkGroup($value): bool
|
|
|
|
|
{
|
|
|
|
|
$id = request()->get('id', 0);
|
|
|
|
|
$result = AdminModel::where('id', $id)->findOrEmpty()->toArray();
|
|
|
|
|
if (empty($result)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$group_id = !empty($value) ? $value . ',' . $result['group_id'] : $result['group_id'];
|
|
|
|
|
$group_id = array_unique(explode(',', $group_id));
|
|
|
|
|
$authService = AuthService::instance();
|
|
|
|
|
if (!$authService->checkRulesForGroup($group_id)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|