RankController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Models\Site;
  4. use App\Http\Requests\Rank\KeywordRequest;
  5. use App\Http\Traits\HasSites;
  6. use App\Imports\Keyword;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Maatwebsite\Excel\Facades\Excel;
  12. use TheSeer\Tokenizer\Exception;
  13. /**
  14. * 关键词排民
  15. * Class RankController
  16. * @package App\Http\Controllers\Admin
  17. */
  18. class RankController extends Controller
  19. {
  20. use HasSites;
  21. //关键词
  22. public function keyword($id)
  23. {
  24. $site = Site::query()->where(['id' => $id])->first();
  25. if (!$site) {
  26. return view('admin/site/tips', [
  27. 'tips' => '报告不存在',
  28. 'siteId' => $id,
  29. ]);
  30. }
  31. try {
  32. $rankConnection = DB::connection('rank');
  33. $old = $rankConnection->table('project')->where(['id' => $site->old_id])->select(['allow_googlerank', 'number', 'interval_time'])->first();
  34. $keywordCount = $rankConnection->table('project_keyword')->where(['project_id' => $site->old_id])->count();
  35. $keywordCountNotRun = $rankConnection->table('project_keyword')->where(['project_id' => $site->old_id])->where(['allow_googlerank' => '0'])->count();
  36. return view('admin/rank/keyword_for_site', [
  37. 'siteId' => $id,
  38. 'site' => $site,
  39. 'old' => $old,
  40. 'keywordCount' => $keywordCount,
  41. 'keywordCountNotRun' => $keywordCountNotRun
  42. ]);
  43. } catch (\Throwable $exception) {
  44. return view('admin/site/tips', [
  45. 'tips' => '数据库连接超时,error:' . $exception->getMessage().'http://test.build.yinqingli.net/admin',
  46. 'siteId' => $id,
  47. ]);
  48. }
  49. }
  50. //客户界面关键词
  51. public function customerKeyword()
  52. {
  53. $site = $this->hasUserOneSite();
  54. if (!$site) {
  55. return view('admin/errors/tips');
  56. }
  57. $rankConnection = DB::connection('rank');
  58. $old = $rankConnection->table('project')->where(['id' => $site->old_id])->select(['allow_googlerank', 'number'])->first();
  59. $keywordCount = $rankConnection->table('project_keyword')->where(['project_id' => $site->old_id])->count();
  60. return view('admin/rank/keyword', [
  61. 'siteId' => $site->id,
  62. 'site' => $site,
  63. 'old' => $old,
  64. 'keywordCount' => $keywordCount
  65. ]);
  66. }
  67. //关键词保存
  68. public function keywordSave(KeywordRequest $request, $id)
  69. {
  70. if (!$request->ajax()) {
  71. return view('admin/rank/keyword_save', [
  72. 'siteId' => $id
  73. ]);
  74. }
  75. $site = Site::query()->where(['id' => $id])->first();
  76. if (!$site) {
  77. return response()->json(['message' => '站点信息不存在'], 400);
  78. }
  79. if (!$site->old_id) {
  80. return response()->json(['message' => '请先与rank项目关联'], 400);
  81. }
  82. $rankConnection = DB::connection('rank');
  83. $maxSn = $rankConnection->table('project_keyword')->where(['project_id' => $site->old_id])->max('sn') ?? 0;
  84. $validated = $request->validated();
  85. $validated['sn'] = $maxSn;
  86. $validated['project_id'] = $site->old_id;
  87. $validated['google_rank'] = 9999;
  88. $validated['target_url'] = ' ';
  89. $validated['resules'] = 0;
  90. $validated['allintitle'] = -1;
  91. $validated['sync_time'] = strtotime('today');
  92. $rankConnection->table('project_keyword')->insert($validated);
  93. return response()->json(['message' => '操作成功']);
  94. }
  95. //导入关键词
  96. public function importKeyword(Request $request)
  97. {
  98. set_time_limit(0);
  99. $siteId = $request->input('siteId');
  100. if (!$request->input('excel_path') || !$siteId) {
  101. return response()->json(['message' => '请先上传excel文件'], 422);
  102. }
  103. $site = Site::query()->where(['id' => $siteId])->first();
  104. if (!$site) {
  105. return response()->json(['message' => '站点信息不存在'], 400);
  106. }
  107. if (!$site->old_id) {
  108. return response()->json(['message' => '请先与rank项目关联'], 400);
  109. }
  110. try {
  111. Excel::import(new Keyword($site->old_id), $request->input('excel_path'), 'public');
  112. } catch (\Throwable $throwable) {
  113. Log::error(var_export($throwable->getMessage(), 1));
  114. return response()->json(['message' => $throwable->getMessage()], 400);
  115. }
  116. return response()->json(['message' => '操作成功']);
  117. }
  118. public function keywordTpl($siteId)
  119. {
  120. }
  121. //清空关键词
  122. public function clearKeyword($siteId)
  123. {
  124. $site = Site::query()->where(['id' => $siteId])->first();
  125. if (!$site) {
  126. return response()->json(['message' => '站点信息不存在'], 400);
  127. }
  128. if (!$site->old_id) {
  129. return response()->json(['message' => '请先与rank项目关联'], 400);
  130. }
  131. DB::connection('rank')->table('project_keyword')->where(['project_id' => $site->old_id])->delete();
  132. return response()->json(['message' => '操作成功']);
  133. }
  134. //允许排名
  135. public function allowRank(Request $request, $oldId)
  136. {
  137. if ($oldId < 1) {
  138. return response()->json(['message' => '请先与rank项目关联'], 400);
  139. }
  140. $isAllow = $request->input('isAllow');
  141. $number = $request->input('number');
  142. $intervalTime = $request->input('interval_time');
  143. if (!in_array($isAllow, [1, 0])) {
  144. return response()->json(['message' => '参数错误'], 400);
  145. }
  146. if (!in_array($number, [1, 2, 3, 4])) {
  147. return response()->json(['message' => '请选择排名服务器'], 400);
  148. }
  149. if (!in_array($intervalTime, [0, 5, 7, 15, 30])) {
  150. return response()->json(['message' => '请选择排名服务器'], 400);
  151. }
  152. DB::connection('rank')->table('project')->where(['id' => $oldId])->update([
  153. 'allow_googlerank' => $isAllow,
  154. 'number' => $number,
  155. 'interval_time' => $intervalTime,
  156. ]);
  157. return response()->json(['message' => '操作成功']);
  158. }
  159. }