Product.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\api\controller\product;
  3. use app\api\model\product\Product as ProductModel;
  4. use app\api\model\order\Cart as CartModel;
  5. use app\api\controller\Controller;
  6. use app\api\model\settings\Setting as SettingModel;
  7. use app\api\service\common\RecommendService;
  8. use app\common\service\qrcode\ProductService;
  9. use app\common\model\user\Favorite as FavoriteModel;
  10. /**
  11. * 商品控制器
  12. */
  13. class Product extends Controller
  14. {
  15. /**
  16. * 商品列表
  17. */
  18. public function lists()
  19. {
  20. // 整理请求的参数
  21. $param = array_merge($this->postData(), [
  22. 'product_status' => 10
  23. ]);
  24. // 获取列表数据
  25. $model = new ProductModel;
  26. $list = $model->getList($param, $this->getUser(false));
  27. return $this->renderSuccess('', compact('list'));
  28. }
  29. /**
  30. * 推荐产品
  31. */
  32. public function recommendProduct($location)
  33. {
  34. $recommend = SettingModel::getItem('recommend');
  35. $model = new ProductModel;
  36. $is_recommend = RecommendService::checkRecommend($recommend, $location);
  37. $list = [];
  38. if($is_recommend){
  39. $list = $model->getRecommendProduct($recommend);
  40. }
  41. return $this->renderSuccess('', compact('list', 'recommend', 'is_recommend'));
  42. }
  43. /**
  44. * 获取商品详情
  45. */
  46. public function detail($product_id, $url = '')
  47. {
  48. // 用户信息
  49. $user = $this->getUser(false);
  50. // 商品详情
  51. $model = new ProductModel;
  52. $product = $model->getDetails($product_id, $this->getUser(false));
  53. if ($product === false) {
  54. return $this->renderError($model->getError() ?: '商品信息不存在');
  55. }
  56. // 多规格商品sku信息
  57. $specData = $product['spec_type'] == 20 ? $model->getManySpecData($product['spec_rel'], $product['sku']) : null;
  58. return $this->renderSuccess('', [
  59. // 商品详情
  60. 'detail' => $product,
  61. // 购物车商品总数量
  62. 'cart_total_num' => $user ? (new CartModel($user))->getProductNum() : 0,
  63. // 多规格商品sku信息
  64. 'specData' => $specData,
  65. // 是否收藏
  66. 'is_fav' => $user ? FavoriteModel::isFav($product_id, $user['user_id']) : false,
  67. // 微信公众号分享参数
  68. 'share' => $this->getShareParams($url, $product['product_name'], $product['product_name'], '/pages/product/detail/detail', $product['image'][0]['file_path']),
  69. ]);
  70. }
  71. /**
  72. * 生成商品海报
  73. */
  74. public function poster($product_id, $source)
  75. {
  76. // 商品详情
  77. $detail = ProductModel::detail($product_id);
  78. $Qrcode = new ProductService($detail, $this->getUser(false), $source);
  79. return $this->renderSuccess('', [
  80. 'qrcode' => $Qrcode->getImage(),
  81. ]);
  82. }
  83. }