作者 lyh
@@ -64,7 +64,7 @@ class SyncProject extends Command @@ -64,7 +64,7 @@ class SyncProject extends Command
64 while (true){ 64 while (true){
65 $list = NoticeLog::where('type', NoticeLog::TYPE_PROJECT)->where('status', NoticeLog::STATUS_PENDING)->get(); 65 $list = NoticeLog::where('type', NoticeLog::TYPE_PROJECT)->where('status', NoticeLog::STATUS_PENDING)->get();
66 foreach ($list as $item){ 66 foreach ($list as $item){
67 - echo 'start:' . $item['id'] . PHP_EOL; 67 + echo date('Y-m-d') . ' start:' . $item['id'] . PHP_EOL;
68 try { 68 try {
69 $is_update = $item['data']['is_update']??0;//是否是4.0或5.0更新到6.0 69 $is_update = $item['data']['is_update']??0;//是否是4.0或5.0更新到6.0
70 $order_id = $item['data']['order_id']; 70 $order_id = $item['data']['order_id'];
@@ -97,7 +97,7 @@ class SyncProject extends Command @@ -97,7 +97,7 @@ class SyncProject extends Command
97 $item->save(); 97 $item->save();
98 echo 'success:' . $item['id'] . PHP_EOL; 98 echo 'success:' . $item['id'] . PHP_EOL;
99 }catch (\Exception $e){ 99 }catch (\Exception $e){
100 - echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL; 100 + echo 'error:' . $item['id'] . $e->getMessage() .' line ' . $e->getLine() . PHP_EOL;
101 errorLog('项目同步失败', $item, $e); 101 errorLog('项目同步失败', $item, $e);
102 $this->retry($item, $e->getMessage()); 102 $this->retry($item, $e->getMessage());
103 } 103 }
@@ -217,7 +217,7 @@ class SyncProject extends Command @@ -217,7 +217,7 @@ class SyncProject extends Command
217 DB::beginTransaction(); 217 DB::beginTransaction();
218 try { 218 try {
219 $id = $this->saveProject($data['project']); 219 $id = $this->saveProject($data['project']);
220 - $this->setPostId($data['deploy_build']['plan'],$id);; 220 + $this->setPostId($data['deploy_build']['plan'],$id);
221 $this->savePayment($data['payment'],$id); 221 $this->savePayment($data['payment'],$id);
222 $this->saveDeployBuild($data['deploy_build'],$id); 222 $this->saveDeployBuild($data['deploy_build'],$id);
223 $this->saveDeployOptimize($data['deploy_optimize'],$id); 223 $this->saveDeployOptimize($data['deploy_optimize'],$id);
@@ -244,7 +244,6 @@ class SyncProject extends Command @@ -244,7 +244,6 @@ class SyncProject extends Command
244 foreach ($task_list as $task){ 244 foreach ($task_list as $task){
245 UpdateLog::createLog($id,$task,$param['get_data_url']); 245 UpdateLog::createLog($id,$task,$param['get_data_url']);
246 } 246 }
247 -  
248 } 247 }
249 248
250 DB::commit(); 249 DB::commit();
@@ -279,9 +278,9 @@ class SyncProject extends Command @@ -279,9 +278,9 @@ class SyncProject extends Command
279 * @time :2023/8/30 15:53 278 * @time :2023/8/30 15:53
280 */ 279 */
281 public function saveProject($param){ 280 public function saveProject($param){
282 - if(isset($param['channel']) && !empty($param['channel'])){ 281 +// if(isset($param['channel']) && !empty($param['channel'])){
283 $param['channel'] = Arr::a2s($param['channel']); 282 $param['channel'] = Arr::a2s($param['channel']);
284 - } 283 +// }
285 $projectModel = new Project(); 284 $projectModel = new Project();
286 $info = $projectModel->read(['from_order_id'=>$param['from_order_id'],'delete_status'=>0]); 285 $info = $projectModel->read(['from_order_id'=>$param['from_order_id'],'delete_status'=>0]);
287 if($info !== false){ 286 if($info !== false){
@@ -85,48 +85,51 @@ class ProjectUpdate extends Command @@ -85,48 +85,51 @@ class ProjectUpdate extends Command
85 $data = http_get($url, ['charset' => 'UTF-8']); 85 $data = http_get($url, ['charset' => 'UTF-8']);
86 if (isset($data['code']) && $data['code'] == 200) { 86 if (isset($data['code']) && $data['code'] == 200) {
87 $items = $data['data'] ?? []; 87 $items = $data['data'] ?? [];
88 - $model = new Category();  
89 - foreach ($items as $item) {  
90 - $parent = $model->read(['pid' => 0, 'title' => $item['name']], 'id');  
91 - if (!$parent) {  
92 - try {  
93 - $parent_id = $model->addReturnId([  
94 - 'project_id' => $project_id,  
95 - 'title' => $item['name'],  
96 - 'pid' => 0,  
97 - 'keywords' => $item['keywords'],  
98 - 'describe' => $item['description']  
99 - ]);  
100 - $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['name'], RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);  
101 - $model->edit(['route' => $route], ['id' => $parent_id]);  
102 - } catch (\Exception $e) {  
103 - echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;  
104 - continue;  
105 - }  
106 - }else{  
107 - $parent_id = $parent['id'];  
108 - }  
109 -  
110 - foreach ($item['children'] as $child) {  
111 - $child_info = $model->read(['pid' => $parent_id, 'title' => $child['name']]);  
112 - if (!$child_info) {  
113 - try {  
114 - $child_id = $model->addReturnId([  
115 - 'project_id' => $project_id,  
116 - 'title' => $child['name'],  
117 - 'pid' => $parent_id,  
118 - 'keywords' => $child['keywords'],  
119 - 'describe' => $child['description']  
120 - ]);  
121 - $route = RouteMap::setRoute($child['url'] ? $this->get_url_route($child['url']) : $child['name'], RouteMap::SOURCE_PRODUCT_CATE, $child_id, $project_id);  
122 - $model->edit(['route' => $route], ['id' => $child_id]);  
123 - } catch (\Exception $e) {  
124 - echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;  
125 - continue;  
126 - }  
127 - }  
128 - }  
129 - } 88 + $this->category_insert($project_id, $items, 0);
  89 +// $model = new Category();
  90 +// foreach ($items as $item) {
  91 +// $parent = $model->read(['pid' => 0, 'title' => $item['name']], 'id');
  92 +// if (!$parent) {
  93 +// try {
  94 +// $parent_id = $model->addReturnId([
  95 +// 'project_id' => $project_id,
  96 +// 'title' => $item['name'],
  97 +// 'pid' => 0,
  98 +// 'keywords' => $item['keywords'],
  99 +// 'describe' => $item['description']
  100 +// ]);
  101 +// $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['name'], RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
  102 +// $model->edit(['route' => $route], ['id' => $parent_id]);
  103 +// } catch (\Exception $e) {
  104 +// echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
  105 +// continue;
  106 +// }
  107 +// } else {
  108 +// $parent_id = $parent['id'];
  109 +// }
  110 +//
  111 +// foreach ($item['children'] as $child) {
  112 +// $child_info = $model->read(['pid' => $parent_id, 'title' => $child['name']]);
  113 +// if (!$child_info) {
  114 +// try {
  115 +// $child_id = $model->addReturnId([
  116 +// 'project_id' => $project_id,
  117 +// 'title' => $child['name'],
  118 +// 'pid' => $parent_id,
  119 +// 'keywords' => $child['keywords'],
  120 +// 'describe' => $child['description']
  121 +// ]);
  122 +// $route = RouteMap::setRoute($child['url'] ? $this->get_url_route($child['url']) : $child['name'], RouteMap::SOURCE_PRODUCT_CATE, $child_id, $project_id);
  123 +// $model->edit(['route' => $route], ['id' => $child_id]);
  124 +// } catch (\Exception $e) {
  125 +// echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
  126 +// continue;
  127 +// }
  128 +// }
  129 +// }
  130 +// }
  131 + } else {
  132 + return true;
130 } 133 }
131 } elseif ($api_type == 'tag') { 134 } elseif ($api_type == 'tag') {
132 //关键词 135 //关键词
@@ -144,26 +147,30 @@ class ProjectUpdate extends Command @@ -144,26 +147,30 @@ class ProjectUpdate extends Command
144 147
145 $model = new Keyword(); 148 $model = new Keyword();
146 foreach ($items as $item) { 149 foreach ($items as $item) {
147 - $keyword = $model->read(['title' => $item['text_title']], 'id');  
148 - if (!$keyword) {  
149 - try {  
150 - $id = $model->addReturnId([  
151 - 'project_id' => $project_id,  
152 - 'title' => $item['text_title'],  
153 - 'seo_title' => $item['seo_title'],  
154 - 'seo_keywords' => $item['seo_keywords'],  
155 - 'seo_description' => $item['seo_description'],  
156 - ]);  
157 - $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['text_title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $project_id);  
158 - $model->edit(['route' => $route], ['id' => $id]);  
159 - } catch (\Exception $e) {  
160 - echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;  
161 - continue; 150 + if ($item['name'] ?? '') {
  151 + $keyword = $model->read(['title' => $item['name']], 'id');
  152 + if (!$keyword) {
  153 + try {
  154 + $id = $model->addReturnId([
  155 + 'project_id' => $project_id,
  156 + 'title' => $item['name'],
  157 + 'seo_title' => $item['seo_title'] ?? '',
  158 + 'seo_keywords' => $item['seo_keywords'] ?? '',
  159 + 'seo_description' => $item['seo_description'] ?? '',
  160 + ]);
  161 + $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['name'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $project_id);
  162 + $model->edit(['route' => $route], ['id' => $id]);
  163 + } catch (\Exception $e) {
  164 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
  165 + continue;
  166 + }
162 } 167 }
163 } 168 }
164 } 169 }
165 } 170 }
166 } 171 }
  172 + } else {
  173 + return true;
167 } 174 }
168 } elseif ($api_type == 'website_info') { 175 } elseif ($api_type == 'website_info') {
169 //网站信息 176 //网站信息
@@ -175,25 +182,37 @@ class ProjectUpdate extends Command @@ -175,25 +182,37 @@ class ProjectUpdate extends Command
175 182
176 $model = new WebSettingReceiving(); 183 $model = new WebSettingReceiving();
177 if ($phones) { 184 if ($phones) {
178 - $receiving_phones = $model->read(['type' => 1, 'values' => $phones]);  
179 - if (!$receiving_phones) {  
180 - $model->add([  
181 - 'type' => 1,  
182 - 'values' => $phones,  
183 - 'project_id' => $project_id  
184 - ]); 185 + $phone_arr = explode(',', $phones);
  186 + foreach ($phone_arr as $v_phone) {
  187 + if ($v_phone) {
  188 + $receiving_phones = $model->read(['type' => 2, 'values' => $v_phone]);
  189 + if (!$receiving_phones) {
  190 + $model->add([
  191 + 'type' => 1,
  192 + 'values' => $v_phone,
  193 + 'project_id' => $project_id
  194 + ]);
  195 + }
  196 + }
185 } 197 }
186 } 198 }
187 if ($emails) { 199 if ($emails) {
188 - $receiving_emails = $model->read(['type' => 2, 'values' => $emails]);  
189 - if (!$receiving_emails) {  
190 - $model->add([  
191 - 'type' => 2,  
192 - 'values' => $emails,  
193 - 'project_id' => $project_id  
194 - ]); 200 + $email_arr = explode(',', $emails);
  201 + foreach ($email_arr as $v_email) {
  202 + if ($v_email) {
  203 + $receiving_emails = $model->read(['type' => 1, 'values' => $v_email]);
  204 + if (!$receiving_emails) {
  205 + $model->add([
  206 + 'type' => 2,
  207 + 'values' => $v_email,
  208 + 'project_id' => $project_id
  209 + ]);
  210 + }
  211 + }
195 } 212 }
196 } 213 }
  214 + } else {
  215 + return true;
197 } 216 }
198 } elseif ($api_type == 'post') { 217 } elseif ($api_type == 'post') {
199 //产品 218 //产品
@@ -212,56 +231,61 @@ class ProjectUpdate extends Command @@ -212,56 +231,61 @@ class ProjectUpdate extends Command
212 $model = new Product(); 231 $model = new Product();
213 232
214 foreach ($items as $item) { 233 foreach ($items as $item) {
215 -  
216 - $product = $model->read(['title' => $item['ttile']], 'id');  
217 - if (!$product) {  
218 - //图片  
219 - $gallery = [];  
220 - foreach ($item['images'] as $k_img => $img) {  
221 - $gallery[] = ['alt' => '这是一张产品图', 'url' => $img];  
222 - }  
223 - //分类  
224 - $category_id = '';  
225 - if ($item['category']) {  
226 - $category_arr = [];  
227 - foreach ($item['category'] as $cate) {  
228 - if ($cate['parent'] == 0) {  
229 - array_unshift($category_arr, $cate['name']);  
230 - } else {  
231 - array_push($category_arr, $cate['name']); 234 + if ($item['ttile'] ?? '') {
  235 + $product = $model->read(['title' => $item['ttile']], 'id');
  236 + if (!$product) {
  237 + //图片
  238 + $gallery = [];
  239 + if ($item['images'] ?? []) {
  240 + foreach ($item['images'] as $k_img => $img) {
  241 + $gallery[] = ['alt' => '这是一张产品图', 'url' => $img];
232 } 242 }
233 } 243 }
234 - if ($category_arr) {  
235 - $categoryLogic = new CategoryLogic();  
236 - $category_id = $categoryLogic->importProductCategory($project_id, implode('/', $category_arr)); 244 + //分类
  245 + $category_id = '';
  246 + if ($item['category'] ?? []) {
  247 + $category_arr = [];
  248 + foreach ($item['category'] as $cate) {
  249 + if ($cate['parent'] == 0) {
  250 + array_unshift($category_arr, $cate['name']);
  251 + } else {
  252 + array_push($category_arr, $cate['name']);
  253 + }
  254 + }
  255 + if ($category_arr) {
  256 + $categoryLogic = new CategoryLogic();
  257 + $category_id = $categoryLogic->importProductCategory($project_id, implode('/', $category_arr));
  258 + }
237 } 259 }
238 - }  
239 - try {  
240 - $id = $model->addReturnId([  
241 - 'project_id' => $project_id,  
242 - 'title' => $item['ttile'],  
243 - 'intro' => $item['description'],  
244 - 'content' => $item['content'],  
245 - 'category_id' => $category_id,  
246 - 'thumb' => isset($gallery[0]) ? Arr::a2s($gallery[0]) : '',  
247 - 'gallery' => Arr::a2s($gallery),  
248 - 'seo_mate' => Arr::a2s([ 260 + try {
  261 + $id = $model->addReturnId([
  262 + 'project_id' => $project_id,
249 'title' => $item['ttile'], 263 'title' => $item['ttile'],
250 - 'keyword' => $item['keywords'],  
251 - 'description' => $item['description']  
252 - ]),  
253 - 'status' => Product::STATUS_ON  
254 - ]);  
255 - $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], RouteMap::SOURCE_PRODUCT, $id, $project_id);  
256 - $model->edit(['route' => $route], ['id' => $id]);  
257 - } catch (\Exception $e) {  
258 - echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;  
259 - continue; 264 + 'intro' => $item['description'] ?? '',
  265 + 'content' => $item['content'] ?? '',
  266 + 'category_id' => $category_id,
  267 + 'thumb' => isset($gallery[0]) ? Arr::a2s($gallery[0]) : '',
  268 + 'gallery' => Arr::a2s($gallery),
  269 + 'seo_mate' => Arr::a2s([
  270 + 'title' => $item['ttile'],
  271 + 'keyword' => $item['keywords'] ?? '',
  272 + 'description' => $item['description'] ?? ''
  273 + ]),
  274 + 'status' => Product::STATUS_ON
  275 + ]);
  276 + $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], RouteMap::SOURCE_PRODUCT, $id, $project_id);
  277 + $model->edit(['route' => $route], ['id' => $id]);
  278 + } catch (\Exception $e) {
  279 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
  280 + continue;
  281 + }
260 } 282 }
261 } 283 }
262 } 284 }
263 } 285 }
264 } 286 }
  287 + } else {
  288 + return true;
265 } 289 }
266 } elseif ($api_type == 'news' || $api_type == 'blog') { 290 } elseif ($api_type == 'news' || $api_type == 'blog') {
267 //新闻或博客 291 //新闻或博客
@@ -285,29 +309,33 @@ class ProjectUpdate extends Command @@ -285,29 +309,33 @@ class ProjectUpdate extends Command
285 309
286 foreach ($items as $item) { 310 foreach ($items as $item) {
287 311
288 - $news = $model->read(['name' => $item['ttile']], 'id');  
289 - if (!$news) {  
290 - try {  
291 - $id = $model->addReturnId([  
292 - 'project_id' => $project_id,  
293 - 'name' => $item['ttile'],  
294 - 'seo_title' => $item['ttile'],  
295 - 'seo_keywords' => $item['keywords'],  
296 - 'seo_description' => $item['description'],  
297 - 'text' => $item['content'],  
298 - 'image' => $item['images'][0] ?? '',  
299 - 'status' => $api_type == 'news' ? News::STATUS_ONE : Blog::STATUS_ONE  
300 - ]);  
301 - $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $project_id);  
302 - $model->edit(['url' => $route], ['id' => $id]);  
303 - } catch (\Exception $e) {  
304 - echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;  
305 - continue; 312 + if ($item['ttile'] ?? '') {
  313 + $news = $model->read(['name' => $item['ttile']], 'id');
  314 + if (!$news) {
  315 + try {
  316 + $id = $model->addReturnId([
  317 + 'project_id' => $project_id,
  318 + 'name' => $item['ttile'],
  319 + 'seo_title' => $item['ttile'],
  320 + 'seo_keywords' => $item['keywords'] ?? '',
  321 + 'seo_description' => $item['description'] ?? '',
  322 + 'text' => $item['content'] ?? '',
  323 + 'image' => $item['images'][0] ?? '',
  324 + 'status' => $api_type == 'news' ? News::STATUS_ONE : Blog::STATUS_ONE
  325 + ]);
  326 + $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $project_id);
  327 + $model->edit(['url' => $route], ['id' => $id]);
  328 + } catch (\Exception $e) {
  329 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
  330 + continue;
  331 + }
306 } 332 }
307 } 333 }
308 } 334 }
309 } 335 }
310 } 336 }
  337 + } else {
  338 + return true;
311 } 339 }
312 } else { 340 } else {
313 //单页 341 //单页
@@ -326,29 +354,32 @@ class ProjectUpdate extends Command @@ -326,29 +354,32 @@ class ProjectUpdate extends Command
326 $model = new BCustomTemplate(); 354 $model = new BCustomTemplate();
327 355
328 foreach ($items as $item) { 356 foreach ($items as $item) {
329 -  
330 - $custom = $model->read(['name' => $item['ttile']], 'id');  
331 - if (!$custom) {  
332 - try {  
333 - $id = $model->addReturnId([  
334 - 'project_id' => $project_id,  
335 - 'name' => $item['ttile'],  
336 - 'title' => $item['ttile'],  
337 - 'keywords' => $item['keywords'],  
338 - 'description' => $item['description'],  
339 - 'html' => $item['content'],  
340 - 'status' => 1  
341 - ]);  
342 - $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], RouteMap::SOURCE_PAGE, $id, $project_id);  
343 - $model->edit(['url' => $route], ['id' => $id]);  
344 - } catch (\Exception $e) {  
345 - echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;  
346 - continue; 357 + if ($item['ttile'] ?? '') {
  358 + $custom = $model->read(['name' => $item['ttile']], 'id');
  359 + if (!$custom) {
  360 + try {
  361 + $id = $model->addReturnId([
  362 + 'project_id' => $project_id,
  363 + 'name' => $item['ttile'],
  364 + 'title' => $item['ttile'],
  365 + 'keywords' => $item['keywords'] ?? '',
  366 + 'description' => $item['description'] ?? '',
  367 + 'html' => $item['content'] ?? '',
  368 + 'status' => 1
  369 + ]);
  370 + $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], RouteMap::SOURCE_PAGE, $id, $project_id);
  371 + $model->edit(['url' => $route], ['id' => $id]);
  372 + } catch (\Exception $e) {
  373 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
  374 + continue;
  375 + }
347 } 376 }
348 } 377 }
349 } 378 }
350 } 379 }
351 } 380 }
  381 + } else {
  382 + return true;
352 } 383 }
353 } 384 }
354 } 385 }
@@ -391,4 +422,31 @@ class ProjectUpdate extends Command @@ -391,4 +422,31 @@ class ProjectUpdate extends Command
391 $arr = explode('/', $url); 422 $arr = explode('/', $url);
392 return $arr[count($arr) - 2]; 423 return $arr[count($arr) - 2];
393 } 424 }
  425 +
  426 + protected function category_insert($project_id, $items, $pid = 0)
  427 + {
  428 + $model = new Category();
  429 + foreach ($items as $item) {
  430 + if ($item['name'] ?? '') {
  431 + $parent = $model->read(['pid' => $pid, 'title' => $item['name']], 'id');
  432 + if (!$parent) {
  433 + $parent_id = $model->addReturnId([
  434 + 'project_id' => $project_id,
  435 + 'title' => $item['name'],
  436 + 'pid' => $pid,
  437 + 'keywords' => $item['keywords'] ?? '',
  438 + 'describe' => $item['description'] ?? ''
  439 + ]);
  440 + $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['name'], RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
  441 + $model->edit(['route' => $route], ['id' => $parent_id]);
  442 + } else {
  443 + $parent_id = $parent['id'];
  444 + }
  445 +
  446 + if (!empty($item['children'])) {
  447 + $this->category_insert($project_id, $item['children'], $parent_id);
  448 + }
  449 + }
  450 + }
  451 + }
