SearchLog.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\admin\controller\cms;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 搜索记录管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class SearchLog extends Backend
  13. {
  14. /**
  15. * SearchLog模型对象
  16. * @var \app\admin\model\cms\SearchLog
  17. */
  18. protected $model = null;
  19. protected $searchFields = 'id,keywords';
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\cms\SearchLog;
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 添加
  33. *
  34. * @return string
  35. * @throws \think\Exception
  36. */
  37. public function add()
  38. {
  39. if (false === $this->request->isPost()) {
  40. return $this->view->fetch();
  41. }
  42. $params = $this->request->post('row/a');
  43. if (empty($params)) {
  44. $this->error(__('Parameter %s can not be empty', ''));
  45. }
  46. $params = $this->preExcludeFields($params);
  47. $log = $this->model->where(['keywords' => $params['keywords']])->find();
  48. if ($log) {
  49. $this->error(__('Keyword already exists'));
  50. }
  51. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  52. $params[$this->dataLimitField] = $this->auth->id;
  53. }
  54. $result = false;
  55. Db::startTrans();
  56. try {
  57. //是否采用模型验证
  58. if ($this->modelValidate) {
  59. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  60. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  61. $this->model->validateFailException()->validate($validate);
  62. }
  63. $result = $this->model->allowField(true)->save($params);
  64. Db::commit();
  65. } catch (ValidateException|PDOException|Exception $e) {
  66. Db::rollback();
  67. $this->error($e->getMessage());
  68. }
  69. if ($result === false) {
  70. $this->error(__('No rows were inserted'));
  71. }
  72. $this->success();
  73. }
  74. }