Keyword.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\admin\controller\clientmanage\search;
  3. use app\common\controller\Backend;
  4. use think\Controller;
  5. use think\Request;
  6. /**
  7. * 热门搜索词
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Keyword extends Backend
  12. {
  13. /**
  14. * SearchKeyword模型对象
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('ClientSearchKeyword');
  21. $this->view->assign("sexList", $this->model->getSexList());
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. public function add()
  29. {
  30. if ($this->request->isAjax()) {
  31. $ids = $this->request->get('ids');
  32. $row = model('Book')->field('id,name,sex')->where('id', 'in', $ids)->select();
  33. $params = [];
  34. $i =0;
  35. foreach ($row as $value) {
  36. $keywordexists = [];
  37. $keywordexists = model('ClientSearchKeyword')->get(['keyword' => $value['name']]);
  38. if(empty($keywordexists)){
  39. $params[$i]['keyword'] = $value['name'];
  40. $params[$i]['book_id'] = $value['id'];
  41. $params[$i]['sex'] = $value['sex'];
  42. $i = $i+1;
  43. }
  44. }
  45. model('ClientSearchKeyword')->saveAll($params);
  46. $this->success('添加成功' .json_encode($ids));
  47. }
  48. $this->error("非Ajax请求");
  49. return $this->view->fetch();
  50. }
  51. }