394 } 452 }
@@ -9,11 +9,12 @@ use App\Helper\Translate; @@ -9,11 +9,12 @@ use App\Helper\Translate;
9 use App\Models\Ai\AiCommand; 9 use App\Models\Ai\AiCommand;
10 use App\Models\Mail\Mail; 10 use App\Models\Mail\Mail;
11 use App\Models\Project\DeployOptimize; 11 use App\Models\Project\DeployOptimize;
  12 +use App\Models\Project\ProjectUpdateTdk;
12 use App\Models\User\User; 13 use App\Models\User\User;
13 use App\Services\ProjectServer; 14 use App\Services\ProjectServer;
14 use Illuminate\Console\Command; 15 use Illuminate\Console\Command;
  16 +use Illuminate\Support\Facades\Cache;
15 use Illuminate\Support\Facades\DB; 17 use Illuminate\Support\Facades\DB;
16 -use Illuminate\Support\Facades\Redis;  
17 18
18 /** 19 /**
19 * 初始化项目 20 * 初始化项目
@@ -132,47 +133,54 @@ class UpdateSeoTdk extends Command @@ -132,47 +133,54 @@ class UpdateSeoTdk extends Command
132 public function handle() 133 public function handle()
133 { 134 {
134 while (true) { 135 while (true) {
135 - $project_id = Redis::rpop('updateSeoTdk');  
136 - if (!$project_id) {  
137 - sleep(2); 136 + $task = ProjectUpdateTdk::getPendingTask();
  137 + if (!$task) {
  138 + sleep(10);
138 continue; 139 continue;
139 } 140 }
  141 + $project_id = $task->project_id;
  142 +
140 echo date('Y-m-d H:i:s') . ' start project_id: ' . $project_id . PHP_EOL; 143 echo date('Y-m-d H:i:s') . ' start project_id: ' . $project_id . PHP_EOL;
141 try { 144 try {
142 ProjectServer::useProject($project_id); 145 ProjectServer::useProject($project_id);
143 - $this->seo_tdk($project_id); 146 + $this->seo_tdk($project_id, $task->id);
144 DB::disconnect('custom_mysql'); 147 DB::disconnect('custom_mysql');
145 }catch (\Exception $e){ 148 }catch (\Exception $e){
146 - echo date('Y-m-d H:i:s') . ' error: ' . $project_id . '->' . $e->getMessage() . PHP_EOL; 149 + echo date('Y-m-d H:i:s') . 'line: '. $e->getLine() .' error: ' . $project_id . '->' . $e->getMessage() . PHP_EOL;
  150 + ProjectUpdateTdk::retry($task->id, $e->getMessage());
147 } 151 }
148 - echo date('Y-m-d H:i:s') . ' end: ' . $project_id . PHP_EOL; 152 + echo date('Y-m-d H:i:s') . ' end project_id: ' . $project_id . PHP_EOL;
149 } 153 }
150 } 154 }
151 155
152 - public function seo_tdk($project_id) 156 + public function seo_tdk($project_id, $task_id)
153 { 157 {
154 - $data = [];  
155 - $update = [  
156 - 'created_at'=>date('Y-m-d H:i:s'),  
157 - 'updated_at'=>date('Y-m-d H:i:s'),  
158 - 'project_id'=>$project_id,  
159 - ];//更新统计 158 +
  159 + //更新统计
  160 + $update = [];
160 $ai_commands = AiCommand::where('is_batch', 1)->select('key', 'scene', 'ai')->get()->toArray(); 161 $ai_commands = AiCommand::where('is_batch', 1)->select('key', 'scene', 'ai')->get()->toArray();
161 $ai_commands = Arr::setValueToKey($ai_commands, 'key'); 162 $ai_commands = Arr::setValueToKey($ai_commands, 'key');
162 foreach ($this->maps as $table => $map) { 163 foreach ($this->maps as $table => $map) {
163 $update[$table] = ['total_page'=>0, 'title'=>0, 'keyword'=>0, 'des'=>0]; 164 $update[$table] = ['total_page'=>0, 'title'=>0, 'keyword'=>0, 'des'=>0];
164 - echo date('Y-m-d H:i:s') . '更新--' . $table . ': 项目id' . $project_id . PHP_EOL . json_encode($update[$table]); 165 + echo date('Y-m-d H:i:s') . '更新--' . $table . ': 项目id' . $project_id . PHP_EOL;
165 $list = DB::connection('custom_mysql')->table($table)->get(); 166 $list = DB::connection('custom_mysql')->table($table)->get();
166 if (!empty($list)) { 167 if (!empty($list)) {
167 $list = $list->toArray(); 168 $list = $list->toArray();
168 foreach ($list as $v) { 169 foreach ($list as $v) {
169 - echo '打印数据:'.date('Y-m-d H:i:s') . $update[$table]['total_page'].PHP_EOL;  
170 $update[$table]['total_page']++; 170 $update[$table]['total_page']++;
171 $v = (array)$v; 171 $v = (array)$v;
172 - echo date('Y-m-d H:i:s') . '更新--' . $table . ':id' . $v['id'] . PHP_EOL; 172 +
  173 + //缓存 在处理的 项目 数据 id
  174 + $cache_key = "seo_tdk_{$project_id}_{$table}_{$v['id']}";
  175 + if(Cache::get($cache_key)){
  176 + continue;
  177 + }
  178 + Cache::put($cache_key, 1, 120);
  179 +
  180 + echo date('Y-m-d H:i:s') . '更新--' . $table . ': 项目id' . $project_id . ':id' . $v['id'] . PHP_EOL;
  181 +
173 $data = []; 182 $data = [];
174 $json_field = ''; 183 $json_field = '';
175 - echo date('Y-m-d H:i:s') . json_encode($map) . PHP_EOL;  
176 foreach ($map as $ai_key => $field) { 184 foreach ($map as $ai_key => $field) {
177 $field_arr = explode('.', $field); 185 $field_arr = explode('.', $field);
178 if (count($field_arr) > 1) { 186 if (count($field_arr) > 1) {
@@ -186,6 +194,7 @@ class UpdateSeoTdk extends Command @@ -186,6 +194,7 @@ class UpdateSeoTdk extends Command
186 echo $field.'已有值 跳过' . PHP_EOL; 194 echo $field.'已有值 跳过' . PHP_EOL;
187 continue; 195 continue;
188 } 196 }
  197 +
189 //AI生成 198 //AI生成
190 if (!empty($ai_commands[$ai_key]['ai'])) { 199 if (!empty($ai_commands[$ai_key]['ai'])) {
191 $prompt = $this->getPrompt($project_id, $ai_commands[$ai_key]['ai'], $table, $v); 200 $prompt = $this->getPrompt($project_id, $ai_commands[$ai_key]['ai'], $table, $v);
@@ -255,17 +264,16 @@ class UpdateSeoTdk extends Command @@ -255,17 +264,16 @@ class UpdateSeoTdk extends Command
255 } 264 }
256 if($json_field){ 265 if($json_field){
257 $old_data = json_decode($v[$field_arr[0]], true); 266 $old_data = json_decode($v[$field_arr[0]], true);
258 - foreach ($old_data as $kk=>$vv){ 267 + foreach ($old_data ?: [] as $kk=>$vv){
259 empty($data[$json_field][$kk]) && $data[$json_field][$kk] = $vv; 268 empty($data[$json_field][$kk]) && $data[$json_field][$kk] = $vv;
260 } 269 }
261 $data[$json_field] = json_encode($data[$json_field]); 270 $data[$json_field] = json_encode($data[$json_field]);
262 } 271 }
263 DB::connection('custom_mysql')->table($table)->where(['id' => $v['id']])->update($data); 272 DB::connection('custom_mysql')->table($table)->where(['id' => $v['id']])->update($data);
264 } 273 }
265 - $update[$table] = json_encode($update[$table]);  
266 } 274 }
267 } 275 }
268 - DB::table('gl_project_update_tdk')->insert($update); 276 + ProjectUpdateTdk::finish($task_id, $update);
269 } 277 }
270 278
271 public function getPrompt($project_id, $prompt, $table, $data){ 279 public function getPrompt($project_id, $prompt, $table, $data){
@@ -151,6 +151,9 @@ class Arr extends \Illuminate\Support\Arr @@ -151,6 +151,9 @@ class Arr extends \Illuminate\Support\Arr
151 */ 151 */
152 public static function s2a($str) 152 public static function s2a($str)
153 { 153 {
  154 + if(!$str){
  155 + return [];
  156 + }
154 if (is_array($str)) { 157 if (is_array($str)) {
155 return $str; 158 return $str;
156 } 159 }
@@ -74,7 +74,7 @@ class Common @@ -74,7 +74,7 @@ class Common
74 if (isset($result['texts']['sl']) && isset(Translate::$tls_list[$result['texts']['sl']])) { 74 if (isset($result['texts']['sl']) && isset(Translate::$tls_list[$result['texts']['sl']])) {
75 $lang = Translate::$tls_list[$result['texts']['sl']]['text']; 75 $lang = Translate::$tls_list[$result['texts']['sl']]['text'];
76 } else { 76 } else {
77 - $lang = 'Chinese'; 77 + $lang = 'English';
78 } 78 }
79 } 79 }
80 $str = 'Please answer in '.$lang; 80 $str = 'Please answer in '.$lang;
@@ -14,9 +14,20 @@ use Illuminate\Support\Facades\Log; @@ -14,9 +14,20 @@ use Illuminate\Support\Facades\Log;
14 */ 14 */
15 class Gpt 15 class Gpt
16 { 16 {
17 - public $api = 'http://openai.waimaoq.com/'; 17 + /**
  18 + * 头
  19 + * @var string[]
  20 + */
  21 + public $header = [
  22 + 'apikey' => 'UkzZljFv83Z2qBi5YR1o3f2otAVWtug6',
  23 + 'X-CmerApi-Host' => 'bizopenai.p.cmer.com',
  24 + ];
18 25
19 - public $header = []; 26 + /**
  27 + * 请求域名
  28 + * @var string
  29 + */
  30 + public $api = 'https://api.cmer.com';
20 31
21 private static $instance; 32 private static $instance;
22 33
@@ -35,7 +46,7 @@ class Gpt @@ -35,7 +46,7 @@ class Gpt
35 */ 46 */
36 public function openai_chat_qqs($content, $system_content = '') 47 public function openai_chat_qqs($content, $system_content = '')
37 { 48 {
38 - $url = $this->api . 'v2/openai_chat_qqs'; 49 + $url = $this->api . '/v1/openai_chat';
39 50
40 $data = [ 51 $data = [
41 'messages' => [], 52 'messages' => [],
@@ -57,9 +68,10 @@ class Gpt @@ -57,9 +68,10 @@ class Gpt
57 ->withBody(json_encode($data, JSON_UNESCAPED_UNICODE), 'application/json') 68 ->withBody(json_encode($data, JSON_UNESCAPED_UNICODE), 'application/json')
58 ->post($url); 69 ->post($url);
59 $json = $result->json(); 70 $json = $result->json();
60 - if (!isset($json['text'])) { 71 + if (!isset($json['text']) || $json['code'] !==200) {
61 Log::error('openai_chat_qqs data:', $data); 72 Log::error('openai_chat_qqs data:', $data);
62 Log::error('openai_chat_qqs result:' . (time() - $time), $json === null ? ['null'] : $json); 73 Log::error('openai_chat_qqs result:' . (time() - $time), $json === null ? ['null'] : $json);
  74 + $json = [];
63 } 75 }
64 } catch (\Throwable $e) { 76 } catch (\Throwable $e) {
65 Log::error('openai_chat_qqs time ' . (time() - $time) . ' error:' . $e->getMessage()); 77 Log::error('openai_chat_qqs time ' . (time() - $time) . ' error:' . $e->getMessage());
@@ -11,6 +11,7 @@ namespace App\Http\Controllers\Aside\Com; @@ -11,6 +11,7 @@ namespace App\Http\Controllers\Aside\Com;
11 11
12 use App\Helper\Common; 12 use App\Helper\Common;
13 use App\Http\Controllers\Bside\BaseController; 13 use App\Http\Controllers\Bside\BaseController;
  14 +use App\Models\Project\ProjectUpdateTdk;
14 use App\Services\ProjectServer; 15 use App\Services\ProjectServer;
15 use Illuminate\Support\Facades\DB; 16 use Illuminate\Support\Facades\DB;
16 use Illuminate\Support\Facades\Redis; 17 use Illuminate\Support\Facades\Redis;
@@ -37,7 +38,7 @@ class UpdateController extends BaseController @@ -37,7 +38,7 @@ class UpdateController extends BaseController
37 ],[ 38 ],[
38 'project_id.required' => 'project_id不能为空', 39 'project_id.required' => 'project_id不能为空',
39 ]); 40 ]);
40 - Redis::lpush('updateSeoTdk', $this->param['project_id']); 41 + ProjectUpdateTdk::add_task($this->param['project_id']);
41 $this->response('任务添加成功'); 42 $this->response('任务添加成功');
42 } 43 }
43 } 44 }
@@ -9,10 +9,104 @@ @@ -9,10 +9,104 @@
9 9
10 namespace App\Models\Project; 10 namespace App\Models\Project;
11 11
  12 +use App\Helper\Arr;
