User.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. namespace app\admin\validate;
  8. use think\Validate;
  9. /**
  10. * Description of User
  11. *
  12. * @author sgq
  13. */
  14. class User extends Validate {
  15. protected $rule = [
  16. 'account' => 'require|max:50|checkUnique:sys_user',
  17. 'name' => 'require|max:50|regex:/^[\x{4e00}-\x{9fa5}\(\)()\da-zA-Z&]{2,50}$/u',
  18. 'type' => 'require|checkInSelect:type,1,2,3,4,5,6',
  19. 'password' => 'require|min:8|regex:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/',
  20. 're_password' => 'require|confirm:password',
  21. 'phone' => 'require|mobile',
  22. 'companyId' => 'require',
  23. 'roleId' => 'require',
  24. 'email' => 'email',
  25. 'birthday' => "dateFormat:Y-m-d",
  26. "sex" => "checkInSelect:sex,1,2"
  27. ];
  28. protected $message = [
  29. 'account.require' => '请填写账户!',
  30. 'account.max' => '账户最多50个字符!',
  31. 'account.checkUnique' => '该账户已被注册',
  32. 'name.require' => '请输入姓名',
  33. 'name.max' => '姓名最多50个字符!',
  34. 'name.regex' => '姓名只能是中文',
  35. 'type.require' => "请选择账号类型",
  36. 'password.require' => '请填写密码!',
  37. 'password.min' => '密码最少长度8位',
  38. 'password.regex' => '密码应包含字母与数字两种字符',
  39. 're_password.require' => '请输入重复密码',
  40. 're_password.confirm' => '两次密码输入不一致',
  41. "phone.require" => "请填写手机号",
  42. "phone.mobile" => "请填写正确的手机号",
  43. "companyId.require" => "请选择所属单位",
  44. "roleId.require" => "请选择角色",
  45. "email.email" => "电子邮箱格式错误",
  46. "birthday.dateFormat" => "生日必需是有效日期格式[yyyy-MM-dd]"
  47. ];
  48. protected $scene = [
  49. 'add' => ['account', 'name', 'type', 'password', 're_password', 'phone', 'companyId', 'email', 'birthday', "sex"],
  50. 'edit' => ['account', 'name', 'type', 'phone', 'companyId', 'email', 'birthday', "sex"],
  51. 'info' => ['name', 'phone', 'email', 'birthday', "sex"],
  52. 'change_pwd' => ['old_password', 'password', "re_password"],
  53. ];
  54. protected function checkUnique($value, $rule, $data = [], $field) {
  55. $db = $this->db->name($rule);
  56. $pk = $db->getPk();
  57. $map = [];
  58. $map[] = [$field, "=", $data[$field]];
  59. $map[] = ["status", "<>", 3];
  60. if ($data[$pk]) {
  61. $map[] = [$pk, "<>", $data[$pk]];
  62. }
  63. if ($db->where($map)->field($pk)->find()) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. protected function checkInSelect($value, $rule, $data = []) {
  69. $title = "";
  70. $select = explode(",", $rule);
  71. $type = array_shift($select);
  72. switch ($type) {
  73. case "type":
  74. $title = "账号类型";
  75. break;
  76. case "sex":
  77. $title = "性别";
  78. break;
  79. }
  80. return in_array($value, $select) ?: "{$title}只能在预设列表中选择";
  81. }
  82. }