123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace app\api\validate;
- use think\Validate;
- use app\common\Enum\UserEnum;
- class User extends Validate
- {
- public function __construct(array $rules = [], $message = [], $field = [])
- {
- // 动态设置枚举值
- $this->rule['gender'] = 'require|in:' .implode(',', UserEnum::getGenderList());
- parent::__construct($rules, $message, $field);
- }
- /**
- * 验证规则
- */
- protected $rule = [
- 'account' => 'require|length:3,30',
- 'username' => 'length:3,30',
- 'nickname' => 'require|length:3,30',
- 'password' => 'require|length:6,30',
- 'mobile' => 'require|regex:/^1\d{10}$/',
- 'email' => 'email',
- // 验证头像 验证文件后缀
- 'avatar' => [
- 'require',
- 'regex'=>'/\\.(jpg|jpeg|png|gif|bmp|webp)$/i'
- ],
- // 'gender' => 'require|in:' .implode(',', UserEnum::getGenderList()),
- 'bio' => 'max:255',
- 'money' => 'float|egt:0',
- 'balance' => 'float|egt:0',
- 'score' => 'integer|egt:0',
- 'level' => 'integer|egt:0',
- 'age' => 'require|integer|egt:0|elt:200',
- 'captcha' => 'require|length:4,6'
- ];
- /**
- * 提示消息
- */
- protected $message = [
- 'account.require' => '账号不能为空',
- 'account.length' => '账号长度必须在3-30个字符之间',
- // 'username.require' => '用户名不能为空',
- 'username.length' => '用户名长度必须在3-30个字符之间',
- 'nickname.require' => '昵称不能为空',
- 'nickname.length' => '昵称长度必须在3-30个字符之间',
- 'password.require' => '密码不能为空',
- 'password.length' => '密码长度必须在6-30个字符之间',
- 'mobile.regex' => '手机号格式不正确',
- 'mobile.require' => '手机号不能为空',
- 'email' => '邮箱格式不正确',
- 'avatar.require' => '头像不能为空',
- 'avatar.regex' => '头像格式不正确',
- 'gender.in' => '性别只能是未知、男、女',
- 'gender.require' => '性别不能为空',
- 'age.require' => '年龄不能为空',
- 'age.integer' => '年龄必须是整数',
- 'age.egt' => '年龄不能为负数',
- 'age.elt' => '年龄不能大于200',
- 'bio.max' => '个人简介最多255个字符',
- 'money.float' => '余额必须是数字',
- 'money.egt' => '余额不能为负数',
- 'balance.float' => '余额必须是数字',
- 'balance.egt' => '余额不能为负数',
- 'score.integer' => '积分必须是整数',
- 'score.egt' => '积分不能为负数',
- 'level.integer' => '等级必须是整数',
- 'level.egt' => '等级不能为负数',
- 'captcha.require' => '验证码不能为空',
- 'captcha.length' => '验证码长度不正确'
- ];
- /**
- * 验证场景
- */
- protected $scene = [
- 'register' => ['username', 'password', 'mobile', 'captcha'],
- 'login' => ['account', 'password'],
- 'mobilelogin' => ['mobile', 'captcha'],
- 'profile' => ['username', 'nickname', 'bio', 'avatar','age','gender'],
- 'changeMobile' => ['mobile', 'captcha'],
- ];
- }
|