379 lines
10 KiB
PHP
379 lines
10 KiB
PHP
<?php
|
|
|
|
namespace app\controller;
|
|
|
|
use app\Service\PdfService;
|
|
use app\Service\UsedCarService;
|
|
use Webman\Http\Request;
|
|
use Webman\Http\Response;
|
|
|
|
/**
|
|
* 二手车控制器
|
|
*/
|
|
class UsedCarController extends BaseController
|
|
{
|
|
private UsedCarService $service;
|
|
private PdfService $pdfService;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->service = new UsedCarService();
|
|
$this->pdfService = new PdfService();
|
|
}
|
|
|
|
/**
|
|
* 获取二手车列表
|
|
*/
|
|
public function list(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$type = $request->get('type', 'self');
|
|
$page = (int)$request->get('page', 1);
|
|
$pageSize = (int)$request->get('page_size', 20);
|
|
$keyword = $request->get('keyword');
|
|
$status = $request->get('status');
|
|
|
|
try {
|
|
$result = $this->service->getList(
|
|
$userId,
|
|
$type,
|
|
$page,
|
|
$pageSize,
|
|
$keyword,
|
|
$status !== null ? (int)$status : null
|
|
);
|
|
|
|
return $this->success($result);
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取二手车详情
|
|
*/
|
|
public function detail(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int)$request->get('id');
|
|
|
|
if ($id <= 0) {
|
|
return $this->error('无效的ID');
|
|
}
|
|
|
|
try {
|
|
$detail = $this->service->getDetail($id, $userId);
|
|
return $this->success($detail);
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建二手车记录
|
|
*/
|
|
public function create(Request $request): Response
|
|
{
|
|
$userId = $this->requireAuth($request);
|
|
$data = $request->post();
|
|
|
|
try {
|
|
$car = $this->service->create($data, $userId);
|
|
return $this->success($car->toArray(), '创建成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新二手车记录
|
|
*/
|
|
public function update(Request $request): Response
|
|
{
|
|
$userId = $this->requireAuth($request);
|
|
$id = (int)$request->get('id');
|
|
$data = $request->post();
|
|
|
|
if ($id <= 0) {
|
|
return $this->error('无效的ID');
|
|
}
|
|
|
|
try {
|
|
$car = $this->service->update($id, $data, $userId);
|
|
return $this->success($car->toArray(), '更新成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除二手车记录
|
|
*/
|
|
public function delete(Request $request): Response
|
|
{
|
|
$userId = $this->requireAuth($request);
|
|
$id = (int)$request->get('id');
|
|
|
|
if ($id <= 0) {
|
|
return $this->error('无效的ID');
|
|
}
|
|
|
|
try {
|
|
$this->service->delete($id, $userId);
|
|
return $this->success(null, '删除成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新状态
|
|
*/
|
|
public function updateStatus(Request $request): Response
|
|
{
|
|
$userId = $this->requireAuth($request);
|
|
$id = (int)$request->post('id');
|
|
$status = (int)$request->post('status');
|
|
|
|
if ($id <= 0) {
|
|
return $this->error('无效的ID');
|
|
}
|
|
|
|
try {
|
|
$this->service->updateStatus($id, $status, $userId);
|
|
return $this->success(null, '状态更新成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* URL抓取车辆信息
|
|
*/
|
|
public function crawlByUrl(Request $request): Response
|
|
{
|
|
$this->requireAuth($request);
|
|
$url = $request->post('url') ?: $request->get('url');
|
|
|
|
if (empty($url)) {
|
|
return $this->error('URL不能为空');
|
|
}
|
|
|
|
try {
|
|
$data = $this->service->crawlByUrl($url);
|
|
return $this->success($data);
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 移动端链接抓取(处理短链)
|
|
*/
|
|
public function crawlMobileUrl(Request $request): Response
|
|
{
|
|
$this->requireAuth($request);
|
|
$url = $request->post('url') ?: $request->get('url');
|
|
|
|
if (empty($url)) {
|
|
return $this->error('URL不能为空');
|
|
}
|
|
|
|
try {
|
|
$data = $this->service->crawlByUrl($url);
|
|
return $this->success($data);
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 关键词搜索懂车帝车型数据
|
|
*/
|
|
public function crawlKeyword(Request $request): Response
|
|
{
|
|
$this->requireAuth($request);
|
|
$keyword = $request->post('keyword') ?: $request->get('keyword');
|
|
|
|
if (empty($keyword)) {
|
|
return $this->error('关键词不能为空');
|
|
}
|
|
|
|
try {
|
|
$crawlService = new \app\Service\UsedCarCrawlService();
|
|
$data = $crawlService->searchByKeyword($keyword);
|
|
return $this->success($data);
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 生成PDF
|
|
*/
|
|
public function generatePdf(Request $request): Response
|
|
{
|
|
$userId = $this->requireAuth($request);
|
|
$id = (int)$request->post('id');
|
|
$price = (float)$request->post('price');
|
|
$lang = $request->post('lang', 'zh');
|
|
$currency = $request->post('currency', 'CNY');
|
|
$tradeTerm = $request->post('trade_term', '');
|
|
|
|
if ($id <= 0) {
|
|
return $this->error('无效的ID');
|
|
}
|
|
|
|
if ($price <= 0) {
|
|
return $this->error('请输入有效的价格');
|
|
}
|
|
|
|
try {
|
|
$pdfFileName = $this->pdfService->generateUsedCarPdf(
|
|
$id,
|
|
$userId,
|
|
$price,
|
|
$lang,
|
|
$currency,
|
|
$tradeTerm
|
|
);
|
|
|
|
return $this->success([
|
|
'pdf_file_name' => $pdfFileName,
|
|
], 'PDF生成成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 下载PDF
|
|
*/
|
|
public function downloadPdf(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int)$request->get('id');
|
|
|
|
if ($id <= 0) {
|
|
return $this->error('无效的ID');
|
|
}
|
|
|
|
try {
|
|
$car = \app\Model\UsedCar::findOrFail($id);
|
|
|
|
if (empty($car->pdf_src)) {
|
|
return $this->error('PDF文件不存在,请先生成PDF');
|
|
}
|
|
|
|
$pdfPath = $this->pdfService->getPdfPath($car->pdf_src);
|
|
|
|
if (!$this->pdfService->pdfExists($car->pdf_src)) {
|
|
return $this->error('PDF文件不存在');
|
|
}
|
|
|
|
// 设置下载的文件名
|
|
$filename = $car->product_name . '-' . $car->code . '-' . $car->id . '.pdf';
|
|
|
|
// 返回文件下载响应
|
|
return response()->file($pdfPath, $filename);
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取PDF预览URL
|
|
*/
|
|
public function getPdfUrl(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int)$request->get('id');
|
|
|
|
if ($id <= 0) {
|
|
return $this->error('无效的ID');
|
|
}
|
|
|
|
try {
|
|
$car = \app\Model\UsedCar::findOrFail($id);
|
|
|
|
if (empty($car->pdf_src)) {
|
|
return $this->error('PDF文件不存在,请先生成PDF');
|
|
}
|
|
|
|
if (!$this->pdfService->pdfExists($car->pdf_src)) {
|
|
return $this->error('PDF文件不存在');
|
|
}
|
|
|
|
// 将PDF复制到public目录以便访问
|
|
$publicPdfDir = public_path('pdf');
|
|
if (!is_dir($publicPdfDir)) {
|
|
mkdir($publicPdfDir, 0755, true);
|
|
}
|
|
|
|
$publicPdfPath = $publicPdfDir . '/' . $car->pdf_src;
|
|
if (!file_exists($publicPdfPath)) {
|
|
copy($this->pdfService->getPdfPath($car->pdf_src), $publicPdfPath);
|
|
}
|
|
|
|
// 构建访问URL
|
|
$baseUrl = $this->getBaseUrl($request);
|
|
$pdfUrl = $baseUrl . '/pdf/' . $car->pdf_src;
|
|
|
|
return $this->success([
|
|
'pdf_url' => $pdfUrl,
|
|
'file_name' => $car->product_name . '-' . $car->code . '-' . $car->id . '.pdf',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 预览PDF(直接返回PDF内容)
|
|
*/
|
|
public function previewPdf(Request $request): Response
|
|
{
|
|
$id = (int)$request->get('id');
|
|
|
|
if ($id <= 0) {
|
|
return $this->error('无效的ID');
|
|
}
|
|
|
|
try {
|
|
$car = \app\Model\UsedCar::findOrFail($id);
|
|
|
|
if (empty($car->pdf_src)) {
|
|
return $this->error('PDF文件不存在,请先生成PDF');
|
|
}
|
|
|
|
$pdfPath = $this->pdfService->getPdfPath($car->pdf_src);
|
|
|
|
if (!$this->pdfService->pdfExists($car->pdf_src)) {
|
|
return $this->error('PDF文件不存在');
|
|
}
|
|
|
|
// 返回PDF内容
|
|
$content = file_get_contents($pdfPath);
|
|
|
|
return response($content, 200, [
|
|
'Content-Type' => 'application/pdf',
|
|
'Access-Control-Allow-Origin' => '*',
|
|
'Access-Control-Allow-Methods' => 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers' => '*',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取基础URL
|
|
*/
|
|
private function getBaseUrl(Request $request): string
|
|
{
|
|
$scheme = $request->header('x-forwarded-proto') ?? $request->header('x-scheme') ?? 'https';
|
|
$host = $request->host();
|
|
return $scheme . '://' . $host;
|
|
}
|
|
}
|