作者 刘锟

Merge remote-tracking branch 'origin/master' into akun

<?php
/**
* @remark :
* @name :AiBlogAuthorTask.php
* @author :lyh
* @method :post
* @time :2025/2/21 11:12
*/
namespace App\Console\Commands\AiBlog;
use App\Models\Ai\AiBlog;
use App\Models\Ai\AiBlogAuthor;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use App\Models\Project\ProjectAiSetting;
use App\Models\RouteMap\RouteMap;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class AiBlogAuthorTask extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'save_ai_blog_author';
/**
* The console command description.
*
* @var string
*/
protected $description = '查询ai_blog_author是否已经生成';
/**
* @remark :获取作者
* @name :handle
* @author :lyh
* @method :post
* @time :2025/2/21 11:30
*/
public function handle(){
$aiBlogTaskModel = new AiBlogTaskModel();
while (true){
$info = $aiBlogTaskModel->where('status',1)->where('type',1)->inRandomOrder()->first();
if(empty($info)){
sleep(300);
continue;
}
$info = $info->toArray();
echo '开始->project_id:' . $info['project_id'] . PHP_EOL . date('Y-m-d H:i:s');
//获取配置
$aiSettingInfo = $this->getSetting($info['project_id']);
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$result = $aiBlogService->getAuthor();
if(!isset($result['status'])){
continue;
}
if($result['status'] != 200){
sleep(10);
continue;
}
//保存当前项目ai_blog数据
ProjectServer::useProject($info['project_id']);
$this->saveAiBlogAuthor($result['data'] ?? [],$info['project_id']);
DB::disconnect('custom_mysql');
//修改任务状态
$aiBlogTaskModel->edit(['status'=>2],['id'=>$info['id']]);
echo '结束->任务id:' . $info['task_id'] . PHP_EOL . date('Y-m-d H:i:s');
}
return true;
}
/**
* @remark :获取项目配置
* @name :getSetting
* @author :lyh
* @method :post
* @time :2025/2/14 11:27
*/
public function getSetting($project_id){
$ai_cache = Cache::get('ai_blog_'.$project_id);
if($ai_cache){
return $ai_cache;
}
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
Cache::put('ai_blog_'.$project_id,$aiSettingInfo,3600);
return $aiSettingInfo;
}
/**
* @remark :保存数据
* @name :saveAiBlogAuthor
* @author :lyh
* @method :post
* @time :2025/2/21 11:36
*/
public function saveAiBlogAuthor($data,$project_id){
if(empty($data)){
return true;
}
$aiBlogAuthorModel = new AiBlogAuthor();
foreach ($data as $v){
$param = [
'author_id'=>$v['id'],
'title'=>$v['title'],
'image'=>str_replace_url($v['picture']),
'description'=>$v['description'],
];
$id = $aiBlogAuthorModel->addReturnId($param);
$route = RouteMap::setRoute($v['title'], RouteMap::SOURCE_AI_BLOG_AUTHOR, $id, $project_id);
$aiBlogAuthorModel->edit(['route'=>$route],['id'=>$id]);
}
return true;
}
}
... ...
... ... @@ -10,7 +10,9 @@
namespace App\Console\Commands\AiBlog;
use App\Models\Ai\AiBlog;
use App\Models\Ai\AiBlogAuthor;
use App\Models\Project\ProjectAiSetting;
use App\Models\RouteMap\RouteMap;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
... ... @@ -38,9 +40,9 @@ class AiBlogTask extends Command
public function handle(){
$aiBlogTaskModel = new AiBlogTaskModel();
while (true){
$info = $aiBlogTaskModel->where('status',1)->orderBy('id','asc')->first();
$info = $aiBlogTaskModel->where('status',1)->where('type',2)->inRandomOrder()->first();
if(empty($info)){
sleep(30);
sleep(300);
continue;
}
$info = $info->toArray();
... ... @@ -53,26 +55,36 @@ class AiBlogTask extends Command
$aiBlogService->task_id = $info['task_id'];
$result = $aiBlogService->getDetail();
if(!isset($result['status'])){
//修改任务状态
$aiBlogTaskModel->edit(['status'=>3],['id'=>$info['id']]);
continue;
}
if($result['status'] != 200){
sleep(300);
sleep(10);
continue;
}
//修改任务状态
$aiBlogTaskModel->edit(['status'=>2],['id'=>$info['id']]);
//保存当前项目ai_blog数据
ProjectServer::useProject($info['project_id']);
$aiBlogModel = new AiBlog();
$aiBlogModel->edit(['new_title'=>$result['data']['title'],'image'=>$result['data']['thumb'],'text'=>$result['data']['section'],'status'=>2],['task_id'=>$info['task_id']]);
$aiBlogInfo = $aiBlogModel->read(['task_id'=>$info['task_id']],['id']);
if($aiBlogInfo === false){
$aiBlogTaskModel->edit(['status'=>2],['id'=>$info['id']]);
continue;
}
//拿到返回的路由查看是否重复
$route = RouteMap::setRoute($result['data']['url'], RouteMap::SOURCE_AI_BLOG, $aiBlogInfo['id'], $info['project_id']);
if($route != $result['data']['url']){
$aiBlogService->updateDetail(['route'=>$this->param['route']]);
}
$aiBlogModel->edit(['new_title'=>$result['data']['title'], 'image'=>$result['data']['thumb'], 'text'=>$result['data']['section'], 'author_id'=>$result['data']['author_id'], 'route'=>$route ,'status'=>2], ['task_id'=>$info['task_id']]);
$this->updateAiBlogAuthor($aiSettingInfo,$result['data']['author_id']);
DB::disconnect('custom_mysql');
//修改任务状态
$aiBlogTaskModel->edit(['status'=>2],['id'=>$info['id']]);
echo '结束->任务id:' . $info['task_id'] . PHP_EOL . date('Y-m-d H:i:s');
}
return true;
}
/**
* @remark :获取项目配置
* @name :getSetting
... ... @@ -90,4 +102,30 @@ class AiBlogTask extends Command
Cache::put('ai_blog_'.$project_id,$aiSettingInfo,3600);
return $aiSettingInfo;
}
/**
* @remark :更新作者的页面
* @name :updateAiBlogAuthor
* @author :lyh
* @method :post
* @time :2025/2/21 11:53
*/
public function updateAiBlogAuthor($aiSettingInfo,$author_id){
if(empty($author_id)){
return true;
}
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$aiBlogService->author_id = $author_id;
$result = $aiBlogService->getAuthorDetail();
if(isset($result['status']) && $result['status'] == 200){
//当前作者的页面
$aiBlogAuthorModel = new AiBlogAuthor();
if(!empty($result['data']['section'])){
$aiBlogAuthorModel->edit(['text'=>$result['data']['section']],['author_id'=>$author_id]);
}
}
return true;
}
}
... ...
<?php
/**
* @remark :
* @name :RequestUrlLog.php
* @author :lyh
* @method :post
* @time :2025/2/21 9:55
*/
namespace App\Console\Commands\RequestUrlLog;
use Illuminate\Console\Command;
class RequestUrlLog extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'request_url_log';
/**
* The console command description.
*
* @var string
*/
protected $description = '统计昨日数据';
}
... ...
... ... @@ -7,6 +7,7 @@ use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Ai\AiBlogLogic;
use App\Http\Requests\Bside\Ai\AiBlogRequest;
use App\Models\Ai\AiBlog;
use App\Models\Ai\AiBlogAuthor;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\DB;
... ... @@ -27,6 +28,42 @@ class AiBlogController extends BaseController
}
/**
* @remark :获取详情
* @name :getInfo
* @author :lyh
* @method :post
* @time :2025/2/20 18:17
*/
public function getInfo(AiBlog $aiBlog){
$this->request->validate([
'id'=>['required'],
],[
'id.required' => '主键不能为空',
]);
$info = $aiBlog->read(['id'=>$this->param['id']]);
$info['image'] = getImageUrl($info['image']);
$this->response('success',Code::SUCCESS,$info);
}
/**
* @remark :删除
* @name :delete
* @author :lyh
* @method :post
* @time :2025/2/20 18:19
*/
public function delete(AiBlogLogic $aiBlogLogic)
{
$this->request->validate([
'ids'=>['required', new Ids()]
],[
'ids.required' => 'ID不能为空'
]);
$aiBlogLogic->blogDelete();
$this->response('success');
}
/**
* @remark :获取ai博客列表
* @name :getAiBlog
* @author :lyh
... ... @@ -37,7 +74,7 @@ class AiBlogController extends BaseController
$lists = $aiBlog->lists($this->map,$this->page,$this->row,'id',['id','new_title','image','task_id','status','created_at','updated_at']);
if(!empty($lists) && !empty($lists['list'])){
foreach ($lists['list'] as $k => $v){
$v['image'] = getImageUrl($v['image'],$this->user['storage_type'],$this->user['project_location']);
$v['image'] = getImageUrl($v['image']);
$lists['list'][$k] = $v;
}
}
... ... @@ -55,11 +92,9 @@ class AiBlogController extends BaseController
$this->request->validate([
'keyword'=>['required'],
'type'=>['required'],
'route'=>['required']
],[
'keyword.required' => '关键字不能为空',
'type.required' => '场景不能为空',
'route.required' => '路由不能为空',
]);
//获取当前项目的ai_blog设置
$result = $aiBlogLogic->sendTask();
... ... @@ -67,15 +102,50 @@ class AiBlogController extends BaseController
}
/**
* @remark :创建作者
* @name :createAuthor
* @remark :获取作者列表
* @name :getAiBlogAuthor
* @author :lyh
* @method :post
* @time :2025/2/20 10:45
* @time :2025/2/21 11:44
*/
public function createAuthor(AiBlogLogic $aiBlogLogic){
//获取当前项目的ai_blog设置
$result = $aiBlogLogic->createAuthor();
$this->response('success',Code::SUCCESS,$result);
public function getAiBlogAuthor(AiBlogAuthor $aiBlogAuthor){
$field = ['id','route','author_id','title','image','created_at','updated_at'];
$lists = $aiBlogAuthor->lists($this->map,$this->page,$this->row,'id',$field);
$this->response('success',Code::SUCCESS,$lists);
}
/**
* @remark :获取详情数据
* @name :getAuthorInfo
* @author :lyh
* @method :post
* @time :2025/2/21 13:54
*/
public function getAuthorInfo(AiBlogAuthor $aiBlogAuthor){
$this->request->validate([
'id'=>['required'],
],[
'id.required' => '主键不能为空',
]);
$info = $aiBlogAuthor->read($this->map);
$info['image'] = getImageUrl($info['image']);
$this->response('success',Code::SUCCESS,$info);
}
/**
* @remark :编辑详情数据
* @name :saveBlogAuthor
* @author :lyh
* @method :post
* @time :2025/2/21 13:54
*/
public function saveBlogAuthor(AiBlogLogic $aiBlogLogic){
$this->request->validate([
'id'=>['required'],
],[
'id.required' => '主键不能为空',
]);
$info = $aiBlogLogic->saveBlogAuthor();
$this->response('success',Code::SUCCESS,$info);
}
}
... ...
... ... @@ -12,6 +12,7 @@ use App\Helper\FormGlobalsoApi;
use App\Http\Logic\Aside\BaseLogic;
use App\Jobs\CopyImageFileJob;
use App\Jobs\CopyProjectJob;
use App\Models\Ai\AiBlogAuthor;
use App\Models\Channel\Channel;
use App\Models\Channel\User;
use App\Models\Channel\Zone;
... ... @@ -24,6 +25,7 @@ use App\Models\Inquiry\InquiryIP;
use App\Models\Inquiry\InquirySet;
use App\Models\Manage\Manage;
use App\Models\Project\After;
use App\Models\Project\AiBlogTask;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\Project\InquiryFilterConfig;
... ... @@ -222,6 +224,7 @@ class ProjectLogic extends BaseLogic
'key'=>$result['data']['key'],
];
$aiSettingModel->add($resData);
$this->createAuthor($project_id,$result['data']['mch_id'],$result['data']['key']);
}
}else{
//有信息更新
... ... @@ -244,6 +247,30 @@ class ProjectLogic extends BaseLogic
}
/**
* @remark :创建作者
* @name :createAuthor
* @author :lyh
* @method :post
* @time :2025/2/21 11:17
*/
public function createAuthor($project_id,$mch_id,$key){
//查看当前项目是否已经创建了作者
$aiBlogTaskModel = new AiBlogTask();
$count = $aiBlogTaskModel->counts();
if($count > 0){
return true;
}
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $mch_id;
$aiBlogService->key = $key;
$result = $aiBlogService->createAuthor();
if($result['status'] == 200){
$aiBlogTaskModel->add(['project_id'=>$project_id,'status'=>1,'type'=>1]);
}
return true;
}
/**
* @remark :选择服务器后双向绑定
* @name :setServers
* @author :lyh
... ...
... ... @@ -28,30 +28,68 @@ class AiBlogLogic extends BaseLogic
* @time :2023/7/5 14:46
*/
public function blogSave(){
try {
if(!empty($this->param['image'])){
$this->param['image'] = str_replace_url($this->param['image']);
}
$this->param['route'] = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_PRODUCT, $this->param['id'], $this->user['project_id']);
$rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error');
$this->param['route'] = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_AI_BLOG, $this->param['id'], $this->user['project_id']);
$this->model->edit($this->param,['id'=>$this->param['id']]);
$aiSettingInfo = $this->getProjectAiSetting();
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$aiBlogService->updateDetail(['title'=>$this->param['title'],'thumb'=>$this->param['image'],'route'=>$this->param['route'],'author_id'=>$this->param['author_id']]);
}catch (\Exception $e){
$this->fail('保存失败,请联系管理员');
}
return $this->success();
}
/**
* @remark :发布任务
* @name :sendTask
* @remark :获取配置信息
* @name :getProjectAiSetting
* @author :lyh
* @method :post
* @time :2025/2/14 10:28
* @time :2025/2/21 14:51
*/
public function sendTask(){
public function getProjectAiSetting(){
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$this->user['project_id']]);
if($aiSettingInfo === false){
$this->fail('请先联系管理员开启Ai博客');
}
return $aiSettingInfo;
}
/**
* @remark :编辑作者
* @name :saveAuthor
* @author :lyh
* @method :post
* @time :2025/2/21 14:46
*/
public function saveBlogAuthor(){
try {
if(!empty($this->param['image'])){
$this->param['image'] = str_replace_url($this->param['image']);
}
$this->param['route'] = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_AI_BLOG_AUTHOR, $this->param['id'], $this->user['project_id']);
$this->model->edit($this->param,['id'=>$this->param['id']]);
}catch (\Exception $e){
$this->fail('保存失败,请联系管理员');
}
return $this->success();
}
/**
* @remark :发布任务
* @name :sendTask
* @author :lyh
* @method :post
* @time :2025/2/14 10:28
*/
public function sendTask(){
$aiSettingInfo = $this->getProjectAiSetting();
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
... ... @@ -61,10 +99,8 @@ class AiBlogLogic extends BaseLogic
$aiBlogTaskModel = new AiBlogTask();
$aiBlogTaskModel->addReturnId(['project_id'=>$this->user['project_id'],'task_id'=>$result['data']['task_id'],'status'=>$result['data']['status']]);
$aiBlogModel = new AiBlog();
$aiBlogId = $aiBlogModel->addReturnId(['keyword'=>$this->param['keyword'], 'status'=>$result['data']['status'], 'task_id'=>$result['data']['task_id'],'project_id'=>$this->user['project_id'],
$aiBlogModel->addReturnId(['keyword'=>$this->param['keyword'], 'status'=>$result['data']['status'], 'task_id'=>$result['data']['task_id'],'project_id'=>$this->user['project_id'],
]);
$route = RouteMap::setRoute($aiBlogService->route, RouteMap::SOURCE_AI_BLOG, $aiBlogId, $this->user['project_id']);
$aiBlogModel->edit(['route'=>$route],['id'=>$aiBlogId]);
}
return $this->success();
}
... ... @@ -77,15 +113,31 @@ class AiBlogLogic extends BaseLogic
* @time :2025/2/20 10:46
*/
public function createAuthor(){
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$this->user['project_id']]);
if($aiSettingInfo === false){
$this->fail('请先联系管理员开启Ai博客');
}
$aiSettingInfo = $this->getProjectAiSetting();
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$result = $aiBlogService->createAuthor();
return $this->success($result);
}
/**
* @remark :删除
* @name :blogDelete
* @author :lyh
* @method :post
* @time :2025/2/20 18:21
*/
public function blogDelete(){
try {
foreach ($this->param['ids'] as $id) {
//删除路由映射
RouteMap::delRoute(RouteMap::SOURCE_AI_BLOG, $id, $this->user['project_id']);
$this->model->del(['id'=>$id]);
}
}catch (\Exception $e){
$this->fail('删除失败');
}
return $this->success();
}
}
... ...
<?php
/**
* @remark :
* @name :AiBlogAuthor.php
* @author :lyh
* @method :post
* @time :2025/2/21 10:59
*/
namespace App\Models\Ai;
use App\Models\Base;
class AiBlogAuthor extends Base
{
protected $table = 'gl_ai_blog_author';
//连接数据库
protected $connection = 'custom_mysql';
}
... ...
<?php
/**
* @remark :
* @name :RequestUrlLog.php
* @author :lyh
* @method :post
* @time :2025/2/21 9:38
*/
namespace App\Models\RequestUrlLog;
use App\Models\Base;
/**
* @remark :url请求日志
* @name :RequestUrlLog
* @author :lyh
* @method :post
* @time :2025/2/21 9:48
*/
class RequestUrlLog extends Base
{
protected $table = 'gl_request_url_log';
}
... ...
... ... @@ -33,8 +33,8 @@ class RouteMap extends Base
const SOURCE_NEWS_CATE = 'news_category';
//自定义模块
const SOURCE_MODULE = 'module';
const SOURCE_AI_BLOG = 'ai_blog';
const SOURCE_AI_BLOG_AUTHOR = 'ai_blog_author';//ai博客作者
//自定义模块分类
const SOURCE_MODULE_CATE = 'module_category';
... ...
... ... @@ -20,6 +20,7 @@ class AiBlogService
public $route = '';//回调地址
public $task_id = '';//任务id
public $author_id = '';//作者id
/**
* @remark :创建项目
* @name :createProject
... ... @@ -104,6 +105,24 @@ class AiBlogService
}
/**
* @remark :获取作者信息
* @name :getAuthor
* @author :lyh
* @method :post
* @time :2025/2/21 10:57
*/
public function getAuthor(){
$request_url = $this->url.'api/author/list';
$param = [
'mch_id'=>$this->mch_id,
];
$this->sign = $this->generateSign($param,$this->key);
$param['sign'] = $this->sign;
$result = http_post($request_url,json_encode($param,true));
return $result;
}
/**
* @remark :计算签名
* @name :generateSign
* @author :lyh
... ... @@ -143,4 +162,40 @@ class AiBlogService
$result = http_post($request_url,json_encode($param,true));
return $result;
}
/**
* @remark :获取作者页面
* @name :getAuthorDetail
* @author :lyh
* @method :post
* @time :2025/2/21 11:55
*/
public function getAuthorDetail(){
$request_url = $this->url.'api/author/detail';
$param = [
'mch_id'=>$this->mch_id,
'author_id'=>$this->author_id,
];
$this->sign = $this->generateSign($param,$this->key);
$param['sign'] = $this->sign;
$result = http_post($request_url,json_encode($param,true));
return $result;
}
/**
* @remark :更新文章
* @name :updateDetail
* @author :lyh
* @method :post
* @time :2025/2/21 14:38
* @param :title , thumb , route , task_id
*/
public function updateDetail($param){
$request_url = $this->url.'api/result/save';
$param['mch_id'] = $this->mch_id;
$this->sign = $this->generateSign($param,$this->key);
$param['sign'] = $this->sign;
$result = http_post($request_url,json_encode($param,true));
return $result;
}
}
... ...
... ... @@ -156,7 +156,11 @@ Route::middleware(['bloginauth'])->group(function () {
Route::any('/blog/getAiBlog', [\App\Http\Controllers\Bside\Ai\AiBlogController::class, 'getAiBlog'])->name('ai_blog_getAiBlog');
Route::any('/blog/save', [\App\Http\Controllers\Bside\Ai\AiBlogController::class, 'save'])->name('ai_blog_save');
Route::any('/blog/sendTask', [\App\Http\Controllers\Bside\Ai\AiBlogController::class, 'sendTask'])->name('ai_blog_sendTask');
Route::any('/blog/createAuthor', [\App\Http\Controllers\Bside\Ai\AiBlogController::class, 'createAuthor'])->name('ai_blog_createAuthor');
Route::any('/blog/del', [\App\Http\Controllers\Bside\Ai\AiBlogController::class, 'delete'])->name('ai_blog_delete');
Route::any('/blog/getAiBlogAuthor', [\App\Http\Controllers\Bside\Ai\AiBlogController::class, 'getAiBlogAuthor'])->name('ai_blog_getAiBlogAuthor');
Route::any('/blog/getAuthorInfo', [\App\Http\Controllers\Bside\Ai\AiBlogController::class, 'getAuthorInfo'])->name('ai_blog_getAuthorInfo');
Route::any('/blog/saveBlogAuthor', [\App\Http\Controllers\Bside\Ai\AiBlogController::class, 'saveBlogAuthor'])->name('ai_blog_saveBlogAuthor');
Route::any('/blog/getInfo', [\App\Http\Controllers\Bside\Ai\AiBlogController::class, 'getInfo'])->name('ai_blog_getInfo');
Route::any('/product/', [\App\Http\Controllers\Bside\Ai\AiProductController::class, 'save'])->name('ai_product_save');
Route::any('/product/productList', [\App\Http\Controllers\Bside\Ai\AiProductController::class, 'productList'])->name('ai_product_productList');
Route::any('/product/productCateList', [\App\Http\Controllers\Bside\Ai\AiProductController::class, 'productCateList'])->name('ai_product_productCateList');
... ...