作者 李美松

文件名修正

  1 +<?php
  2 +
  3 +namespace App\Console\Commands;
  4 +
  5 +use App\Models\ProjectAssociation\ProjectAssociation;
  6 +use App\Models\File\DataFile;
  7 +use Dompdf\Dompdf;
  8 +use Dompdf\Options;
  9 +use Illuminate\Console\Command;
  10 +
  11 +class ProjectFilePDF extends Command
  12 +{
  13 + /**
  14 + * The name and signature of the console command.
  15 + *
  16 + * @var string
  17 + */
  18 + protected $signature = 'project_file_pdf';
  19 +
  20 + /**
  21 + * The console command description.
  22 + *
  23 + * @var string
  24 + */
  25 + protected $description = '网站项目数据,生成PDF文件';
  26 +
  27 + protected $AiccWechat;
  28 +
  29 + protected $DataFile;
  30 +
  31 + protected $time;
  32 +
  33 + /**
  34 + * Create a new command instance.
  35 + *
  36 + * @return void
  37 + */
  38 + public function __construct()
  39 + {
  40 + $this->AiccWechat = new ProjectAssociation();
  41 + $this->DataFile = new DataFile();
  42 + $this->time = date("Y-m-d");
  43 + parent::__construct();
  44 + }
  45 +
  46 + /**
  47 + * Execute the console command.
  48 + *
  49 + * @return int
  50 + */
  51 + public function handle()
  52 + {
  53 + $data = $this->AiccWechat->allData();
  54 + # 详细数据
  55 + $items = $data['items'];
  56 + # 总分页
  57 + $totalPage = $data['totalPage'];
  58 + $this->dataPush($items);
  59 + if ($totalPage > 1) {
  60 + for ($page = 2; $page <= $totalPage; $page++) {
  61 + $da = $this->get_data();
  62 + $this->dataPush($da['items']);
  63 + }
  64 + }
  65 + $this->info('生成pdf完成');
  66 + return 0;
  67 + }
  68 +
  69 + /**
  70 + * 数据生成并保存
  71 + * @param array $items
  72 + * @return void
  73 + */
  74 + public function dataPush(array $items)
  75 + {
  76 + foreach ($items as $item) {
  77 + $project_id = $item->project_id;
  78 + $application_id = $item->wx_id;
  79 + $wx_user_id = $item->wx_user_id;
  80 + // todo 根据项目查询数据
  81 + $project_data = [];
  82 + $html = $this->html($project_data);
  83 + $filename = hash('md5', $this->time . '-' . $project_id . '-' . $application_id);
  84 + $file_path = $this->savePDF($html, $filename);
  85 + $this->DataFile->saveData(compact('project_id', 'application_id', 'file_path') + ['time' => $this->time]);
  86 + }
  87 + }
  88 +
  89 +
  90 + public function get_data($page = 1, $perPage = 20)
  91 + {
  92 + $data = $this->AiccWechat->allData($page, $perPage);
  93 + # 总条数
  94 + $total = $data['total'];
  95 + if (empty($total)) {
  96 + $this->error('暂无绑定AICC微信数据');
  97 + return 0;
  98 + }
  99 + # 详细数据
  100 + $items = $data['items'];
  101 + # 总分页
  102 + $totalPage = $data['totalPage'];
  103 + # 当前页
  104 + $currentPage = $data['currentPage'];
  105 + return compact('total', 'items', 'totalPage', 'currentPage');
  106 + }
  107 +
  108 + public function savePDF($html, $filename)
  109 + {
  110 + // todo 生成中文有问题
  111 + # 实例化并使用dompdf类
  112 + $options = new Options();
  113 + $options->setDefaultFont('arial');
  114 + $dompdf = new Dompdf($options);
  115 + $dompdf->loadHtml($html);
  116 + #(可选)设置纸张大小和方向
  117 + $dompdf->setPaper('A4', 'landscape');
  118 +
  119 + # 将HTML渲染为PDF
  120 + $dompdf->render();
  121 +
  122 + // 获取PDF内容
  123 + $pdfContent = $dompdf->output();
  124 +
  125 + // 指定保存路径和文件名
  126 + $savePath = public_path('PDF/' . $filename . '.pdf');
  127 +
  128 + // 将PDF内容保存到文件
  129 + file_put_contents($savePath, $pdfContent);
  130 +
  131 + // 输出保存成功消息
  132 + return $savePath;
  133 + }
  134 +
  135 + /**
  136 + * 根据数据生成 Html
  137 + * @param $item
  138 + * @return string
  139 + */
  140 + protected function html($item)
  141 + {
  142 + $html = '<html>';
  143 + $html .= '<body style="font-family:arial">';
  144 + $html .= '<h1>Hello, World!</h1>';
  145 + $html .= '<p>中文内容</p>';
  146 + $html .= '</body>';
  147 + $html .= '</html>';
  148 + return $html;
  149 + }
  150 +}
