UserValidate.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 $rule = [
  25. 'id' => 'require|checkUser',
  26. 'field' => 'require|checkField',
  27. 'value' => 'require',
  28. ];
  29. protected $message = [
  30. 'id.require' => '请选择用户',
  31. 'field.require' => '请选择操作',
  32. 'value.require' => '请输入内容',
  33. ];
  34. /**
  35. * @notes 详情场景
  36. * @return UserValidate
  37. * @author 段誉
  38. * @date 2022/9/22 16:35
  39. */
  40. public function sceneDetail()
  41. {
  42. return $this->only(['id']);
  43. }
  44. /**
  45. * @notes 用户信息校验
  46. * @param $value
  47. * @param $rule
  48. * @param $data
  49. * @return bool|string
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @author 段誉
  54. * @date 2022/9/22 17:03
  55. */
  56. public function checkUser($value, $rule, $data)
  57. {
  58. $userIds = is_array($value) ? $value : [$value];
  59. foreach ($userIds as $item) {
  60. if (!User::find($item)) {
  61. return '用户不存在!';
  62. }
  63. }
  64. return true;
  65. }
  66. /**
  67. * @notes 校验是否可更新信息
  68. * @param $value
  69. * @param $rule
  70. * @param $data
  71. * @return bool|string
  72. * @author 段誉
  73. * @date 2022/9/22 16:37
  74. */
  75. public function checkField($value, $rule, $data)
  76. {
  77. $allowField = ['account', 'sex', 'mobile', 'real_name'];
  78. if (!in_array($value, $allowField)) {
  79. return '用户信息不允许更新';
  80. }
  81. switch ($value) {
  82. case 'account':
  83. //验证手机号码是否存在
  84. $account = User::where([
  85. ['id', '<>', $data['id']],
  86. ['account', '=', $data['value']]
  87. ])->findOrEmpty();
  88. if (!$account->isEmpty()) {
  89. return '账号已被使用';
  90. }
  91. break;
  92. case 'mobile':
  93. if (false == $this->validate($data['value'], 'mobile', $data)) {
  94. return '手机号码格式错误';
  95. }
  96. //验证手机号码是否存在
  97. $mobile = User::where([
  98. ['id', '<>', $data['id']],
  99. ['mobile', '=', $data['value']]
  100. ])->findOrEmpty();
  101. if (!$mobile->isEmpty()) {
  102. return '手机号码已存在';
  103. }
  104. break;
  105. }
  106. return true;
  107. }
  108. }