| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- // +----------------------------------------------------------------------
- // | likeadmin快速开发前后端分离管理后台(PHP版)
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
- // | github下载:https://github.com/likeshop-github/likeadmin
- // | 访问官网:https://www.likeadmin.cn
- // | likeadmin团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeadminTeam
- // +----------------------------------------------------------------------
- namespace app\adminapi\validate\user;
- use app\common\model\user\User;
- use app\common\validate\BaseValidate;
- /**
- * 用户验证
- * Class UserValidate
- * @package app\adminapi\validate\user
- */
- class UserValidate extends BaseValidate
- {
- protected $regex = [
- 'register' => '^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+$',
- 'password' => '/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)]|[\(\)])+$)([^(0-9a-zA-Z)]|[\(\)]|[a-z]|[A-Z]|[0-9]){6,20}$/'
- ];
- protected $rule = [
- 'id' => 'require|checkUser',
- 'field' => 'require|checkField',
- 'value' => 'require',
- 'account'=>'require|length:3,12|unique:' . User::class . '|regex:register',
- 'password'=>'require|length:6,20|regex:password',
- ];
- protected $message = [
- 'id.require' => '请选择用户',
- 'field.require' => '请选择操作',
- 'account.require' => '请输入账号',
- 'account.regex' => '账号须为字母数字组合',
- 'account.length' => '账号须为3-12位之间',
- 'account.unique' => '账号已存在',
- 'password.require' => '请输入密码',
- 'password.length' => '密码须在6-25位之间',
- 'password.regex' => '密码须为数字,字母或符号组合',
- ];
- public function sceneAddInfo(){
- return $this->only(['account','password']);
- }
- /**
- * @notes 详情场景
- * @return UserValidate
- * @author 段誉
- * @date 2022/9/22 16:35
- */
- public function sceneDetail()
- {
- return $this->only(['id']);
- }
- /**
- * @notes 删除场景
- * @return UserValidate
- * @author 段誉
- * @date 2022/9/22 16:35
- */
- public function sceneDelete()
- {
- return $this->only(['id']);
- }
- /**
- * @notes 用户信息校验
- * @param $value
- * @param $rule
- * @param $data
- * @return bool|string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 段誉
- * @date 2022/9/22 17:03
- */
- public function checkUser($value, $rule, $data)
- {
- $userIds = is_array($value) ? $value : [$value];
- foreach ($userIds as $item) {
- if (!User::find($item)) {
- return '用户不存在!';
- }
- }
- return true;
- }
- /**
- * @notes 校验是否可更新信息
- * @param $value
- * @param $rule
- * @param $data
- * @return bool|string
- * @author 段誉
- * @date 2022/9/22 16:37
- */
- public function checkField($value, $rule, $data)
- {
- $allowField = ['account', 'sex', 'mobile', 'real_name'];
- if (!in_array($value, $allowField)) {
- return '用户信息不允许更新';
- }
- switch ($value) {
- case 'account':
- //验证手机号码是否存在
- $account = User::where([
- ['id', '<>', $data['id']],
- ['account', '=', $data['value']]
- ])->findOrEmpty();
- if (!$account->isEmpty()) {
- return '账号已被使用';
- }
- break;
- case 'mobile':
- if (false == $this->validate($data['value'], 'mobile', $data)) {
- return '手机号码格式错误';
- }
- //验证手机号码是否存在
- $mobile = User::where([
- ['id', '<>', $data['id']],
- ['mobile', '=', $data['value']]
- ])->findOrEmpty();
- if (!$mobile->isEmpty()) {
- return '手机号码已存在';
- }
- break;
- }
- return true;
- }
- }
|