作者 赵彬吉
@@ -2,21 +2,17 @@ @@ -2,21 +2,17 @@
2 2
3 namespace App\Console\Commands; 3 namespace App\Console\Commands;
4 4
5 -use App\Http\Logic\Aside\Project\ProjectLogic;  
6 use App\Models\Com\NoticeLog; 5 use App\Models\Com\NoticeLog;
7 use App\Models\Product\Keyword; 6 use App\Models\Product\Keyword;
8 -use App\Models\Project\Project;  
9 use App\Models\RouteMap\RouteMap; 7 use App\Models\RouteMap\RouteMap;
10 use App\Services\ProjectServer; 8 use App\Services\ProjectServer;
11 use Illuminate\Console\Command; 9 use Illuminate\Console\Command;
12 use Illuminate\Support\Facades\DB; 10 use Illuminate\Support\Facades\DB;
  11 +use Illuminate\Support\Facades\Redis;
13 12
14 /** 13 /**
15 - * 初始化项目  
16 - * Class InitProject 14 + * Class InitKeyword
17 * @package App\Console\Commands 15 * @package App\Console\Commands
18 - * @author zbj  
19 - * @date 2023/10/8  
20 */ 16 */
21 class InitKeyword extends Command 17 class InitKeyword extends Command
22 { 18 {
@@ -50,29 +46,79 @@ class InitKeyword extends Command @@ -50,29 +46,79 @@ class InitKeyword extends Command
50 public function handle() 46 public function handle()
51 { 47 {
52 while (true){ 48 while (true){
53 - $list = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD)->where('status', NoticeLog::STATUS_PENDING)->get();  
54 - if(!empty($list)){  
55 - foreach ($list as $item){  
56 - echo 'start:' . $item['data']['project_id'] . PHP_EOL;  
57 - ProjectServer::useProject($item['data']['project_id']);  
58 - $keywordModel = new Keyword();  
59 - $list = $keywordModel->list(['route'=>'']);  
60 - echo 'start:' . json_encode($list) . PHP_EOL;  
61 - foreach ($list as $v){  
62 - $route = RouteMap::setRoute($v['title'],RouteMap::SOURCE_PRODUCT_KEYWORD,$v['id'],$item['data']['project_id']);  
63 - if(empty($route)){  
64 - $keywordModel->del(['id'=>$v['id']]);  
65 - continue;  
66 - }  
67 - $keywordModel->edit(['route'=>$route],['id'=>$v['id']]);  
68 - }  
69 - $item->status = NoticeLog::STATUS_SUCCESS;  
70 - $item->save();  
71 - DB::disconnect('custom_mysql');  
72 - } 49 + $notice_id = $this->getTask();
  50 + if (empty($notice_id)) {
  51 + sleep(30);
  52 + continue;
  53 + }
  54 + try {
  55 + $this->output(' taskID: ' . $notice_id . ' start');
  56 + $this->bind($notice_id);
  57 + $this->output(' taskID: ' . $notice_id . ' end');
  58 + } catch (\Exception $e) {
  59 + $this->output(' taskID: ' . $notice_id . ', error: ' . $e->getMessage());
73 } 60 }
74 sleep(2); 61 sleep(2);
75 } 62 }
  63 + return true;
  64 + }
  65 +
  66 + /**
  67 + * 处理子任务
  68 + * @param $notice_id
  69 + * @return bool
  70 + * @throws \Exception
  71 + */
  72 + public function bind($notice_id)
  73 + {
  74 + $notice = NoticeLog::where(['id' => $notice_id])->first();
  75 + if (empty($notice) || $notice->type != NoticeLog::TYPE_INIT_KEYWORD || $notice->status != NoticeLog::STATUS_PENDING){
  76 + return true;
  77 + }
  78 + ProjectServer::useProject($notice['data']['project_id']);
  79 + $keyword = Keyword::whereNull('route')->get();
  80 + foreach ($keyword as $val) {
  81 + $this->output(' keywordID: ' . $val->id . ', title: ' . $val->title);
  82 + try {
  83 + $route = RouteMap::setRoute($val['title'],RouteMap::SOURCE_PRODUCT_KEYWORD, $val->id, $notice['data']['project_id']);
  84 + $val->route = $route;
  85 + $val->save();
  86 + } catch (\Exception $e) {
  87 + $this->output(' keywordID: ' . $val->id . ', title: ' . $val->title . ', error: ' . $e->getMessage());
  88 + }
  89 + }
  90 +
  91 + $notice->status = NoticeLog::STATUS_SUCCESS;
  92 + $notice->save();
  93 + DB::disconnect('custom_mysql');
  94 + return true;
  95 + }
  96 +
  97 + /**
  98 + * 获取需要处理的任务
  99 + * @return mixed
  100 + */
  101 + public function getTask()
  102 + {
  103 + $key = 'notice_log_type_keyword';
  104 + $notice_id = Redis::rpop($key);
  105 + if ($notice_id){
  106 + return $notice_id;
  107 + }
  108 + $ids = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD)->where('status', NoticeLog::STATUS_PENDING)->limit(100)->pluck('id');
  109 + foreach ($ids as $id) {
  110 + Redis::lpush($key, $id);
  111 + }
  112 + $notice_id = Redis::rpop($key);
  113 + return $notice_id;
76 } 114 }
77 115
  116 + /**
  117 + * 输出message
  118 + * @param $message
  119 + */
  120 + public function output($message)
  121 + {
  122 + echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
  123 + }
