123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\admin\controller\search;
- use app\common\controller\Backend;
- use app\common\model\SearchKeyword;
- use think\Controller;
- use think\Request;
- /**
- * 热门搜索词
- *
- * @icon fa fa-circle-o
- */
- class Keyword extends Backend
- {
- /**
- * @var SearchKeyword
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('SearchKeyword');
- $this->view->assign("sexList", $this->model->getSexList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
- * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name'))
- {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->with('book')
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->with('book')
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- public function add()
- {
- if ($this->request->isAjax()) {
- $ids = $this->request->get('ids');
- $row = model('Book')->field('id,name,sex')->where('id', 'in', $ids)->select();
- $params = [];
- $i =0;
- foreach ($row as $value) {
- $keywordexists = [];
- $keywordexists = model('SearchKeyword')->get(['keyword' => $value['name']]);
- if(empty($keywordexists)){
- $params[$i]['keyword'] = $value['name'];
- $params[$i]['book_id'] = $value['id'];
- $params[$i]['sex'] = $value['sex'];
- $i = $i+1;
- }
- }
- model('SearchKeyword')->saveAll($params);
- $this->success('添加成功' .json_encode($ids));
- }
- $this->error("非Ajax请求");
- return $this->view->fetch();
- }
- }
|