ProductController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\pc;
  12. use app\Request;
  13. use app\services\pc\ProductServices;
  14. use app\services\product\product\StoreCategoryServices;
  15. use app\services\product\product\StoreProductServices;
  16. class ProductController
  17. {
  18. protected $services;
  19. public function __construct(ProductServices $services)
  20. {
  21. $this->services = $services;
  22. }
  23. /**
  24. * 获取商品列表
  25. * @param Request $request
  26. * @param StoreCategoryServices $services
  27. * @return mixed
  28. */
  29. public function getProductList(Request $request, StoreCategoryServices $services)
  30. {
  31. $where = $request->getMore([
  32. [['sid', 'd'], 0],
  33. [['cid', 'd'], 0],
  34. ['keyword', '', '', 'store_name'],
  35. ['priceOrder', ''],
  36. ['salesOrder', ''],
  37. [['news', 'd'], 0, '', 'timeOrder'],
  38. [['type', 0], 0],
  39. ['ids', ''],
  40. ['selectId', '']
  41. ]);
  42. if ($where['selectId'] && (!$where['sid'] || !$where['cid'])) {
  43. if ($services->value(['id' => $where['selectId']], 'pid')) {
  44. $where['sid'] = $where['selectId'];
  45. } else {
  46. $where['cid'] = $where['selectId'];
  47. }
  48. }
  49. if ($where['ids'] && is_string($where['ids'])) {
  50. $where['ids'] = explode(',', $where['ids']);
  51. }
  52. if (!$where['ids']) {
  53. unset($where['ids']);
  54. }
  55. return app('json')->success($this->services->getProductList($where, $request->uid()));
  56. }
  57. /**
  58. * PC端商品详情小程序码
  59. * @param Request $request
  60. * @return mixed
  61. */
  62. public function getProductRoutineCode(Request $request)
  63. {
  64. list($product_id, $type) = $request->getMore([
  65. ['product_id', 0],
  66. ['type', 'product'],
  67. ], true);
  68. $routineCode = $this->services->getProductRoutineCode((int)$product_id, $type);
  69. return app('json')->success(['routineCode' => $routineCode]);
  70. }
  71. /**
  72. * 推荐商品
  73. * @param Request $request
  74. * @param $type
  75. * @return mixed
  76. */
  77. public function getRecommendList(Request $request, $type)
  78. {
  79. /** @var StoreProductServices $product */
  80. $product = app()->make(StoreProductServices::class);
  81. $data = [];
  82. $data['list'] = [];
  83. $where['is_show'] = 1;
  84. $where['is_del'] = 0;
  85. if ($type == 1) {//TODO 精品推荐
  86. $data['list'] = $product->getRecommendProduct($request->uid(), 'is_best', 0, 'mid');//TODO 精品推荐个数
  87. $where['is_best'] = 1;
  88. } else if ($type == 2) {//TODO 热门榜单
  89. $data['list'] = $product->getRecommendProduct($request->uid(), 'is_hot', 0, 'mid');//TODO 热门榜单 猜你喜欢
  90. $where['is_hot'] = 1;
  91. } else if ($type == 3) {//TODO 首发新品
  92. $data['list'] = $product->getRecommendProduct($request->uid(), 'is_new', 0, 'mid');//TODO 首发新品
  93. $where['is_new'] = 1;
  94. } else if ($type == 4) {//TODO 促销单品
  95. $data['list'] = $product->getRecommendProduct($request->uid(), 'is_benefit', 0, 'mid');//TODO 促销单品
  96. $where['is_benefit'] = 1;
  97. }
  98. foreach ($data['list'] as &$item) {
  99. if (count($item['star'])) {
  100. $item['star'] = bcdiv((string)array_sum(array_column($item['star'], 'product_score')), (string)count($item['star']), 1);
  101. } else {
  102. $item['star'] = '5.0';
  103. }
  104. }
  105. $data['count'] = $product->getCount($where);
  106. return app('json')->success($data);
  107. }
  108. /**
  109. * 获取优品推荐
  110. * @return mixed
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function getGoodProduct()
  116. {
  117. /** @var StoreProductServices $product */
  118. $product = app()->make(StoreProductServices::class);
  119. $list = get_thumb_water($product->getProducts(['is_good' => 1, 'is_del' => 0, 'is_show' => 1]), 'mid');
  120. return app('json')->success(compact('list'));
  121. }
  122. }