Product.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api\controller;
  4. use app\BaseController;
  5. use app\model\api\StoreCategory;
  6. use app\model\api\StoreBrand;
  7. use app\model\api\StoreProduct;
  8. use app\model\api\StoreProductAttrValue;
  9. use app\model\api\StoreProductReply;
  10. use app\Request;
  11. use library\services\UtilService;
  12. use think\facade\Db;
  13. class Product extends BaseController
  14. {
  15. /**
  16. * 获取分类列表
  17. * @param Request $request
  18. */
  19. public function categoryList(Request $request)
  20. {
  21. $post = UtilService::getMore([
  22. ['page', 1],
  23. ['pageSize', 20],
  24. ['pid', 0],
  25. ], $request);
  26. $where = [];
  27. if ($post['pid'] > 0) {
  28. $where[] = ['pid', '=', $post['pid']];
  29. }
  30. $result = (new StoreCategory())->getList($where, $post['page'], $post['pageSize']);
  31. return app('json')->success($result);
  32. }
  33. /**
  34. * 获取分类树
  35. */
  36. public function categoryTree()
  37. {
  38. $tree = (new StoreCategory())->getTree();
  39. return app('json')->success($tree);
  40. }
  41. /**
  42. * 获取品牌列表
  43. * @param Request $request
  44. */
  45. public function brandList(Request $request)
  46. {
  47. $post = UtilService::getMore([
  48. ['page', 1],
  49. ['pageSize', 20],
  50. ], $request);
  51. $result = (new StoreBrand())->getList([], $post['page'], $post['pageSize']);
  52. return app('json')->success($result);
  53. }
  54. /**
  55. * 获取商品列表
  56. * @param Request $request
  57. */
  58. public function productList(Request $request)
  59. {
  60. $post = UtilService::getMore([
  61. ['page', 1],
  62. ['pageSize', 20],
  63. ['category_id', ''],
  64. ['brand_id', ''],
  65. ['keyword', ''],
  66. ['sort', ''],
  67. ['order', ''],
  68. ], $request);
  69. $where = [
  70. ['is_show', '=', 1]
  71. ];
  72. if (!empty($post['category_id'])) {
  73. $where[] = ['category_id', '=', $post['category_id']];
  74. }
  75. if (!empty($post['brand_id'])) {
  76. $where[] = ['brand_id', '=', $post['brand_id']];
  77. }
  78. if (!empty($post['keyword'])) {
  79. $where[] = ['title', 'like', '%' . $post['keyword'] . '%'];
  80. }
  81. $orderField = $post['sort'] ?: 'id';
  82. $orderType = $post['order'] ?: 'desc';
  83. $query = (new StoreProduct())->where($where);
  84. $totalCount = $query->count();
  85. $page = max(1, (int)$post['page']);
  86. $pageSize = max(1, (int)$post['pageSize']);
  87. $list = $query
  88. ->order($orderField, $orderType)
  89. ->page($page, $pageSize)
  90. ->select()
  91. ->toArray();
  92. return app('json')->success([
  93. 'list' => $list,
  94. 'totalCount' => $totalCount,
  95. 'pageSize' => $pageSize,
  96. 'page' => $page,
  97. ]);
  98. }
  99. /**
  100. * 获取商品详情
  101. * @param Request $request
  102. */
  103. public function productDetail(Request $request)
  104. {
  105. $post = UtilService::getMore([
  106. ['id', '', 'empty', '参数错误'],
  107. ], $request);
  108. $detail = (new StoreProduct())->getDetail($post['id']);
  109. if (!$detail) {
  110. return app('json')->fail('商品不存在');
  111. }
  112. return app('json')->success($detail);
  113. }
  114. /**
  115. * 获取商品评价列表
  116. * @param Request $request
  117. */
  118. public function replyList(Request $request)
  119. {
  120. $post = UtilService::getMore([
  121. ['page', 1],
  122. ['pageSize', 20],
  123. ['product_id', ''],
  124. ], $request);
  125. $where = [];
  126. if (!empty($post['product_id'])) {
  127. $where[] = ['product_id', '=', $post['product_id']];
  128. }
  129. $result = (new StoreProductReply())->getList($where, $post['page'], $post['pageSize']);
  130. return app('json')->success($result);
  131. }
  132. /**
  133. * 添加商品评价
  134. * @param Request $request
  135. */
  136. public function addReply(Request $request)
  137. {
  138. $post = UtilService::getMore([
  139. ['product_id', '', 'empty', '参数错误'],
  140. ['order_id', '', 'empty', '参数错误'],
  141. ['score', 5],
  142. ['comment', ''],
  143. ['pics', ''],
  144. ], $request);
  145. $data = [
  146. 'uid' => $request->user['uid'],
  147. 'product_id' => $post['product_id'],
  148. 'order_id' => $post['order_id'],
  149. 'score' => $post['score'],
  150. 'comment' => $post['comment'],
  151. 'pics' => $post['pics'],
  152. 'time' => time(),
  153. ];
  154. $id = Db::name('store_product_reply')->insertGetId($data);
  155. if ($id) {
  156. // 更新商品评价数
  157. Db::name('store_product')->where('id', $post['product_id'])->inc('reply_count', 1)->update();
  158. return app('json')->success('评价成功');
  159. } else {
  160. return app('json')->fail('评价失败');
  161. }
  162. }
  163. }