GoogleTrendsController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. * 谷歌趋势
  4. * @copyright 2021-浙江引擎力营销策划有限公司
  5. * @author Lc<sunshinecc1@163.com>
  6. * @since 2021-10-13
  7. */
  8. namespace App\Http\Controllers\Admin\Analyze;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Models\GoogleTrendsKeyword;
  11. use App\Http\Models\Role;
  12. use App\Http\Models\User;
  13. use App\Http\Models\WeekTaskHistory;
  14. use App\Http\Models\WeekTaskInfo;
  15. use App\Http\Services\SendMessageService;
  16. use App\Imports\GoogleTrendsKeywordImport;
  17. use GuzzleHttp\Client;
  18. use Illuminate\Http\Request;
  19. use Illuminate\Contracts\View\Factory;
  20. use Illuminate\Support\Facades\Log;
  21. use Illuminate\View\View;
  22. use Maatwebsite\Excel\Facades\Excel;
  23. use Illuminate\Http\JsonResponse;
  24. use Illuminate\Http;
  25. class GoogleTrendsController extends Controller
  26. {
  27. public $sendMessageService;
  28. // const MOBILE = '17805855987';//cc
  29. //const MOBILE = '17771777094';//test
  30. const MOBILE = '18705195517';//cc
  31. /**
  32. * 短信服务类
  33. * GoogleTrendsController constructor.
  34. * @param SendMessageService $sendMessageService
  35. */
  36. public function __construct(SendMessageService $sendMessageService)
  37. {
  38. $this->sendMessageService = $sendMessageService;
  39. }
  40. /**
  41. * 报表
  42. * @param Request $request
  43. * @return Factory|Http\RedirectResponse|View
  44. */
  45. public function index(Request $request)
  46. {
  47. try {
  48. $keyWold = $request->input('keyWold');
  49. $disableList = GoogleTrendsKeyword::query()->where('enable', 1)->pluck('keyword');
  50. if (in_array($keyWold, $disableList->toArray())) {
  51. return view('admin/site/not_found', [
  52. 'tips' => '该词不在接单范围内',
  53. 'siteId' => 0,
  54. ]);
  55. }
  56. $keyWoldInfo = GoogleTrendsKeyword::query()->where('keyword', $keyWold)->first();
  57. if (empty($keyWoldInfo)) {
  58. $this->sendMessageService->sendMessage(self::MOBILE, $keyWold);
  59. return view('admin/site/not_found', [
  60. 'tips' => '暂无资料',
  61. 'siteId' => 0,
  62. ]);
  63. }
  64. if (empty($keyWoldInfo->monthly_searches) || empty($keyWoldInfo->monthly_searches) || empty($keyWoldInfo->monthly_searches)) {
  65. return view('admin/site/not_found', [
  66. 'tips' => '暂无资料',
  67. 'siteId' => 0,
  68. ]);
  69. }
  70. if (!empty($keyWoldInfo->cache)) {
  71. $cache = \GuzzleHttp\json_decode($keyWoldInfo->cache, true);
  72. $relatedInformation = $cache['relatedInformation'] ?? [];
  73. $relatedTopic = $cache['relatedTopic'] ?? [];
  74. $dateList = $cache['dateList'] ?? [];
  75. $countryList = $cache['countryList'] ?? [];
  76. } else {
  77. //请求香港的后台服务器47.56.232.20
  78. $client = new Client();
  79. $response = $client->post('http://test.build.yinqingli.net/googleTrendsApi/getKeyWordResult', [
  80. 'form_params' => ['keyWold' => $keyWold],
  81. ]);
  82. $result = \GuzzleHttp\json_decode($response->getBody()->getContents(), true);
  83. if ($result['status'] == 200) {
  84. GoogleTrendsKeyword::query()->where('id', $keyWoldInfo->id)->update(['cache' => \GuzzleHttp\json_encode($result['data'])]);
  85. }
  86. $relatedInformation = $result['data']['relatedInformation'] ?? [];
  87. $relatedTopic = $result['data']['relatedTopic'] ?? [];
  88. $dateList = $result['data']['dateList'] ?? [];
  89. $countryList = $result['data']['countryList'] ?? [];
  90. }
  91. return view('admin/google_trends/google_trends', [
  92. 'relatedInformation' => $relatedInformation,
  93. 'relatedTopic' => $relatedTopic,
  94. 'dateList' => $dateList,
  95. 'countryList' => $countryList,
  96. 'keyWoldInfo' => $keyWoldInfo,
  97. 'country' => array_merge($this->arraySort($countryList, 'value', 'desc')),
  98. ]);
  99. } catch (\Exception $e) {
  100. echo $e->getMessage();
  101. }
  102. }
  103. /**
  104. * 查询
  105. * @param Request $request
  106. * @return Factory|JsonResponse|View
  107. */
  108. public function query(Request $request)
  109. {
  110. if (!$request->ajax()) {
  111. return view('admin/google_trends/google_trends_query', [
  112. ]);
  113. }
  114. $keyWold = strtolower(trim($request->input('keyWold')));
  115. if (empty($keyWold)) {
  116. return response()->json(['status' => 500, 'message' => '请输入关键词']);
  117. }
  118. $cnKeyWord = strtolower(trim($request->input('cnKeyWord'))) ?? '';
  119. $website = strtolower(trim($request->input('website'))) ?? '';
  120. $disableList = GoogleTrendsKeyword::query()->where('enable', 1)->pluck('keyword');
  121. if (in_array($keyWold, $disableList->toArray())) {
  122. $remarks = GoogleTrendsKeyword::query()->where('keyword', $keyWold)->value('remarks') ?? '';
  123. return response()->json(['status' => 500, 'message' => $remarks]);
  124. }
  125. $keyWoldInfo = GoogleTrendsKeyword::query()->where('keyword', $keyWold)->first();
  126. if (empty($keyWoldInfo)) {
  127. GoogleTrendsKeyword::query()->insert(
  128. [
  129. 'keyword' => $keyWold,
  130. 'cn_keyword' => $cnKeyWord,
  131. 'website' => $website,
  132. 'created_at' => date('Y-m-d H:i:s'),
  133. 'user_id' => $authUser = auth()->user()->id,
  134. ]);
  135. $this->sendMessageService->sendMessage(self::MOBILE, $keyWold);
  136. return response()->json(['status' => 301]);
  137. }
  138. if (empty($keyWoldInfo->monthly_searches)) {
  139. return response()->json(['status' => 301]);
  140. }
  141. return response()->json(['status' => 200]);
  142. }
  143. /**
  144. * 关键词
  145. * @param Request $request
  146. * @return Factory|View
  147. */
  148. public function keyword(Request $request)
  149. {
  150. if (!$request->ajax()) {
  151. $disabled = GoogleTrendsKeyword::query()->where('enable', 1)->count() ?? 0;//禁用
  152. $enable = GoogleTrendsKeyword::query()->where('enable', 0)->count() ?? 0;//启用
  153. return view('admin/analyze/keyword', [
  154. 'optimizers' => Role::getUsers(Role::TYPE_OPTIMIZER),
  155. 'seller' => Role::getUsers(Role::TYPE_SELLER),
  156. 'disabled' => $disabled,
  157. 'enable' => $enable
  158. ]);
  159. }
  160. $keyword = $request->input('keyword');
  161. $result = GoogleTrendsKeyword::query()->whereNull('deleted_at');
  162. if (!empty($keyword)) {
  163. $result = $result->where('keyword', 'like', '%' . $keyword . '%');
  164. }
  165. $optimizerId = $request->input('optimizerId');
  166. if (!empty($optimizerId)) {
  167. $result = $result->where('principal', $optimizerId);
  168. }
  169. $sellerId = $request->input('sellerId');
  170. if (!empty($sellerId)) {
  171. $result = $result->where('user_id', $sellerId);
  172. }
  173. $enable = $request->input('enable');
  174. if (isset($enable)) {
  175. $result = $result->where('enable', $enable);
  176. }
  177. $result = $result->orderByDesc('id')->paginate($request->input('pageSize') ?? TABLE_PAGE_SIZE);
  178. $userList = User::query()->where('status', 1)->pluck('nickname', 'id');
  179. foreach ($result->items() as $item) {
  180. $item->user = $userList[$item->user_id] ?? '';
  181. $item->taskUser = $userList[$item->principal] ?? '';
  182. $keywordsCondition = [
  183. 'user_type' => $item->principal,
  184. 'describe' => $item->keyword,
  185. ];
  186. $item->taskStatus = '未完成';
  187. $task = WeekTaskInfo::query()->where($keywordsCondition)->first();
  188. $historyTask = WeekTaskHistory::query()->where($keywordsCondition)->orderBy('id', 'desc')->first();
  189. if (!empty($task) && $task->status == 'ok') {
  190. $item->taskStatus = '完成';
  191. }
  192. if (!empty($historyTask) && $historyTask->status == 'ok') {
  193. $item->taskStatus = '完成';
  194. }
  195. }
  196. return response()->json([
  197. 'rows' => $result->items(),
  198. 'total' => $result->total()
  199. ]);
  200. }
  201. /**
  202. * 分配任务,并发送短信
  203. * @param Request $request
  204. * @return JsonResponse
  205. */
  206. public function keywordTask(Request $request)
  207. {
  208. $taskId = $request->input('taskId');
  209. $userId = $request->input('userId');
  210. $keyword = GoogleTrendsKeyword::query()->where('id', $taskId)->first();
  211. if (!empty($keyword)) {
  212. GoogleTrendsKeyword::query()->where('id', $taskId)->update(['principal' => $userId]);
  213. }
  214. $user = User::query()->where('id', $userId)->first();
  215. if (!empty($user->phone)) {
  216. //自动创建任务并发短信给优化
  217. $this->sendMessageService->sendMessage($user->phone, $keyword->keyword ?? '', 3);
  218. $data = [
  219. 'type' => 'now',
  220. 'day' => date('w'),
  221. 'user_type' => $userId,
  222. 'duty_id' => $keyword->user_id,
  223. 'describe' => $keyword->keyword,
  224. 'remark' => $keyword->keyword . ' 接单关键词',
  225. 'cond_id' => 87,
  226. 'created_at' => date('Y-m-d H:i:s'),
  227. 'deadline' => date("Y-m-d", strtotime("+1 day")),//一天完成
  228. ];
  229. $info = WeekTaskInfo::query()->where(['describe' => $data['describe'], 'cond_id' => 87])->first();
  230. if (empty($info)) {
  231. WeekTaskInfo::query()->insert($data);
  232. } else {
  233. WeekTaskInfo::query()->where('id', $info->id)->update(['user_type' => $userId]);
  234. }
  235. }
  236. return response()->json(['message' => '操作成功']);
  237. }
  238. /**
  239. * 删除关键词
  240. * @param Request $request
  241. * @return JsonResponse
  242. */
  243. public function deleteKeyword(Request $request)
  244. {
  245. $taskId = $request->input('taskId');
  246. $update = [
  247. 'updated_at' => date('Y-m-d H:i:s'),
  248. 'deleted_at' => date('Y-m-d H:i:s'),
  249. ];
  250. GoogleTrendsKeyword::query()->where('id', $taskId)->update($update);
  251. return response()->json(['message' => '操作成功']);
  252. }
  253. /**
  254. * 保存
  255. * @param Request $request
  256. * @param $id
  257. * @return Factory|JsonResponse|View
  258. */
  259. public function keywordSave(Request $request, $id)
  260. {
  261. if (!$request->ajax()) {
  262. $info = GoogleTrendsKeyword::query()->where('id', $id)->first();
  263. return view('admin/analyze/save_keyword', [
  264. 'info' => $info
  265. ]);
  266. }
  267. $result = $request->all();
  268. if (!empty($result['monthly_searches']) && !is_numeric($result['monthly_searches'])) {
  269. return response()->json(['message' => '请填纯数字,不要带格式'], 400);
  270. }
  271. if (!empty($result['mumber_of_search_results']) && !is_numeric($result['mumber_of_search_results'])) {
  272. return response()->json(['message' => '请填纯数字,不要带格式'], 400);
  273. }
  274. if (!empty($result['competition_index']) && !is_numeric($result['competition_index'])) {
  275. return response()->json(['message' => '请填纯数字,不要带格式'], 400);
  276. }
  277. $result['updated_at'] = date('Y-m-d H:i:s');
  278. GoogleTrendsKeyword::query()->where('id', $id)->update($result);
  279. return response()->json(['message' => '操作成功']);
  280. }
  281. /**
  282. * 导入
  283. * @param Request $request
  284. * @return JsonResponse
  285. */
  286. public function importKeyWord(Request $request)
  287. {
  288. set_time_limit(0);
  289. try {
  290. $excelPath = $request->input('excel_path');
  291. if (!$excelPath) {
  292. return response()->json(['message' => '请先上传excel文件'], 422);
  293. }
  294. Excel::import(new GoogleTrendsKeywordImport(), $excelPath, 'public');
  295. } catch (\Throwable $throwable) {
  296. Log::error(var_export($throwable->getMessage(), 1));
  297. return response()->json(['message' => '导入失败'], 400);
  298. }
  299. return response()->json(['message' => '操作成功']);
  300. }
  301. /**
  302. * 关键词列表
  303. * @param Request $request
  304. * @return JsonResponse
  305. */
  306. public function getKeyWordList(Request $request)
  307. {
  308. $data = [];
  309. $keyword = $request->input('keyword');
  310. $list = GoogleTrendsKeyword::query()->where('keyword', 'like', '%' . $keyword . '%')->pluck('keyword') ?? [];
  311. if (!empty($list)) {
  312. foreach ($list as $key => $item) {
  313. $data[] = [
  314. 'value' => $item,
  315. 'data' => $item,
  316. ];
  317. }
  318. }
  319. return response()->json(['data' => $data]);
  320. }
  321. }