ThinkPHP自定义验证器+PHP新特性use的使用,让代码书写更舒服

旧城冷巷雨未停 提交于 2020-10-29 11:08:15

贴极端关键代码

  1. 业务代码
       /**
         * 批量新增用户
         * @return false|string
         */
        public function batchAddUser()
        {
            //验证请求参数
            (new BatchAddUser())->runCheck();
            // 启动事务
            Db::startTrans();
            foreach ($this->args['user_list'] as $index => $user) {
                //验证用户信息
                (new BatchAddUserForUserInfo())->runCheck(function () {
                    Db::rollback();
                }, $user, true);
                //添加用户
                if (!is_numeric($user['key'])) {
                    continue;
                }
                $newUser = UserClient::create($user);
                //验证认证信息
                $continuFlag = false;
                (new BatchAddUserForUserQualification())->runCheck(function ($error) use ($continuFlag) {
                    $continuFlag = true;
                }, $user, false);
                //忽略认证信息错误,可继续添加用户
                if ($continuFlag) continue;
            }
            //提交事务
            Db::commit();
            //请求返回
            return $this->makeApiReturn('导入成功');
        }

     

  2. 继承重写验证器
    • 重写验证器
      /**
       * Api验证器基类
       *
       * Class ApiBaseValidate
       * @package app\cigo_admin_core\library
       */
      class ApiBaseValidate extends Validate
      {
          use ApiCommon;
      
          /**
           * 获取新实例
           *
           * @return static
           */
          public static function instance()
          {
              return new static();
          }
      
          public function runCheck($callBack = '', $params = [], $abortFlag = true)
          {
              $request = Request::instance();
              if ($this->check(empty($params) ? input() : $params) == false) {
                  if ($callBack) {
                      $callBack($this->getError());
                  }
                  if (!$abortFlag) return;
                  abort($this->makeApiReturn(
                      $this->getError(), [],
                      ErrorCode::ClientError_ArgsWrong,
                      HttpReponseCode::ClientError_BadRequest
                  ));
              }
          }
      }

       

    • 验证器示例类
      class BatchAddUser extends ApiBaseValidate
      {
          /**
           * 定义验证规则
           * 格式:'字段名'    =>    ['规则1','规则2'...]
           *
           * @var array
           */
          protected $rule = [
              'user_list' => 'require:array',
          ];
      
          /**
           * 定义错误信息
           * 格式:'字段名.规则名'    =>    '错误信息'
           *
           * @var array
           */
          protected $message = [
              'user_list.require' => '缺少用户列表字段',
              'user_list.array' => '用户列表为数组',
          ];
      }
      
      class BatchAddUserForUserInfo extends ApiBaseValidate
      {
          /**
           * 定义验证规则
           * 格式:'字段名'    =>    ['规则1','规则2'...]
           *
           * @var array
           */
          protected $rule = [
              'key' => 'require',
              'user_name' => 'require',
              'phone' => 'require|mobile',
              'realname' => 'require',
              'nickname' => 'require',
              'sex' => 'require|in:0,1,2',
              'password' => 'require|min:6|max:20',
          ];
      
          /**
           * 定义错误信息
           * 格式:'字段名.规则名'    =>    '错误信息'
           *
           * @var array
           */
          protected $message = [
              'key.require' => '序号字段缺失',
              'user_name.require' => '用户名字段缺失',
              'phone.require' => '手机号字段缺失',
              'phone.mobile' => '手机号格式错误',
              'realname.require' => '姓名字段缺失',
              'nickname.require' => '昵称字段缺失',
              'sex.require' => '性别字段缺失',
              'sex.in' => '性别填写错误',
              'password.require' => '密码字段缺失',
              'password.min' => '密码最短6位',
              'password.max' => '密码最长20位',
          ];
      }
      
      class BatchAddUserForUserQualification extends ApiBaseValidate
      {
          /**
           * 定义验证规则
           * 格式:'字段名'    =>    ['规则1','规则2'...]
           *
           * @var array
           */
          protected $rule = [
              'hospital' => 'require',
              'department' => 'require|number',
              'professional' => 'require|number',
          ];
      
          /**
           * 定义错误信息
           * 格式:'字段名.规则名'    =>    '错误信息'
           *
           * @var array
           */
          protected $message = [
              'hospital.require' => '医院字段缺失',
              'department.require' => '科室字段缺失',
              'department.number' => '科室字段应为数字',
              'professional.require' => '职称字段缺失',
              'professional.number' => '职称字段应为数字',
          ];
      }

       

  3. 上传字段说明
  4. 附件:上传模板
    http://cdn.tjmingzhou.com/yunde-mingdaodoc-batchAddUser.xlsx

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!