UserValidate.php 3.9 KB

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