User.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\common\validate;
  3. use think\Db;
  4. use think\Validate;
  5. class User extends Validate{
  6. protected $rule = [
  7. 'account|账号' => 'require|length:4,20|checkAccount',
  8. 'real_name|姓名' => 'require|length:1,20',
  9. 'mobile|手机号' => 'checkMobile',
  10. 'password|密码' => 'length:6,16|checkPassword',
  11. 'oldpass|旧密码' => 'require',
  12. 'pass|新密码' => 'require|length:6,16|different:oldpass',
  13. 'pass_confirm|重复新密码' => 'require|confirm:pass',
  14. 'depId' => 'require|gt:0',
  15. 'rolesId' => 'require|gt:0',
  16. ];
  17. protected $message = [
  18. 'account.length' => '账号长度必须在4-20字之间',
  19. 'password.length' => '密码长度必须在6-16字之间',
  20. 'depId.require' => '未选择部门',
  21. 'depId.gt' => '未选择部门',
  22. 'rolesId.require' => '未选择角色',
  23. 'rolesId.gt' => '未选择角色',
  24. ];
  25. protected $scene = [
  26. 'add' => ['account','real_name','mobile','password','depId','rolesId'],
  27. 'addCompany' => ['account','real_name','mobile','password','rolesId'],
  28. 'modifyPass' => ['oldpass','pass','newpass'],
  29. 'wxadd' => ['mobile','real_name','password'],
  30. 'wxedit' => ['real_name'],
  31. 'bachAdd' => ['account','real_name','mobile'],
  32. ];
  33. protected function checkAccount($value,$rule,$data=[])
  34. {
  35. $info = Db::name('user')->where('account',$value)->where('del',0)->find();
  36. if($data['id'] <= 0 && $info){
  37. return '账号已被使用';
  38. }
  39. if($info && $data['id'] > 0 && $info['id'] != $data['id']){
  40. return '账号已被使用';
  41. }
  42. return true;
  43. }
  44. protected function checkMobile($value,$rule,$data=[])
  45. {
  46. if($value && !check_mobile($value)){
  47. return '手机号格式不正确';
  48. }
  49. if($value){
  50. $info = Db::name('user')->where('mobile',$value)->where('del',0)->find();
  51. if($data['id'] <= 0 && $info){
  52. return '手机号已被使用';
  53. }
  54. if($info && $data['id'] > 0 && $info['id'] != $data['id']){
  55. return '手机号已被使用';
  56. }
  57. }
  58. return true;
  59. }
  60. protected function checkPassword($value,$rule,$data=[])
  61. {
  62. if($data['id'] <= 0 && !$value){
  63. return '未输入密码';
  64. }
  65. // $res = preg_match('/(?=.*?[A-Z])(?=.*[0-9])(?=.*[#@!~%^&*])[A-Z0-9#@!~%^&*]{6,16}/i', $value);
  66. // if(!$res){
  67. // return '密码必须有数字和字母并且包含‘~!@#$%^&*’符号';
  68. // }
  69. $res = preg_match('/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9]{6,16}/', $value);
  70. if(!$res){
  71. return '密码必须有大小写字母和数字';
  72. }
  73. return true;
  74. }
  75. }