User.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. return true;
  66. }
  67. }