Files
useds/app/common/controller/UsedCarController.php
T

209 lines
6.8 KiB
PHP
Raw Normal View History

2026-01-05 15:28:19 +08:00
<?php
namespace app\common\controller;
use app\common\controller\BaseController;
2026-01-09 13:08:17 +08:00
use app\common\model\UsedCar;
2026-01-12 11:52:07 +08:00
use app\common\model\UsedCarPdfLog;
2026-01-05 16:15:27 +08:00
use app\common\service\OssService;
2026-01-05 15:28:19 +08:00
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\UsedCarService;
2026-01-09 13:08:17 +08:00
use app\common\utils\pdf\service\MpdfDriver;
2026-01-09 16:17:50 +08:00
use support\exception\BusinessException;
2026-01-08 14:30:42 +08:00
use support\Request;
2026-01-05 15:28:19 +08:00
class UsedCarController extends BaseController
{
use CrudControllerTrait;
protected $service;
2026-01-09 15:03:30 +08:00
protected $noNeedLogin = ['downloadPdf'];
protected $noNeedHeaderData = ['downloadPdf'];
2026-01-05 15:28:19 +08:00
public function __construct()
{
$this->service = new UsedCarService();
}
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
2026-01-05 16:15:27 +08:00
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;
}
2026-01-05 17:30:53 +08:00
/**
* 列表页数据格式化
*
* @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;
2026-01-09 15:32:27 +08:00
}else{
$data['images_pull'] = [];
2026-01-09 13:08:17 +08:00
}
if (!empty($data['attachment'])){
2026-01-09 15:03:30 +08:00
//$attachments = explode(',', $data['attachment']);
$attachments = json_decode($data['attachment'], true);
2026-01-09 13:08:17 +08:00
$ossService = new OssService();
$attachment_pull = [];
foreach ($attachments as $attachment){
2026-01-09 15:03:30 +08:00
$attachment_pull[] = [
'name'=>$attachment['name'],
'src'=>$ossService->generatePrivateUrl($attachment['src'])
];
2026-01-09 13:08:17 +08:00
}
$data['attachment_pull'] = $attachment_pull;
2026-01-09 15:32:27 +08:00
}else{
$data['attachment_pull'] = [];
2026-01-05 17:30:53 +08:00
}
2026-01-09 15:32:27 +08:00
2026-01-05 17:30:53 +08:00
return $data;
}
2026-01-08 14:30:42 +08:00
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());
}
}
2026-01-09 13:08:17 +08:00
2026-01-12 11:52:07 +08:00
/**
* 生成pdf
* @param Request $request
* @return \support\Response
*/
2026-01-09 13:08:17 +08:00
public function producePdf(Request $request){
try {
2026-01-12 11:52:07 +08:00
$price = $request->get('price');
if(empty($price)){
throw new BusinessException("请输入生成pdf中的价格");
}
2026-01-09 13:08:17 +08:00
$raw = $this->service->serviceShow($request);
$data = $this->transformData($raw, method_exists($this, 'showFormat')
? [$this, 'showFormat']
: null);
$images = [];
2026-01-12 11:52:07 +08:00
foreach ($data['images_pull'] as $image){
2026-01-09 13:08:17 +08:00
$images[] = $this->service->downloadImage($image);
}
$data['images'] = $images;
2026-01-09 15:03:30 +08:00
$attachments = [];
foreach ($data['attachment_pull'] as $attachment){
// 方法1:使用 pathinfo
$path = parse_url($attachment['src'], PHP_URL_PATH); // 获取路径部分
$extension = pathinfo($path, PATHINFO_EXTENSION); // 获取扩展名
if (strtolower($extension) === 'pdf') { //如果是pdf则下载到本地
$attachments[] = $this->service->downloadAttachment($attachment['src']);
}
}
$data['attachment'] = $attachments;
2026-01-12 13:40:57 +08:00
$data['price'] = $price;
2026-01-09 15:03:30 +08:00
2026-01-09 13:08:17 +08:00
$pdf = (new MpdfDriver())->exportUsedCarPdf($data);
UsedCar::where('id', $data['id'])->update(['pdf_src' => $pdf]);
array_map('unlink', glob(runtime_path('pdf/images/*.jpg')));
2026-01-09 15:03:30 +08:00
array_map('unlink', glob(runtime_path('pdf/attachment/*.pdf')));
2026-01-12 11:52:07 +08:00
UsedCarPdfLog::create([
'user_id' => $request->user['id'],
'used_car_id' => $request->get('id'),
'price_snapshot' => $price,
'pdf_path' => $pdf,
]);
2026-01-09 13:08:17 +08:00
return $this->success($pdf);
} catch (\Throwable $e) {
2026-01-09 16:17:50 +08:00
return $this->error($e->getMessage());
2026-01-09 13:08:17 +08:00
}
}
public function downloadPdf(Request $request){
$raw = $this->service->serviceShow($request);
2026-01-09 15:03:30 +08:00
$filePath = runtime_path('pdf') .'/'. $raw['pdf_src'];
2026-01-09 13:08:17 +08:00
if (!file_exists($filePath)) {
2026-01-09 15:03:30 +08:00
return response($filePath.'文件不存在', 404);
2026-01-09 13:08:17 +08:00
}
return response()->download(
$filePath,
2026-01-09 15:03:30 +08:00
$raw['product_name'].'-'.$raw['id'].'.pdf' // 下载时显示的文件名
2026-01-09 13:08:17 +08:00
);
}
2026-01-09 16:17:50 +08:00
public function getPdf(Request $request)
{
try {
$raw = $this->service->serviceShow($request);
$filePath = runtime_path('pdf') . '/' . $raw['pdf_src'];
if (empty($raw['pdf_src']) || !file_exists($filePath)) {
throw new BusinessException('文件不存在');
}
// 确保 public/pdf 存在
$publicDir = public_path('pdf');
if (!is_dir($publicDir)) {
mkdir($publicDir, 0755, true);
}
// 拷贝到 public 目录
$publicFile = $publicDir . '/' . $raw['pdf_src'];
if (!file_exists($publicFile)) {
copy($filePath, $publicFile);
}
// 返回访问地址
$baseUrl = 'https://' . request()->header('host');
$url = $baseUrl . '/pdf/' . $raw['pdf_src'];
return $this->success([
'pdf_url' => $url,
'file_name' => $raw['product_name'] . '-' . $raw['id'] . '.pdf'
]);
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}
}
2026-01-05 15:28:19 +08:00
}