Comment.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace app\admin\controller\shopro\goods;
  3. use think\Db;
  4. use app\admin\controller\shopro\Common;
  5. use app\admin\model\shopro\goods\Comment as CommentModel;
  6. use app\admin\model\shopro\data\FakeUser as FakeUserModel;
  7. class Comment extends Common
  8. {
  9. protected $model = null;
  10. public function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->model = new CommentModel;
  14. }
  15. /**
  16. * 商品评价列表
  17. *
  18. * @return \think\Response
  19. */
  20. public function index()
  21. {
  22. if (!$this->request->isAjax()) {
  23. return $this->view->fetch();
  24. }
  25. $comments = $this->model->sheepFilter()->with(['goods' => function ($query) {
  26. $query->field('id,image,title');
  27. }, 'order' => function ($query) {
  28. $query->removeOption('soft_delete');
  29. }, 'order_item'])->paginate($this->request->param('list_rows', 10));
  30. $morphs = [
  31. 'user' => \app\admin\model\shopro\user\User::class,
  32. 'fake_user' => \app\admin\model\shopro\data\FakeUser::class
  33. ];
  34. $comments = morph_to($comments, $morphs, ['user_type', 'user_id']);
  35. $this->success('获取成功', null, $comments);
  36. }
  37. public function detail($id)
  38. {
  39. $comment = $this->model->with(['admin', 'goods' => function ($query) {
  40. $query->field('id,image,title,price');
  41. }, 'order' => function ($query) {
  42. $query->removeOption('soft_delete');
  43. }, 'order_item'])->where('id', $id)->find();
  44. if (!$comment) {
  45. $this->error(__('No Results were found'));
  46. }
  47. $morphs = [
  48. 'user' => \app\admin\model\shopro\user\User::class,
  49. 'fake_user' => \app\admin\model\shopro\data\FakeUser::class
  50. ];
  51. $comments = morph_to([$comment], $morphs, ['user_type', 'user_id']);
  52. $this->success('获取成功', null, $comments->all()[0]);
  53. }
  54. public function add()
  55. {
  56. if (!$this->request->isAjax()) {
  57. return $this->view->fetch();
  58. }
  59. $params = $this->request->only([
  60. 'goods_id', 'user_id', 'level', 'content', 'images', 'status'
  61. ]);
  62. $params['user_type'] = 'fake_user';
  63. $this->svalidate($params, ".add");
  64. $fakeUser = FakeUserModel::find($params['user_id']);
  65. $params['user_nickname'] = $fakeUser ? $fakeUser->nickname : null;
  66. $params['user_avatar'] = $fakeUser ? $fakeUser->avatar : null;
  67. Db::transaction(function () use ($params) {
  68. $this->model->save($params);
  69. });
  70. $this->success('保存成功');
  71. }
  72. public function edit($id = null)
  73. {
  74. if (!$this->request->isAjax()) {
  75. return $this->view->fetch('add');
  76. }
  77. $params = $this->request->only([
  78. 'status'
  79. ]);
  80. $id = explode(',', $id);
  81. $list = $this->model->whereIn('id', $id)->select();
  82. $result = Db::transaction(function () use ($list, $params) {
  83. $count = 0;
  84. foreach ($list as $comment) {
  85. $comment->status = $params['status'] ?? 'hidden';
  86. $count += $comment->save();
  87. }
  88. return $count;
  89. });
  90. if ($result) {
  91. $this->success('更新成功', null, $result);
  92. } else {
  93. $this->error('更新失败,未改变任何记录');
  94. }
  95. }
  96. public function reply($id)
  97. {
  98. if (!$this->request->isAjax()) {
  99. return $this->view->fetch();
  100. }
  101. $params = $this->request->only([
  102. 'content'
  103. ]);
  104. $this->svalidate($params, '.reply');
  105. $comment = $this->model->noReply()->find($id);
  106. if (!$comment) {
  107. $this->error(__('No Results were found'));
  108. }
  109. $comment->reply_content = $params['content'];
  110. $comment->reply_time = time();
  111. $comment->admin_id = $this->auth->id;
  112. $comment->save();
  113. $this->success('回复成功');
  114. }
  115. /**
  116. * 删除商品评价
  117. *
  118. * @param $id
  119. * @return \think\Response
  120. */
  121. public function delete($id)
  122. {
  123. if (empty($id)) {
  124. $this->error(__('Parameter %s can not be empty', 'id'));
  125. }
  126. $list = $this->model->where('id', 'in', $id)->select();
  127. Db::transaction(function () use ($list) {
  128. $count = 0;
  129. foreach ($list as $item) {
  130. $count += $item->delete();
  131. }
  132. return $count;
  133. });
  134. $this->success('删除成功');
  135. }
  136. /**
  137. * 评价回收站
  138. *
  139. * @return void
  140. */
  141. public function recyclebin()
  142. {
  143. if (!$this->request->isAjax()) {
  144. return $this->view->fetch();
  145. }
  146. $comments = $this->model->onlyTrashed()->sheepFilter()->paginate($this->request->param('list_rows', 10));
  147. $this->success('获取成功', null, $comments);
  148. }
  149. /**
  150. * 还原(支持批量)
  151. *
  152. * @param $id
  153. * @return \think\Response
  154. */
  155. public function restore($id = null)
  156. {
  157. if (empty($id)) {
  158. $this->error(__('Parameter %s can not be empty', 'id'));
  159. }
  160. $items = $this->model->onlyTrashed()->where('id', 'in', $id)->select();
  161. Db::transaction(function () use ($items) {
  162. $count = 0;
  163. foreach ($items as $item) {
  164. $count += $item->restore();
  165. }
  166. return $count;
  167. });
  168. $this->success('还原成功');
  169. }
  170. /**
  171. * 销毁(支持批量)
  172. *
  173. * @param $id
  174. * @return \think\Response
  175. */
  176. public function destroy($id = null)
  177. {
  178. if (empty($id)) {
  179. $this->error(__('Parameter %s can not be empty', 'id'));
  180. }
  181. if ($id !== 'all') {
  182. $items = $this->model->onlyTrashed()->whereIn('id', $id)->select();
  183. } else {
  184. $items = $this->model->onlyTrashed()->select();
  185. }
  186. $result = Db::transaction(function () use ($items) {
  187. $count = 0;
  188. foreach ($items as $comment) {
  189. // 删除评价
  190. $count += $comment->delete(true);
  191. }
  192. return $count;
  193. });
  194. if ($result) {
  195. $this->success('销毁成功', null, $result);
  196. }
  197. $this->error('销毁失败');
  198. }
  199. }