Search.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\CommodityModel;
  4. use app\api\model\SearchModel;
  5. use app\common\controller\Api;
  6. /**
  7. * 商品搜索
  8. */
  9. class Search extends Api
  10. {
  11. protected $noNeedRight = '*';
  12. protected $noNeedLogin = '*';
  13. /**
  14. * 搜索历史显示
  15. * @ApiMethod (POST)
  16. * @param string $user_id 用户id
  17. */
  18. public function serachIndex()
  19. {
  20. $user_id = $this->request->post('user_id');
  21. if (!$user_id) {
  22. return $this->result('网络错误', [], 100);
  23. }
  24. $data = SearchModel::where('user_id', $user_id)->group('content')->select();
  25. if ($data) {
  26. return $this->result('', $data, 200);
  27. } else {
  28. return $this->result('暂无数据', [], 100);
  29. }
  30. }
  31. /**
  32. * 清空历史记录
  33. * @ApiMethod (POST)
  34. * @param string $user_id 用户id
  35. */
  36. public function serachEmpty()
  37. {
  38. $user_id = $this->request->post('user_id');
  39. if (!$user_id) {
  40. return $this->result('网络错误', [], 100);
  41. }
  42. $del = SearchModel::where('user_id', $user_id)->delete();
  43. if ($del) {
  44. return $this->result('成功', [], 200);
  45. } else {
  46. return $this->result('失败', [], 100);
  47. }
  48. }
  49. /**
  50. * 关键词显示
  51. * @ApiMethod (POST)
  52. * @param string $content 文字
  53. */
  54. public function keyIndex()
  55. {
  56. $content = $this->request->post('content');
  57. if (!$content) {
  58. return $this->result('网络错误', [], 100);
  59. }
  60. $where['c_name'] = ['like', "%" . $content . '%'];
  61. $data = CommodityModel::where($where)->field('c_name')->group('c_name')->select();
  62. if ($data) {
  63. return $this->result('', $data, 200);
  64. } else {
  65. return $this->result('暂无数据', [], 100);
  66. }
  67. }
  68. /**
  69. * 使用关键词查询
  70. * @ApiMethod (POST)
  71. * @param string $content 文字
  72. * @param string $user_id 用户ID
  73. * @param string $page 页数
  74. */
  75. public function searchKey()
  76. {
  77. $content = $this->request->post('content');
  78. $user_id = $this->request->post('user_id');
  79. $page = $this->request->post('page');
  80. if (!isset($page)) {
  81. $page = 0;
  82. }
  83. if (!$user_id) {
  84. return $this->result('网络错误', [], 100);
  85. }
  86. if (!$content) {
  87. return $this->result('网络错误', [], 100);
  88. }
  89. $where['c_name'] = ['like', "%" . $content . "%"];
  90. $data = CommodityModel::where($where)->page($page, 5)->select();
  91. $params = array(
  92. 'user_id' => $user_id,
  93. 'content' => $content,
  94. 'type' => 1,
  95. );
  96. $model = new SearchModel();
  97. $addSearch = $model->allowField(true)->save($params);
  98. if ($data) {
  99. return $this->result('', $data, 200);
  100. } else {
  101. return $this->result('暂无数据', [], 100);
  102. }
  103. }
  104. /**
  105. * 不使用关键词查询
  106. * @ApiMethod (POST)
  107. * @param string $content 文字
  108. * @param string $user_id 用户ID
  109. * @param string $page 页数
  110. */
  111. public function searchTitle()
  112. {
  113. $content = $this->request->post('content');
  114. $user_id = $this->request->post('user_id');
  115. $page = $this->request->post('page');
  116. if (!isset($page)) {
  117. $page = 0;
  118. }
  119. if (!$user_id) {
  120. return $this->result('网络错误', [], 100);
  121. }
  122. if (!$content) {
  123. return $this->result('网络错误', [], 100);
  124. }
  125. $where['c_title_content'] = ['like', "%" . $content . "%"];
  126. $data = CommodityModel::where($where)->page($page, 5)->select();
  127. $params = array(
  128. 'user_id' => $user_id,
  129. 'content' => $content,
  130. 'type' => 2,
  131. );
  132. $model = new SearchModel();
  133. $addSearch = $model->allowField(true)->save($params);
  134. if ($data) {
  135. return $this->result('', $data, 200);
  136. } else {
  137. return $this->result('暂无数据', [], 100);
  138. }
  139. }
  140. }