SearchHistoryController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Http\Controllers\Admin\Info;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\Info\SearchHistoryService;
  5. use Illuminate\Http\Request;
  6. /**
  7. * 搜索历史记录
  8. */
  9. class SearchHistoryController extends Controller
  10. {
  11. /**
  12. * @var SearchHistoryService
  13. */
  14. private $searchHistoryService;
  15. /**
  16. * SearchHistoryController constructor.
  17. *
  18. * @param SearchHistoryService $searchHistoryService
  19. */
  20. public function __construct(SearchHistoryService $searchHistoryService)
  21. {
  22. parent::__construct();
  23. $this->middleware('checkUserPermission');
  24. $this->searchHistoryService = $searchHistoryService;
  25. }
  26. /**
  27. * 新增
  28. * @must
  29. * @param Request $request
  30. *
  31. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  32. * @throws \Prettus\Validator\Exceptions\ValidatorException
  33. */
  34. public function store(Request $request)
  35. {
  36. $data = $this->validateData($request, [
  37. 'keyword' => 'required|string',
  38. 'type' => 'required',
  39. ], [
  40. 'keyword' => '关键字',
  41. 'type' => '类型',
  42. ]);
  43. $data['guard'] = 'admins';
  44. $data['user_id'] = login_admin_id();
  45. $searchHistory = $this->searchHistoryService->handleStore($data);
  46. return $this->response->created($searchHistory, '创建成功');
  47. }
  48. /**
  49. * 搜索历史
  50. * @must
  51. * @param Request $request
  52. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  53. * @throws \Illuminate\Validation\ValidationException
  54. */
  55. public function history(Request $request)
  56. {
  57. $data = $this->validateData($request, [
  58. 'type' => 'required',
  59. ], [
  60. 'type' => '类型',
  61. ]);
  62. $lists = $this->searchHistoryService->handleHistory($data['type']);
  63. return $this->response->success($lists);
  64. }
  65. /**
  66. * 删除
  67. * @must
  68. * @param Request $request
  69. */
  70. public function destroy(Request $request)
  71. {
  72. $this->validate($request, ['id' => 'required|integer']);
  73. $re = $this->searchHistoryService->handleDelete($request->get('id'));
  74. if ($re) {
  75. return $this->response->ok('删除成功');
  76. }
  77. return $this->response->fail('删除失败');
  78. }
  79. }