User.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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|alphaNum|unique:user|length:3,16',
  8. 'password|原始密码' => 'require',
  9. 'newPassword|新密码' => 'require|alphaNum|length:6,16|different:password',
  10. 'phone|手机号' => 'require|checkPhone',
  11. 'name|姓名' => 'require',
  12. 'company_id' => 'require|egt:0',
  13. ];
  14. protected $message = [
  15. 'account.unique' => '账号已被占用',
  16. 'account.length' => '账号长度必须在3-16之间',
  17. 'newPassword.length' => '新密码长度必须在6-16之间',
  18. 'newPassword.different' => '新密码与原密码不能一样',
  19. 'company_id.require' => '未选择公司'
  20. ];
  21. protected $scene = [
  22. 'account' => ['account'],
  23. 'modifypass' => ['password','newPassword'],
  24. 'add' => ['phone','name','company_id']
  25. ];
  26. // 自定义验证规则
  27. protected function checkPhone($value,$rule,$data=[])
  28. {
  29. if(!check_mobile($value)){
  30. return '手机号格式不正确';
  31. }
  32. $info = Db::name('user')->where('phone',$data['phone'])->where('del',0)->find();
  33. if($data['id'] <= 0 && $info){
  34. return '手机号已被使用';
  35. }
  36. if($info && $data['id'] > 0 && $info['id'] != $data['id']){
  37. return '手机号已被使用';
  38. }
  39. return true;
  40. }
  41. }