Comment.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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;
  13. use app\api\model\Comment as CommentModel;
  14. use app\api\service\User as UserService;
  15. use app\common\model\CommentPraise as CommentPraiseModel;
  16. /**
  17. * 商品评价控制器
  18. * Class Comment
  19. * @package app\api\controller
  20. */
  21. class Comment extends Controller
  22. {
  23. /**
  24. * 商品评价列表
  25. * @param int $goodsId 商品ID
  26. * @param int|null $scoreType 评价评分
  27. * @param int $sortType 排序方式 0-时间倒序 1-点赞倒序
  28. * @return array
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function list(int $goodsId, int $scoreType = null, $sortType = 0)
  34. {
  35. // 评价列表
  36. $model = new CommentModel;
  37. $list = $model->getCommentList($goodsId, $scoreType, $sortType);
  38. return $this->renderSuccess(compact('list'));
  39. }
  40. /**
  41. * 商品评分总数
  42. * @param int $goodsId
  43. * @return array|\think\response\Json
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public function total(int $goodsId)
  49. {
  50. // 指定评分总数
  51. $model = new CommentModel;
  52. $total = $model->getTotal($goodsId);
  53. return $this->renderSuccess(compact('total'));
  54. }
  55. /**
  56. * 商品评价列表 (限制数量, 用于商品详情页展示)
  57. * @param int $goodsId
  58. * @param int $limit
  59. * @return array|\think\response\Json
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function listRows(int $goodsId, int $limit = 5)
  65. {
  66. // 评价列表
  67. $model = new CommentModel;
  68. $list = $model->listRows($goodsId, $limit);
  69. // 评价总数量
  70. $total = $model->rowsTotal($goodsId);
  71. return $this->renderSuccess(compact('list', 'total'));
  72. }
  73. public function praise($commentId) {
  74. $userinfo = UserService::getCurrentLoginUser(true);
  75. $one = CommentPraiseModel::where('comment_id', $commentId)->where('user_id', $userinfo->user_id)->find();
  76. $commentPraiseModel = new CommentPraiseModel;
  77. $data = [
  78. 'comment_id' => $commentId,
  79. 'user_id' => $userinfo->user_id
  80. ];
  81. if (empty($one)) {
  82. // 点赞
  83. $commentPraiseModel->addOne($data);
  84. CommentModel::where($data)->inc('praise_num', 1);
  85. CommentModel::setIncByField($commentId, 'praise_num', 1);
  86. return $this->renderSuccess([], '点赞成功');
  87. } else {
  88. // 取消点赞
  89. $commentPraiseModel->deleteOne($data);
  90. CommentModel::setDecByField($commentId, 'praise_num', 1);
  91. return $this->renderSuccess([], '取消点赞成功');
  92. }
  93. }
  94. }