123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace app\api\controller;
- use app\api\model\CommodityModel;
- use app\api\model\SearchModel;
- use app\common\controller\Api;
- /**
- * 商品搜索
- */
- class Search extends Api
- {
- protected $noNeedRight = '*';
- protected $noNeedLogin = '*';
- /**
- * 搜索历史显示
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- */
- public function serachIndex()
- {
- $user_id = $this->request->post('user_id');
- if (!$user_id) {
- return $this->result('网络错误', [], 100);
- }
- $data = SearchModel::where('user_id', $user_id)->group('content')->select();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('暂无数据', [], 100);
- }
- }
- /**
- * 清空历史记录
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- */
- public function serachEmpty()
- {
- $user_id = $this->request->post('user_id');
- if (!$user_id) {
- return $this->result('网络错误', [], 100);
- }
- $del = SearchModel::where('user_id', $user_id)->delete();
- if ($del) {
- return $this->result('成功', [], 200);
- } else {
- return $this->result('失败', [], 100);
- }
- }
- /**
- * 关键词显示
- * @ApiMethod (POST)
- * @param string $content 文字
- */
- public function keyIndex()
- {
- $content = $this->request->post('content');
- if (!$content) {
- return $this->result('网络错误', [], 100);
- }
- $where['c_name'] = ['like', "%" . $content . '%'];
- $data = CommodityModel::where($where)->field('c_name')->group('c_name')->select();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('暂无数据', [], 100);
- }
- }
- /**
- * 使用关键词查询
- * @ApiMethod (POST)
- * @param string $content 文字
- * @param string $user_id 用户ID
- * @param string $page 页数
- */
- public function searchKey()
- {
- $content = $this->request->post('content');
- $user_id = $this->request->post('user_id');
- $page = $this->request->post('page');
- if (!isset($page)) {
- $page = 0;
- }
- if (!$user_id) {
- return $this->result('网络错误', [], 100);
- }
- if (!$content) {
- return $this->result('网络错误', [], 100);
- }
- $where['c_name'] = ['like', "%" . $content . "%"];
- $data = CommodityModel::where($where)->page($page, 5)->select();
- $params = array(
- 'user_id' => $user_id,
- 'content' => $content,
- 'type' => 1,
- );
- $model = new SearchModel();
- $addSearch = $model->allowField(true)->save($params);
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('暂无数据', [], 100);
- }
- }
- /**
- * 不使用关键词查询
- * @ApiMethod (POST)
- * @param string $content 文字
- * @param string $user_id 用户ID
- * @param string $page 页数
- */
- public function searchTitle()
- {
- $content = $this->request->post('content');
- $user_id = $this->request->post('user_id');
- $page = $this->request->post('page');
- if (!isset($page)) {
- $page = 0;
- }
- if (!$user_id) {
- return $this->result('网络错误', [], 100);
- }
- if (!$content) {
- return $this->result('网络错误', [], 100);
- }
- $where['c_title_content'] = ['like', "%" . $content . "%"];
- $data = CommodityModel::where($where)->page($page, 5)->select();
- $params = array(
- 'user_id' => $user_id,
- 'content' => $content,
- 'type' => 2,
- );
- $model = new SearchModel();
- $addSearch = $model->allowField(true)->save($params);
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('暂无数据', [], 100);
- }
- }
- }
|