1 -<?php  
2 -  
3 -namespace App\Http\Controllers\Bside\Aicc;  
4 -  
5 -use App\Enums\Common\Code;  
6 -use App\Http\Controllers\Bside\BaseController;  
7 -use App\Http\Logic\Aside\Aicc\AiccV6Logic;  
8 -use Illuminate\Http\Request;  
9 -  
10 -class AiccV6Controller extends BaseController  
11 -{  
12 - private $aiccV6Logic;  
13 -  
14 - public function __construct(Request $request)  
15 - {  
16 - $this->aiccV6Logic = new AiccV6Logic();  
17 - parent::__construct($request);  
18 - }  
19 -  
20 - /**  
21 - * V6与AICC数据关联  
22 - * @return void  
23 - */  
24 - public function save()  
25 - {  
26 - $this->aiccV6Logic->saveData();  
27 - $this->response('success');  
28 - }  
29 -  
30 - /**  
31 - * 数据推送到AICC  
32 - * @return void  
33 - */  
34 - public function dataPush()  
35 - {  
36 - $data = $this->aiccV6Logic->V6AiccLists();  
37 - $this->response('success', Code::SUCCESS, $data);  
38 - }  
39 -}  
1 <?php 1 <?php
2 2
3 -namespace App\Http\Controllers\Bside\Aicc; 3 +namespace App\Http\Controllers\Bside\ProjectAssociation;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Exceptions\BsideGlobalException; 6 use App\Exceptions\BsideGlobalException;
7 use App\Http\Controllers\Bside\BaseController; 7 use App\Http\Controllers\Bside\BaseController;
8 -use App\Http\Logic\Aside\Aicc\AiccWeChatLogic; 8 +use App\Http\Logic\Aside\ProjectAssociation\ProjectAssociationLogic;
9 use Illuminate\Http\Request; 9 use Illuminate\Http\Request;
10 use Psr\Container\ContainerExceptionInterface; 10 use Psr\Container\ContainerExceptionInterface;
11 use Psr\Container\NotFoundExceptionInterface; 11 use Psr\Container\NotFoundExceptionInterface;
12 12
13 -class AiccWeChatController extends BaseController 13 +class ProjectAssociationController extends BaseController
14 { 14 {
15 - private $aiccWeChatLogic; 15 + private $ProjectAssociationLogic;
16 16
17 public function __construct(Request $request) 17 public function __construct(Request $request)
18 { 18 {
19 - $this->aiccWeChatLogic = new AiccWeChatLogic(); 19 + $this->ProjectAssociationLogic = new ProjectAssociationLogic();
20 parent::__construct($request); 20 parent::__construct($request);
21 } 21 }
22 22
@@ -25,7 +25,7 @@ class AiccWeChatController extends BaseController @@ -25,7 +25,7 @@ class AiccWeChatController extends BaseController
25 * @return void 25 * @return void
26 * @throws BsideGlobalException 26 * @throws BsideGlobalException
27 */ 27 */
28 - public function save() 28 + public function saveWeChatData()
29 { 29 {
30 $project_id = (int)request()->post('project_id', 0); 30 $project_id = (int)request()->post('project_id', 0);
31 if (empty($project_id)) { 31 if (empty($project_id)) {
@@ -44,21 +44,7 @@ class AiccWeChatController extends BaseController @@ -44,21 +44,7 @@ class AiccWeChatController extends BaseController
44 $wx_user_name = request()->post('user_name', ''); 44 $wx_user_name = request()->post('user_name', '');
45 $wx_image = request()->post('image', ''); 45 $wx_image = request()->post('image', '');
46 $data = compact('project_id', 'wx_id', 'wx_nickname', 'wx_user_id', 'wx_user_name', 'wx_image'); 46 $data = compact('project_id', 'wx_id', 'wx_nickname', 'wx_user_id', 'wx_user_name', 'wx_image');
47 - $this->aiccWeChatLogic->saveData($data); 47 + $this->ProjectAssociationLogic->saveWeChatData($data);
48 $this->response('success'); 48 $this->response('success');
49 } 49 }
50 -  
51 - /**  
52 - * 数据推送到AICC  
53 - * @return void  
54 - * @throws ContainerExceptionInterface  
55 - * @throws NotFoundExceptionInterface  
56 - */  
57 - public function dataPush()  
58 - {  
59 - $page = (int)request()->get('page', 1);  
60 - $perPage = (int)request()->get('perPage', 15); # 分页页数  
61 - $data = $this->aiccWeChatLogic->AiccWeChatLists($page, $perPage);  
62 - $this->response('success', Code::SUCCESS, $data);  
63 - }  
64 } 50 }
1 -<?php  
2 -  
3 -namespace App\Http\Logic\Aside\Aicc;  
4 -  
5 -use App\Enums\Common\Code;  
6 -use App\Http\Logic\Logic;  
7 -use App\Models\AICC\V6Aicc;  
8 -use Illuminate\Support\Facades\DB;  
9 -  
10 -class AiccV6Logic extends Logic  
11 -{  
12 - public function saveData()  
13 - {  
14 - $v6_project_id = (int)request()->post('project_id', 0);  
15 - if (empty($v6_project_id)) {  
16 - $this->fail('请选择项目!', Code::USER_PARAMS_ERROE);  
17 - }  
18 - $status = (bool)request()->post('status', 1); # 1 - 正常, 0 - 禁用  
19 - $aicc_user_id = (int)request()->post('aicc_user_id', 0);  
20 - if (empty($aicc_user_id) && $status) {  
21 - $this->fail('请选择要绑定的AICC用户!', Code::USER_PARAMS_ERROE);  
22 - }  
23 - $aicc_project_id = (int)request()->post('aicc_project_id', 0);  
24 - if (empty($aicc_project_id) && $status) {  
25 - $this->fail('请选择要绑定的AICC项目!', Code::USER_PARAMS_ERROE);  
26 - }  
27 - $aicc_nickname = request()->post('aicc_nickname', '');  
28 - $aicc_user_name = request()->post('aicc_user_name', '');  
29 - $aicc_image = request()->post('aicc_image', '');  
30 - $data = compact('v6_project_id', 'aicc_project_id', 'aicc_nickname', 'aicc_user_id', 'aicc_user_name', 'aicc_image');  
31 - $aicc = new V6Aicc();  
32 - DB::beginTransaction();  
33 - try {  
34 - $status = $aicc->saveData($data);  
35 - DB::commit();  
36 - } catch (\Exception $e) {  
37 - DB::rollBack();  
38 - $e->getMessage();  
39 - errorLog('V6与AICC关联失败', $aicc, $e);  
40 - $this->fail('请检查操作是否正确!', Code::SERVER_MYSQL_ERROR);  
41 - }  
42 - return $this->success($status);  
43 - }  
44 -  
45 - /**  
46 - * @return array  
47 - */  
48 - public function V6AiccLists()  
49 - {  
50 - $page = (int)request()->route('page', 1);  
51 - $status = 1; # 1 - 正常, 0 - 禁用  
52 - $perPage = (int)request()->route('perPage', 15); # 分页页数  
53 - $lists = V6Aicc::query()->where('status', $status)  
54 - ->whereNotNull('aicc_project_id')  
55 - ->whereNotNull('aicc_user_id')  
56 - ->paginate($perPage, ['v6_project_id', 'aicc_project_id', 'aicc_user_id'], 'page', $page);  
57 - $items = $lists->Items();  
58 - $totalPage = $lists->lastPage();  
59 - $total = $lists->total();  
60 - $currentPage = $lists->currentPage();  
61 - return compact('total', 'items', 'totalPage', 'currentPage');  
62 - }  
63 -}  
1 <?php 1 <?php
2 2
3 -namespace App\Http\Logic\Aside\Aicc; 3 +namespace App\Http\Logic\Aside\ProjectAssociation;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Http\Logic\Logic; 6 use App\Http\Logic\Logic;
7 -use App\Models\AICC\AiccWechat; 7 +use App\Models\ProjectAssociation\ProjectAssociation;
8 use Illuminate\Support\Facades\DB; 8 use Illuminate\Support\Facades\DB;
9 9
10 -class AiccWeChatLogic extends Logic 10 +class ProjectAssociationLogic extends Logic
11 { 11 {
12 - public function saveData($data) 12 + public function saveWeChatData($data)
13 { 13 {
14 - $wx = new AiccWechat(); 14 + $wx = new ProjectAssociation();
15 DB::beginTransaction(); 15 DB::beginTransaction();
16 try { 16 try {
17 $status = $wx->saveData($data); 17 $status = $wx->saveData($data);
@@ -24,21 +24,4 @@ class AiccWeChatLogic extends Logic @@ -24,21 +24,4 @@ class AiccWeChatLogic extends Logic
24 } 24 }
25 return $status; 25 return $status;
26 } 26 }
27 -  
28 - /**  
29 - * @return array  
30 - */  
31 - public function AiccWeChatLists($page = 1, $perPage = 15)  
32 - {  
33 - $status = 1; # 1 - 正常, 0 - 禁用  
34 - $lists = AiccWechat::query()->where('status', $status)  
35 - ->whereNotNull('project_id')  
36 - ->whereNotNull('wx_user_id')  
37 - ->paginate($perPage, ['project_id', 'wx_id', 'wx_user_id'], 'page', $page);  
38 - $items = $lists->Items();  
39 - $totalPage = $lists->lastPage();  
40 - $total = $lists->total();  
41 - $currentPage = $lists->currentPage();  
42 - return compact('total', 'items', 'totalPage', 'currentPage');  
43 - }  
44 } 27 }
1 -<?php  
2 -  
3 -namespace App\Models\AICC;  
4 -  
5 -use Illuminate\Database\Eloquent\Builder;  
6 -use Illuminate\Database\Eloquent\Model;  
7 -  
8 -class V6Aicc extends Model  
9 -{  
10 - protected $table = 'gl_v6_aicc';  
11 -  
12 - /**  
13 - * 保存|修改数据  
14 - * @param array $data  
15 - * @return bool  
16 - */  
17 - public function saveData(array $data): bool  
18 - {  
19 - $project_id = (int)$data['v6_project_id'] ?? 0;  
20 - $isRes = self::query()->where('v6_project_id', $project_id)->first();  
21 - if (!$isRes) {  
22 - $isRes = new self();  
23 - $isRes->v6_project_id = $project_id;  
24 - }  
25 - $isRes->aicc_project_id = $data['aicc_project_id'];  
26 - $isRes->aicc_nickname = $data['aicc_nickname'];  
27 - $isRes->aicc_user_id = $data['aicc_user_id'];  
28 - $isRes->aicc_user_name = $data['aicc_user_name'];  
29 - $isRes->aicc_image = $data['aicc_image'];  
30 - return $isRes->save();  
31 - }  
32 -  
33 - /**  
34 - * 检查项目是否存在  
35 - * @param $project_id  
36 - * @return Builder|Model|object|null  
37 - */  
38 - public function check($project_id)  
39 - {  
40 - return self::query()->where('v6_project_id', $project_id)->first();  
41 - }  
42 -}  
  1 +<?php
  2 +
  3 +namespace App\Models\File;
  4 +
  5 +use App\Models\Base;
  6 +
  7 +class DataFile extends Base
  8 +{
  9 + protected $table = 'gl_data_file';
  10 +
  11 + public function saveData(array $data): bool
  12 + {
  13 + $project_id = (int)$data['project_id'] ?? 0;
  14 + $isRes = self::query()->where('project_id', $project_id)->where('created_at', 'like', $data['time'] . '%')->first();
  15 + if (!$isRes) {
  16 + $isRes = new self();
  17 + $isRes->project_id = $project_id;
  18 + }
  19 + $isRes->file_path = $data['file_path'];
  20 + $isRes->application_id = $data['application_id'];
  21 + return $isRes->save();
  22 + }
  23 +
  24 + /**
  25 + * @param int $page
  26 + * @param int $perPage
  27 + * @return array
  28 + */
  29 + public function allData(int $page = 1, int $perPage = 15)
  30 + {
  31 + $lists = self::query()->paginate($perPage, ['*'], 'page', $page);
  32 + $items = $lists->Items();
  33 + $totalPage = $lists->lastPage();
  34 + $total = $lists->total();
  35 + $currentPage = $lists->currentPage();
  36 + return compact('total', 'items', 'totalPage', 'currentPage');
  37 + }
  38 +
  39 +}
1 <?php 1 <?php
2 2
3 -namespace App\Models\AICC; 3 +namespace App\Models\ProjectAssociation;
4 4
5 use Illuminate\Database\Eloquent\Builder; 5 use Illuminate\Database\Eloquent\Builder;
6 use Illuminate\Database\Eloquent\Model; 6 use Illuminate\Database\Eloquent\Model;
7 7
8 -class AiccWechat extends Model 8 +class ProjectAssociation extends Model
9 { 9 {
10 - protected $table = 'gl_aicc_wechat'; 10 + protected $table = 'gl_project_association';
11 11
12 /** 12 /**
13 * 保存|修改数据 13 * 保存|修改数据
@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 "bensampo/laravel-enum": "^4.2", 8 "bensampo/laravel-enum": "^4.2",
9 "beyondcode/laravel-websockets": "^1.14", 9 "beyondcode/laravel-websockets": "^1.14",
10 "doctrine/dbal": "^3.6", 10 "doctrine/dbal": "^3.6",
  11 + "dompdf/dompdf": "^2.0",
11 "fruitcake/laravel-cors": "^2.0", 12 "fruitcake/laravel-cors": "^2.0",
12 "guzzlehttp/guzzle": "^7.0.1", 13 "guzzlehttp/guzzle": "^7.0.1",
13 "hashids/hashids": "^4.1", 14 "hashids/hashids": "^4.1",
@@ -13,7 +13,7 @@ Route::middleware(['aloginauth'])->group(function () { @@ -13,7 +13,7 @@ Route::middleware(['aloginauth'])->group(function () {
13 Route::any('/editPassword', [Aside\Com\IndexController::class, 'editPassword'])->name('admin.editPassword.white'); 13 Route::any('/editPassword', [Aside\Com\IndexController::class, 'editPassword'])->name('admin.editPassword.white');
14 Route::get('/logout', [Aside\LoginController::class, 'logout'])->name('admin.logout.white'); 14 Route::get('/logout', [Aside\LoginController::class, 'logout'])->name('admin.logout.white');
15 Route::any('/getAccessAddress', [Aside\LoginController::class, 'getAccessAddress'])->name('admin.getAccessAddress');//获取B端地址 15 Route::any('/getAccessAddress', [Aside\LoginController::class, 'getAccessAddress'])->name('admin.getAccessAddress');//获取B端地址
16 - Route::post('/aicc/wechat', [\App\Http\Controllers\Bside\Aicc\AiccWeChatController::class, 'save'])->name('admin.aicc.wechat'); 16 + Route::post('/aicc/wechat', [\App\Http\Controllers\Bside\ProjectAssociation\ProjectAssociationController::class, 'saveWeChatData'])->name('admin.aicc.wechat');
17 //会员相关 17 //会员相关
18 Route::prefix('user')->group(function () { 18 Route::prefix('user')->group(function () {
19 //会员管理 19 //会员管理
@@ -343,8 +343,6 @@ Route::group([], function () { @@ -343,8 +343,6 @@ Route::group([], function () {
343 Route::any('get_template_detail', [Aside\Template\ATemplateController::class, 'getTemplateDetail'])->name('admin.get_template_detail'); 343 Route::any('get_template_detail', [Aside\Template\ATemplateController::class, 'getTemplateDetail'])->name('admin.get_template_detail');
344 344
345 Route::any('/collect', [Aside\Collect\CollectController::class, 'index'])->name('admin.collect'); 345 Route::any('/collect', [Aside\Collect\CollectController::class, 'index'])->name('admin.collect');
346 - # AICC与V6.0数据推送  
347 - Route::get('/data_push', [\App\Http\Controllers\Bside\Aicc\AiccWeChatController::class, 'dataPush'])->name('admin.dataPush');  
348 }); 346 });
349 347
350 348