Comment.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 think\response\Json;
  14. use app\api\controller\Controller;
  15. use app\api\model\Order as OrderModel;
  16. use app\api\model\Comment as CommentModel;
  17. use app\api\model\OrderGoods as OrderGoodsModel;
  18. /**
  19. * 订单评价管理
  20. * Class Comment
  21. * @package app\api\controller\order
  22. */
  23. class Comment extends Controller
  24. {
  25. /**
  26. * 待评价订单商品列表
  27. * @param int $orderId
  28. * @return Json
  29. * @throws \cores\exception\BaseException
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function list(int $orderId): Json
  35. {
  36. // 订单信息
  37. $orderInfo = OrderModel::getDetail($orderId);
  38. // 验证订单是否已完成
  39. $model = new CommentModel;
  40. if (!$model->checkOrderAllowComment($orderInfo)) {
  41. return $this->renderError($model->getError());
  42. }
  43. // 待评价商品列表
  44. $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId);
  45. if ($goodsList->isEmpty()) {
  46. return $this->renderError('该订单没有可评价的商品');
  47. }
  48. return $this->renderSuccess(compact('goodsList'));
  49. }
  50. /**
  51. * 创建商品评价
  52. * @param int $orderId
  53. * @return Json
  54. * @throws \cores\exception\BaseException
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function submit(int $orderId): Json
  60. {
  61. // 订单信息
  62. $orderInfo = OrderModel::getDetail($orderId);
  63. // 验证订单是否已完成
  64. $model = new CommentModel;
  65. if (!$model->checkOrderAllowComment($orderInfo)) {
  66. return $this->renderError($model->getError());
  67. }
  68. // 待评价商品列表
  69. $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId);
  70. if ($goodsList->isEmpty()) {
  71. return $this->renderError('该订单没有可评价的商品');
  72. }
  73. // 提交商品评价
  74. $model = new CommentModel;
  75. if ($model->increased($orderInfo, $goodsList, $this->postForm())) {
  76. return $this->renderSuccess([], '评价发表成功');
  77. }
  78. return $this->renderError($model->getError() ?: '评价发表失败');
  79. }
  80. }