Comment.php 5.4 KB

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