Keyword.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\admin\controller\search;
  3. use app\common\controller\Backend;
  4. use app\common\model\SearchKeyword;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. * 热门搜索词
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Keyword extends Backend
  13. {
  14. /**
  15. * @var SearchKeyword
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('SearchKeyword');
  22. $this->view->assign("sexList", $this->model->getSexList());
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags']);
  36. if ($this->request->isAjax())
  37. {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('pkey_name'))
  40. {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $total = $this->model
  45. ->with('book')
  46. ->where($where)
  47. ->order($sort, $order)
  48. ->count();
  49. $list = $this->model
  50. ->with('book')
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->limit($offset, $limit)
  54. ->select();
  55. $result = array("total" => $total, "rows" => $list);
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. public function add()
  61. {
  62. if ($this->request->isAjax()) {
  63. $ids = $this->request->get('ids');
  64. $row = model('Book')->field('id,name,sex')->where('id', 'in', $ids)->select();
  65. $params = [];
  66. $i =0;
  67. foreach ($row as $value) {
  68. $keywordexists = [];
  69. $keywordexists = model('SearchKeyword')->get(['keyword' => $value['name']]);
  70. if(empty($keywordexists)){
  71. $params[$i]['keyword'] = $value['name'];
  72. $params[$i]['book_id'] = $value['id'];
  73. $params[$i]['sex'] = $value['sex'];
  74. $i = $i+1;
  75. }
  76. }
  77. model('SearchKeyword')->saveAll($params);
  78. $this->success('添加成功' .json_encode($ids));
  79. }
  80. $this->error("非Ajax请求");
  81. return $this->view->fetch();
  82. }
  83. }