Comment.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\index\controller;
  13. use app\index\model\Order as OrderModel;
  14. use app\index\model\Comment as CommentModel;
  15. use app\index\model\OrderGoods as OrderGoodsModel;
  16. use think\facade\Session;
  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 \think\response\Json
  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. $userId = Session::get('user_id');
  60. if (empty($userId)) {
  61. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  62. }
  63. // 订单信息
  64. $orderInfo = OrderModel::getDetail($orderId);
  65. // 验证订单是否已完成
  66. $model = new CommentModel;
  67. if (!$model->checkOrderAllowComment($orderInfo)) {
  68. return $this->renderError($model->getError());
  69. }
  70. // 待评价商品列表
  71. $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId);
  72. if ($goodsList->isEmpty()) {
  73. return $this->renderError('该订单没有可评价的商品');
  74. }
  75. // 提交商品评价
  76. $model = new CommentModel;
  77. if ($model->increased($orderInfo, $goodsList, $this->postForm())) {
  78. return $this->renderSuccess([], '评价发表成功');
  79. }
  80. return $this->renderError($model->getError() ?: '评价发表失败');
  81. }
  82. }