作者 lms

合并分支 'lms' 到 'develop'

Lms - 推送数据到aicc



查看合并请求 !69
<?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->get_data();
# 详细数据
$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;
}
}
... ...
... ... @@ -81,12 +81,12 @@ class HtmlCollect extends Command
$url_web_config = 'https://' . $collect_info->domain . '/wp-content/cache/user_config.text';
$data_config = http_get($url_web_config, ['charset' => 'UTF-8']);
if ($data_config) {
$web_url_arr = parse_url($data_config['web_url_domain']);
$web_url_arr = parse_url($data_config['web_url_domain'] ?? '');
if (isset($web_url_arr['host'])) {
$web_url_domain = $web_url_arr['host'];
}
$home_url_arr = parse_url($data_config['home_url']);
$home_url_arr = parse_url($data_config['home_url'] ?? '');
if (isset($home_url_arr['host'])) {
$home_url = $home_url_arr['host'];
}
... ...
... ... @@ -80,12 +80,12 @@ class HtmlLanguageCollect extends Command
$url_web_config = 'https://' . $collect_info->domain . '/wp-content/cache/user_config.text';
$data_config = http_get($url_web_config, ['charset' => 'UTF-8']);
if ($data_config) {
$web_url_arr = parse_url($data_config['web_url_domain']);
$web_url_arr = parse_url($data_config['web_url_domain'] ?? '');
if (isset($web_url_arr['host'])) {
$web_url_domain = $web_url_arr['host'];
}
$home_url_arr = parse_url($data_config['home_url']);
$home_url_arr = parse_url($data_config['home_url'] ?? '');
if (isset($home_url_arr['host'])) {
$home_url = $home_url_arr['host'];
}
... ...
<?php
namespace App\Console\Commands;
use App\Models\File\DataFile;
use Illuminate\Console\Command;
class WebsiteData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'website_data';
/**
* The console command description.
*
* @var string
*/
protected $description = '向AICC推送数据';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$DataFile = new DataFile();
$data = $DataFile->allData();
# 详细数据
$items = $data['items'];
# 总分页
$totalPage = $data['totalPage'];
$this->post_data($items);
if ($totalPage > 1) {
for ($page = 2; $page <= $totalPage; $page++) {
$da = $DataFile->allData($page);
$this->post_data($da['items']);
}
}
$this->info('项目文件数据推送完成!');
return 0;
}
public function post_data($data)
{
return http_post("http://aicc-local.com/api/save_file_data", json_encode(compact('data')));
}
}
... ...
... ... @@ -38,6 +38,8 @@ class Kernel extends ConsoleKernel
$schedule->command('last_inquiry')->dailyAt('04:00')->withoutOverlapping(1);// 最近一次询盘信息
$schedule->command('update_progress')->everyThirtyMinutes()->withoutOverlapping(1);//监控更新
$schedule->command('update_seo_tdk_crontab')->dailyAt('00:00')->withoutOverlapping(1); //更新上线项目TDK
$schedule->command('website_data')->everyMinute()->withoutOverlapping(1); // 向AICC推送数据
$schedule->command('project_file_pdf')->everyMinute()->withoutOverlapping(1); // 网站项目数据,生成PDF文件
}
/**
... ...
<?php
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\ProjectAssociation\ProjectAssociationLogic;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class ProjectAssociationController extends BaseController
{
private $ProjectAssociationLogic;
public function __construct(Request $request)
{
$this->ProjectAssociationLogic = new ProjectAssociationLogic();
parent::__construct($request);
}
/**
* V6与AICC数据关联
* @return void
* @throws BsideGlobalException
*/
public function saveWeChatData()
{
$project_id = (int)request()->post('project_id', 0);
if (empty($project_id)) {
$this->fail('请选择项目!', Code::USER_PARAMS_ERROE);
}
$status = (bool)request()->post('status', 1); # 1 - 正常, 0 - 禁用
$wx_user_id = (int)request()->post('user_id', 0);
if (empty($wx_user_id) && $status) {
$this->fail('请选择要绑定的AICC用户!', Code::USER_PARAMS_ERROE);
}
$wx_id = (int)request()->post('wx_id', 0);
if (empty($wx_id) && $status) {
$this->fail('请选择要绑定的AICC项目!', Code::USER_PARAMS_ERROE);
}
$wx_nickname = request()->post('nickname', '');
$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->ProjectAssociationLogic->saveWeChatData($data);
$this->response('success');
}
}
... ...
<?php
namespace App\Http\Logic\Aside\ProjectAssociation;
use App\Enums\Common\Code;
use App\Http\Logic\Logic;
use App\Models\ProjectAssociation\ProjectAssociation;
use Illuminate\Support\Facades\DB;
class ProjectAssociationLogic extends Logic
{
public function saveWeChatData($data)
{
$wx = new ProjectAssociation();
DB::beginTransaction();
try {
$status = $wx->saveData($data);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$e->getMessage();
errorLog('V6与AICC关联失败', $wx, $e);
$this->fail('请检查操作是否正确!', Code::SERVER_MYSQL_ERROR);
}
return $status;
}
}
... ...
... ... @@ -44,6 +44,7 @@ class NavLogic extends BaseLogic
$this->param['group_id'] = BNavGroup::DEFAULT_FOOTER_ID;
}
}
unset($this->param['able_import']);
$this->param['image'] = str_replace_url(isset($this->param['image']) ? $this->param['image'] : '');
$this->param['remark_image'] = str_replace_url(isset($this->param['remark_image']) ? $this->param['remark_image'] : '');
if(isset($this->param['id']) && !empty($this->param['id'])){
... ...
<?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\ProjectAssociation;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class ProjectAssociation extends Model
{
protected $table = 'gl_project_association';
/**
* 保存|修改数据
* @param array $data
* @return bool
*/
public function saveData(array $data): bool
{
$project_id = (int)$data['project_id'] ?? 0;
$isRes = self::query()->where('project_id', $project_id)->first();
if (!$isRes) {
$isRes = new self();
$isRes->project_id = $project_id;
}
$isRes->wx_id = $data['wx_id'];
$isRes->wx_nickname = $data['wx_nickname'];
$isRes->wx_user_id = $data['wx_user_id'];
$isRes->wx_user_name = $data['wx_user_name'];
$isRes->wx_image = $data['wx_image'];
return $isRes->save();
}
/**
* 检查项目是否存在
* @param $project_id
* @return Builder|Model|object|null
*/
public function check($project_id)
{
return self::query()->where('project_id', $project_id)->first();
}
/**
* @param int $page
* @param int $perPage
* @return array
*/
public function allData(int $page = 1, int $perPage = 15)
{
$status = 1; # 1 - 正常, 0 - 禁用
$lists = self::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');
}
}
... ...
... ... @@ -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,6 +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\ProjectAssociation\ProjectAssociationController::class, 'saveWeChatData'])->name('admin.aicc.wechat');
//会员相关
Route::prefix('user')->group(function () {
//会员管理
... ...