AiBlogLogic.php
8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace App\Http\Logic\Bside\Ai;
use App\Helper\Translate;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Ai\AiBlog;
use App\Models\Ai\AiBlogAuthor;
use App\Models\Com\Notify;
use App\Models\Project\AiBlogTask;
use App\Models\Project\ProjectAiSetting;
use App\Models\RouteMap\RouteMap;
use App\Services\AiBlogService;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;
class AiBlogLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new AiBlog();
}
/**
* @remark :获取配置信息
* @name :getProjectAiSetting
* @author :lyh
* @method :post
* @time :2025/2/21 14:51
*/
public function getProjectAiSetting(){
$ai_cache = Cache::get('ai_blog_'.$this->user['project_id']);
if($ai_cache){
return $ai_cache;
}
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$this->user['project_id']]);
if($aiSettingInfo === false){
$this->fail('请先联系管理员开启Ai博客');
}
Cache::put('ai_blog_'.$this->user['project_id'],$aiSettingInfo,3600);
return $aiSettingInfo;
}
/**
* @remark :编辑ai发布博客
* @name :blogSave
* @author :lyh
* @method :post
* @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_AI_BLOG, $this->param['id'], $this->user['project_id']);
$anchor = $this->param['anchor'] ?? [];
$this->param['anchor'] = json_encode($anchor,true);
$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(['task_id'=>$this->param['task_id'],'title'=>$this->param['new_title'],'thumb'=>$this->param['image'],'route'=>$this->param['route'],'author_id'=>$anchor]);
}catch (\Exception $e){
$this->fail('保存失败,请联系管理员');
}
$this->sendHttpC([$this->param['route'],'top-blog']);
shell_exec("php artisan save_ai_blog_list {$this->user['project_id']} > /dev/null 2>&1 &");
return $this->success();
}
/**
* @remark :编辑作者
* @name :saveAuthor
* @author :lyh
* @method :post
* @time :2025/2/21 14:46
*/
public function saveBlogAuthor(){
try {
$aiAuthorModel = new AiBlogAuthor();
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']);
$aiAuthorModel->edit($this->param,['id'=>$this->param['id']]);
$aiSettingInfo = $this->getProjectAiSetting();
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$aiBlogService->updateAuthorInfo(['author_id'=>$this->param['author_id'],'route'=>$this->param['route'],'title'=>$this->param['title'],'picture'=>$this->param['image'],'description'=>$this->param['description']]);
$aiBlogTask = new AiBlogTask();
$aiBlogTask->addReturnId(['type'=>$aiBlogTask::TYPE_AUTHOR_ID,'task_id'=>$this->param['author_id'],'status'=>1,'project_id'=>$this->user['project_id']]);
}catch (\Exception $e){
$this->fail('保存失败,请联系管理员');
}
$this->sendHttpC([$this->param['route'],'top-blog']);
return $this->success();
}
/**
* @remark :发布任务
* @name :sendTask
* @author :lyh
* @method :post
* @time :2025/2/14 10:28
* @detail :type=2/生成文章 type=3/更新列表页记录
* @detail :status=1/待执行
*/
public function sendTask(){
$aiSettingInfo = $this->getProjectAiSetting();
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$aiBlogService->route = generateRoute(Translate::tran($this->param['keyword'], 'en'));
$result = $aiBlogService->createTask($this->param['keyword'],2,'blog',$this->param['anchor'] ?? [],$this->param['title'] ?? '');
if($result['status'] == 200){
$aiBlogTaskModel = new AiBlogTask();
$aiBlogTaskModel->addReturnId(['project_id'=>$this->user['project_id'],'type'=>2,'task_id'=>$result['data']['task_id'],'status'=>1]);
$this->model->addReturnId(['keyword'=>$this->param['keyword'], 'status'=>1, 'task_id'=>$result['data']['task_id'],'project_id'=>$this->user['project_id'],'anchor'=>json_encode($this->param['anchor'] ?? [],true)
]);
}else{
@file_put_contents(storage_path('logs/lyh_error.log'), var_export($result, true) . PHP_EOL, FILE_APPEND);
}
return $this->success();
}
/**
* @remark :删除
* @name :blogDelete
* @author :lyh
* @method :post
* @time :2025/2/20 18:21
*/
public function blogDelete(){
try {
$aiSettingInfo = $this->getProjectAiSetting();
$aiBlogService = new AiBlogService();
$aiBlogTask = new AiBlogTask();
foreach ($this->param['ids'] as $id) {
$info = $this->model->read(['id'=>$id],['task_id','route','author_id']);
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$aiBlogService->delDetail($info['task_id']);
//重新生成作者页面
$aiBlogTask->addReturnId(['type'=>$aiBlogTask::TYPE_AUTHOR_ID,'task_id'=>$info['author_id'],'status'=>1,'project_id'=>$this->user['project_id']]);
//删除路由映射
RouteMap::delRoute(RouteMap::SOURCE_AI_BLOG, $id, $this->user['project_id']);
$this->model->del(['id'=>$id]);
$this->curlDelRoute(['old_route'=>$info['route']]);
}
Artisan::call('save_ai_blog_list', ['project_id' => $this->user['project_id']]);
}catch (\Exception $e){
$this->fail('删除失败');
}
return $this->success();
}
/**
* @remark :保存自定义
* @name :customSaveBlog
* @author :lyh
* @method :post
* @time :2025/9/16 11:43
*/
public function customSaveBlog($param)
{
try {
//处理路由
if(empty($param['route'])){
$param['route'] = generateRoute(Translate::tran($param['new_title'], 'en'));
}
//推送到ai的数据结构
$data = [
'title'=>$param['new_title'], 'thumb'=>$param['image'], 'foreword'=>$param['description'],
'author_id'=>$this->param['author_id'], 'url'=>$param['route'],
];
if(isset($param['id']) && !empty($param['id'])){
$id = $param['id'];
$data['task_id'] = $param['task_id'];
$param['route'] = RouteMap::setRoute($param['route'], RouteMap::SOURCE_AI_BLOG, $id, $this->user['project_id']);
$this->model->edit($param,['id'=>$param['id']]);
}else{
$param['uuid'] = md5(date('YmdHis').rand(100,999).$this->user['project_id']);
$param['project_id'] = $this->user['project_id'];
$param['status'] = $this->model::STATUS_RUNNING;
$id = $this->model->addReturnId($param);
$route = RouteMap::setRoute($param['route'], RouteMap::SOURCE_AI_BLOG, $id, $this->user['project_id']);
$this->model->edit(['route'=>$route],['id'=>$id]);
}
}catch (\Exception $e){
$this->fail('保存失败,请联系管理员');
}
$aiBlogService = new AiBlogService($this->user['project_id']);
$result = $aiBlogService->createCustomBlog($data);
if(isset($result['status']) && $result['status'] == 200){
$this->model->edit(['task_id'=>$result['data']['task_id'],'status'=>$this->model::STATUS_FINISH],['id'=>$id]);
//todo::更新列表页
shell_exec("php artisan save_ai_blog_list {$this->user['project_id']} > /dev/null 2>&1 &");
}
return $this->success();
}
}