Comment.php 5.0 KB

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