Merge branch 'develop'
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
// app/service/ResourceService.php
|
||||
|
||||
namespace app\common\utils;
|
||||
|
||||
use support\Db;
|
||||
|
||||
class MigrationResources
|
||||
{
|
||||
/**
|
||||
* 获取完整的资源树结构(不区分系统)
|
||||
*
|
||||
* @param array $conditions 筛选条件
|
||||
* @return array
|
||||
*/
|
||||
public function getFullResourceTree($table = 'resources', array $conditions = []): array
|
||||
{
|
||||
// 查询所有资源数据
|
||||
$query = Db::table($table)->where($conditions);
|
||||
|
||||
// 按排序字段排序
|
||||
$resources = $query->orderBy('sort')->orderBy('id')->get();
|
||||
// var_dump($resources->count());
|
||||
// 转换为数组
|
||||
$resourceList = [];
|
||||
foreach ($resources as $resource) {
|
||||
$resourceList[] = (array) $resource;
|
||||
}
|
||||
|
||||
// 构建树状结构
|
||||
return $this->buildTree($resourceList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建树状结构
|
||||
*
|
||||
* @param array $resources 资源列表
|
||||
* @param int $parentId 父级ID
|
||||
* @return array
|
||||
*/
|
||||
private function buildTree(array $resources, int $parentId = 0): array
|
||||
{
|
||||
$tree = [];
|
||||
|
||||
foreach ($resources as $resource) {
|
||||
if ($resource['parent_id'] == $parentId) {
|
||||
// 递归获取子节点
|
||||
$children = $this->buildTree($resources, $resource['id']);
|
||||
|
||||
// 如果有子节点,则添加到children字段
|
||||
if (!empty($children)) {
|
||||
$resource['children'] = $children;
|
||||
}
|
||||
|
||||
$tree[] = $resource;
|
||||
}
|
||||
}
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用引用方式构建树(性能优化版)
|
||||
*
|
||||
* @param array $conditions 筛选条件
|
||||
* @return array
|
||||
*/
|
||||
public function getFullResourceTreeOptimized($table = 'resources', array $conditions = []): array
|
||||
{
|
||||
// 查询所有资源数据
|
||||
$query = Db::table($table)->where($conditions);
|
||||
|
||||
// 按排序字段排序
|
||||
$resources = $query->orderBy('sort')->orderBy('id')->get();
|
||||
|
||||
// 构建ID映射和引用关系
|
||||
$resourceMap = [];
|
||||
$tree = [];
|
||||
|
||||
// 初始化所有节点
|
||||
foreach ($resources as $resource) {
|
||||
$resourceArray = (array) $resource;
|
||||
$resourceMap[$resourceArray['id']] = $resourceArray;
|
||||
}
|
||||
|
||||
// 构建父子关系
|
||||
foreach ($resourceMap as $id => &$node) {
|
||||
if ($node['parent_id'] == 0) {
|
||||
// 根节点
|
||||
$tree[] = &$node;
|
||||
} else {
|
||||
// 子节点
|
||||
if (isset($resourceMap[$node['parent_id']])) {
|
||||
if (!isset($resourceMap[$node['parent_id']]['children'])) {
|
||||
$resourceMap[$node['parent_id']]['children'] = [];
|
||||
}
|
||||
$resourceMap[$node['parent_id']]['children'][] = &$node;
|
||||
} else {
|
||||
// 父节点不存在,作为根节点处理
|
||||
$tree[] = &$node;
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($node);
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取扁平化的资源列表
|
||||
*
|
||||
* @param array $conditions 筛选条件
|
||||
* @return array
|
||||
*/
|
||||
public function getResourceList($table = 'resources', array $conditions = []): array
|
||||
{
|
||||
$query = Db::table($table)->where($conditions);
|
||||
|
||||
// 按排序字段排序
|
||||
$resources = $query->orderBy('sort')->orderBy('id')->get();
|
||||
|
||||
// 转换为数组
|
||||
$resourceList = [];
|
||||
foreach ($resources as $resource) {
|
||||
$resourceList[] = (array) $resource;
|
||||
}
|
||||
|
||||
return $resourceList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 递归插入资源树
|
||||
*/
|
||||
private function insertResourceTree($table, $tree, $parentId, $keys = [])
|
||||
{
|
||||
$keys = ['system_id','name','desc','type','code','parent_id','url','icon','open_type','link',
|
||||
'is_hide','is_hide_tab','no_auth','is_home','instance','keep_alive','sort','status'];
|
||||
foreach ($tree as $item) {
|
||||
// 保存子节点
|
||||
$children = $item['children'] ?? [];
|
||||
|
||||
// 过滤字段
|
||||
$filteredItem = [];
|
||||
foreach ($item as $k => $v) {
|
||||
if (in_array($k, $keys)) {
|
||||
$filteredItem[$k] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
// 设置parent_id
|
||||
$filteredItem['parent_id'] = $parentId;
|
||||
|
||||
// 插入并获取新ID
|
||||
$newId = Db::table($table)->insertGetId($filteredItem);
|
||||
echo "插入节点: {$filteredItem['name']}, 新ID: $newId\n";
|
||||
|
||||
// 递归处理子节点
|
||||
if (!empty($children)) {
|
||||
$this->insertResourceTree($table, $children, $newId, $keys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function do(string $from = 'resources_20250916_bak', string $to = 'resources_yuanshi', array $where = [])
|
||||
{
|
||||
try {
|
||||
// 开启事务
|
||||
Db::beginTransaction();
|
||||
|
||||
// 查询迁移资源的树状结果集
|
||||
$list = $this->getFullResourceTreeOptimized($from, $where);
|
||||
|
||||
// 入库字段
|
||||
$keys = ['system_id','name','desc','type','code','parent_id','url','icon','open_type','link',
|
||||
'is_hide','is_hide_tab','no_auth','is_home','instance','keep_alive','sort','status'];
|
||||
|
||||
// 使用递归处理树结构
|
||||
$this->insertResourceTree($to, $list, 0, $keys);
|
||||
|
||||
// 提交事务
|
||||
Db::commit();
|
||||
|
||||
echo "数据插入成功";
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// 回滚事务
|
||||
Db::rollBack();
|
||||
|
||||
echo "数据插入失败: " . $e->getMessage() . " 在文件 " . $e->getFile() . " 第 " . $e->getLine() . " 行";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\utils\approval\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
class ApprovalCcs extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'approval_ccs';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'instance_id', 'user_id', 'notified_at', 'read_at', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\utils\approval\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
class ApprovalFlows extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'approval_flows';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'name', 'type', 'steps', 'is_active', 'timeout_hours', 'timeout_action', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\utils\approval\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
class ApprovalInstances extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'approval_instances';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'flow_type', 'biz_id', 'biz_type', 'form_data', 'initiator_id', 'status', 'current_step', 'finished_at', 'deadline_at', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\utils\approval\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
class ApprovalRecords extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'approval_records';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'instance_id', 'approver_id', 'step_index', 'action', 'comment', 'approved_at', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
// app/controller/PdfController.php
|
||||
namespace app\controller;
|
||||
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use support\View;
|
||||
use app\common\utils\pdf\service\MpdfDriver;
|
||||
use app\common\utils\pdf\service\PdfHelper;
|
||||
|
||||
class PdfController
|
||||
{
|
||||
// ① 从“现有 HTML 文件 + 外链CSS”生成并在线预览
|
||||
public function previewFromFile(Request $request): Response
|
||||
{
|
||||
// 你的模板路径(示例)
|
||||
$tpl = base_path('resource/templates/stock-order.html');
|
||||
// 可选:把 {{变量}} 替换
|
||||
$html = PdfHelper::loadTemplate($tpl, [
|
||||
'title' => '备货单',
|
||||
'order_no' => 'SO' . date('YmdHis'),
|
||||
'customer' => 'Magic Bridge Auto FZE',
|
||||
]);
|
||||
|
||||
$driver = new MpdfDriver();
|
||||
$pdf = $driver->htmlToPdf($html, [
|
||||
// 若 HTML 里用到相对资源(img/css),给个基准目录
|
||||
'asset_base' => public_path('assets/pdf'),
|
||||
// 也可以单独传 CSS 文件(推荐把 CSS 放在 public/assets/pdf)
|
||||
'css_files' => [
|
||||
public_path('assets/pdf/stock-order.css'),
|
||||
],
|
||||
'header_html' => '<div style="text-align:right;color:#888;font-size:10px">Company • Stock Order</div>',
|
||||
'footer_html' => '<div style="text-align:center;color:#888;font-size:10px">{PAGENO} / {nbpg}</div>',
|
||||
'watermark_text' => '', // 'DRAFT'
|
||||
]);
|
||||
|
||||
return new Response(200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="stock-order.pdf"',
|
||||
], $pdf);
|
||||
}
|
||||
|
||||
// ② 直接下载
|
||||
public function download(Request $request): Response
|
||||
{
|
||||
$html = '<h1>发票</h1><p>客户:永生花</p>';
|
||||
$pdf = (new MpdfDriver())->htmlToPdf($html);
|
||||
return new Response(200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'attachment; filename="invoice.pdf"',
|
||||
], $pdf);
|
||||
}
|
||||
|
||||
// ③ 保存到本地(或上传 OSS 前先落盘)
|
||||
public function save(Request $request): Response
|
||||
{
|
||||
$tpl = base_path('resource/templates/invoice.html');
|
||||
$html = PdfHelper::loadTemplate($tpl, [
|
||||
'invoice_no' => 'INV' . date('YmdHis'),
|
||||
'amount' => '12345.67',
|
||||
]);
|
||||
|
||||
$pdf = (new MpdfDriver())->htmlToPdf($html, [
|
||||
'css_files' => [ public_path('assets/pdf/invoice.css') ],
|
||||
'asset_base'=> public_path('assets/pdf'),
|
||||
]);
|
||||
|
||||
$dir = runtime_path('pdf');
|
||||
if (!is_dir($dir)) mkdir($dir, 0777, true);
|
||||
$path = $dir . '/invoice-' . date('YmdHis') . '.pdf';
|
||||
file_put_contents($path, $pdf);
|
||||
|
||||
return json(['path' => $path]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
// app/service/pdf/MpdfDriver.php
|
||||
namespace app\common\utils\pdf\service;
|
||||
|
||||
use Mpdf\Mpdf;
|
||||
use Mpdf\Config\ConfigVariables;
|
||||
use Mpdf\Config\FontVariables;
|
||||
use Mpdf\HTMLParserMode;
|
||||
|
||||
class MpdfDriver
|
||||
{
|
||||
|
||||
public function htmlToPdf(string $html, array $options = []): string
|
||||
{
|
||||
$opts = array_merge([
|
||||
'format' => 'A4',
|
||||
'orientation' => 'L',
|
||||
'margin_top' => 12,
|
||||
'margin_bottom' => 12,
|
||||
'margin_left' => 10,
|
||||
'margin_right' => 10,
|
||||
'tempDir' => runtime_path('mpdf'),
|
||||
'header_html' => '',
|
||||
'footer_html' => '',
|
||||
'watermark_text'=> '',
|
||||
'css_files' => [],
|
||||
'default_font' => 'notosanssc',
|
||||
'font_dirs' => [public_path('fonts')],
|
||||
'font_data' => [
|
||||
'notosanssc' => ['R'=>'NotoSansSC-Regular.ttf','B'=>'NotoSansSC-Bold.ttf'],
|
||||
],
|
||||
'asset_base' => '',
|
||||
'enable_html5_parser' => true,
|
||||
], $options);
|
||||
|
||||
$defaultConfig = (new ConfigVariables())->getDefaults();
|
||||
$defaultFontConfig = (new FontVariables())->getDefaults();
|
||||
|
||||
$fontDirs = array_merge($defaultConfig['fontDir'], $opts['font_dirs']);
|
||||
$fontData = array_merge($defaultFontConfig['fontdata'], $opts['font_data']);
|
||||
|
||||
$mpdf = new Mpdf([
|
||||
'format' => $opts['format'],
|
||||
'orientation' => $opts['orientation'],
|
||||
'margin_left' => $opts['margin_left'],
|
||||
'margin_right' => $opts['margin_right'],
|
||||
'margin_top' => $opts['margin_top'],
|
||||
'margin_bottom' => $opts['margin_bottom'],
|
||||
'tempDir' => $opts['tempDir'],
|
||||
'fontDir' => $fontDirs,
|
||||
'fontdata' => $fontData,
|
||||
'default_font' => $opts['default_font'],
|
||||
'enable_html5_parser' => $opts['enable_html5_parser'],
|
||||
]);
|
||||
|
||||
if ($opts['header_html']) $mpdf->SetHTMLHeader($opts['header_html']);
|
||||
if ($opts['footer_html']) $mpdf->SetHTMLFooter($opts['footer_html']);
|
||||
|
||||
if (!empty($opts['watermark_text'])) {
|
||||
$mpdf->SetWatermarkText($opts['watermark_text']);
|
||||
$mpdf->showWatermarkText = true;
|
||||
$mpdf->watermarkTextAlpha = 0.08;
|
||||
}
|
||||
|
||||
// 资源绝对化(如果你有相对路径的图片等)
|
||||
$html = PdfHelper::absolutizeAssetUrls($html, $opts['asset_base']);
|
||||
|
||||
// === 关键改动:把 <style>…</style> 抽出来,作为 HEADER_CSS 写入 ===
|
||||
[$inlineCss, $cleanHtml] = $this->extractCssAndCleanHtml($html);
|
||||
|
||||
// 先写外部 css_files
|
||||
if (!empty($opts['css_files'])) {
|
||||
foreach ($opts['css_files'] as $cssFile) {
|
||||
if (is_file($cssFile)) {
|
||||
$mpdf->WriteHTML(file_get_contents($cssFile), HTMLParserMode::HEADER_CSS);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 再写内联 <style> 的 CSS
|
||||
if ($inlineCss !== '') {
|
||||
$mpdf->WriteHTML($inlineCss, HTMLParserMode::HEADER_CSS);
|
||||
}
|
||||
|
||||
// 最后写 HTML 正文(已剔除 <style>,避免任何情况下把 CSS 当文字打印)
|
||||
$mpdf->WriteHTML($cleanHtml, HTMLParserMode::HTML_BODY);
|
||||
|
||||
return $mpdf->Output('', \Mpdf\Output\Destination::STRING_RETURN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把所有 <style>...</style> 的内容拼成一段 CSS,并把这些 <style> 标签从 HTML 中移除
|
||||
* @return array{0:string,1:string} [css, htmlWithoutStyle]
|
||||
*/
|
||||
private function extractCssAndCleanHtml(string $html): array
|
||||
{
|
||||
$cssParts = [];
|
||||
// 抓取所有 <style ...>...</style>,DOTALL 支持跨行
|
||||
if (preg_match_all('/<style\b[^>]*>(.*?)<\/style>/is', $html, $m)) {
|
||||
foreach ($m[1] as $block) {
|
||||
// 去掉 HTML 注释,以防注释把 CSS 包起来
|
||||
$block = preg_replace('/<!--|-->/', '', $block);
|
||||
$cssParts[] = trim($block);
|
||||
}
|
||||
// 从 HTML 删除所有 <style> 块
|
||||
$html = preg_replace('/<style\b[^>]*>.*?<\/style>/is', '', $html);
|
||||
}
|
||||
$css = implode("\n\n", $cssParts);
|
||||
|
||||
// 额外保险:把 <head> 也清理掉,防止某些解析器把 head 里残余文本当正文
|
||||
$html = preg_replace('/<head\b[^>]*>.*?<\/head>/is', '', $html);
|
||||
|
||||
// 确保剩余 HTML 有 <body>,没有就包一层
|
||||
if (!preg_match('/<body\b/i', $html)) {
|
||||
$html = '<body>' . $html . '</body>';
|
||||
}
|
||||
return [$css, $html];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
// app/service/pdf/PdfHelper.php
|
||||
namespace app\common\utils\pdf\service;
|
||||
|
||||
class PdfHelper
|
||||
{
|
||||
/** 从文件加载 HTML(支持占位符 {{key}} 简单替换) */
|
||||
public static function loadTemplate(string $file, array $vars = []): string
|
||||
{
|
||||
if (!is_file($file)) {
|
||||
throw new \RuntimeException("Template not found: {$file}");
|
||||
}
|
||||
$html = file_get_contents($file);
|
||||
if ($vars) {
|
||||
$search = [];
|
||||
$replace = [];
|
||||
foreach ($vars as $k => $v) {
|
||||
$search[] = '{{' . $k . '}}';
|
||||
$replace[] = htmlspecialchars((string)$v, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
}
|
||||
$html = str_replace($search, $replace, $html);
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把 HTML 里的相对资源路径(img/link/script href/src)改成绝对路径
|
||||
* 仅处理以 ./ ../ 或非 http(s) 的相对量;base 传 public/assets/pdf 等目录
|
||||
*/
|
||||
public static function absolutizeAssetUrls(string $html, string $baseDir): string
|
||||
{
|
||||
$baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR);
|
||||
// img src
|
||||
$html = preg_replace_callback('#(<img[^>]+src=["\'])([^"\']+)(["\'])#i', function($m) use ($baseDir){
|
||||
$u = $m[2];
|
||||
if (preg_match('#^https?://#i', $u) || strpos($u, 'data:') === 0) return $m[0];
|
||||
$abs = realpath($baseDir . DIRECTORY_SEPARATOR . ltrim($u, '/')) ?: ($baseDir . '/' . ltrim($u, '/'));
|
||||
return $m[1] . $abs . $m[3];
|
||||
}, $html);
|
||||
|
||||
// link href(如果你在 HTML 里写了 <link>,通常建议走 css_files 方式,这里也兼容)
|
||||
$html = preg_replace_callback('#(<link[^>]+href=["\'])([^"\']+)(["\'])#i', function($m) use ($baseDir){
|
||||
$u = $m[2];
|
||||
if (preg_match('#^https?://#i', $u)) return $m[0];
|
||||
$abs = realpath($baseDir . DIRECTORY_SEPARATOR . ltrim($u, '/')) ?: ($baseDir . '/' . ltrim($u, '/'));
|
||||
return $m[1] . $abs . $m[3];
|
||||
}, $html);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 渲染一个原生 PHP 模板为字符串
|
||||
* @param string $file 模板路径(如 resource/templates/stock-order.php)
|
||||
* @param array $vars 将在模板中以变量名存在(如 $data)
|
||||
* @return string 完整 HTML
|
||||
*/
|
||||
public static function render(string $file, array $vars = []): string
|
||||
{
|
||||
if (!is_file($file)) {
|
||||
throw new \RuntimeException("Template not found: {$file}");
|
||||
}
|
||||
// 将键名导出为同名变量,例如 'data' => [...] 变为 $data
|
||||
extract($vars, EXTR_OVERWRITE);
|
||||
|
||||
// 开启缓冲,将 include 的输出抓取为字符串
|
||||
ob_start();
|
||||
ob_implicit_flush(false);
|
||||
try {
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
include $file;
|
||||
} catch (\Throwable $e) {
|
||||
// 清理缓冲并抛出更清晰的异常
|
||||
ob_end_clean();
|
||||
throw $e;
|
||||
}
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace app\common\utils\pdf\service;
|
||||
|
||||
final class PdfStorage
|
||||
{
|
||||
/**
|
||||
* @param string $pdfBin PDF 二进制
|
||||
* @param string|null $dir 保存目录,默认 runtime/pdf
|
||||
* @param string|null $name 文件名(不含目录),默认:stock-order-YYYYmmdd-His-uniqid.pdf
|
||||
* @return string 绝对路径
|
||||
*/
|
||||
public static function saveBinary(string $pdfBin, ?string $dir = null, ?string $name = null): string
|
||||
{
|
||||
$dir = $dir ?: runtime_path('pdf');
|
||||
if (!is_dir($dir)) {
|
||||
if (!@mkdir($dir, 0775, true) && !is_dir($dir)) {
|
||||
throw new \RuntimeException("Failed to create dir: {$dir}");
|
||||
}
|
||||
}
|
||||
|
||||
// 生成安全文件名
|
||||
$name = $name ?: ('stock-order-' . date('Ymd-His') . '-' . substr(uniqid('', true), -6) . '.pdf');
|
||||
$name = preg_replace('/[^a-zA-Z0-9_\-.]/', '_', $name); // 简易净化
|
||||
$path = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name;
|
||||
|
||||
// 先写入临时文件,再原子重命名,避免并发/写入中被读取
|
||||
$tmp = $path . '.part';
|
||||
if (file_put_contents($tmp, $pdfBin, LOCK_EX) === false) {
|
||||
throw new \RuntimeException("Failed to write temp file: {$tmp}");
|
||||
}
|
||||
if (!@rename($tmp, $path)) {
|
||||
@unlink($tmp);
|
||||
throw new \RuntimeException("Failed to move temp to: {$path}");
|
||||
}
|
||||
@chmod($path, 0664);
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>合塑实业-汽车事业部-备货单(客户下定)</title>
|
||||
<style>
|
||||
/* 横版:A4 297mm,左右各 12mm => 可用宽度 273mm */
|
||||
@page { margin: 12mm; }
|
||||
|
||||
body {
|
||||
font-family: 'notosanssc', 'DejaVu Sans', Arial, sans-serif;
|
||||
font-size: 10pt;
|
||||
line-height: 1.3;
|
||||
color: #000;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.container {
|
||||
width: 273mm; /* 横向版心 */
|
||||
margin: 0 auto;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.header {
|
||||
text-align: center;
|
||||
margin-bottom: 10pt;
|
||||
padding-bottom: 6pt;
|
||||
}
|
||||
.title {
|
||||
font-size: 14pt;
|
||||
font-weight: bold;
|
||||
margin-bottom: 6pt;
|
||||
}
|
||||
.sub-title {
|
||||
font-size: 9pt;
|
||||
color: #000;
|
||||
margin: 0 6pt;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-bottom: 10pt;
|
||||
font-size: 9pt;
|
||||
table-layout: fixed; /* 横向固定布局,配合 colgroup 更稳 */
|
||||
}
|
||||
th, td {
|
||||
border: 0.5pt solid #000;
|
||||
padding: 6pt 6pt;
|
||||
text-align: center;
|
||||
vertical-align: middle;
|
||||
word-break: break-all;
|
||||
}
|
||||
th {
|
||||
background-color: #f0f0f0;
|
||||
font-weight: bold;
|
||||
color: #000;
|
||||
}
|
||||
|
||||
/* 跨页友好 */
|
||||
thead { display: table-header-group; }
|
||||
tfoot { display: table-row-group; }
|
||||
tr, img { page-break-inside: avoid; }
|
||||
|
||||
/* 信息类表格左对齐 */
|
||||
.info-table td,
|
||||
.logistics-table td { text-align: left; }
|
||||
|
||||
.info-label { font-weight: bold; color: #000; }
|
||||
.info-value { color: #000; }
|
||||
|
||||
.summary-row {
|
||||
background-color: #f0f0f0;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.payment-title {
|
||||
background-color: #f0f0f0;
|
||||
font-weight: bold;
|
||||
text-align: center;
|
||||
vertical-align: middle;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.nowrap { white-space: nowrap; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<!-- 头部 -->
|
||||
<div class="header">
|
||||
<h1 class="title">合塑实业-汽车事业部-备货单(客户下定)</h1>
|
||||
<div>
|
||||
<span class="sub-title">备货单号: <?php echo htmlspecialchars($data['order_no'] ?? 'N/A'); ?></span>
|
||||
<span class="sub-title">业务员: <?php echo htmlspecialchars($data['salesperson_snapshot'] ?? ' - '); ?></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 基本信息 -->
|
||||
<table class="info-table">
|
||||
<colgroup>
|
||||
<col style="width:18%">
|
||||
<col style="width:15%">
|
||||
<col style="width:14%">
|
||||
<col style="width:14%">
|
||||
<col style="width:19%">
|
||||
<col style="width:20%">
|
||||
</colgroup>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><span class="info-label">阿里订单号/PI号:</span> <span class="info-value"><?php echo htmlspecialchars($data['pi_no'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">日期:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['biz_date'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">关目的国:</span> <span class="info-value"><?php echo htmlspecialchars($data['clearance_country_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">客户来源:</span> <span class="info-value"><?php echo htmlspecialchars($data['customer_source_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">客户名称:</span> <span class="info-value"><?php echo htmlspecialchars($data['customer_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">客户所在国:</span> <span class="info-value"><?php echo htmlspecialchars($data['customer_country_snapshot'] ?? ' - '); ?></span></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- 产品列表 -->
|
||||
<table class="product-table">
|
||||
<colgroup>
|
||||
<col style="width:6%">
|
||||
<col style="width:34%">
|
||||
<col style="width:6%">
|
||||
<col style="width:6%">
|
||||
<col style="width:6%">
|
||||
<col style="width:14%">
|
||||
<col style="width:8%">
|
||||
<col style="width:8%">
|
||||
<col style="width:6%">
|
||||
<col style="width:6%">
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>编号</th>
|
||||
<th>车型</th>
|
||||
<th>数量/台</th>
|
||||
<th>颜色/外</th>
|
||||
<th>内饰</th>
|
||||
<th>改装</th>
|
||||
<th class="nowrap">官方指导价/二手挂牌价</th>
|
||||
<th class="nowrap">优惠</th>
|
||||
<th class="nowrap">单价</th>
|
||||
<th class="nowrap">总价</th>
|
||||
<th class="nowrap">利润</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach (($data['veh_list'] ?? []) as $vehicle): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars($vehicle['index'] ?? ' - '); ?></td>
|
||||
<td>
|
||||
<?php echo htmlspecialchars($vehicle['model_snapshot'] ?? ' - '); ?>
|
||||
<?php if ($vehicle['mileage'] > 0): ?>
|
||||
<span class="nowrap">(<?php echo htmlspecialchars($vehicle['mileage']).'km'; ?>)</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?php echo htmlspecialchars($vehicle['num'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['color_exterior_snapshot'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['color_interior_snapshot'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['modify'] ?? ' - '); ?></td>
|
||||
|
||||
<?php if ($vehicle['mileage'] > 0): ?>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['listing_price_text'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['reduction_price_text'] ?? ' - '); ?></td>
|
||||
<?php else: ?>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['official_price_text'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['discount_price_text'] ?? ' - '); ?></td>
|
||||
<?php endif; ?>
|
||||
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['unit_price_text'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['total_price_text'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['profit_text'] ?? ' - '); ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<tr class="summary-row">
|
||||
<td colspan="6" style="text-align:left;">
|
||||
<span class="info-label">合同汇率:</span> <span class="info-value"><?php echo htmlspecialchars($data['contract_exchange_rate'] ?? ' - '); ?></span>
|
||||
</td>
|
||||
<td colspan="5" style="text-align:left;">
|
||||
<span class="info-label">合计到岸价:</span> <span class="info-value"><?php echo htmlspecialchars($data['cif_total_amount'] ?? ' - '); ?></span>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- 物流信息 -->
|
||||
<table class="logistics-table">
|
||||
<colgroup>
|
||||
<col style="width:16%">
|
||||
<col style="width:18%">
|
||||
<col style="width:12%">
|
||||
<col style="width:18%">
|
||||
<col style="width:22%">
|
||||
<col style="width:14%">
|
||||
</colgroup>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><span class="info-label">清关人:</span> <span class="info-value"><?php echo htmlspecialchars($data['declarant_name'] ?? ' - '); ?></span></td>
|
||||
|
||||
<?php if ($data['is_final'] == 1): ?>
|
||||
<td><span class="info-label">全款收齐日:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['collect_all_date'] ?? ' - '); ?></span></td>
|
||||
<?php else: ?>
|
||||
<td><span class="info-label">预计发运日期:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['eta_ship_date'] ?? ' - '); ?></span></td>
|
||||
<?php endif; ?>
|
||||
|
||||
<td><span class="info-label">出运方式:</span> <span class="info-value"><?php echo htmlspecialchars($data['shipping_method_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">国内收货港:</span> <span class="info-value"><?php echo htmlspecialchars($data['domestic_port_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">贸易条款/清关目的港:</span> <span class="info-value"><?php echo htmlspecialchars($data['trade_term_snapshot'] ?? ' - '); ?>/<?php echo htmlspecialchars($data['clearance_port_snapshot'] ?? ' - '); ?></span></td>
|
||||
|
||||
<?php if ($data['is_final'] == 1): ?>
|
||||
<td><span class="info-label">提车日期:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['pick_up_date'] ?? ' - '); ?></span></td>
|
||||
<?php else: ?>
|
||||
<td><span class="info-label">最迟到港时间:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['latest_arrival_date'] ?? ' - '); ?></span></td>
|
||||
<?php endif; ?>
|
||||
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- 计划收款信息 -->
|
||||
<table class="payment-table">
|
||||
<colgroup>
|
||||
<col style="width:12%">
|
||||
<col style="width:10%">
|
||||
<col style="width:14%">
|
||||
<col style="width:12%">
|
||||
<col style="width:12%">
|
||||
<col style="width:20%">
|
||||
<col style="width:10%">
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width:24mm" rowspan="<?php echo htmlspecialchars($data['acc_index']); ?>">计划收款信息</th>
|
||||
<th>序号</th>
|
||||
<th>汇款国</th>
|
||||
<th>日期</th>
|
||||
<th>币种</th>
|
||||
<th>账户</th>
|
||||
<th>汇款人</th>
|
||||
<th>金额</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach (($data['account_list'] ?? []) as $account): ?>
|
||||
<tr>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['index'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($account['remit_country_name_snapshot'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['remit_date'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['currency_name_snapshot'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['account_name_snapshot'] ?? ' - '); ?></td>
|
||||
<td style="text-align:left;"><?php echo htmlspecialchars($account['remitter_name'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['amount'] ?? ' - '); ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- PDF 底部签名区 -->
|
||||
<div style="margin-top: 6mm; font-size: 12px;">
|
||||
<!-- 左侧:业务员签字 / 备发货负责人签字 -->
|
||||
<div style="width: 50%; float: left; text-align: left;">
|
||||
<!-- 行距通过 margin-bottom 控制;行内元素通过固定行高和 vertical-align: middle 垂直居中 -->
|
||||
<div style="height: 14mm; line-height: 14mm; margin-bottom: 5mm;">
|
||||
<span>业务员签字:</span>
|
||||
<?php if (!empty($data['sign_img_url'])): ?>
|
||||
<img src="<?php echo htmlspecialchars($data['sign_img_url']); ?>" style="height: 12mm; vertical-align: middle;">
|
||||
<?php else: ?>
|
||||
<span style="display:inline-block; width:21mm; height:12mm; vertical-align: middle; border-bottom:0.2mm solid #000;"></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<div style="height: 14mm; line-height: 14mm;">
|
||||
<span>备发货负责人签字:</span>
|
||||
<?php if (!empty($data['sign_img_url'])): ?>
|
||||
<img src="<?php echo htmlspecialchars($data['sign_img_url']); ?>" style="height: 12mm; vertical-align: middle;">
|
||||
<?php else: ?>
|
||||
<span style="display:inline-block; width:21mm; height:12mm; vertical-align: middle; border-bottom:0.2mm solid #000;"></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 右侧:组长签字 / 日期 -->
|
||||
<div style="width: 50%; float: right; text-align: left;">
|
||||
<div style="height: 14mm; line-height: 14mm; margin-bottom: 5mm;">
|
||||
<span>组长签字:</span>
|
||||
<?php if (!empty($data['sign_img_url'])): ?>
|
||||
<img src="<?php echo htmlspecialchars($data['sign_img_url']); ?>" style="height: 12mm; vertical-align: middle;">
|
||||
<?php else: ?>
|
||||
<span style="display:inline-block; width:21mm; height:12mm; vertical-align: middle; border-bottom:0.2mm solid #000;"></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<div style="height: 14mm; line-height: 14mm;">
|
||||
<span>日期:</span>
|
||||
<?php
|
||||
$_dt = $data['signed_date'] ?? ($data['created_at'] ?? '');
|
||||
if (is_numeric($_dt)) {
|
||||
echo htmlspecialchars(date('Y-m-d', (int)$_dt));
|
||||
} elseif (!empty($_dt)) {
|
||||
$t = strtotime($_dt);
|
||||
echo $t ? htmlspecialchars(date('Y-m-d', $t)) : ' - ';
|
||||
} else {
|
||||
echo ' - ';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style="clear: both;"></div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,319 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>合塑实业-汽车事业部-备货单(客户下定)</title>
|
||||
<style>
|
||||
/* 横版:A4 297mm,左右各 12mm => 可用宽度 273mm */
|
||||
@page { margin: 12mm; }
|
||||
|
||||
body {
|
||||
font-family: 'notosanssc', 'DejaVu Sans', Arial, sans-serif;
|
||||
font-size: 10pt;
|
||||
line-height: 1.3;
|
||||
color: #000;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.container {
|
||||
width: 273mm; /* 横向版心 */
|
||||
margin: 0 auto;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.header {
|
||||
text-align: center;
|
||||
margin-bottom: 10pt;
|
||||
padding-bottom: 6pt;
|
||||
}
|
||||
.title {
|
||||
font-size: 14pt;
|
||||
font-weight: bold;
|
||||
margin-bottom: 6pt;
|
||||
}
|
||||
.sub-title {
|
||||
font-size: 9pt;
|
||||
color: #000;
|
||||
margin: 0 6pt;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-bottom: 10pt;
|
||||
font-size: 9pt;
|
||||
table-layout: fixed; /* 横向固定布局,配合 colgroup 更稳 */
|
||||
}
|
||||
th, td {
|
||||
border: 0.5pt solid #000;
|
||||
padding: 6pt 6pt;
|
||||
text-align: center;
|
||||
vertical-align: middle;
|
||||
word-break: break-all;
|
||||
}
|
||||
th {
|
||||
background-color: #f0f0f0;
|
||||
font-weight: bold;
|
||||
color: #000;
|
||||
}
|
||||
|
||||
/* 跨页友好 */
|
||||
thead { display: table-header-group; }
|
||||
tfoot { display: table-row-group; }
|
||||
tr, img { page-break-inside: avoid; }
|
||||
|
||||
/* 信息类表格左对齐 */
|
||||
.info-table td,
|
||||
.logistics-table td { text-align: left; }
|
||||
|
||||
.info-label { font-weight: bold; color: #000; }
|
||||
.info-value { color: #000; }
|
||||
|
||||
.summary-row {
|
||||
background-color: #f0f0f0;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.payment-title {
|
||||
background-color: #f0f0f0;
|
||||
font-weight: bold;
|
||||
text-align: center;
|
||||
vertical-align: middle;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.nowrap { white-space: nowrap; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<!-- 头部 -->
|
||||
<div class="header">
|
||||
<h1 class="title">合塑实业-汽车事业部-备货单(客户下定)</h1>
|
||||
<div>
|
||||
<span class="sub-title">备货单号: <?php echo htmlspecialchars($data['order_no'] ?? 'N/A'); ?></span>
|
||||
<span class="sub-title">业务员: <?php echo htmlspecialchars($data['salesperson_snapshot'] ?? ' - '); ?></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 基本信息 -->
|
||||
<table class="info-table">
|
||||
<colgroup>
|
||||
<col style="width:18%">
|
||||
<col style="width:15%">
|
||||
<col style="width:14%">
|
||||
<col style="width:14%">
|
||||
<col style="width:19%">
|
||||
<col style="width:20%">
|
||||
</colgroup>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><span class="info-label">阿里订单号/PI号:</span> <span class="info-value"><?php echo htmlspecialchars($data['pi_no'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">日期:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['biz_date'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">关目的国:</span> <span class="info-value"><?php echo htmlspecialchars($data['clearance_country_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">客户来源:</span> <span class="info-value"><?php echo htmlspecialchars($data['customer_source_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">客户名称:</span> <span class="info-value"><?php echo htmlspecialchars($data['customer_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">客户所在国:</span> <span class="info-value"><?php echo htmlspecialchars($data['customer_country_snapshot'] ?? ' - '); ?></span></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- 产品列表 -->
|
||||
<table class="product-table">
|
||||
<colgroup>
|
||||
<col style="width:6%">
|
||||
<col style="width:34%">
|
||||
<col style="width:6%">
|
||||
<col style="width:6%">
|
||||
<col style="width:6%">
|
||||
<col style="width:14%">
|
||||
<col style="width:8%">
|
||||
<col style="width:8%">
|
||||
<col style="width:6%">
|
||||
<col style="width:6%">
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>编号</th>
|
||||
<th>车型</th>
|
||||
<th>数量/台</th>
|
||||
<th>颜色/外</th>
|
||||
<th>内饰</th>
|
||||
<th>改装</th>
|
||||
<th class="nowrap">官方指导价/二手挂牌价</th>
|
||||
<th class="nowrap">优惠</th>
|
||||
<th class="nowrap">单价</th>
|
||||
<th class="nowrap">总价</th>
|
||||
<th class="nowrap">利润</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach (($data['veh_list'] ?? []) as $vehicle): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars($vehicle['index'] ?? ' - '); ?></td>
|
||||
<td>
|
||||
<?php echo htmlspecialchars($vehicle['model_snapshot'] ?? ' - '); ?>
|
||||
<?php if ($vehicle['mileage'] > 0): ?>
|
||||
<span class="nowrap">(<?php echo htmlspecialchars($vehicle['mileage']).'km'; ?>)</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?php echo htmlspecialchars($vehicle['num'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['color_exterior_snapshot'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['color_interior_snapshot'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['modify'] ?? ' - '); ?></td>
|
||||
|
||||
<?php if ($vehicle['mileage'] > 0): ?>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['listing_price']['currency_info'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['reduction_price']['currency_info'] ?? ' - '); ?></td>
|
||||
<?php else: ?>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['official_price']['currency_info'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['discount_price']['currency_info'] ?? ' - '); ?></td>
|
||||
<?php endif; ?>
|
||||
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['unit_price']['currency_info'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['total_price']['currency_info'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['profit']['currency_info'] ?? ' - '); ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<tr class="summary-row">
|
||||
<td colspan="6" style="text-align:left;">
|
||||
<span class="info-label">合同汇率:</span> <span class="info-value"><?php echo htmlspecialchars($data['contract_exchange_rate'] ?? ' - '); ?></span>
|
||||
</td>
|
||||
<td colspan="5" style="text-align:left;">
|
||||
<span class="info-label">合计到岸价:</span> <span class="info-value"><?php echo htmlspecialchars($data['cif_total_amount']['currency_info'] ?? ' - '); ?></span>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- 物流信息 -->
|
||||
<table class="logistics-table">
|
||||
<colgroup>
|
||||
<col style="width:16%">
|
||||
<col style="width:18%">
|
||||
<col style="width:12%">
|
||||
<col style="width:18%">
|
||||
<col style="width:22%">
|
||||
<col style="width:14%">
|
||||
</colgroup>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><span class="info-label">清关人:</span> <span class="info-value"><?php echo htmlspecialchars($data['declarant_name'] ?? ' - '); ?></span></td>
|
||||
|
||||
<?php if ($data['is_final'] == 1): ?>
|
||||
<td><span class="info-label">全款收齐日:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['collect_all_date'] ?? ' - '); ?></span></td>
|
||||
<?php else: ?>
|
||||
<td><span class="info-label">预计发运日期:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['eta_ship_date'] ?? ' - '); ?></span></td>
|
||||
<?php endif; ?>
|
||||
|
||||
<td><span class="info-label">出运方式:</span> <span class="info-value"><?php echo htmlspecialchars($data['shipping_method_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">国内收货港:</span> <span class="info-value"><?php echo htmlspecialchars($data['domestic_port_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">贸易条款/清关目的港:</span> <span class="info-value"><?php echo htmlspecialchars($data['trade_term_snapshot'] ?? ' - '); ?>/<?php echo htmlspecialchars($data['clearance_port_snapshot'] ?? ' - '); ?></span></td>
|
||||
|
||||
<?php if ($data['is_final'] == 1): ?>
|
||||
<td><span class="info-label">提车日期:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['pick_up_date'] ?? ' - '); ?></span></td>
|
||||
<?php else: ?>
|
||||
<td><span class="info-label">最迟到港时间:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['latest_arrival_date'] ?? ' - '); ?></span></td>
|
||||
<?php endif; ?>
|
||||
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- 计划收款信息 -->
|
||||
<table class="payment-table">
|
||||
<colgroup>
|
||||
<col style="width:12%">
|
||||
<col style="width:10%">
|
||||
<col style="width:14%">
|
||||
<col style="width:12%">
|
||||
<col style="width:12%">
|
||||
<col style="width:20%">
|
||||
<col style="width:10%">
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width:24mm" rowspan="<?php echo htmlspecialchars($data['acc_index']); ?>">计划收款信息</th>
|
||||
<th>序号</th>
|
||||
<th>汇款国</th>
|
||||
<th>日期</th>
|
||||
<th>币种</th>
|
||||
<th>账户</th>
|
||||
<th>汇款人</th>
|
||||
<th>金额</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach (($data['account_list'] ?? []) as $account): ?>
|
||||
<tr>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['index'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($account['remit_country_name_snapshot'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['remit_date'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['currency_name_snapshot'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['account_name_snapshot'] ?? ' - '); ?></td>
|
||||
<td style="text-align:left;"><?php echo htmlspecialchars($account['remitter_name'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['amount'] ?? ' - '); ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- PDF 底部签名区 -->
|
||||
<div style="margin-top: 6mm; font-size: 12px;">
|
||||
<!-- 左侧:业务员签字 / 备发货负责人签字 -->
|
||||
<div style="width: 50%; float: left; text-align: left;">
|
||||
<!-- 行距通过 margin-bottom 控制;行内元素通过固定行高和 vertical-align: middle 垂直居中 -->
|
||||
<div style="height: 14mm; line-height: 14mm; margin-bottom: 5mm;">
|
||||
<span>业务员签字:</span>
|
||||
<?php if (!empty($data['sign_img_url'])): ?>
|
||||
<img src="<?php echo htmlspecialchars($data['sign_img_url']); ?>" style="height: 12mm; vertical-align: middle;">
|
||||
<?php else: ?>
|
||||
<span style="display:inline-block; width:21mm; height:12mm; vertical-align: middle; border-bottom:0.2mm solid #000;"></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<div style="height: 14mm; line-height: 14mm;">
|
||||
<span>备发货负责人签字:</span>
|
||||
<?php if (!empty($data['sign_imgs'][1])): ?>
|
||||
<img src="<?php echo htmlspecialchars($data['sign_imgs'][1]); ?>" style="height: 12mm; vertical-align: middle;">
|
||||
<?php else: ?>
|
||||
<span style="display:inline-block; width:21mm; height:12mm; vertical-align: middle; border-bottom:0.2mm solid #000;"></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 右侧:组长签字 / 日期 -->
|
||||
<div style="width: 50%; float: right; text-align: left;">
|
||||
<div style="height: 14mm; line-height: 14mm; margin-bottom: 5mm;">
|
||||
<span>组长签字:</span>
|
||||
<?php if (!empty($data['sign_imgs'][0])): ?>
|
||||
<img src="<?php echo htmlspecialchars($data['sign_imgs'][0]); ?>" style="height: 12mm; vertical-align: middle;">
|
||||
<?php else: ?>
|
||||
<span style="display:inline-block; width:21mm; height:12mm; vertical-align: middle; border-bottom:0.2mm solid #000;"></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<div style="height: 14mm; line-height: 14mm;">
|
||||
<span>日期:</span>
|
||||
<?php
|
||||
$_dt = $data['signed_date'] ?? ($data['created_at'] ?? '');
|
||||
if (is_numeric($_dt)) {
|
||||
echo htmlspecialchars(date('Y-m-d', (int)$_dt));
|
||||
} elseif (!empty($_dt)) {
|
||||
$t = strtotime($_dt);
|
||||
echo $t ? htmlspecialchars(date('Y-m-d', $t)) : ' - ';
|
||||
} else {
|
||||
echo ' - ';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style="clear: both;"></div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,255 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>合塑实业-汽车事业部-备货单(客户下定)</title>
|
||||
<style>
|
||||
/* 横版:A4 297mm,左右各 12mm => 可用宽度 273mm */
|
||||
@page { margin: 12mm; }
|
||||
|
||||
body {
|
||||
font-family: 'notosanssc', 'DejaVu Sans', Arial, sans-serif;
|
||||
font-size: 10pt;
|
||||
line-height: 1.3;
|
||||
color: #000;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.container {
|
||||
width: 273mm; /* 横向版心 */
|
||||
margin: 0 auto;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.header {
|
||||
text-align: center;
|
||||
margin-bottom: 10pt;
|
||||
padding-bottom: 6pt;
|
||||
}
|
||||
.title {
|
||||
font-size: 14pt;
|
||||
font-weight: bold;
|
||||
margin-bottom: 6pt;
|
||||
}
|
||||
.sub-title {
|
||||
font-size: 9pt;
|
||||
color: #000;
|
||||
margin: 0 6pt;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-bottom: 10pt;
|
||||
font-size: 9pt;
|
||||
table-layout: fixed; /* 横向固定布局,配合 colgroup 更稳 */
|
||||
}
|
||||
th, td {
|
||||
border: 0.5pt solid #000;
|
||||
padding: 6pt 6pt;
|
||||
text-align: center;
|
||||
vertical-align: middle;
|
||||
word-break: break-all;
|
||||
}
|
||||
th {
|
||||
background-color: #f0f0f0;
|
||||
font-weight: bold;
|
||||
color: #000;
|
||||
}
|
||||
|
||||
/* 跨页友好 */
|
||||
thead { display: table-header-group; }
|
||||
tfoot { display: table-row-group; }
|
||||
tr, img { page-break-inside: avoid; }
|
||||
|
||||
/* 信息类表格左对齐 */
|
||||
.info-table td,
|
||||
.logistics-table td { text-align: left; }
|
||||
|
||||
.info-label { font-weight: bold; color: #000; }
|
||||
.info-value { color: #000; }
|
||||
|
||||
.summary-row {
|
||||
background-color: #f0f0f0;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.payment-title {
|
||||
background-color: #f0f0f0;
|
||||
font-weight: bold;
|
||||
text-align: center;
|
||||
vertical-align: middle;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.nowrap { white-space: nowrap; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<!-- 头部 -->
|
||||
<div class="header">
|
||||
<h1 class="title">合塑实业-汽车事业部-备货单(客户下定)</h1>
|
||||
<div>
|
||||
<span class="sub-title">备货单号: <?php echo htmlspecialchars($data['order_no'] ?? 'N/A'); ?></span>
|
||||
<span class="sub-title">业务员: <?php echo htmlspecialchars($data['salesperson_snapshot'] ?? ' - '); ?></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 基本信息 -->
|
||||
<table class="info-table">
|
||||
<colgroup>
|
||||
<col style="width:18%">
|
||||
<col style="width:15%">
|
||||
<col style="width:14%">
|
||||
<col style="width:14%">
|
||||
<col style="width:19%">
|
||||
<col style="width:20%">
|
||||
</colgroup>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><span class="info-label">阿里订单号/PI号:</span> <span class="info-value"><?php echo htmlspecialchars($data['pi_no'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">日期:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['biz_date'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">关目的国:</span> <span class="info-value"><?php echo htmlspecialchars($data['clearance_country_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">客户来源:</span> <span class="info-value"><?php echo htmlspecialchars($data['customer_source_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">客户名称:</span> <span class="info-value"><?php echo htmlspecialchars($data['customer_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">客户所在国:</span> <span class="info-value"><?php echo htmlspecialchars($data['customer_country_snapshot'] ?? ' - '); ?></span></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- 产品列表 -->
|
||||
<table class="product-table">
|
||||
<colgroup>
|
||||
<col style="width:6%">
|
||||
<col style="width:34%">
|
||||
<col style="width:6%">
|
||||
<col style="width:6%">
|
||||
<col style="width:6%">
|
||||
<col style="width:14%">
|
||||
<col style="width:8%">
|
||||
<col style="width:8%">
|
||||
<col style="width:6%">
|
||||
<col style="width:6%">
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>编号</th>
|
||||
<th>车型</th>
|
||||
<th>数量/台</th>
|
||||
<th>颜色/外</th>
|
||||
<th>内饰</th>
|
||||
<th>改装</th>
|
||||
<th class="nowrap">单价</th>
|
||||
<th class="nowrap">总价</th>
|
||||
<th class="nowrap">官方指导价/二手挂牌价</th>
|
||||
<th class="nowrap">优惠/二手减价</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach (($data['veh_list'] ?? []) as $vehicle): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars($vehicle['index'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['model_snapshot'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['num'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['color_exterior_snapshot'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['color_interior_snapshot'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($vehicle['modify'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['unit_price_text'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['total_price_text'] ?? ' - '); ?></td>
|
||||
|
||||
<?php if ($vehicle['mileage'] > 0): ?>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['listing_price'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['reduction_price'] ?? ' - '); ?></td>
|
||||
<?php else: ?>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['official_price'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($vehicle['discount_price'] ?? ' - '); ?></td>
|
||||
<?php endif; ?>
|
||||
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<tr class="summary-row">
|
||||
<td colspan="6" style="text-align:left;">
|
||||
<span class="info-label">合同汇率:</span> <span class="info-value"><?php echo htmlspecialchars($data['contract_exchange_rate'] ?? ' - '); ?></span>
|
||||
</td>
|
||||
<td colspan="4" style="text-align:left;">
|
||||
<span class="info-label">合计到岸价:</span> <span class="info-value"><?php echo htmlspecialchars($data['cif_total_amount'] ?? ' - '); ?></span>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- 物流信息 -->
|
||||
<table class="logistics-table">
|
||||
<colgroup>
|
||||
<col style="width:16%">
|
||||
<col style="width:18%">
|
||||
<col style="width:12%">
|
||||
<col style="width:18%">
|
||||
<col style="width:22%">
|
||||
<col style="width:14%">
|
||||
</colgroup>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><span class="info-label">清关人:</span> <span class="info-value"><?php echo htmlspecialchars($data['declarant_name'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">预计发运日期:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['eta_ship_date'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">出运方式:</span> <span class="info-value"><?php echo htmlspecialchars($data['shipping_method_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">国内收货港:</span> <span class="info-value"><?php echo htmlspecialchars($data['domestic_port_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">贸易条款/清关目的港:</span> <span class="info-value"><?php echo htmlspecialchars($data['trade_term_snapshot'] ?? ' - '); ?>/<?php echo htmlspecialchars($data['clearance_port_snapshot'] ?? ' - '); ?></span></td>
|
||||
<td><span class="info-label">最迟到港时间:</span> <span class="info-value nowrap"><?php echo htmlspecialchars($data['latest_arrival_date'] ?? ' - '); ?></span></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- 计划收款信息 -->
|
||||
<table class="payment-table">
|
||||
<colgroup>
|
||||
<col style="width:12%">
|
||||
<col style="width:10%">
|
||||
<col style="width:14%">
|
||||
<col style="width:12%">
|
||||
<col style="width:12%">
|
||||
<col style="width:20%">
|
||||
<col style="width:10%">
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width:24mm" rowspan="<?php echo htmlspecialchars($data['acc_index']); ?>">计划收款信息</th>
|
||||
<th>序号</th>
|
||||
<th>汇款国</th>
|
||||
<th>日期</th>
|
||||
<th>币种</th>
|
||||
<th>账户</th>
|
||||
<th>汇款人</th>
|
||||
<th>金额</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach (($data['account_list'] ?? []) as $account): ?>
|
||||
<tr>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['index'] ?? ' - '); ?></td>
|
||||
<td><?php echo htmlspecialchars($account['remit_country_name_snapshot'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['remit_date'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['currency_name_snapshot'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['account_name_snapshot'] ?? ' - '); ?></td>
|
||||
<td style="text-align:left;"><?php echo htmlspecialchars($account['remitter_name'] ?? ' - '); ?></td>
|
||||
<td class="nowrap"><?php echo htmlspecialchars($account['amount'] ?? ' - '); ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div class="" style="text-align:right;">
|
||||
<div style="padding-top: 6mm; padding-bottom: 6mm; display: table; margin: 0 0 0 auto;">
|
||||
<div style="display: table-cell; vertical-align: middle;">
|
||||
<span>下单人:</span>
|
||||
<img src="<?php echo htmlspecialchars($data['sign_img_url'] ?? ' - '); ?>" style="width: 21mm; height: 12mm; vertical-align: middle;">
|
||||
</div>
|
||||
</div>
|
||||
<div>下单时间:<?php echo htmlspecialchars($data['created_at'] ?? ' - '); ?></div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -156,11 +156,11 @@ class Generate
|
||||
protected function deleteFileIfExists($path)
|
||||
{
|
||||
if (file_exists($path)) {
|
||||
"文件: <comment>{$path}</comment> 已存在,核对后手动删除。\n";
|
||||
return;
|
||||
// "文件: <comment>{$path}</comment> 已存在,核对后手动删除。\n";
|
||||
// return;
|
||||
|
||||
// unlink($path);
|
||||
// echo "删除已存在文件: <comment>{$path}</comment> \n";
|
||||
unlink($path);
|
||||
echo "删除已存在文件: <comment>{$path}</comment> \n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
// 定义所有需要多语言支持的实体
|
||||
'entities' => [
|
||||
'ticket_categorie' => [
|
||||
'translation_model' => \app\ticket\model\CategorieTranslation::class,
|
||||
'foreign_key' => 'categorie_id',
|
||||
],
|
||||
'sys_currency' => [
|
||||
'translation_model' => app\common\model\SysCurrencyTranslation::class,
|
||||
'foreign_key' => 'currency_id',
|
||||
],
|
||||
'sys_dict' => [
|
||||
'translation_model' => app\common\model\SysDictTranslation::class,
|
||||
'foreign_key' => 'dict_id',
|
||||
],
|
||||
'sys_dict_item' => [
|
||||
'translation_model' => app\common\model\SysDictItemTranslation::class,
|
||||
'foreign_key' => 'dict_item_id',
|
||||
],
|
||||
'sys_port_translation' => [
|
||||
'translation_model' => app\common\model\SysPortTranslation::class,
|
||||
'foreign_key' => 'port_id',
|
||||
],
|
||||
'veh_color_translation' => [
|
||||
'translation_model' => app\common\model\VehColorTranslation::class,
|
||||
'foreign_key' => 'color_id',
|
||||
],
|
||||
],
|
||||
|
||||
// 默认支持的目标语种列表
|
||||
'locales' => ['zh','en','ru','ar'],
|
||||
];
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace app\common\utils\translation\jobs;
|
||||
|
||||
use support\Log;
|
||||
use app\common\utils\translation\service\TranslatorClient;
|
||||
|
||||
class TranslationJob
|
||||
{
|
||||
protected $config = [];
|
||||
protected array $payload;
|
||||
|
||||
public function __construct(array $payload)
|
||||
{
|
||||
$this->config = getConfig('/common/utils/translation/config/trans.php');
|
||||
// $this->config = getConfig('/queue/translation/config/trans.php');
|
||||
$this->payload = $payload;
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$cfgAll = $this->config['entities'];
|
||||
$pt = $this->payload;
|
||||
$type = $pt['entityType'];
|
||||
$id = $pt['entityId'];
|
||||
$locale = $pt['locale'];
|
||||
$sourceLocale = $pt['sourceLocale'];
|
||||
$sourceFields = $pt['sourceFields'];
|
||||
$clientFields = $pt['clientFields'];
|
||||
|
||||
if (! isset($cfgAll[$type])) {
|
||||
Log::warning("TranslationJob: 未配置实体 {$type}");
|
||||
return;
|
||||
}
|
||||
|
||||
$cfg = $cfgAll[$type];
|
||||
$modelClass = $cfg['translation_model'];
|
||||
$foreignKey = $cfg['foreign_key'];
|
||||
|
||||
// 决定采用前端提供、源语种还是第三方翻译
|
||||
if (! empty($clientFields)) {
|
||||
$fields = $clientFields;
|
||||
} elseif ($locale === $sourceLocale) {
|
||||
$fields = $sourceFields;
|
||||
} else {
|
||||
$translator = new TranslatorClient();
|
||||
$fields = [];
|
||||
foreach ($sourceFields as $key => $text) {
|
||||
$fields[$key] = $translator->translate($text, $sourceLocale, $locale);
|
||||
}
|
||||
}
|
||||
|
||||
// 写库
|
||||
try {
|
||||
$modelClass::updateOrCreate(
|
||||
[
|
||||
$foreignKey => $id,
|
||||
'locale' => $locale,
|
||||
],
|
||||
array_merge($fields, ['locale' => $locale])
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error("TranslationJob 保存失败: {$type}#{$id} locale={$locale}: ".$e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\utils\translation\queue;
|
||||
|
||||
use support\Log;
|
||||
use Webman\RedisQueue\Consumer;
|
||||
use app\common\utils\translation\jobs\TranslationJob;
|
||||
|
||||
class Translation implements Consumer
|
||||
{
|
||||
// 要消费的队列名
|
||||
public $queue = 'transTask';
|
||||
|
||||
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
|
||||
public $connection = 'default';
|
||||
|
||||
// 消费
|
||||
public function consume($data)
|
||||
{
|
||||
try {
|
||||
// 无需反序列化
|
||||
// var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
|
||||
|
||||
$job = new TranslationJob($data);
|
||||
$job->handle();
|
||||
} catch (\Throwable $e) {
|
||||
Log::error("TranslationService 异步任务异常: ".$e->getMessage());
|
||||
}
|
||||
}
|
||||
// 消费失败回调
|
||||
/*
|
||||
$package = [
|
||||
'id' => 1357277951, // 消息ID
|
||||
'time' => 1709170510, // 消息时间
|
||||
'delay' => 0, // 延迟时间
|
||||
'attempts' => 2, // 消费次数
|
||||
'queue' => 'send-mail', // 队列名
|
||||
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
|
||||
'max_attempts' => 5, // 最大重试次数
|
||||
'error' => '错误信息' // 错误信息
|
||||
]
|
||||
*/
|
||||
public function onConsumeFailure(\Throwable $e, $package)
|
||||
{
|
||||
echo "consume failure\n";
|
||||
echo $e->getMessage() . "\n";
|
||||
// 无需反序列化
|
||||
var_export($package);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace app\common\utils\translation\service;
|
||||
|
||||
// use Webman\RedisQueue\Client;
|
||||
use Webman\RedisQueue\Redis;
|
||||
|
||||
class TranslationService
|
||||
{
|
||||
|
||||
// 配置
|
||||
protected $config = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = getConfig('/common/utils/translation/config/trans.php');
|
||||
// $this->config = getConfig('/queue/translation/config/trans.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步排队所有语种的翻译任务
|
||||
*
|
||||
* @param string $entityType 实体名,如 'vehicle'
|
||||
* @param int $entityId 主表 ID
|
||||
* @param string $sourceLocale 源语种,如 'zh'
|
||||
* @param array $sourceFields 源语种字段 ['name'=>…,'condition_desc'=>…]
|
||||
* @param array $clientFields 前端已传翻译 ['en'=>[...], …]
|
||||
* @param array $intactFields 不需要翻译的字段
|
||||
*/
|
||||
public function queueTranslations(string $entityType, int $entityId, string $sourceLocale, array $sourceFields, array $clientFields = [], array $intactFields = []): void
|
||||
{
|
||||
foreach ($this->config['locales'] as $locale) {
|
||||
$payload = [
|
||||
'entityType' => $entityType,
|
||||
'entityId' => $entityId,
|
||||
'locale' => $locale,
|
||||
'sourceLocale' => $sourceLocale,
|
||||
'sourceFields' => $sourceFields,
|
||||
'clientFields' => $clientFields[$locale] ?? [],
|
||||
];
|
||||
Redis::send('transTask', $payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace app\common\utils\translation\service;
|
||||
|
||||
use app\queue\utils\XunfeiFanyi;
|
||||
use support\Log;
|
||||
|
||||
class TranslatorClient
|
||||
{
|
||||
|
||||
// protected string $endpoint;
|
||||
// protected string $apiKey;
|
||||
|
||||
// public function __construct()
|
||||
// {
|
||||
// $this->endpoint = env('TRANSLATION_API_URL');
|
||||
// $this->apiKey = env('TRANSLATION_API_KEY');
|
||||
// }
|
||||
|
||||
/**
|
||||
* 翻译文本
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function translate(string $text, string $from = 'cn', string $to = 'ru'): string
|
||||
{
|
||||
try {
|
||||
$xunfei = new XunfeiFanyi();
|
||||
$rst_dst = $xunfei->fanyi($text, $from, $to);
|
||||
if ($rst_dst) {
|
||||
return $rst_dst["trans_result"]["dst"];
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
Log::error("TranslatorClient 错误: ".$e->getMessage());
|
||||
throw new \RuntimeException('翻译服务调用失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user