User.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. ];
  32. protected function checkAccount($value,$rule,$data=[])
  33. {
  34. $info = Db::name('user')->where('account',$value)->where('del',0)->find();
  35. if($data['id'] <= 0 && $info){
  36. return '账号已被使用';
  37. }
  38. if($info && $data['id'] > 0 && $info['id'] != $data['id']){
  39. return '账号已被使用';
  40. }
  41. return true;
  42. }
  43. protected function checkMobile($value,$rule,$data=[])
  44. {
  45. if($value && !check_mobile($value)){
  46. return '手机号格式不正确';
  47. }
  48. if($value){
  49. $info = Db::name('user')->where('mobile',$value)->where('del',0)->find();
  50. if($data['id'] <= 0 && $info){
  51. return '手机号已被使用';
  52. }
  53. if($info && $data['id'] > 0 && $info['id'] != $data['id']){
  54. return '手机号已被使用';
  55. }
  56. }
  57. return true;
  58. }
  59. protected function checkPassword($value,$rule,$data=[])
  60. {
  61. if($data['id'] <= 0 && !$value){
  62. return '未输入密码';
  63. }
  64. return true;
  65. }
  66. }