78 } 124 }
@@ -8,6 +8,8 @@ @@ -8,6 +8,8 @@
8 namespace App\Http\Controllers\Api; 8 namespace App\Http\Controllers\Api;
9 9
10 use App\Models\Domain\DomainInfo; 10 use App\Models\Domain\DomainInfo;
  11 +use App\Models\Product\Category;
  12 +use App\Models\Product\Product;
11 use App\Models\Project\OnlineCheck; 13 use App\Models\Project\OnlineCheck;
12 use App\Models\Project\Project; 14 use App\Models\Project\Project;
13 use App\Models\RouteMap\RouteMap; 15 use App\Models\RouteMap\RouteMap;
@@ -60,18 +62,37 @@ class PrivateController extends BaseController @@ -60,18 +62,37 @@ class PrivateController extends BaseController
60 $domain = DomainInfo::where(['project_id' => $project_id])->first(); 62 $domain = DomainInfo::where(['project_id' => $project_id])->first();
61 $host = FALSE == empty($domain) ? 'https://' . $domain->domain . '/' : $project->deploy_build->test_domain; 63 $host = FALSE == empty($domain) ? 'https://' . $domain->domain . '/' : $project->deploy_build->test_domain;
62 64
63 - $list = RouteMap::where(['project_id' => $project_id])  
64 - ->when($type, function ($query) use ($type) {  
65 - return $query->whereIn('source', $type);  
66 - })  
67 - ->get();  
68 - 65 + // 需要标题, 不能直接查询map表
  66 +// $list = RouteMap::where(['project_id' => $project_id])
  67 +// ->when($type, function ($query) use ($type) {
  68 +// return $query->whereIn('source', $type);
  69 +// })
  70 +// ->get();
  71 +//
  72 +// $result = [];
  73 +// foreach ($list as $val) {
  74 +// // 排除首页
  75 +// if ($val->source == RouteMap::SOURCE_PAGE && in_array($val->route, ['index', '']))
  76 +// continue;
  77 +// $result[$val->source][] = $host . $val->route;
  78 +// }