12 use App\Models\Base; 13 use App\Models\Base;
  14 +use Illuminate\Support\Facades\DB;
  15 +use Illuminate\Support\Facades\Log;
13 16
14 class ProjectUpdateTdk extends Base 17 class ProjectUpdateTdk extends Base
15 { 18 {
16 //设置关联表名 19 //设置关联表名
17 protected $table = 'gl_project_update_tdk'; 20 protected $table = 'gl_project_update_tdk';
  21 +
  22 + const STATUS_PENDING = 0;
  23 + const STATUS_SUCCESS = 1;
  24 + const STATUS_FAIL = 2;
  25 +
  26 + /**
  27 + * 新建任务
  28 + * @param $project_id
  29 + * @author zbj
  30 + * @date 2023/11/9
  31 + */
  32 + public static function add_task($project_id){
  33 + $model = new self();
  34 + $model->project_id = $project_id;
  35 + $model->save();
  36 + }
  37 +
  38 + /**
  39 + * 获取待处理任务
  40 + * @return mixed
  41 + * @author zbj
  42 + * @date 2023/11/9
  43 + */
  44 + public static function getPendingTask(){
  45 + return self::where('status', self::STATUS_PENDING)->orderBy('id', 'asc')->first();
  46 + }
  47 +
  48 + /**
  49 + * 重试任务
  50 + * @param $id
  51 + * @param $remark
  52 + * @author zbj
  53 + * @date 2023/11/9
  54 + */
  55 + public static function retry($id, $remark)
  56 + {
  57 + DB::beginTransaction();
  58 + try {
  59 + //行锁 避免脏读写
  60 + $data = self::where('id', $id)->lockForUpdate()->first();
  61 + $data->retry = $data->retry + 1;
  62 + if ($data->retry > 3) {
  63 + $data->status = self::STATUS_FAIL;
  64 + }else{
  65 + $data->status = self::STATUS_PENDING;
  66 + }
  67 + $data->remark = mb_substr($remark, 0, 250);
  68 + $data->save();
  69 +
  70 + DB::commit();
  71 + } catch (\Exception $e) {
  72 + DB::rollback();
  73 + Log::error('project_update_tdk retry error:' . $e->getMessage());
  74 + }
  75 + }
  76 +
  77 + /**
  78 + * 完成
  79 + * @param $id
  80 + * @param $update_data
  81 + * @author zbj
  82 + * @date 2023/11/9
  83 + */
  84 + public static function finish($id, $update_data){
  85 + DB::beginTransaction();
  86 + try {
  87 + //行锁 避免脏读写
  88 + $data = self::where('id', $id)->lockForUpdate()->first();
  89 + $data->status = self::STATUS_SUCCESS;
  90 +
  91 + foreach($update_data as $field => $item){
  92 + $old_date = Arr::s2a($data->$field);
  93 + $new_data = [];
  94 + foreach($item as $k=>$v){
  95 + if($k == 'total_page'){
  96 + $new_data[$k] = $v;
  97 + }else{
  98 + $new_data[$k] = $v + ($old_date[$k] ?? 0);
  99 + }
  100 + }
  101 + $data->$field = $new_data;
  102 + }
  103 +
  104 + $data->save();
  105 +
  106 + DB::commit();
  107 + } catch (\Exception $e) {
  108 + DB::rollback();
  109 + Log::error('project_update_tdk finish error:' . $e->getMessage());
  110 + }
  111 + }
18 } 112 }