desc; //用户报告日期 $time_arr = ReportAnswer::where([ 'm_order_id'=> $order->id])->distinct('commitd_at')->pluck('commitd_at')->toArray(); sort($time_arr); $new_time_arr = []; if ($start_date && $end_date) { foreach ($time_arr as $time) { if (strtotime($time) >= strtotime($start_date) && strtotime($time) <= strtotime($end_date)) { $new_time_arr[] = $time; } } }else { $new_time_arr = $time_arr; } $php_word = new PhpWord(); //设置默认样式 $php_word->setDefaultFontName('宋体');//字体 $php_word->setDefaultFontSize(14);//字号 $section = $php_word->addSection(); //预定义样式 $imageStyle = ['width' => 150, 'height' => 150, 'align' => 'left']; $font_center = ['align'=>'center']; $font_left = ['align'=>'left']; $pic_height = $pic_width = 150; $php_word->addTitleStyle(1, ['bold' => true, 'color' => '000', 'size' => 22, 'name' => '宋体'],$font_center); $section->addTitle(ReportExport::filterOutXMLIllicit($activity_name). '用户报告',1); $section->addTextBreak(2); $php_word->addTitleStyle(2, ['bold' => true, 'color' => '000', 'size' => 16, 'name' => '宋体'],$font_left); $fontStyleName = 'textstyle'; $php_word->addFontStyle( $fontStyleName, array('name' => '宋体', 'size' => 16, 'color' => '1B2232', 'bold' => true) ); foreach ($new_time_arr as $time) { $section->addTitle('·'.date("Y-m-d", strtotime($time)), 2); $section->addTextBreak(1); $answers = ReportAnswer::with('question')->where(['m_order_id'=> $order->id])->where('commitd_at', $time)->get(); $index = 1; foreach ($answers as $answer) { if (empty($answer->question)) continue; $section->addText(ReportExport::filterOutXMLIllicit($index.'. '.$answer->question->title), $fontStyleName); if (is_array(json_decode($answer->answer2)) && count(json_decode($answer->answer2))) { $res = implode('、', json_decode($answer->answer2)); $section->addText(ReportExport::filterOutXMLIllicit($res), $fontStyleName); }elseif($answer->answer2) { $section->addText(ReportExport::filterOutXMLIllicit($answer->answer2), $fontStyleName); } $section->addText(ReportExport::filterOutXMLIllicit($answer->answer), $fontStyleName); if ($answer->pics) { $pics = json_decode($answer->pics, true); //添加表格 $styleTable = [ 'borderColor' => '006699', 'borderSize' => 6, 'cellMargin' => 50, ]; $php_word->addTableStyle('pic_table', $styleTable); $table = $section->addTable('pic_table'); $cell_index = 1; if ($pics && count($pics)) { foreach ($pics as $pic) { if ($cell_index % 3 == 1) { $table->addRow($pic_height); } if (!strstr($pic, 'x-oss-process')) { $pic = $pic.'?x-oss-process=image/auto-orient,1/resize,p_30/quality,q_80'; } try { $table->addCell($pic_width)->addImage($pic, $imageStyle); $cell_index++; }catch (\Exception $e) { Log::info($e->getMessage()); } } } } $index++; $section->addTextBreak(1); } $section->addTextBreak(1); } $writer = IOFactory::createWriter($php_word, 'Word2007'); $fileName = '用户报告--' . $order->name .$order->id. '.docx'; $path = storage_path('qrcode/') . $fileName; $writer->save($path); $url = ''; if (file_exists($path)) { $url = self::uploadFile($path); unlink($path); } ReportFile::updateOrCreate(['order_id'=>$order->id], ['url'=>$url, 'file_type'=>'docx']); // return $url; // return response()->download($url); } public static function uploadFile($file) { $ossClient = UploadController::getOssClient(); //生成file $object = date('Y').date('m')."/".date('d')."/".basename($file); $url = 'https://'.config('alioss.picture_domain').'/'.$object; try { $ossClient->uploadFile(config('alioss.buckets.picture'), $object, $file); } catch(\OSS\Core\OssException $e) { Log::info($e->getMessage()); return false; } return $url; } private static function filterOutXMLIllicit($htmlStr) { if (empty($htmlStr)) return $htmlStr; $xmlStr=str_replace('<','<',$htmlStr); $xmlStr=str_replace('>','>',$xmlStr); $xmlStr=str_replace('"','"',$xmlStr); $xmlStr=str_replace("'",''',$xmlStr); $xmlStr=str_replace("&",'&',$xmlStr); return $xmlStr; } }