Comment.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\controller\order;
  13. use app\api\controller\Controller;
  14. use app\api\model\Order as OrderModel;
  15. use app\api\model\Comment as CommentModel;
  16. use app\api\model\OrderGoods as OrderGoodsModel;
  17. /**
  18. * 订单评价管理
  19. * Class Comment
  20. * @package app\api\controller\order
  21. */
  22. class Comment extends Controller
  23. {
  24. /**
  25. * 待评价订单商品列表
  26. * @param int $orderId
  27. * @return array
  28. * @throws \Exception
  29. * @throws \app\common\exception\BaseException
  30. * @throws \think\exception\DbException
  31. */
  32. public function list(int $orderId)
  33. {
  34. // 订单信息
  35. $orderInfo = OrderModel::getDetail($orderId);
  36. // 验证订单是否已完成
  37. $model = new CommentModel;
  38. if (!$model->checkOrderAllowComment($orderInfo)) {
  39. return $this->renderError($model->getError());
  40. }
  41. // 待评价商品列表
  42. $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId);
  43. if ($goodsList->isEmpty()) {
  44. return $this->renderError('该订单没有可评价的商品');
  45. }
  46. return $this->renderSuccess(compact('goodsList'));
  47. }
  48. /**
  49. * 创建商品评价
  50. * @param int $orderId
  51. * @return array|\think\response\Json
  52. * @throws \app\common\exception\BaseException
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function submit(int $orderId)
  58. {
  59. // 订单信息
  60. $orderInfo = OrderModel::getDetail($orderId);
  61. // 验证订单是否已完成
  62. $model = new CommentModel;
  63. if (!$model->checkOrderAllowComment($orderInfo)) {
  64. return $this->renderError($model->getError());
  65. }
  66. // 待评价商品列表
  67. $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId);
  68. if ($goodsList->isEmpty()) {
  69. return $this->renderError('该订单没有可评价的商品');
  70. }
  71. // 提交商品评价
  72. $model = new CommentModel;
  73. if ($model->increased($orderInfo, $goodsList, $this->postForm())) {
  74. return $this->renderSuccess([], '评价发表成功');
  75. }
  76. return $this->renderError($model->getError() ?: '评价发表失败');
  77. }
  78. }