作者 lyh

GXgeo设置

  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :InitKeywordComment.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2025/6/3 15:38
  8 + */
  9 +
  10 +namespace App\Console\Commands\Project;
  11 +
  12 +use App\Helper\Common;
  13 +use App\Helper\Gpt;
  14 +use App\Models\Ai\AiCommand;
  15 +use App\Models\Com\NoticeLog;
  16 +use App\Models\Project\AggregateKeywordComment;
  17 +use App\Models\Project\Project;
  18 +use Illuminate\Console\Command;
  19 +
  20 +class ProjectComment extends Command
  21 +{
  22 + /**
  23 + * The name and signature of the console command.
  24 + *
  25 + * @var string
  26 + */
  27 + protected $signature = 'project_comment';
  28 +
  29 + /**
  30 + * The console command description.
  31 + *
  32 + * @var string
  33 + */
  34 + protected $description = '初始化关键字评论';
  35 +
  36 + public $number = 100;
  37 +
  38 + public function handle(){
  39 + $keywordCommentModel = new AggregateKeywordComment();
  40 + $projectModel = new Project();
  41 + while (true){
  42 + $ids = $projectModel->selectField(['delete_status' => 0,'project_type'=>0,'extend_type'=>0,'type'=>['in',[1,2,3,4,6]]], 'id');
  43 + if(empty($list)){
  44 + sleep(200);
  45 + continue;
  46 + }
  47 + foreach ($ids as $id){
  48 + echo date('Y-m-d H:i:s').'start:' . $id . PHP_EOL;
  49 + $project_id = $id;
  50 + echo date('Y-m-d H:i:s').'执行的项目id:' . $project_id . PHP_EOL;
  51 + try {
  52 + $this->_action($project_id);
  53 + }catch (\Exception $e){
  54 + echo date('Y-m-d H:i:s').'错误信息:'.$e->getMessage().PHP_EOL;
  55 + continue;
  56 + }
  57 + echo date('Y-m-d H:i:s').'end:' . $id . PHP_EOL;
  58 + }
  59 + }
  60 + return true;
  61 + }
  62 +
  63 + /**
  64 + * @remark :执行的方法
  65 + * @name :_action
  66 + * @author :lyh
  67 + * @method :post
  68 + * @time :2025/6/3 15:42
  69 + */
  70 + public function _action($project_id){
  71 + $aiCommonModel = new AiCommand();
  72 + $info = $aiCommonModel->read(['key'=>'tag_comment']);
  73 + $info['ai'] = str_replace('50', '100', $info['ai']);
  74 + $text = Gpt::instance()->openai_chat_qqs($info['ai']);
  75 + $text = Common::deal_keywords($text);
  76 + preg_match_all('/\{[^{}]*\}/', $text, $matches);
  77 + if(!empty($text)){
  78 + $data = [];
  79 + foreach ($matches[0] as $item){
  80 + $item = str_replace("'", '"', $item);
  81 + // 解码成 PHP 关联数组
  82 + $item = json_decode($item, true);
  83 + if(!isset($item['name']) || !isset($item['comment'])){
  84 + continue;
  85 + }
  86 + $twoMonthsAgo = strtotime('-2 months');
  87 + $randomTimestamp = rand($twoMonthsAgo, time());
  88 + $randomDateTime = date('Y-m-d H:i:s', $randomTimestamp);
  89 + $data[] = [
  90 + 'nickname'=>$item['name'],
  91 + 'text'=>$item['comment'],
  92 + 'project_id'=>$project_id,
  93 + 'type'=>1,
  94 + 'uid'=>0,
  95 + 'start_time'=>$randomDateTime,
  96 + 'created_at'=>date('Y-m-d H:i:s'),
  97 + 'updated_at'=>date('Y-m-d H:i:s')
  98 + ];
  99 + }
  100 + $keywordCommentModel = new AggregateKeywordComment();
  101 + $keywordCommentModel->insertAll($data);
  102 + }
  103 + return true;
  104 + }
  105 +}