first commit
This commit is contained in:
188
app/Controller/admin/api/KeywordsController.php
Normal file
188
app/Controller/admin/api/KeywordsController.php
Normal file
@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\admin\api;
|
||||
|
||||
use App\Controller\AbstractController;
|
||||
use App\Helpers\ExcelHelper;
|
||||
use App\Model\AppKeywordsMonitor;
|
||||
use App\Model\AppKeywordsMonitorResult;
|
||||
use App\Model\AppKeywordsMonitorTask;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
use Hyperf\View\RenderInterface;
|
||||
use Laminas\Stdlib\ArrayUtils;
|
||||
|
||||
#[Controller(prefix: 'admin/api/keywords')]
|
||||
class KeywordsController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* 列表数据
|
||||
* @url /admin/api/keywords/monitor
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
#[RequestMapping(path: 'monitor', methods: 'get')]
|
||||
public function monitor(): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$ids = [];
|
||||
$query = AppKeywordsMonitor::query()
|
||||
->select(['id'])
|
||||
->where('is_delete', 0)
|
||||
->orderBy('id', 'desc');
|
||||
$pagination = $query->paginate($this->request->input('limit', 10), page: $this->request->input('page'));
|
||||
foreach ($pagination->items() as $item) {
|
||||
$ids[] = $item->id;
|
||||
}
|
||||
|
||||
$value = AppKeywordsMonitor::query()->whereIn('id', $ids)->orderBy('id', 'desc')->get()->toArray();
|
||||
return $this->response->json(['code' => 0, 'msg' => 'ok', 'count' => $pagination->total(), 'data' => $value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增关键词
|
||||
* @url /admin/api/keywords/monitor/insert
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
#[RequestMapping(path: 'monitor/insert', methods: 'post')]
|
||||
public function monitorInsert(): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$keyword = $this->request->post('keyword');
|
||||
|
||||
$query = AppKeywordsMonitor::query()->where([
|
||||
['keyword', $keyword],
|
||||
['is_delete', 0]
|
||||
])->get()->toArray();
|
||||
if ($query) {
|
||||
return $this->response->json(['code' => 400, 'msg' => '已重复添加']);
|
||||
}
|
||||
|
||||
$query = new AppKeywordsMonitor();
|
||||
|
||||
$query->keyword = $keyword;
|
||||
|
||||
$query->save();
|
||||
|
||||
foreach ($this->request->post('platform', []) as $platform) {
|
||||
$task = new AppKeywordsMonitorTask();
|
||||
$task->keyword = $keyword;
|
||||
$task->aid = $query->id;
|
||||
$task->platform = $platform;
|
||||
$task->save();
|
||||
}
|
||||
|
||||
return $this->response->json(['code' => 0, 'msg' => 'ok']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看关键词
|
||||
* @url /admin/api/keywords/monitor/view
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
#[RequestMapping(path: 'monitor/view', methods: 'get')]
|
||||
public function monitorView(): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$id = $this->request->input('id');
|
||||
|
||||
$query = AppKeywordsMonitor::query()->where(['id' => $id])->first()->toArray();
|
||||
if (!$query) {
|
||||
return $this->response->json(['code' => 400, 'msg' => 'id 有误']);
|
||||
}
|
||||
|
||||
$query['platform'] = AppKeywordsMonitorTask::query()->select(['platform'])
|
||||
->where([
|
||||
['aid', $query['id']],
|
||||
['is_delete', 0],
|
||||
])
|
||||
->get()?->pluck('platform');
|
||||
|
||||
return $this->response->json(['code' => 0, 'msg' => 'ok', 'data' => $query]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑关键词
|
||||
* @url /admin/api/keywords/monitor/save
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
#[RequestMapping(path: 'monitor/save', methods: 'post')]
|
||||
public function monitorSave(): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$id = $this->request->post('id');
|
||||
$keyword = $this->request->post('keyword');
|
||||
$platform = $this->request->post('platform', []);
|
||||
|
||||
$query = AppKeywordsMonitor::find($id);
|
||||
if (!$query) {
|
||||
return $this->response->json(['code' => 400, 'msg' => 'id 有误']);
|
||||
}
|
||||
|
||||
$query->keyword = $keyword;
|
||||
$query->save();
|
||||
|
||||
// 先全部删掉
|
||||
AppKeywordsMonitorTask::query()->where(['aid' => $id])->update(['is_delete' => 1]);
|
||||
foreach ($platform as $platformItem) {
|
||||
$query = AppKeywordsMonitorTask::query()->where('aid', $id)->first();
|
||||
if ($query) {
|
||||
$query->is_delete = 0;
|
||||
$query->save();
|
||||
} else {
|
||||
$query = new AppKeywordsMonitorTask();
|
||||
$query->platform = $platformItem;
|
||||
$query->aid = $id;
|
||||
$query->keyword = $keyword;
|
||||
$query->save();
|
||||
}
|
||||
}
|
||||
|
||||
return $this->response->json(['code' => 0, 'msg' => 'ok', 'data' => $query]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除关键词
|
||||
* @url /admin/api/keywords/monitor/delete
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
#[RequestMapping(path: 'monitor/delete', methods: 'post')]
|
||||
public function monitorDelete(): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$id = $this->request->post('id');
|
||||
|
||||
$query = AppKeywordsMonitor::find($id);
|
||||
if (!$query) {
|
||||
return $this->response->json(['code' => 400, 'msg' => 'id 有误']);
|
||||
}
|
||||
|
||||
$query->is_delete = 1;
|
||||
$query->save();
|
||||
|
||||
AppKeywordsMonitorTask::query()->where(['aid' => $id])->update(['is_delete' => 1]);
|
||||
|
||||
return $this->response->json(['code' => 0, 'msg' => 'ok', 'data' => $query]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出关键词报表
|
||||
* @url /admin/api/keywords/monitor/export-all
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
#[RequestMapping(path: 'monitor/export-all', methods: 'get')]
|
||||
public function monitorExportAll(): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$res = AppKeywordsMonitorResult::query()->orderBy('aid', 'desc')->get()->toArray();
|
||||
|
||||
foreach ($res as &$v) {
|
||||
$v['keyword'] = AppKeywordsMonitorTask::find($v['aid'])->keyword;
|
||||
$v['screen_path'] = 'http://127..0.0.1:9503' . $v['screen_path'];
|
||||
}
|
||||
$fileName = date('Y-m-d') . '关键词监控结果';
|
||||
return ExcelHelper::exportData($this->response, list: $res, header: [
|
||||
['关键词', 'keyword', 'text'],
|
||||
['标题', 'title', 'text'],
|
||||
['排名', 'order', 'text'],
|
||||
['链接地址', 'url', 'text'],
|
||||
['ip归属地', 'ip_source', 'text'],
|
||||
['截图地址', 'screen_path', 'text'],
|
||||
], filename:$fileName);
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user