AdminValidate.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\validate\auth;
  20. use app\common\validate\BaseValidate;
  21. use app\common\model\Admin;
  22. class AdminValidate extends BaseValidate
  23. {
  24. protected $rule = [
  25. 'id' => 'require|checkAdmin',
  26. 'account' => 'require|length:1,32|checkUsed',
  27. 'password' => 'require|length:6,32|edit',
  28. 'password_confirm' => 'requireWith:password|confirm',
  29. 'name' => 'require|length:1,16',
  30. 'role_id' => 'require',
  31. 'disable' => 'require|in:0,1',
  32. 'multipoint_login' => 'require|in:0,1',
  33. ];
  34. protected $message = [
  35. 'id.require' => '管理员id不能为空',
  36. 'account.require' => '账号不能为空',
  37. 'account.length' => '账号长度须在1-32位字符',
  38. 'password.require' => '密码不能为空',
  39. 'password.length' => '密码长度须在6-32位字符',
  40. 'password_confirm.requireWith' => '确认密码不能为空',
  41. 'password_confirm.confirm' => '两次输入的密码不一致',
  42. 'name.require' => '名称不能为空',
  43. 'name.length' => '名称须在1-16位字符',
  44. 'role_id.require' => '请选择角色',
  45. 'disable.require' => '请选择状态',
  46. 'disable.in' => '状态值错误',
  47. 'multipoint_login.require' => '请选择是否支持多处登录',
  48. 'multipoint_login.in' => '多处登录状态值为误',
  49. ];
  50. public function sceneAdd()
  51. {
  52. return $this->remove(['password', 'edit'])
  53. ->remove('id', 'require|checkAdmin');
  54. }
  55. public function sceneDetail()
  56. {
  57. return $this->only(['id']);
  58. }
  59. public function sceneEdit()
  60. {
  61. return $this->remove('password', 'require|length')
  62. ->append('id', 'require|checkAdmin');
  63. }
  64. public function sceneDelete()
  65. {
  66. return $this->only(['id']);
  67. }
  68. /**
  69. * @notes 检查账号是否已被使用
  70. * @param $value
  71. * @return bool|string
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @author Tab
  76. * @date 2021/7/13 11:38
  77. */
  78. public function checkUsed($value, $rule, $data)
  79. {
  80. $where = [
  81. ['account', '=', $value]
  82. ];
  83. // 编辑的情况,要排除自身ID
  84. if (isset($data['id'])) {
  85. $where[] = ['id', '<>', $data['id']];
  86. }
  87. $admins = Admin::where($where)->select()->toArray();
  88. if ($admins) {
  89. return '账号已被占用';
  90. }
  91. return true;
  92. }
  93. /**
  94. * @notes 编辑情况下,检查是否填密码
  95. * @param $value
  96. * @param $rule
  97. * @param $data
  98. * @return bool|string
  99. * @author Tab
  100. * @date 2021/7/13 11:38
  101. */
  102. public function edit($value, $rule, $data)
  103. {
  104. if (empty($data['password']) && empty($data['password_confirm'])) {
  105. return true;
  106. }
  107. $len = strlen($value);
  108. if ($len < 6 || $len > 32) {
  109. return '密码长度须在6-32位字符';
  110. }
  111. return true;
  112. }
  113. /**
  114. * @notes 检查指定管理员是否存在
  115. * @param $value
  116. * @return bool|string
  117. * @author Tab
  118. * @date 2021/7/13 11:39
  119. */
  120. public function checkAdmin($value)
  121. {
  122. $admin = Admin::findOrEmpty($value);
  123. if ($admin->isEmpty()) {
  124. return '管理员不存在';
  125. }
  126. return true;
  127. }
  128. }