ProductController.php 5.0 KB

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