Comment.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /**
  9. * 评论
  10. */
  11. class Comment extends Base
  12. {
  13. protected $noNeedLogin = ['index'];
  14. /**
  15. * 评论列表
  16. */
  17. public function index()
  18. {
  19. $list = CommentModel::getCommentList($this->request->param());
  20. foreach ($list as $item) {
  21. if ($item->user) {
  22. $item->user->avatar = cdnurl($item->user->avatar, true);
  23. $item->user->visible(explode(',', 'id,nickname,avatar'));
  24. }
  25. $item->hidden(explode(',', 'subscribe,status,ip,useragent'));
  26. }
  27. $this->success('获取成功', $list);
  28. }
  29. //添加评论
  30. public function add()
  31. {
  32. $pid = $this->request->post('pid/d', 0);
  33. $order_sn = $this->request->post('order_sn');
  34. $remark = $this->request->post('remark/a', '', 'trim,xss_clean');
  35. if (empty($remark) || !is_array($remark)) {
  36. $this->error('评论内容不能为空');
  37. }
  38. $order = Order::with(['OrderGoods'])
  39. ->where('order_sn', $order_sn)
  40. ->where('orderstate', 0)
  41. ->where('shippingstate', 2)
  42. ->where('paystate', 1)
  43. ->where('user_id', $this->auth->id)->find();
  44. if (!$order) {
  45. $this->error('未找到可评论的订单');
  46. }
  47. $row = CommentModel::where('user_id', $this->auth->id)->where('order_id', $order->id)->find();
  48. if ($row) {
  49. $this->error('订单已评论');
  50. }
  51. $data = [];
  52. $goods_ids = [];
  53. //可以评价的商品
  54. foreach ($order->order_goods as $item) {
  55. if (in_array($item['salestate'], [0, 6])) {
  56. $goods_ids[] = $item['goods_id'];
  57. }
  58. }
  59. foreach ($remark as $item) {
  60. if (!isset($item['goods_id'])) {
  61. $this->error('缺少参数goods_id');
  62. }
  63. if (!isset($item['star'])) {
  64. $this->error('缺少评分参数');
  65. }
  66. if (!isset($item['images'])) {
  67. $this->error('缺少参数images');
  68. }
  69. if (!in_array($item['goods_id'], $goods_ids)) {
  70. $this->error('存在不可评价的商品');
  71. }
  72. if (empty($item['content'])) {
  73. $this->error('评论内容不能为空');
  74. }
  75. $data[] = [
  76. 'pid' => $pid,
  77. 'order_id' => $order['id'],
  78. 'user_id' => $this->auth->id,
  79. 'goods_id' => $item['goods_id'],
  80. 'star' => $item['star'],
  81. 'content' => $item['content'],
  82. 'images' => $item['images'],
  83. 'ip' => request()->ip(),
  84. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  85. 'status' => 'hidden'
  86. ];
  87. }
  88. Db::startTrans();
  89. try {
  90. (new CommentModel())->saveAll($data);
  91. $order->orderstate = 3;
  92. $order->save();
  93. foreach ($order->order_goods as $item) {
  94. $item->save(['commentstate' => 1]);
  95. }
  96. //是否有积分
  97. $config = get_addon_config('shop');
  98. if ($config['comment_score'] > 0) {
  99. \app\common\model\User::score($config['comment_score'], $this->auth->id, '评论订单赠送' . $config['comment_score'] . '积分');
  100. }
  101. //结束,订单完成,给积分
  102. if (isset($config['money_score']) && $config['money_score'] > 0 && $order->shippingstate == 2 && $order->paystate == 1) {
  103. //减去退款金额
  104. $refund = OrderAftersales::where('order_id', $order->id)->where('type', '<>', 3)->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. }