69 $result = []; 79 $result = [];
70 - foreach ($list as $val) {  
71 - // 排除首页  
72 - if ($val->source == RouteMap::SOURCE_PAGE && in_array($val->route, ['index', '']))  
73 - continue;  
74 - $result[$val->source][] = $host . $val->route; 80 + $product = Product::where(['status' => Product::STATUS_ON])->get(['title', 'route'])->toArray();
  81 + foreach ($product as $val) {
  82 + $val['route'] = $host . $val['route'];
  83 + // FALSE == preg_match('/(\.html|\.htm)$/', $val['route'])
  84 + if (FALSE === strpos($val['route'], '.htm')) {
  85 + $val['route'] .= '/';
  86 + }
  87 + $result[RouteMap::SOURCE_PRODUCT][] = $val;
  88 + }
  89 + $product_category= Category::get(['title', 'route'])->toArray();
  90 + foreach ($product_category as $val) {
  91 + $val['route'] = $host . $val['route'];
  92 + if (FALSE === strpos($val['route'], '.htm')) {
  93 + $val['route'] .= '/';
  94 + }
  95 + $result[RouteMap::SOURCE_PRODUCT_CATE][] = $val;
75 } 96 }
76 return $this->success($result); 97 return $this->success($result);
77 } 98 }
@@ -100,11 +100,11 @@ class KeywordController extends BaseController @@ -100,11 +100,11 @@ class KeywordController extends BaseController
100 } 100 }
101 101
102 /** 102 /**
103 - * @remark :批量添加  
104 - * @name :batchAdd  
105 - * @author :lyh  
106 - * @method :post  
107 - * @time :2023/8/28 14:25 103 + * 批量添加关键词
  104 + * FIXME 添加通知, 异步处理任务
  105 + * @param KeywordLogic $logic
  106 + * @throws \App\Exceptions\AsideGlobalException
  107 + * @throws \App\Exceptions\BsideGlobalException
108 */ 108 */
109 public function batchAdd(KeywordLogic $logic){ 109 public function batchAdd(KeywordLogic $logic){
110 $this->request->validate([ 110 $this->request->validate([
@@ -115,7 +115,7 @@ class KeywordController extends BaseController @@ -115,7 +115,7 @@ class KeywordController extends BaseController
115 'title.max' => '批量操作不能超过1000条数据' 115 'title.max' => '批量操作不能超过1000条数据'
116 ]); 116 ]);
117 $logic->batchAdd(); 117 $logic->batchAdd();
118 - $this->response('路由生成中,请稍后刷新查看'); 118 + $this->response('关键词后台异步添加中,请稍后刷新查看!');
119 } 119 }
120 120
121 /** 121 /**
@@ -297,7 +297,12 @@ class ProductController extends BaseController @@ -297,7 +297,12 @@ class ProductController extends BaseController
297 }elseif($v['type'] == 4){ 297 }elseif($v['type'] == 4){
298 $arr1 = json_decode($info['values']); 298 $arr1 = json_decode($info['values']);
299 foreach ($arr1 as $k1=>$v1){ 299 foreach ($arr1 as $k1=>$v1){
300 - $v1 = getFileUrl($v1); 300 + $v1 = (array)$v1;
  301 + if(isset($v1['url'])){
  302 + $v1['url'] = getFileUrl($v1['url']);
  303 + }else{
  304 + $v1 = getFileUrl($v1);
  305 + }
301 $arr1[$k1] = $v1; 306 $arr1[$k1] = $v1;
302 } 307 }
303 $v['values'] = $arr1; 308 $v['values'] = $arr1;
@@ -30,7 +30,7 @@ class MonthCountLogic extends BaseLogic @@ -30,7 +30,7 @@ class MonthCountLogic extends BaseLogic
30 */ 30 */
31 public function getCountLists($map,$order = 'created_at',$filed = ['*']){ 31 public function getCountLists($map,$order = 'created_at',$filed = ['*']){
32 $map['project_id'] = $this->user['project_id']; 32 $map['project_id'] = $this->user['project_id'];
33 - $lists = $this->model->list($map,$order,$filed); 33 + $lists = $this->model->list($map,$order,$filed,'desc',10);
34 if(isset($this->project['is_record_china_visit']) && ($this->project['is_record_china_visit'] == 0)){ 34 if(isset($this->project['is_record_china_visit']) && ($this->project['is_record_china_visit'] == 0)){
35 foreach ($lists as $k => $v){ 35 foreach ($lists as $k => $v){
36 if(empty($v['source_country'])){ 36 if(empty($v['source_country'])){
@@ -130,11 +130,10 @@ class KeywordLogic extends BaseLogic @@ -130,11 +130,10 @@ class KeywordLogic extends BaseLogic
130 } 130 }
131 131
132 /** 132 /**
133 - * @remark :批量添加数据  
134 - * @name :batchAdd  
135 - * @author :lyh  
136 - * @method :post  
137 - * @time :2023/8/28 14:03 133 + * 批量添加关键词任务, 异步处理
  134 + * @return array
  135 + * @throws BsideGlobalException
  136 + * @throws \App\Exceptions\AsideGlobalException
138 */ 137 */
139 public function batchAdd(){ 138 public function batchAdd(){
140 try { 139 try {
@@ -154,7 +153,7 @@ class KeywordLogic extends BaseLogic @@ -154,7 +153,7 @@ class KeywordLogic extends BaseLogic
154 } 153 }
155 NoticeLog::createLog(NoticeLog::TYPE_INIT_KEYWORD, ['project_id' => $this->user['project_id']]); 154 NoticeLog::createLog(NoticeLog::TYPE_INIT_KEYWORD, ['project_id' => $this->user['project_id']]);
156 }catch (\Exception $e){ 155 }catch (\Exception $e){
157 - $this->fail('error'); 156 + $this->fail('创建任务添加关键词任务失败,请稍后重试!');
158 } 157 }
159 return $this->success(); 158 return $this->success();
160 } 159 }
@@ -175,7 +175,7 @@ class ProductLogic extends BaseLogic @@ -175,7 +175,7 @@ class ProductLogic extends BaseLogic
175 $v['values'] = json_encode($v['values']); 175 $v['values'] = json_encode($v['values']);
176 }elseif ($v['type'] == 4){ 176 }elseif ($v['type'] == 4){
177 foreach ($v['values'] as $k1=>$v1){ 177 foreach ($v['values'] as $k1=>$v1){
178 - $v1 = str_replace_url($v1); 178 + $v1['url'] = str_replace_url($v1['url']);
179 $v['values'][$k1] = $v1; 179 $v['values'][$k1] = $v1;
180 } 180 }
181 $v['values'] = json_encode($v['values']); 181 $v['values'] = json_encode($v['values']);