123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\index\controller;
- use app\index\model\Order as OrderModel;
- use app\index\model\Comment as CommentModel;
- use app\index\model\OrderGoods as OrderGoodsModel;
- use think\facade\Session;
- /**
- * 订单评价管理
- * Class Comment
- * @package app\api\controller\order
- */
- class Comment extends Controller
- {
- /**
- * 待评价订单商品列表
- * @param int $orderId
- * @return \think\response\Json
- * @throws \Exception
- * @throws \app\common\exception\BaseException
- * @throws \think\exception\DbException
- */
- public function list(int $orderId)
- {
- // 订单信息
- $orderInfo = OrderModel::getDetail($orderId);
- // 验证订单是否已完成
- $model = new CommentModel;
- if (!$model->checkOrderAllowComment($orderInfo)) {
- return $this->renderError($model->getError());
- }
- // 待评价商品列表
- $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId);
- if ($goodsList->isEmpty()) {
- return $this->renderError('该订单没有可评价的商品');
- }
- return $this->renderSuccess(compact('goodsList'));
- }
- /**
- * 创建商品评价
- * @param int $orderId
- * @return array|\think\response\Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function submit(int $orderId)
- {
- $userId = Session::get('user_id');
- if (empty($userId)) {
- return $this->renderJson(config('status.not_logged'), 'Log in please!');
- }
- // 订单信息
- $orderInfo = OrderModel::getDetail($orderId);
- // 验证订单是否已完成
- $model = new CommentModel;
- if (!$model->checkOrderAllowComment($orderInfo)) {
- return $this->renderError($model->getError());
- }
- // 待评价商品列表
- $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId);
- if ($goodsList->isEmpty()) {
- return $this->renderError('该订单没有可评价的商品');
- }
- // 提交商品评价
- $model = new CommentModel;
- if ($model->increased($orderInfo, $goodsList, $this->postForm())) {
- return $this->renderSuccess([], '评价发表成功');
- }
- return $this->renderError($model->getError() ?: '评价发表失败');
- }
- /**
- * 商品评价列表
- * @param int $goodsId 商品ID
- * @param int|null $scoreType 评价评分
- * @return \think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function listPage(int $goodsId, int $scoreType = null)
- {
- // 评价列表
- $model = new CommentModel;
- $list = $model->getCommentList($goodsId, $scoreType);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 商品评分总数
- * @param int $goodsId
- * @return array|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function total(int $goodsId)
- {
- // 指定评分总数
- $model = new CommentModel;
- $total = $model->getTotal($goodsId);
- return $this->renderSuccess(compact('total'));
- }
- /**
- * 商品评价列表 (限制数量, 用于商品详情页展示)
- * @param int $goodsId
- * @param int $limit
- * @return array|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function listRows(int $goodsId, int $limit = 5)
- {
- // 评价列表
- $model = new CommentModel;
- $list = $model->listRows($goodsId, $limit);
- // 评价总数量
- $total = $model->rowsTotal($goodsId);
- return $this->renderSuccess(compact('list', 'total'));
- }
- }
|