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'); $ignoreUrl = $this->request->post('ignore_url'); $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; $ignoreUrl = array_filter(array_unique(explode(PHP_EOL, $ignoreUrl))); $query->ignore_url = json_encode($ignoreUrl); $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::formatQuery(['ignore_url'])->where(['id' => $id])->first()->toArray(); if (!$query) { return $this->response->json(['code' => 400, 'msg' => 'id 有误']); } if ($query['ignore_url']) { $query['ignore_url'] = implode(PHP_EOL, $query['ignore_url']); } $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', []); $ignoreUrl = $this->request->post('ignore_url'); $query = AppKeywordsMonitor::find($id); if (!$query) { return $this->response->json(['code' => 400, 'msg' => 'id 有误']); } $query->keyword = $keyword; $ignoreUrl = array_filter(array_unique(explode(PHP_EOL, $ignoreUrl))); $query->ignore_url = json_encode($ignoreUrl); $query->save(); // 先全部删掉 AppKeywordsMonitorTask::query()->where(['aid' => $id])->update(['is_delete' => 1]); foreach ($platform as $platformItem) { $query = AppKeywordsMonitorTask::query()->where([ ['aid', $id], ['platform', $platformItem] ])->first(); if ($query) { $query->is_delete = 0; $query->save(); } else { $query = new AppKeywordsMonitorTask(); $query->platform = $platformItem; $query->aid = $id; $query->keyword = $keyword; $query->page = 2; // 写死的只查前两页 $query->save(); } } return $this->response->json(['code' => 0, 'msg' => 'ok', 'data' => $query]); } // /admin/api/keywords/monitor/research /** * 重查关键词 * @url /admin/api/keywords/monitor/research * @return \Psr\Http\Message\ResponseInterface */ #[RequestMapping(path: 'monitor/research', methods: 'post')] public function monitorResearch(): \Psr\Http\Message\ResponseInterface { $id = $this->request->input('id'); $query = AppKeywordsMonitorTask::query()->where('aid', $id)->update(['queried_at' => 0]); 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 */ #[RequestMapping(path: 'monitor/export-all', methods: 'get')] public function monitorExportAll() { $baiduPCRes = AppKeywordsMonitorResult::query() ->where('is_delete', 0) ->where('platform', 1) ->where('created_at', '>', date('Y-m-d H:i:s', strtotime('today'))) ->orderBy('aid', 'desc') ->get() ->toArray(); foreach ($baiduPCRes as &$v) { $v['keyword'] = AppKeywordsMonitorTask::find($v['aid'])->keyword; $v['screen_path'] = Config::getDomain() . $v['screen_path']; } $baiduWAPRes = AppKeywordsMonitorResult::query() ->where('is_delete', 0) ->where('platform', 2) ->where('created_at', '>', date('Y-m-d H:i:s', strtotime('today'))) ->orderBy('aid', 'desc') ->get() ->toArray(); foreach ($baiduWAPRes as &$v) { $v['keyword'] = AppKeywordsMonitorTask::find($v['aid'])->keyword; $v['screen_path'] = Config::getDomain() . $v['screen_path']; } $fileName = date('Y-m-d') . '关键词监控结果'; $exportList = []; // 百度PC非负率 $total = AppKeywordsMonitorTask::query()->where('is_delete', 0)->where('platform', 1)->sum('length') ?: 1; $percent = round(($total - count($baiduPCRes)) / $total, 2) * 100; $exportList["百度PC端_$percent%"] = $baiduPCRes; // 百度WAP非负率 $total = AppKeywordsMonitorTask::query()->where('is_delete', 0)->where('platform', 2)->sum('length'); $percent = round(($total - count($baiduWAPRes)) / $total, 2) * 100; $exportList["百度WAP端_$percent%"] = $baiduWAPRes; return ExcelHelper::exportData($this->response, list: $exportList, header: [ ['关键词', 'keyword', 'text'], ['标题', 'title', 'text'], ['排名', 'order', 'text'], ['链接地址', 'url', 'text'], ['ip归属地', 'ip_source', 'text'], ['截图地址', 'screen_path', 'text'], ], filename:$fileName); return []; } }