AdminCheck.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. namespace app\admin\validate;
  8. use think\Validate;
  9. class AdminCheck extends Validate
  10. {
  11. protected $regex = [ 'checkUser' => '/^[A-Za-z]{1}[A-Za-z0-9_-]{4,19}$/'];
  12. protected $rule = [
  13. 'username' => 'require|regex:checkUser|unique:admin',
  14. 'pwd' => 'require|min:6|confirm',
  15. 'edit_pwd' => 'min:6|confirm',
  16. 'mobile' => 'require|mobile',
  17. 'nickname' => 'require|chsAlpha',
  18. 'group_id' => 'require',
  19. 'id' => 'require',
  20. 'status' => 'require|checkStatus:-1,1',
  21. 'old_pwd' => 'require|different:pwd',
  22. ];
  23. protected $message = [
  24. 'username.require' => '登录账号不能为空',
  25. 'username.regex' => '登录账号必须是以字母开头,只能包含字母数字下划线和减号,5到20位',
  26. 'username.unique' => '同样的登录账号已经存在',
  27. 'pwd.require' => '密码不能为空',
  28. 'pwd.min' => '密码至少要6个字符',
  29. 'pwd.confirm' => '两次密码不一致',
  30. 'edit_pwd.min' => '密码至少要6个字符',
  31. 'edit_pwd.confirm' => '两次密码不一致',
  32. 'mobile.require' => '手机不能为空',
  33. 'mobile.mobile' => '手机格式错误',
  34. 'nickname.require' => '昵称不能为空',
  35. 'nickname.chsAlpha' => '昵称只能是汉字和字母',
  36. 'group_id.require' => '至少要选择一个用户角色',
  37. 'id.require' => '缺少更新条件',
  38. 'status.require' => '状态为必选',
  39. 'status.checkStatus' => '系统所有者不能被禁用',
  40. 'old_pwd.require' => '请提供旧密码',
  41. 'old_pwd.different' => '新密码不能和旧密码一样',
  42. ];
  43. protected $scene = [
  44. 'add' => ['mobile', 'nickname', 'group_id', 'pwd', 'username', 'status'],
  45. 'edit' => ['mobile', 'nickname', 'group_id', 'edit_pwd','id', 'username', 'status'],
  46. 'editPersonal' => ['mobile', 'nickname'],
  47. 'editpwd' => ['old_pwd', 'pwd'],
  48. ];
  49. // 自定义验证规则
  50. protected function checkStatus($value, $rule, $data)
  51. {
  52. if ($value == -1 and $data['id'] == 1) {
  53. return $rule == false;
  54. }
  55. return $rule == true;
  56. }
  57. }