作者 赵彬吉
... ... @@ -2,21 +2,17 @@
namespace App\Console\Commands;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Com\NoticeLog;
use App\Models\Product\Keyword;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
/**
* 初始化项目
* Class InitProject
* Class InitKeyword
* @package App\Console\Commands
* @author zbj
* @date 2023/10/8
*/
class InitKeyword extends Command
{
... ... @@ -50,29 +46,79 @@ class InitKeyword extends Command
public function handle()
{
while (true){
$list = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD)->where('status', NoticeLog::STATUS_PENDING)->get();
if(!empty($list)){
foreach ($list as $item){
echo 'start:' . $item['data']['project_id'] . PHP_EOL;
ProjectServer::useProject($item['data']['project_id']);
$keywordModel = new Keyword();
$list = $keywordModel->list(['route'=>'']);
echo 'start:' . json_encode($list) . PHP_EOL;
foreach ($list as $v){
$route = RouteMap::setRoute($v['title'],RouteMap::SOURCE_PRODUCT_KEYWORD,$v['id'],$item['data']['project_id']);
if(empty($route)){
$keywordModel->del(['id'=>$v['id']]);
continue;
}
$keywordModel->edit(['route'=>$route],['id'=>$v['id']]);
}
$item->status = NoticeLog::STATUS_SUCCESS;
$item->save();
DB::disconnect('custom_mysql');
}
$notice_id = $this->getTask();
if (empty($notice_id)) {
sleep(30);
continue;
}
try {
$this->output(' taskID: ' . $notice_id . ' start');
$this->bind($notice_id);
$this->output(' taskID: ' . $notice_id . ' end');
} catch (\Exception $e) {
$this->output(' taskID: ' . $notice_id . ', error: ' . $e->getMessage());
}
sleep(2);
}
return true;
}
/**
* 处理子任务
* @param $notice_id
* @return bool
* @throws \Exception
*/
public function bind($notice_id)
{
$notice = NoticeLog::where(['id' => $notice_id])->first();
if (empty($notice) || $notice->type != NoticeLog::TYPE_INIT_KEYWORD || $notice->status != NoticeLog::STATUS_PENDING){
return true;
}
ProjectServer::useProject($notice['data']['project_id']);
$keyword = Keyword::whereNull('route')->get();
foreach ($keyword as $val) {
$this->output(' keywordID: ' . $val->id . ', title: ' . $val->title);
try {
$route = RouteMap::setRoute($val['title'],RouteMap::SOURCE_PRODUCT_KEYWORD, $val->id, $notice['data']['project_id']);
$val->route = $route;
$val->save();
} catch (\Exception $e) {
$this->output(' keywordID: ' . $val->id . ', title: ' . $val->title . ', error: ' . $e->getMessage());
}
}
$notice->status = NoticeLog::STATUS_SUCCESS;
$notice->save();
DB::disconnect('custom_mysql');
return true;
}
/**
* 获取需要处理的任务
* @return mixed
*/
public function getTask()
{
$key = 'notice_log_type_keyword';
$notice_id = Redis::rpop($key);
if ($notice_id){
return $notice_id;
}
$ids = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD)->where('status', NoticeLog::STATUS_PENDING)->limit(100)->pluck('id');
foreach ($ids as $id) {
Redis::lpush($key, $id);
}
$notice_id = Redis::rpop($key);
return $notice_id;
}
/**
* 输出message
* @param $message
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
}
}
... ...
... ... @@ -8,6 +8,8 @@
namespace App\Http\Controllers\Api;
use App\Models\Domain\DomainInfo;
use App\Models\Product\Category;
use App\Models\Product\Product;
use App\Models\Project\OnlineCheck;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
... ... @@ -60,18 +62,37 @@ class PrivateController extends BaseController
$domain = DomainInfo::where(['project_id' => $project_id])->first();
$host = FALSE == empty($domain) ? 'https://' . $domain->domain . '/' : $project->deploy_build->test_domain;
$list = RouteMap::where(['project_id' => $project_id])
->when($type, function ($query) use ($type) {
return $query->whereIn('source', $type);
})
->get();
// 需要标题, 不能直接查询map表
// $list = RouteMap::where(['project_id' => $project_id])
// ->when($type, function ($query) use ($type) {
// return $query->whereIn('source', $type);
// })
// ->get();
//
// $result = [];
// foreach ($list as $val) {
// // 排除首页
// if ($val->source == RouteMap::SOURCE_PAGE && in_array($val->route, ['index', '']))
// continue;
// $result[$val->source][] = $host . $val->route;
// }
$result = [];
foreach ($list as $val) {
// 排除首页
if ($val->source == RouteMap::SOURCE_PAGE && in_array($val->route, ['index', '']))
continue;
$result[$val->source][] = $host . $val->route;
$product = Product::where(['status' => Product::STATUS_ON])->get(['title', 'route'])->toArray();
foreach ($product as $val) {
$val['route'] = $host . $val['route'];
// FALSE == preg_match('/(\.html|\.htm)$/', $val['route'])
if (FALSE === strpos($val['route'], '.htm')) {
$val['route'] .= '/';
}
$result[RouteMap::SOURCE_PRODUCT][] = $val;
}
$product_category= Category::get(['title', 'route'])->toArray();
foreach ($product_category as $val) {
$val['route'] = $host . $val['route'];
if (FALSE === strpos($val['route'], '.htm')) {
$val['route'] .= '/';
}
$result[RouteMap::SOURCE_PRODUCT_CATE][] = $val;
}
return $this->success($result);
}
... ...
... ... @@ -100,11 +100,11 @@ class KeywordController extends BaseController
}
/**
* @remark :批量添加
* @name :batchAdd
* @author :lyh
* @method :post
* @time :2023/8/28 14:25
* 批量添加关键词
* FIXME 添加通知, 异步处理任务
* @param KeywordLogic $logic
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
*/
public function batchAdd(KeywordLogic $logic){
$this->request->validate([
... ... @@ -115,7 +115,7 @@ class KeywordController extends BaseController
'title.max' => '批量操作不能超过1000条数据'
]);
$logic->batchAdd();
$this->response('路由生成中,请稍后刷新查看');
$this->response('关键词后台异步添加中,请稍后刷新查看!');
}
/**
... ...
... ... @@ -297,7 +297,12 @@ class ProductController extends BaseController
}elseif($v['type'] == 4){
$arr1 = json_decode($info['values']);
foreach ($arr1 as $k1=>$v1){
$v1 = getFileUrl($v1);
$v1 = (array)$v1;
if(isset($v1['url'])){
$v1['url'] = getFileUrl($v1['url']);
}else{
$v1 = getFileUrl($v1);
}
$arr1[$k1] = $v1;
}
$v['values'] = $arr1;
... ...
... ... @@ -30,7 +30,7 @@ class MonthCountLogic extends BaseLogic
*/
public function getCountLists($map,$order = 'created_at',$filed = ['*']){
$map['project_id'] = $this->user['project_id'];
$lists = $this->model->list($map,$order,$filed);
$lists = $this->model->list($map,$order,$filed,'desc',10);
if(isset($this->project['is_record_china_visit']) && ($this->project['is_record_china_visit'] == 0)){
foreach ($lists as $k => $v){
if(empty($v['source_country'])){
... ...
... ... @@ -130,11 +130,10 @@ class KeywordLogic extends BaseLogic
}
/**
* @remark :批量添加数据
* @name :batchAdd
* @author :lyh
* @method :post
* @time :2023/8/28 14:03
* 批量添加关键词任务, 异步处理
* @return array
* @throws BsideGlobalException
* @throws \App\Exceptions\AsideGlobalException
*/
public function batchAdd(){
try {
... ... @@ -154,7 +153,7 @@ class KeywordLogic extends BaseLogic
}
NoticeLog::createLog(NoticeLog::TYPE_INIT_KEYWORD, ['project_id' => $this->user['project_id']]);
}catch (\Exception $e){
$this->fail('error');
$this->fail('创建任务添加关键词任务失败,请稍后重试!');
}
return $this->success();
}
... ...
... ... @@ -175,7 +175,7 @@ class ProductLogic extends BaseLogic
$v['values'] = json_encode($v['values']);
}elseif ($v['type'] == 4){
foreach ($v['values'] as $k1=>$v1){
$v1 = str_replace_url($v1);
$v1['url'] = str_replace_url($v1['url']);
$v['values'][$k1] = $v1;
}
$v['values'] = json_encode($v['values']);
... ...