作者 刘锟

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

<?php
/**
* @remark :
* @name :AiBlogTask.php
* @author :lyh
* @method :post
* @time :2025/2/14 11:14
*/
namespace App\Console\Commands\AiBlog;
use App\Models\Ai\AiBlog;
use App\Models\Project\ProjectAiSetting;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\String\s;
class AiBlogTask extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'save_ai_blog';
/**
* The console command description.
*
* @var string
*/
protected $description = '查询ai_blog是否已经生成';
public function handle(){
$aiBlogTaskModel = new AiBlogTaskModel();
while (true){
$info = $aiBlogTaskModel->where('status',1)->orderBy('id','asc')->first();
if($info === false){
sleep(20);
continue;
}
$info = $info->toArray();
echo '开始->任务id:' . $info['task_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'];
$aiBlogService->task_id = $info['task_id'];
$result = $aiBlogService->getDetail();
if($result['status'] != 200){
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'] ?? '','text'=>$result['data']['text'] ?? '','status'=>2],['task_id'=>$info['task_id']]);
DB::disconnect('custom_mysql');
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;
}
}
... ...
... ... @@ -2,9 +2,14 @@
namespace App\Http\Controllers\Bside\Ai;
use App\Enums\Common\Code;
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\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\DB;
class AiBlogController extends BaseController
{
... ... @@ -20,4 +25,36 @@ class AiBlogController extends BaseController
$aiBlogLogic->blogSave();
$this->response('success');
}
/**
* @remark :获取ai博客列表
* @name :getAiBlog
* @author :lyh
* @method :post
* @time :2025/2/14 13:59
*/
public function getAiBlog(AiBlog $aiBlog){
$lists = $aiBlog->lists($this->map,$this->page,$this->row,'id',['id','new_title','task_id','status','created_at','updated_at']);
$this->response('success',Code::SUCCESS,$lists);
}
/**
* @remark :发布任务
* @name :sendTask
* @author :lyh
* @method :post
* @time :2025/2/14 10:25
*/
public function sendTask(AiBlogLogic $aiBlogLogic){
$this->request->validate([
'keyword'=>['required'],
'type'=>['required']
],[
'keyword.required' => '关键字不能为空',
'type.required' => '场景不能为空',
]);
//获取当前项目的ai_blog设置
$result = $aiBlogLogic->sendTask();
$this->response('success',Code::SUCCESS,$result);
}
}
... ...
... ... @@ -662,7 +662,7 @@ class ProductController extends BaseController
'product_id.required' => 'product_id不能为空',
]);
$productInfo = $product->read(['id' => $this->param['product_id']]);
if (empty($productInfo)) {
if ($productInfo == false) {
$this->fail('请选择有效产品信息!');
}
$productInfo = $this->handleParam($productInfo);
... ...
... ... @@ -12,6 +12,7 @@ namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Helper\PayStripeApi;
use App\Http\Logic\Bside\News\NewsLogic;
use App\Models\Ai\AiBlog;
use App\Models\Channel\Channel;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
... ... @@ -21,24 +22,38 @@ use App\Models\ExtentModule\ExtensionModuleValue;
use App\Models\Manage\ManageHr;
use App\Models\Project\CountAllProject as AllProject;
use App\Models\Project\Project;
use App\Models\Project\ProjectAiSetting;
use App\Models\RouteMap\RouteMap;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\DB;
class TestController extends BaseController
{
/**
* @remark :非6.0拉取数据
* @name :NoSixProject
* @remark :创建项目
* @name :createProject
* @author :lyh
* @method :post
* @time :2024/11/11 14:51
* @time :2025/2/13 16:34
*/
public function ceshi(){
$pay = new PayStripeApi();
$data = $pay->createPaymentIntent(5000,'cny');
$this->response('success',Code::SUCCESS,$data);
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = '100008';
$aiBlogService->key = '8a9c925bdcca';
$result = $aiBlogService->createTask('apple');
if($result['status'] == 200){
$param = [
'keywords'=>'apple',
'status'=>$result['data']['status'],
'task_id'=>$result['data']['task_id'],
'project_id'=>1,
];
ProjectServer::useProject(1);
$aiBlogModel = new AiBlog();
$aiBlogModel->add($param);
DB::disconnect('custom_mysql');
}
$this->response('success',Code::SUCCESS,$param);
}
}
... ...
... ... @@ -30,6 +30,7 @@ use App\Models\Project\InquiryFilterConfig;
use App\Models\Project\MinorLanguages;
use App\Models\Project\Payment;
use App\Models\Project\Project;
use App\Models\Project\ProjectAiSetting;
use App\Models\Project\ProjectKeyword;
use App\Models\Project\ProjectRenew;
use App\Models\Project\WebTrafficConfig;
... ... @@ -43,6 +44,7 @@ use App\Models\User\ProjectMenu;
use App\Models\User\ProjectRole;
use App\Models\User\User as UserModel;
use App\Models\WebSetting\WebLanguage;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use App\Services\SyncService;
use App\Utils\LogUtils;
... ... @@ -158,6 +160,8 @@ class ProjectLogic extends BaseLogic
$this->setServers($this->param['serve_id'],$this->param['id']);
//保存项目信息
$this->saveProject($this->param);
//ai_blog
$this->setAiBlog($this->param['id'],$this->param['main_lang_id'],$this->param['is_ai_blog'],$this->param['title']);
//保存建站部署信息
$this->saveProjectDeployBuild($this->param['deploy_build']);
//保存付费信息
... ... @@ -184,6 +188,59 @@ class ProjectLogic extends BaseLogic
}
/**
* @remark :开启AI博客后
* @name :setAiBlog
* @author :lyh
* @method :post
* @time :2025/2/13 16:02
*/
public function setAiBlog($project_id,$main_lang_id,$is_ai_blog,$title){
if(empty($main_lang_id) || empty($is_ai_blog)){
return true;
}
$projectModel = new Project();
$projectInfo = $projectModel->read(['id'=>$project_id],['is_ai_blog','main_lang_id']);
//获取项目主语种
$languageModel = new WebLanguage();
$languageInfo = $languageModel->read(['id'=>$main_lang_id],['short']);
if($languageInfo == false){
return true;
}
$aiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $aiSettingModel->read(['project_id'=>$project_id]);
if($aiSettingInfo === false){
$aiBlogService = new AiBlogService();
$result = $aiBlogService->createProject($title,$languageInfo['short'],$projectInfo['company']);
if($result['status'] == 200){
//查看当前项目是否已有记录
$resData = [
'project_id'=>$project_id,
'mch_id'=>$result['data']['mch_id'],
'key'=>$result['data']['key'],
];
$aiSettingModel->add($resData);
}
}else{
//有信息更新
if(($projectInfo['title'] != $title) || ($projectInfo['main_lang_id'] != $main_lang_id)){
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$result = $aiBlogService->updatedProject($title,$languageInfo['short']);
if($result['status'] == 200){
$resData = [
'mch_id'=>$result['data']['mch_id'],
'key'=>$result['data']['key'],
];
$aiSettingModel = new ProjectAiSetting();
$aiSettingModel->edit($resData,['project_id'=>$project_id]);
}
}
}
return true;
}
/**
* @remark :选择服务器后双向绑定
* @name :setServers
* @author :lyh
... ...
... ... @@ -4,6 +4,10 @@ namespace App\Http\Logic\Bside\Ai;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Ai\AiBlog;
use App\Models\Project\AiBlogTask;
use App\Models\Project\Project;
use App\Models\Project\ProjectAiSetting;
use App\Services\AiBlogService;
class AiBlogLogic extends BaseLogic
{
... ... @@ -29,4 +33,35 @@ class AiBlogLogic extends BaseLogic
}
return $this->success();
}
/**
* @remark :发布任务
* @name :sendTask
* @author :lyh
* @method :post
* @time :2025/2/14 10:28
*/
public function sendTask(){
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$this->user['project_id']]);
if($aiSettingInfo === false){
$this->fail('请先联系管理员开启Ai博客');
}
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$result = $aiBlogService->createTask($this->param['keyword'],$this->param['type']);
if($result['status'] == 200){
try {
$aiBlogTaskModel = new AiBlogTask();
$aiBlogTaskModel->addReturnId(['project_id'=>$this->user['project_id'],'task_id'=>$result['data']['task_id'],'status'=>$result['data']['status']]);
$aiBlogModel = new AiBlog();
$aiBlogModel->addReturnId(['keyword'=>$this->param['keyword'], 'status'=>$result['data']['status'], 'task_id'=>$result['data']['task_id'],'project_id'=>$this->user['project_id'],
]);
}catch (\Exception $e){
$this->fail('请求ai_blog失败,请联系管理员');
}
}
return $this->success();
}
}
... ...
... ... @@ -46,10 +46,10 @@ class CustomTemplateLogic extends BaseLogic
*/
public function customTemplateInfo(){
$info = $this->model->read(['id'=>$this->param['id']]);
$info['image'] = getImageUrl($info['image'],$this->user['storage_type'],$this->user['project_location']);
if($info === false){
$this->fail('当前数据不存在');
}
$info['image'] = getImageUrl($info['image'],$this->user['storage_type'],$this->user['project_location']);
if($info['is_visualization'] == 0 || $info['is_visualization'] == 1){
$template_id = $this->getTemplateId();
$html = $this->getTemplateComHtml($info['html'],$info['html_style'],$template_id);
... ...
<?php
/**
* @remark :
* @name :AiBlogTask.php
* @author :lyh
* @method :post
* @time :2025/2/14 11:03
*/
namespace App\Models\Project;
use App\Models\Base;
class AiBlogTask extends Base
{
protected $table = 'gl_ai_blog_task';
}
... ...
<?php
/**
* @remark :
* @name :ProjectAiSetting.php
* @author :lyh
* @method :post
* @time :2025/2/13 16:53
*/
namespace App\Models\Project;
use App\Models\Base;
/**
* @remark :项目ai_blog设置
* @name :ProjectAiSetting
* @author :lyh
* @method :post
* @time :2025/2/13 16:53
*/
class ProjectAiSetting extends Base
{
protected $table = 'gl_project_ai_setting';
}
... ...
<?php
/**
* @remark :
* @name :AiBlogService.php
* @author :lyh
* @method :post
* @time :2025/2/13 14:15
*/
namespace App\Services;
class AiBlogService
{
public $url = 'https://ai-extend.ai.cc/';
public $mch_id = 1;//默认配置
public $sign = '';//签名
public $key = 'b3e4c722b821';//默认key
public $webhook = 'https://develop.globalso.com/api/ai_webhook';//回调地址
public $task_id = '';//任务id
/**
* @remark :创建项目
* @name :createProject
* @author :lyh
* @method :post
* @time :2025/2/13 14:28
*/
public function createProject($project_name,$language = 'en',$profile){
$request_url = $this->url.'api/project/create';
$param = [
'mch_id'=>$this->mch_id,
'title'=>$project_name,
'language'=>$language,
'profile'=>$profile
];
$this->sign = $this->generateSign($param,$this->key);
$param['sign'] = $this->sign;
$result = http_post($request_url,json_encode($param,true));
return $result;
}
/**
* @remark :更新项目
* @name :updatedProject
* @author :lyh
* @method :post
* @time :2025/2/13 14:35
*/
public function updatedProject($project_name,$language = 'en'){
$request_url = $this->url.'api/project/save';
$param = [
'mch_id'=>$this->mch_id,
'title'=>$project_name,
'language'=>$language
];
$this->sign = $this->generateSign($param,$this->key);
$param['sign'] = $this->sign;
$result = http_post($request_url,json_encode($param,true));
return $result;
}
/**
* @remark :创建任务
* @name :createTask
* @author :lyh
* @method :post
* @time :2025/2/13 14:39
* @param :type=(1作者2文章) keyword=关键词 subtype=blog url=回调url
*/
public function createTask($keyword,$type = 1,$subtype = 'Blog',$template_id = 1){
$request_url = $this->url.'api/task/create';
$param = [
'mch_id'=>$this->mch_id,
'keyword'=>$keyword,
'type'=>$type,
'subtype'=>$subtype,
'url'=>$this->webhook,
'template_id'=>$template_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 :createAuthor
* @author :lyh
* @method :post
* @time :2025/2/13 14:43
*/
public function createAuthor(){
$request_url = $this->url.'api/author/create';
$param = [
'mch_id'=>$this->mch_id,
'sign'=>$this->sign,
];
$result = http_post($request_url,json_encode($param,true));
return $result;
}
/**
* @remark :计算签名
* @name :generateSign
* @author :lyh
* @method :post
* @time :2025/2/13 15:07
*/
public function generateSign($params, $key)
{
// 去除数组中所有值为空的项
array_filter($params);
// 按照key值的ASCII码从小到大排序
ksort($params);
// 生成URL的查询字符串
$string = http_build_query($params);
// 生成签名
$sign = md5($string . $key);
// 转换成大写
$sign = strtoupper($sign);
return $sign;
}
/**
* @remark :获取文章详情
* @name :getDetail
* @author :lyh
* @method :post
* @time :2025/2/14 11:23
*/
public function getDetail(){
$request_url = $this->url.'api/result/detail';
$param = [
'mch_id'=>$this->mch_id,
'task_id'=>$this->task_id,
];
$this->sign = $this->generateSign($param,$this->key);
$param['sign'] = $this->sign;
$result = http_post($request_url,json_encode($param,true));
return $result;
}
}
... ...
... ... @@ -153,7 +153,9 @@ Route::middleware(['bloginauth'])->group(function () {
Route::prefix('ai')->group(function () {
//ai
Route::any('/news/', [\App\Http\Controllers\Bside\Ai\AiNewsController::class, 'save'])->name('ai_news_save');
Route::any('/blog/', [\App\Http\Controllers\Bside\Ai\AiBlogController::class, 'save'])->name('ai_blog_save');
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('/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');
... ...