127 lines
4.0 KiB
PHP
127 lines
4.0 KiB
PHP
<?php
|
|
namespace app\common\controller;
|
|
|
|
use app\common\controller\BaseController;
|
|
use app\common\model\UsedCar;
|
|
use app\common\service\OssService;
|
|
use app\common\traits\crud\CrudControllerTrait;
|
|
use app\common\service\UsedCarService;
|
|
use app\common\utils\pdf\service\MpdfDriver;
|
|
use support\Request;
|
|
|
|
class UsedCarController extends BaseController
|
|
{
|
|
use CrudControllerTrait;
|
|
|
|
protected $service;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->service = new UsedCarService();
|
|
}
|
|
|
|
/**
|
|
* 列表页数据格式化
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
protected function indexFormat($data)
|
|
{
|
|
if (isset($data['created_at']))
|
|
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
|
if (isset($data['updated_at']))
|
|
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
|
if (!empty($data['images'])){
|
|
$images = explode(',', $data['images']);
|
|
$ossService = new OssService();
|
|
$images_pull = [];
|
|
foreach ($images as $image){
|
|
$images_pull[] = $ossService->generatePrivateUrl($image);
|
|
}
|
|
$data['images_pull'] = $images_pull;
|
|
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 列表页数据格式化
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
protected function showFormat($data)
|
|
{
|
|
if (isset($data['created_at']))
|
|
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
|
if (isset($data['updated_at']))
|
|
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
|
if (!empty($data['images'])){
|
|
$images = explode(',', $data['images']);
|
|
$ossService = new OssService();
|
|
$images_pull = [];
|
|
foreach ($images as $image){
|
|
$images_pull[] = $ossService->generatePrivateUrl($image);
|
|
}
|
|
$data['images_pull'] = $images_pull;
|
|
|
|
}
|
|
if (!empty($data['attachment'])){
|
|
$attachments = explode(',', $data['attachment']);
|
|
$ossService = new OssService();
|
|
$attachment_pull = [];
|
|
foreach ($attachments as $attachment){
|
|
$attachment_pull[] = $ossService->generatePrivateUrl($attachment);
|
|
}
|
|
$data['attachment_pull'] = $attachment_pull;
|
|
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public function crawlKeyword(Request $request){
|
|
try {
|
|
// $keyword = $request->get('keyword');
|
|
$data = $this->service->crawlKeyword($request);
|
|
return $this->success($data);
|
|
} catch (\Throwable $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function producePdf(Request $request){
|
|
try {
|
|
$raw = $this->service->serviceShow($request);
|
|
$data = $this->transformData($raw, method_exists($this, 'showFormat')
|
|
? [$this, 'showFormat']
|
|
: null);
|
|
$images = [];
|
|
foreach ($data['images_pull'] as $key => $image){
|
|
$images[] = $this->service->downloadImage($image);
|
|
}
|
|
$data['images'] = $images;
|
|
$pdf = (new MpdfDriver())->exportUsedCarPdf($data);
|
|
UsedCar::where('id', $data['id'])->update(['pdf_src' => $pdf]);
|
|
|
|
array_map('unlink', glob(runtime_path('pdf/images/*.jpg')));
|
|
return $this->success($pdf);
|
|
} catch (\Throwable $e) {
|
|
return $this->error($e->getMessage().$e->getLine().$e->getFile());
|
|
}
|
|
}
|
|
|
|
public function downloadPdf(Request $request){
|
|
$raw = $this->service->serviceShow($request);
|
|
$filePath = runtime_path('pdf') . $raw['pdf_src'];
|
|
|
|
if (!file_exists($filePath)) {
|
|
return response('文件不存在', 404);
|
|
}
|
|
|
|
return response()->download(
|
|
$filePath,
|
|
$raw['product_name'].'-'.$raw['id'].'pdf' // 下载时显示的文件名
|
|
);
|
|
}
|
|
} |