setName('exportexcel') ->setDescription('the exportexcel command'); } protected function execute(Input $input, Output $output) { set_time_limit(0); // 无时间限制 ini_set('memory_limit', '512M'); // 增加内存限制 $this->runData(); // 指令输出 $output->writeln('exportexcel'); } protected function runData() { $filename = 'export_data.xls'; $path = public_path() . '/' . $filename; $file = new File($path); $fileInfo = []; //$fileInfo['name'] = $file->getOriginalName(); //上传文件名 //$fileInfo['type'] = $file->getOriginalMime(); //上传文件类型信息 $fileInfo['tmp_name'] = $file->getPathname(); //$fileInfo['size'] = $file->getSize(); $obj = \PHPExcel_IOFactory::createReader('Excel5'); $obj_PHPExcel = $obj->load($fileInfo['tmp_name']); $excel_array = $obj_PHPExcel->getSheet(0)->toArray(); $error_array = []; foreach ($excel_array as $key => $data) { $key = $key + 1; $phone = $data[0]; $email = $data[1] ?? ''; $pucode = $data[2]; $taxId = $data[3] ?? ''; $type = $data[4]; // 验证不能为空 if (empty($phone) || empty($pucode) || empty($type)) { continue; } if (!preg_match('/^1[3-9]\d{9}$/', $phone)) { $error_array[] = "第{$key}行:手机号格式不符合"; continue; } $FeeService = new FeeService($pucode); $feeUserInfo = $FeeService->getUsers(); if ($feeUserInfo['UserCode'] != $pucode) { $error_array[] = "第{$key}行:用户编号不存在"; continue; } if ($feeUserInfo['CellPhone'] != $phone) { $error_array[] = "第{$key}行:手机号或用户编号错误"; continue; } if ($email) { if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { $error_array[] = "第{$key}行:邮箱格式不符合"; continue; } } if ($taxId) { // $str = '0-9A-HJ-NPQRTUWXY'; // $pattern = '/^[' . $str . ']{2}\d{6}[' . $str . ']{10}$/'; // if (!preg_match($pattern, $taxId)) { // $error_array[] = "第{$key}行:纳税人编号格式不符合"; // continue; // } } $typeArr = ['单位', '个人']; if (!in_array($type, $typeArr)) { $error_array[] = "第{$key}行:抬头类型不符合"; continue; } $type = array_search($type, $typeArr); UserService::addUser((int)$phone, $email, $pucode, $taxId, (int)$type); } if (!empty($error_array)) { \think\facade\Log::info(json_encode($error_array)); } } }