pdf改版

This commit is contained in:
2026-01-14 18:37:15 +08:00
parent 539494282c
commit 2126e65397
8 changed files with 465 additions and 26 deletions
+184 -2
View File
@@ -6,9 +6,11 @@ use app\common\model\UsedCar;
use app\common\model\UsedCarPdfLog;
use app\common\model\User;
use app\common\service\OssService;
use app\common\service\SparkService;
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\UsedCarService;
use app\common\utils\pdf\service\MpdfDriver;
use app\queue\utils\XunfeiFanyi;
use support\exception\BusinessException;
use support\Request;
@@ -22,6 +24,19 @@ class UsedCarController extends BaseController
protected $noNeedHeaderData = ['downloadPdf'];
protected $lang = [
'en' => '英语',
'ru' => '俄语',
'es' => '西班牙语',
'ar' => '阿拉伯语',
];
protected $currency = [
'USD' => '$',
'RUB' => '₽',
'AED' => 'د.إ',
'EUR' => '€',
];
public function __construct()
{
$this->service = new UsedCarService();
@@ -114,9 +129,17 @@ class UsedCarController extends BaseController
public function producePdf(Request $request){
try {
$price = $request->get('price');
$lang = $request->get('lang');
$currency = $request->get('currency');
if(empty($price)){
throw new BusinessException("请输入生成pdf中的价格");
}
if(empty($lang) || empty($this->lang[$lang])){
throw new BusinessException("语种错误");
}
if(empty($currency) || empty($this->currency[$currency])){
throw new BusinessException("币种错误");
}
$raw = $this->service->serviceShow($request);
$data = $this->transformData($raw, method_exists($this, 'showFormat')
? [$this, 'showFormat']
@@ -146,7 +169,12 @@ class UsedCarController extends BaseController
$user['social_code'] = $ossService->generatePrivateUrl($user['social_code']);
$user['social_code'] = $this->service->downloadImage($user['social_code']);
}
$pdf = (new MpdfDriver())->exportUsedCarPdf($data,$user,$contact);
$field = $this->field($lang);//字段相关外文
$field['currency'] = $this->currency[$currency];
$data = $this->fanyi($data, $lang);//车辆字段翻译外文
$contactField = $this->contact($lang); //名片相关外文
$pdf = (new MpdfDriver())->exportUsedCarPdf($field,$data,$user,$contactField,$contact);
UsedCar::where('id', $data['id'])->update(['pdf_src' => $pdf]);
array_map('unlink', glob(runtime_path('pdf/images/*.jpg')));
@@ -160,7 +188,7 @@ class UsedCarController extends BaseController
]);
return $this->success($pdf);
} catch (\Throwable $e) {
return $this->error($e->getMessage());
return $this->error($e->getMessage().$e->getLine().$e->getFile());
}
}
@@ -178,6 +206,160 @@ class UsedCarController extends BaseController
);
}
public function fanyi($data, $to){
foreach ($data as $k => $v) {
if (in_array($k, ['product_name', 'source_place', 'body_color', 'condition_desc']) && !empty(trim($v))) {
// $xunfei = new XunfeiFanyi();
// $data[$k] = $xunfei->fanyi($v, $from = 'cn', $to);
$toLang = $this->lang[$to];
$spark = new SparkService();
$str = $spark->chat("将“{$v}”翻译为{$toLang},不要有多余的描述");
$str = preg_replace('/[\x{4e00}-\x{9fa5}]/u', '', $str);
$data[$k] =str_replace(['(',')','(',')','"'], '', $str);;
}
}
return $data;
}
/**
* 字段名
* @param $data
* @param $to
* @return mixed
*/
public function field($to){
$field = [
'cn' => [
'price' => '车价',
'source_place' => '车源地',
'plate_time' => '上牌时间',
'displacement' => '排量',
'mileage' => '里程',
'power' => '马力',
'body_color' => '车身颜色',
'created_at' => '创建时间',
'condition_desc' => '车况介绍',
'images' => '产品图',
],
'en' => [
'price' => 'Price',
'source_place' => 'Vehicle Location',
'plate_time' => 'Registration Date',
'displacement' => 'Engine Displacement',
'mileage' => 'Mileage',
'power' => 'Horsepower',
'body_color' => 'Body Color',
'created_at' => 'Created At',
'condition_desc' => 'Vehicle Condition',
'images' => 'Images',
],
'ru' => [
'price' => 'Цена',
'source_place' => 'Местонахождение автомобиля',
'plate_time' => 'Дата регистрации',
'displacement' => 'Объём двигателя',
'mileage' => 'Пробег',
'power' => 'Мощность',
'body_color' => 'Цвет кузова',
'created_at' => 'Дата создания',
'condition_desc' => 'Состояние автомобиля',
'images' => 'Фотографии',
],
'es' => [
'price' => 'Precio',
'source_place' => 'Ubicación del vehículo',
'plate_time' => 'Fecha de matriculación',
'displacement' => 'Cilindrada',
'mileage' => 'Kilometraje',
'power' => 'Potencia',
'body_color' => 'Color de la carrocería',
'created_at' => 'Fecha de creación',
'condition_desc' => 'Estado del vehículo',
'images' => 'Imágenes',
],
'ar' => [
'price' => 'السعر',
'source_place' => 'موقع السيارة',
'plate_time' => 'تاريخ التسجيل',
'displacement' => 'سعة المحرك',
'mileage' => 'المسافة المقطوعة',
'power' => 'قوة المحرك',
'body_color' => 'لون الهيكل',
'created_at' => 'تاريخ الإنشاء',
'condition_desc' => 'حالة السيارة',
'images' => 'صور المنتج',
],
];
return $field[$to];
// foreach ($field as $k => $v) {
//// $xunfei = new XunfeiFanyi();
//// $field[$k] = $xunfei->fanyi($v, $from = 'cn', $to);
// $toLang = $this->lang[$to];
// $spark = new SparkService();
// $field[$k] = $spark->chat("“{$v}”翻译为{$toLang},不要有多余的描述");
// }
// return $field;
}
/**
* 名片外文
*/
public function contact($to){
$contactField = [
'cn' => [
'title' => '联系人名片',
'name' => '姓名',
'email' => '邮箱',
'phone' => '电话',
'WhatsApp' => 'WhatsApp',
'code' => '二维码',
],
'en' => [
'title' => 'Contact Name Card',
'name' => 'Name',
'email' => 'Email',
'phone' => 'Phone',
'WhatsApp' => 'WhatsApp',
'code' => 'QR Code',
],
'ru' => [
'title' => 'Контактная карточка',
'name' => 'Имя',
'email' => 'Электронная почта',
'phone' => 'Телефон',
'WhatsApp' => 'WhatsApp',
'code' => 'QR-код',
],
'es' => [
'title' => 'Tarjeta de visita de contacto',
'name' => 'Nombre',
'email' => 'Correo electrónico',
'phone' => 'Teléfono',
'WhatsApp' => 'WhatsApp',
'code' => 'Código QR',
],
'ar' => [
'title' => 'بطاقة الاتصال',
'name' => 'الاسم',
'email' => 'البريد الإلكتروني',
'phone' => 'الهاتف',
'WhatsApp' => 'واتساب',
'code' => 'رمز QR',
],
];
return $contactField[$to];
}
public function getPdf(Request $request)
{
try {