Comment.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\index\controller\shop;
  3. use addons\shop\model\Comment as CommentModel;
  4. use addons\shop\model\Order;
  5. use app\common\controller\Frontend;
  6. use think\Config;
  7. use think\Db;
  8. class Comment extends Frontend
  9. {
  10. protected $layout = 'default';
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 我的评价
  15. */
  16. public function index()
  17. {
  18. $param = [];
  19. $param['user_id'] = $this->auth->id;
  20. $param['status'] = 'normal';
  21. $commentList = \addons\shop\model\Comment::with('goods')->where($param)->paginate();
  22. $this->view->assign('commentList', $commentList);
  23. $this->view->assign('title', '我的评价');
  24. return $this->view->fetch();
  25. }
  26. /**
  27. * 发表评价
  28. */
  29. public function post()
  30. {
  31. $order_sn = $this->request->request('orderid');
  32. $order = Order::with(['OrderGoods'])
  33. ->where('order_sn', $order_sn)
  34. ->where('orderstate', 0)
  35. ->where('shippingstate', 2)
  36. ->where('paystate', 1)
  37. ->where('user_id', $this->auth->id)->find();
  38. if (!$order) {
  39. $this->error('未找到可评论的订单');
  40. }
  41. $row = CommentModel::where('user_id', $this->auth->id)->where('order_id', $order->id)->find();
  42. if ($row) {
  43. $this->error('订单已评论');
  44. }
  45. if ($this->request->isPost()) {
  46. $pid = $this->request->post('pid/d', 0);
  47. $pid = 0;
  48. $remark = $this->request->post('remark/a', '', 'trim,xss_clean');
  49. if (empty($remark) || !is_array($remark)) {
  50. $this->error('评论内容不能为空');
  51. }
  52. $data = [];
  53. $goods_ids = array_column($order->order_goods, 'goods_id');
  54. foreach ($remark as $item) {
  55. if (!isset($item['goods_id'])) {
  56. $this->error('缺少参数goods_id');
  57. }
  58. if (!isset($item['star'])) {
  59. $this->error('缺少评分参数');
  60. }
  61. if (!isset($item['images'])) {
  62. $this->error('缺少参数images');
  63. }
  64. if (!in_array($item['goods_id'], $goods_ids)) {
  65. $this->error('不可越权操作');
  66. }
  67. if (empty($item['content'])) {
  68. $this->error('评论内容不能为空');
  69. }
  70. $data[] = [
  71. 'pid' => $pid,
  72. 'order_id' => $order['id'],
  73. 'user_id' => $this->auth->id,
  74. 'goods_id' => $item['goods_id'],
  75. 'star' => $item['star'],
  76. 'content' => $item['content'],
  77. 'images' => $item['images'],
  78. 'ip' => request()->ip(),
  79. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  80. 'status' => 'hidden'
  81. ];
  82. }
  83. Db::startTrans();
  84. try {
  85. (new CommentModel())->saveAll($data);
  86. $order->orderstate = 3;
  87. $order->save();
  88. foreach ($order->order_goods as $item) {
  89. $item->save(['commentstate' => 1]);
  90. }
  91. // 提交事务
  92. Db::commit();
  93. } catch (\Exception $e) {
  94. // 回滚事务
  95. Db::rollback();
  96. $this->error('添加评论失败');
  97. }
  98. $this->success('添加评论成功,等待审核!', $order->url);
  99. }
  100. $this->view->assign('orderInfo', $order);
  101. $this->view->assign('title', '我的评价');
  102. return $this->view->fetch();
  103. }
  104. }