123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Http\Models\Site;
- use App\Http\Requests\Rank\KeywordRequest;
- use App\Http\Traits\HasSites;
- use App\Imports\Keyword;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Maatwebsite\Excel\Facades\Excel;
- use TheSeer\Tokenizer\Exception;
- /**
- * 关键词排民
- * Class RankController
- * @package App\Http\Controllers\Admin
- */
- class RankController extends Controller
- {
- use HasSites;
- //关键词
- public function keyword($id)
- {
- $site = Site::query()->where(['id' => $id])->first();
- if (!$site) {
- return view('admin/site/tips', [
- 'tips' => '报告不存在',
- 'siteId' => $id,
- ]);
- }
- try {
- $rankConnection = DB::connection('rank');
- $old = $rankConnection->table('project')->where(['id' => $site->old_id])->select(['allow_googlerank', 'number', 'interval_time'])->first();
- $keywordCount = $rankConnection->table('project_keyword')->where(['project_id' => $site->old_id])->count();
- $keywordCountNotRun = $rankConnection->table('project_keyword')->where(['project_id' => $site->old_id])->where(['allow_googlerank' => '0'])->count();
- return view('admin/rank/keyword_for_site', [
- 'siteId' => $id,
- 'site' => $site,
- 'old' => $old,
- 'keywordCount' => $keywordCount,
- 'keywordCountNotRun' => $keywordCountNotRun
- ]);
- } catch (\Throwable $exception) {
- return view('admin/site/tips', [
- 'tips' => '数据库连接超时,error:' . $exception->getMessage().'http://test.build.yinqingli.net/admin',
- 'siteId' => $id,
- ]);
- }
- }
- //客户界面关键词
- public function customerKeyword()
- {
- $site = $this->hasUserOneSite();
- if (!$site) {
- return view('admin/errors/tips');
- }
- $rankConnection = DB::connection('rank');
- $old = $rankConnection->table('project')->where(['id' => $site->old_id])->select(['allow_googlerank', 'number'])->first();
- $keywordCount = $rankConnection->table('project_keyword')->where(['project_id' => $site->old_id])->count();
- return view('admin/rank/keyword', [
- 'siteId' => $site->id,
- 'site' => $site,
- 'old' => $old,
- 'keywordCount' => $keywordCount
- ]);
- }
- //关键词保存
- public function keywordSave(KeywordRequest $request, $id)
- {
- if (!$request->ajax()) {
- return view('admin/rank/keyword_save', [
- 'siteId' => $id
- ]);
- }
- $site = Site::query()->where(['id' => $id])->first();
- if (!$site) {
- return response()->json(['message' => '站点信息不存在'], 400);
- }
- if (!$site->old_id) {
- return response()->json(['message' => '请先与rank项目关联'], 400);
- }
- $rankConnection = DB::connection('rank');
- $maxSn = $rankConnection->table('project_keyword')->where(['project_id' => $site->old_id])->max('sn') ?? 0;
- $validated = $request->validated();
- $validated['sn'] = $maxSn;
- $validated['project_id'] = $site->old_id;
- $validated['google_rank'] = 9999;
- $validated['target_url'] = ' ';
- $validated['resules'] = 0;
- $validated['allintitle'] = -1;
- $validated['sync_time'] = strtotime('today');
- $rankConnection->table('project_keyword')->insert($validated);
- return response()->json(['message' => '操作成功']);
- }
- //导入关键词
- public function importKeyword(Request $request)
- {
- set_time_limit(0);
- $siteId = $request->input('siteId');
- if (!$request->input('excel_path') || !$siteId) {
- return response()->json(['message' => '请先上传excel文件'], 422);
- }
- $site = Site::query()->where(['id' => $siteId])->first();
- if (!$site) {
- return response()->json(['message' => '站点信息不存在'], 400);
- }
- if (!$site->old_id) {
- return response()->json(['message' => '请先与rank项目关联'], 400);
- }
- try {
- Excel::import(new Keyword($site->old_id), $request->input('excel_path'), 'public');
- } catch (\Throwable $throwable) {
- Log::error(var_export($throwable->getMessage(), 1));
- return response()->json(['message' => $throwable->getMessage()], 400);
- }
- return response()->json(['message' => '操作成功']);
- }
- public function keywordTpl($siteId)
- {
- }
- //清空关键词
- public function clearKeyword($siteId)
- {
- $site = Site::query()->where(['id' => $siteId])->first();
- if (!$site) {
- return response()->json(['message' => '站点信息不存在'], 400);
- }
- if (!$site->old_id) {
- return response()->json(['message' => '请先与rank项目关联'], 400);
- }
- DB::connection('rank')->table('project_keyword')->where(['project_id' => $site->old_id])->delete();
- return response()->json(['message' => '操作成功']);
- }
- //允许排名
- public function allowRank(Request $request, $oldId)
- {
- if ($oldId < 1) {
- return response()->json(['message' => '请先与rank项目关联'], 400);
- }
- $isAllow = $request->input('isAllow');
- $number = $request->input('number');
- $intervalTime = $request->input('interval_time');
- if (!in_array($isAllow, [1, 0])) {
- return response()->json(['message' => '参数错误'], 400);
- }
- if (!in_array($number, [1, 2, 3, 4])) {
- return response()->json(['message' => '请选择排名服务器'], 400);
- }
- if (!in_array($intervalTime, [0, 5, 7, 15, 30])) {
- return response()->json(['message' => '请选择排名服务器'], 400);
- }
- DB::connection('rank')->table('project')->where(['id' => $oldId])->update([
- 'allow_googlerank' => $isAllow,
- 'number' => $number,
- 'interval_time' => $intervalTime,
- ]);
- return response()->json(['message' => '操作成功']);
- }
- }
|