作者 李美松

文件名修正

<?php
namespace App\Console\Commands;
use App\Models\ProjectAssociation\ProjectAssociation;
use App\Models\File\DataFile;
use Dompdf\Dompdf;
use Dompdf\Options;
use Illuminate\Console\Command;
class ProjectFilePDF extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'project_file_pdf';
/**
* The console command description.
*
* @var string
*/
protected $description = '网站项目数据,生成PDF文件';
protected $AiccWechat;
protected $DataFile;
protected $time;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
$this->AiccWechat = new ProjectAssociation();
$this->DataFile = new DataFile();
$this->time = date("Y-m-d");
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$data = $this->AiccWechat->allData();
# 详细数据
$items = $data['items'];
# 总分页
$totalPage = $data['totalPage'];
$this->dataPush($items);
if ($totalPage > 1) {
for ($page = 2; $page <= $totalPage; $page++) {
$da = $this->get_data();
$this->dataPush($da['items']);
}
}
$this->info('生成pdf完成');
return 0;
}
/**
* 数据生成并保存
* @param array $items
* @return void
*/
public function dataPush(array $items)
{
foreach ($items as $item) {
$project_id = $item->project_id;
$application_id = $item->wx_id;
$wx_user_id = $item->wx_user_id;
// todo 根据项目查询数据
$project_data = [];
$html = $this->html($project_data);
$filename = hash('md5', $this->time . '-' . $project_id . '-' . $application_id);
$file_path = $this->savePDF($html, $filename);
$this->DataFile->saveData(compact('project_id', 'application_id', 'file_path') + ['time' => $this->time]);
}
}
public function get_data($page = 1, $perPage = 20)
{
$data = $this->AiccWechat->allData($page, $perPage);
# 总条数
$total = $data['total'];
if (empty($total)) {
$this->error('暂无绑定AICC微信数据');
return 0;
}
# 详细数据
$items = $data['items'];
# 总分页
$totalPage = $data['totalPage'];
# 当前页
$currentPage = $data['currentPage'];
return compact('total', 'items', 'totalPage', 'currentPage');
}
public function savePDF($html, $filename)
{
// todo 生成中文有问题
# 实例化并使用dompdf类
$options = new Options();
$options->setDefaultFont('arial');
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
#(可选)设置纸张大小和方向
$dompdf->setPaper('A4', 'landscape');
# 将HTML渲染为PDF
$dompdf->render();
// 获取PDF内容
$pdfContent = $dompdf->output();
// 指定保存路径和文件名
$savePath = public_path('PDF/' . $filename . '.pdf');
// 将PDF内容保存到文件
file_put_contents($savePath, $pdfContent);
// 输出保存成功消息
return $savePath;
}
/**
* 根据数据生成 Html
* @param $item
* @return string
*/
protected function html($item)
{
$html = '<html>';
$html .= '<body style="font-family:arial">';
$html .= '<h1>Hello, World!</h1>';
$html .= '<p>中文内容</p>';
$html .= '</body>';
$html .= '</html>';
return $html;
}
}
... ...
<?php
namespace App\Http\Controllers\Bside\Aicc;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Aside\Aicc\AiccV6Logic;
use Illuminate\Http\Request;
class AiccV6Controller extends BaseController
{
private $aiccV6Logic;
public function __construct(Request $request)
{
$this->aiccV6Logic = new AiccV6Logic();
parent::__construct($request);
}
/**
* V6与AICC数据关联
* @return void
*/
public function save()
{
$this->aiccV6Logic->saveData();
$this->response('success');
}
/**
* 数据推送到AICC
* @return void
*/
public function dataPush()
{
$data = $this->aiccV6Logic->V6AiccLists();
$this->response('success', Code::SUCCESS, $data);
}
}
<?php
namespace App\Http\Controllers\Bside\Aicc;
namespace App\Http\Controllers\Bside\ProjectAssociation;
use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Aside\Aicc\AiccWeChatLogic;
use App\Http\Logic\Aside\ProjectAssociation\ProjectAssociationLogic;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class AiccWeChatController extends BaseController
class ProjectAssociationController extends BaseController
{
private $aiccWeChatLogic;
private $ProjectAssociationLogic;
public function __construct(Request $request)
{
$this->aiccWeChatLogic = new AiccWeChatLogic();
$this->ProjectAssociationLogic = new ProjectAssociationLogic();
parent::__construct($request);
}
... ... @@ -25,7 +25,7 @@ class AiccWeChatController extends BaseController
* @return void
* @throws BsideGlobalException
*/
public function save()
public function saveWeChatData()
{
$project_id = (int)request()->post('project_id', 0);
if (empty($project_id)) {
... ... @@ -44,21 +44,7 @@ class AiccWeChatController extends BaseController
$wx_user_name = request()->post('user_name', '');
$wx_image = request()->post('image', '');
$data = compact('project_id', 'wx_id', 'wx_nickname', 'wx_user_id', 'wx_user_name', 'wx_image');
$this->aiccWeChatLogic->saveData($data);
$this->ProjectAssociationLogic->saveWeChatData($data);
$this->response('success');
}
/**
* 数据推送到AICC
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function dataPush()
{
$page = (int)request()->get('page', 1);
$perPage = (int)request()->get('perPage', 15); # 分页页数
$data = $this->aiccWeChatLogic->AiccWeChatLists($page, $perPage);
$this->response('success', Code::SUCCESS, $data);
}
}
... ...
<?php
namespace App\Http\Logic\Aside\Aicc;
use App\Enums\Common\Code;
use App\Http\Logic\Logic;
use App\Models\AICC\V6Aicc;
use Illuminate\Support\Facades\DB;
class AiccV6Logic extends Logic
{
public function saveData()
{
$v6_project_id = (int)request()->post('project_id', 0);
if (empty($v6_project_id)) {
$this->fail('请选择项目!', Code::USER_PARAMS_ERROE);
}
$status = (bool)request()->post('status', 1); # 1 - 正常, 0 - 禁用
$aicc_user_id = (int)request()->post('aicc_user_id', 0);
if (empty($aicc_user_id) && $status) {
$this->fail('请选择要绑定的AICC用户!', Code::USER_PARAMS_ERROE);
}
$aicc_project_id = (int)request()->post('aicc_project_id', 0);
if (empty($aicc_project_id) && $status) {
$this->fail('请选择要绑定的AICC项目!', Code::USER_PARAMS_ERROE);
}
$aicc_nickname = request()->post('aicc_nickname', '');
$aicc_user_name = request()->post('aicc_user_name', '');
$aicc_image = request()->post('aicc_image', '');
$data = compact('v6_project_id', 'aicc_project_id', 'aicc_nickname', 'aicc_user_id', 'aicc_user_name', 'aicc_image');
$aicc = new V6Aicc();
DB::beginTransaction();
try {
$status = $aicc->saveData($data);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$e->getMessage();
errorLog('V6与AICC关联失败', $aicc, $e);
$this->fail('请检查操作是否正确!', Code::SERVER_MYSQL_ERROR);
}
return $this->success($status);
}
/**
* @return array
*/
public function V6AiccLists()
{
$page = (int)request()->route('page', 1);
$status = 1; # 1 - 正常, 0 - 禁用
$perPage = (int)request()->route('perPage', 15); # 分页页数
$lists = V6Aicc::query()->where('status', $status)
->whereNotNull('aicc_project_id')
->whereNotNull('aicc_user_id')
->paginate($perPage, ['v6_project_id', 'aicc_project_id', 'aicc_user_id'], 'page', $page);
$items = $lists->Items();
$totalPage = $lists->lastPage();
$total = $lists->total();
$currentPage = $lists->currentPage();
return compact('total', 'items', 'totalPage', 'currentPage');
}
}
<?php
namespace App\Http\Logic\Aside\Aicc;
namespace App\Http\Logic\Aside\ProjectAssociation;
use App\Enums\Common\Code;
use App\Http\Logic\Logic;
use App\Models\AICC\AiccWechat;
use App\Models\ProjectAssociation\ProjectAssociation;
use Illuminate\Support\Facades\DB;
class AiccWeChatLogic extends Logic
class ProjectAssociationLogic extends Logic
{
public function saveData($data)
public function saveWeChatData($data)
{
$wx = new AiccWechat();
$wx = new ProjectAssociation();
DB::beginTransaction();
try {
$status = $wx->saveData($data);
... ... @@ -24,21 +24,4 @@ class AiccWeChatLogic extends Logic
}
return $status;
}
/**
* @return array
*/
public function AiccWeChatLists($page = 1, $perPage = 15)
{
$status = 1; # 1 - 正常, 0 - 禁用
$lists = AiccWechat::query()->where('status', $status)
->whereNotNull('project_id')
->whereNotNull('wx_user_id')
->paginate($perPage, ['project_id', 'wx_id', 'wx_user_id'], 'page', $page);
$items = $lists->Items();
$totalPage = $lists->lastPage();
$total = $lists->total();
$currentPage = $lists->currentPage();
return compact('total', 'items', 'totalPage', 'currentPage');
}
}
... ...
<?php
namespace App\Models\AICC;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class V6Aicc extends Model
{
protected $table = 'gl_v6_aicc';
/**
* 保存|修改数据
* @param array $data
* @return bool
*/
public function saveData(array $data): bool
{
$project_id = (int)$data['v6_project_id'] ?? 0;
$isRes = self::query()->where('v6_project_id', $project_id)->first();
if (!$isRes) {
$isRes = new self();
$isRes->v6_project_id = $project_id;
}
$isRes->aicc_project_id = $data['aicc_project_id'];
$isRes->aicc_nickname = $data['aicc_nickname'];
$isRes->aicc_user_id = $data['aicc_user_id'];
$isRes->aicc_user_name = $data['aicc_user_name'];
$isRes->aicc_image = $data['aicc_image'];
return $isRes->save();
}
/**
* 检查项目是否存在
* @param $project_id
* @return Builder|Model|object|null
*/
public function check($project_id)
{
return self::query()->where('v6_project_id', $project_id)->first();
}
}
<?php
namespace App\Models\File;
use App\Models\Base;
class DataFile extends Base
{
protected $table = 'gl_data_file';
public function saveData(array $data): bool
{
$project_id = (int)$data['project_id'] ?? 0;
$isRes = self::query()->where('project_id', $project_id)->where('created_at', 'like', $data['time'] . '%')->first();
if (!$isRes) {
$isRes = new self();
$isRes->project_id = $project_id;
}
$isRes->file_path = $data['file_path'];
$isRes->application_id = $data['application_id'];
return $isRes->save();
}
/**
* @param int $page
* @param int $perPage
* @return array
*/
public function allData(int $page = 1, int $perPage = 15)
{
$lists = self::query()->paginate($perPage, ['*'], 'page', $page);
$items = $lists->Items();
$totalPage = $lists->lastPage();
$total = $lists->total();
$currentPage = $lists->currentPage();
return compact('total', 'items', 'totalPage', 'currentPage');
}
}
... ...
<?php
namespace App\Models\AICC;
namespace App\Models\ProjectAssociation;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class AiccWechat extends Model
class ProjectAssociation extends Model
{
protected $table = 'gl_aicc_wechat';
protected $table = 'gl_project_association';
/**
* 保存|修改数据
... ...
... ... @@ -8,6 +8,7 @@
"bensampo/laravel-enum": "^4.2",
"beyondcode/laravel-websockets": "^1.14",
"doctrine/dbal": "^3.6",
"dompdf/dompdf": "^2.0",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"hashids/hashids": "^4.1",
... ...
... ... @@ -13,7 +13,7 @@ Route::middleware(['aloginauth'])->group(function () {
Route::any('/editPassword', [Aside\Com\IndexController::class, 'editPassword'])->name('admin.editPassword.white');
Route::get('/logout', [Aside\LoginController::class, 'logout'])->name('admin.logout.white');
Route::any('/getAccessAddress', [Aside\LoginController::class, 'getAccessAddress'])->name('admin.getAccessAddress');//获取B端地址
Route::post('/aicc/wechat', [\App\Http\Controllers\Bside\Aicc\AiccWeChatController::class, 'save'])->name('admin.aicc.wechat');
Route::post('/aicc/wechat', [\App\Http\Controllers\Bside\ProjectAssociation\ProjectAssociationController::class, 'saveWeChatData'])->name('admin.aicc.wechat');
//会员相关
Route::prefix('user')->group(function () {
//会员管理
... ... @@ -343,8 +343,6 @@ Route::group([], function () {
Route::any('get_template_detail', [Aside\Template\ATemplateController::class, 'getTemplateDetail'])->name('admin.get_template_detail');
Route::any('/collect', [Aside\Collect\CollectController::class, 'index'])->name('admin.collect');
# AICC与V6.0数据推送
Route::get('/data_push', [\App\Http\Controllers\Bside\Aicc\AiccWeChatController::class, 'dataPush'])->name('admin.dataPush');
});
... ...