request->get(); $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT')); $start = $this->request->get('page', 1); $where = []; if (isset($param['key']) && !empty($param['key'])) { $where[] = ['phone', 'like', "%{$param['key']}%"]; } $obj = new WechatUserModel(); $listObj = $obj->order('create_time', 'DESC') ->where($where) ->field('id,openid,headimgurl,phone,create_time,status,last_login_time') ->paginate(['page' => $start, 'list_rows' => $limit]) ->each(function ($item, $key) { $item->last_login_time = $item->last_login_time ? date("Y-m-d H:i:s", $item->last_login_time) : ''; })->toArray(); $listInfo = $listObj['data']; return $this->buildSuccess([ 'list' => $listInfo, 'count' => $listObj['total'] ]); } /** * 下载导入模板 * @return void * @throws \PHPExcel_Exception * @throws \PHPExcel_Reader_Exception * @throws \PHPExcel_Writer_Exception */ public function exportTemplate() { $name = '导入用户信息模板'; $header = ['手机号', '邮箱编号', '用户编号', '纳税人识别号', '抬头类型(单位、个人)']; ExportService::excelExport($name, $header); } /** * 导入用户 * @return Response */ public function importUser():Response { try { $file = $this->request->file('file'); if (empty($file)) { throw new \Exception('没有上传的文件'); } $max_size = '10'; $mimetype = 'xls,xlsx'; $fileInfo = []; $fileInfo['name'] = $file->getOriginalName(); //上传文件名 $fileInfo['type'] = $file->getOriginalMime(); //上传文件类型信息 $fileInfo['tmp_name'] = $file->getPathname(); $fileInfo['size'] = $file->getSize(); if ($fileInfo['size'] > $max_size) { throw new \Exception('文件超过配置的最大值' . $max_size . 'M'); } $suffix = strtolower( pathinfo($fileInfo['name'], PATHINFO_EXTENSION) ); $suffix = $suffix && preg_match('/^[a-zA-Z0-9]+$/', $suffix) ? $suffix : 'file'; $mimetypeArr = explode(',', strtolower($mimetype)); $typeArr = explode('/', $fileInfo['type']); //禁止上传PHP和HTML文件 if ( in_array($fileInfo['type'], ['text/x-php', 'text/html']) || in_array($suffix, ['php', 'html', 'htm']) ) { throw new \Exception('该文件类型不可上传'); } //验证文件后缀 if ((!in_array($suffix, $mimetypeArr) || (stripos($typeArr[0] . '/', $mimetype) !== false && !in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr) )) ) { throw new \Exception('该文件类型不可上传'); } switch($suffix){ case 'xls': $obj = \PHPExcel_IOFactory::createReader('Excel5'); break; case 'xlsx': $obj = \PHPExcel_IOFactory::createReader('Excel2007'); break; default: throw new \Exception('该文件类型不可上传'); } $obj_PHPExcel = $obj->load($fileInfo['tmp_name']); $excel_array = $obj_PHPExcel->getSheet(0)->toArray(); array_shift($excel_array); foreach ($excel_array as $data) { // 验证不能为空 $is_empty = false; foreach ($data as $value) { if (empty($value)) { $is_empty = true; break; } } if ($is_empty) { continue; } $phone = $data[0]; $email = $data[1]; $pucode = $data[2]; $taxId = $data[3]; $type = $data[4]; if (!preg_match('/^1[3-9]\d{9}$/', $phone)) { continue; } if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { continue; } $str = '0-9A-HJ-NPQRTUWXY'; $pattern = '/^[' . $str . ']{2}\d{6}[' . $str . ']{10}$/'; if (!preg_match($pattern, $taxId)) { continue; } $typeArr = ['单位', '个人']; if (!in_array($type, $typeArr)) { continue; } $type = array_search($type, $typeArr); UserService::addUser($phone, $email, $pucode, $taxId, $type); } return $this->buildSuccess(); } catch (\Exception $e) { return $this->buildFailed(400, $e->getMessage()); } } }