Comment.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\Comment as CommentModel;
  4. use app\common\model\Order;
  5. use app\common\model\OrderAftersales;
  6. use app\common\model\OrderAction;
  7. use think\Db;
  8. use app\common\Enum\OrderEnum;
  9. /**
  10. * 评论
  11. */
  12. class Comment extends Base
  13. {
  14. protected $noNeedLogin = ['index'];
  15. /**
  16. * 评论列表
  17. */
  18. public function index()
  19. {
  20. $list = CommentModel::getCommentList($this->request->param());
  21. foreach ($list as $item) {
  22. if ($item->user) {
  23. $item->user->avatar = cdnurl($item->user->avatar, true);
  24. $item->user->visible(explode(',', 'id,nickname,avatar'));
  25. }
  26. $item->hidden(explode(',', 'subscribe,status,ip,useragent'));
  27. }
  28. $this->success('获取成功', $list);
  29. }
  30. //添加评论
  31. public function add()
  32. {
  33. $pid = $this->request->post('pid/d', 0);
  34. $order_id = $this->request->post('order_id/d');
  35. $remark = $this->request->post('remark/a', '', 'trim,xss_clean');
  36. if (empty($remark) || !is_array($remark)) {
  37. $this->error('评论内容不能为空');
  38. }
  39. $order = Order::with(['OrderGoods'])
  40. ->where('id', $order_id)
  41. ->where('order_status', OrderEnum::STATUS_CONFIRM)
  42. ->where('user_id', $this->auth->id)->find();
  43. if (!$order) {
  44. $this->error('未找到可评论的订单');
  45. }
  46. $row = CommentModel::where('user_id', $this->auth->id)->where('order_id', $order->id)->find();
  47. if ($row) {
  48. $this->error('订单已评论');
  49. }
  50. $data = [];
  51. $goods_ids = [];
  52. //可以评价的商品
  53. foreach ($order->order_goods as $item) {
  54. if (in_array($item['sale_status'], [0, 6])) {
  55. $goods_ids[] = $item['goods_id'];
  56. }
  57. }
  58. foreach ($remark as $item) {
  59. if (!isset($item['goods_id'])) {
  60. $this->error('缺少参数goods_id');
  61. }
  62. if (!isset($item['star'])) {
  63. $this->error('缺少评分参数');
  64. }
  65. if (!isset($item['images'])) {
  66. $this->error('缺少参数images');
  67. }
  68. if (!in_array($item['goods_id'], $goods_ids)) {
  69. $this->error('存在不可评价的商品');
  70. }
  71. if (empty($item['content'])) {
  72. $this->error('评论内容不能为空');
  73. }
  74. $data[] = [
  75. 'pid' => $pid,
  76. 'order_id' => $order['id'],
  77. 'user_id' => $this->auth->id,
  78. 'goods_id' => $item['goods_id'],
  79. 'star' => $item['star'],
  80. 'content' => $item['content'],
  81. 'images' => $item['images'],
  82. 'ip' => request()->ip(),
  83. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  84. 'status' => 'hidden'
  85. ];
  86. }
  87. Db::startTrans();
  88. // try {
  89. (new CommentModel())->saveAll($data);
  90. $order->order_status = OrderEnum::STATUS_COMMENT;
  91. $order->save();
  92. foreach ($order->order_goods as $item) {
  93. $item->save(['comment_status' => 1]);
  94. }
  95. // //是否有积分
  96. // $config = get_addon_config('shop');
  97. // if ($config['comment_score'] > 0) {
  98. // \app\common\model\User::score($config['comment_score'], $this->auth->id, '评论订单赠送' . $config['comment_score'] . '积分');
  99. // }
  100. // //结束,订单完成,给积分
  101. // if (isset($config['money_score']) && $config['money_score'] > 0 && $order->shippingstate == 2 && $order->paystate == 1) {
  102. // //减去退款金额
  103. // $refund = OrderAftersales::where('order_id', $order->id)->where('type', '<>', 3)
  104. // ->where('status', 2)->sum('refund');
  105. // $money = bcsub($order['payamount'], $refund, 2);
  106. // if ($money > 0) {
  107. // $score = bcmul($money, $config['money_score']);
  108. // \app\common\model\User::score($score, $this->auth->id, '完成订单奖励' . $score . '积分');
  109. // }
  110. // }
  111. // 提交事务
  112. Db::commit();
  113. // } catch (\Exception $e) {
  114. // // 回滚事务
  115. // Db::rollback();
  116. // $this->error('添加评论失败');
  117. // }
  118. OrderAction::push($order->order_sn, '系统', '订单已完成');
  119. $this->success('添加评论成功,等待审核!');
  120. }
  121. //我的评价
  122. public function myList()
  123. {
  124. $list = CommentModel::with([
  125. 'Goods' => function ($query) {
  126. $query->field('id,title,image');
  127. }
  128. ])->where('user_id', $this->auth->id)->where('pid', 0)->where('status', 'normal')->order('createtime desc')->paginate(10);
  129. foreach ($list as $item) {
  130. $item->hidden(['ip', 'subscribe', 'useragent', 'comments']);
  131. }
  132. $this->success('获取成功', $list);
  133. }
  134. //回复评论
  135. // public function reply()
  136. // {
  137. // $pid = $this->request->post('pid');
  138. // $content = $this->request->post('content');
  139. // if (!$content) {
  140. // $this->error('回复内容不能为空');
  141. // }
  142. // $row = CommentModel::where('id', $pid)->where('status', 'normal')->find();
  143. // if (!$row) {
  144. // $this->error('未找到记录');
  145. // }
  146. // $row->setInc('comments');
  147. // CommentModel::create([
  148. // 'pid' => $pid,
  149. // 'order_id' => $row->order_id,
  150. // 'user_id' => $this->auth->id,
  151. // 'goods_id' => $row->goods_id,
  152. // 'star' => 0,
  153. // 'content' => $content,
  154. // 'ip' => request()->ip(),
  155. // 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  156. // 'status' => 'hidden'
  157. // ]);
  158. // $this->success('提交回复成功');
  159. // }
  160. }