Comment.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 评论管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Comment extends Backend
  14. {
  15. /**
  16. * Comment模型对象
  17. * @var \app\admin\model\shop\Comment
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\shop\Comment;
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 编辑
  28. */
  29. public function edit($ids = null)
  30. {
  31. $this->view->assign('type', $this->request->param('type'));
  32. return parent::edit($ids);
  33. }
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->with(['User', 'Goods'])
  49. ->where($where)
  50. ->where('pid', 0)
  51. ->order($sort, $order)
  52. ->paginate($limit);
  53. foreach ($list as $index => $item) {
  54. if ($item->user) {
  55. $item->user->visible(['id', 'nickname', 'avatar']);
  56. }
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. public function reply()
  64. {
  65. $pid = $this->request->param('pid');
  66. if ($this->request->isPost()) {
  67. $params = $this->request->post("row/a");
  68. $row = $this->model->where('id', $pid)->find();
  69. if (!$row) {
  70. $this->error('未找到记录');
  71. }
  72. if ($params) {
  73. $params = $this->preExcludeFields($params);
  74. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  75. $params[$this->dataLimitField] = $this->auth->id;
  76. }
  77. $result = false;
  78. Db::startTrans();
  79. try {
  80. //是否采用模型验证
  81. if ($this->modelValidate) {
  82. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  83. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  84. $this->model->validateFailException(true)->validate($validate);
  85. }
  86. $params['pid'] = $pid;
  87. $params['user_id'] = $this->auth->id;
  88. $params['ip'] = request()->ip();
  89. $params['useragent'] = substr(request()->server('HTTP_USER_AGENT'), 0, 255);
  90. $row->setInc('comments');
  91. $result = $this->model->allowField(true)->save($params);
  92. Db::commit();
  93. } catch (ValidateException $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. } catch (PDOException $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. } catch (Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. if ($result !== false) {
  104. $this->success();
  105. } else {
  106. $this->error(__('No rows were inserted'));
  107. }
  108. }
  109. $this->error(__('Parameter %s can not be empty', ''));
  110. }
  111. $list = $this->model->where('pid', 'eq', $pid)->select();
  112. $this->assign('reply_list', $list);
  113. return $this->view->fetch();
  114. }
  115. }