UserValidate.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\validate\user;
  15. use app\common\model\user\User;
  16. use app\common\validate\BaseValidate;
  17. /**
  18. * 用户验证
  19. * Class UserValidate
  20. * @package app\adminapi\validate\user
  21. */
  22. class UserValidate extends BaseValidate
  23. {
  24. protected $regex = [
  25. 'register' => '^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+$',
  26. 'password' => '/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)]|[\(\)])+$)([^(0-9a-zA-Z)]|[\(\)]|[a-z]|[A-Z]|[0-9]){6,20}$/'
  27. ];
  28. protected $rule = [
  29. 'id' => 'require|checkUser',
  30. 'field' => 'require|checkField',
  31. 'value' => 'require',
  32. 'account'=>'require|length:3,12|unique:' . User::class . '|regex:register',
  33. 'password'=>'require|length:6,20|regex:password',
  34. ];
  35. protected $message = [
  36. 'id.require' => '请选择用户',
  37. 'field.require' => '请选择操作',
  38. 'account.require' => '请输入账号',
  39. 'account.regex' => '账号须为字母数字组合',
  40. 'account.length' => '账号须为3-12位之间',
  41. 'account.unique' => '账号已存在',
  42. 'password.require' => '请输入密码',
  43. 'password.length' => '密码须在6-25位之间',
  44. 'password.regex' => '密码须为数字,字母或符号组合',
  45. ];
  46. public function sceneAddInfo(){
  47. return $this->only(['account','password']);
  48. }
  49. /**
  50. * @notes 详情场景
  51. * @return UserValidate
  52. * @author 段誉
  53. * @date 2022/9/22 16:35
  54. */
  55. public function sceneDetail()
  56. {
  57. return $this->only(['id']);
  58. }
  59. /**
  60. * @notes 删除场景
  61. * @return UserValidate
  62. * @author 段誉
  63. * @date 2022/9/22 16:35
  64. */
  65. public function sceneDelete()
  66. {
  67. return $this->only(['id']);
  68. }
  69. /**
  70. * @notes 用户信息校验
  71. * @param $value
  72. * @param $rule
  73. * @param $data
  74. * @return bool|string
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. * @author 段誉
  79. * @date 2022/9/22 17:03
  80. */
  81. public function checkUser($value, $rule, $data)
  82. {
  83. $userIds = is_array($value) ? $value : [$value];
  84. foreach ($userIds as $item) {
  85. if (!User::find($item)) {
  86. return '用户不存在!';
  87. }
  88. }
  89. return true;
  90. }
  91. /**
  92. * @notes 校验是否可更新信息
  93. * @param $value
  94. * @param $rule
  95. * @param $data
  96. * @return bool|string
  97. * @author 段誉
  98. * @date 2022/9/22 16:37
  99. */
  100. public function checkField($value, $rule, $data)
  101. {
  102. $allowField = ['account', 'sex', 'mobile', 'real_name'];
  103. if (!in_array($value, $allowField)) {
  104. return '用户信息不允许更新';
  105. }
  106. switch ($value) {
  107. case 'account':
  108. //验证手机号码是否存在
  109. $account = User::where([
  110. ['id', '<>', $data['id']],
  111. ['account', '=', $data['value']]
  112. ])->findOrEmpty();
  113. if (!$account->isEmpty()) {
  114. return '账号已被使用';
  115. }
  116. break;
  117. case 'mobile':
  118. if (false == $this->validate($data['value'], 'mobile', $data)) {
  119. return '手机号码格式错误';
  120. }
  121. //验证手机号码是否存在
  122. $mobile = User::where([
  123. ['id', '<>', $data['id']],
  124. ['mobile', '=', $data['value']]
  125. ])->findOrEmpty();
  126. if (!$mobile->isEmpty()) {
  127. return '手机号码已存在';
  128. }
  129. break;
  130. }
  131. return true;
  132. }
  133. }