Index.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\BaseController;
  4. use app\model\api\Product;
  5. use app\model\system\Category as CategoryModel;
  6. use app\model\system\ProductAttr;
  7. use app\Request;
  8. use library\services\UtilService;
  9. use library\services\QrcodeService;
  10. use think\facade\Db;
  11. class Index extends BaseController
  12. {
  13. /**
  14. * 获取产品分类
  15. */
  16. public function getCategory(){
  17. $category = new CategoryModel();
  18. $menus = $category->getArMenu('',0,1);
  19. return app('json')->success($menus);
  20. }
  21. /**
  22. * 获取产品标签
  23. */
  24. public function getKeyword($cid){
  25. $data = Product::where('is_del', 0)->where('is_show', 1)->where('cate_id',$cid)->where('keyword','<>',"")->group('keyword')->column('keyword');
  26. $keyword = [];
  27. foreach ($data as $k => $v) {
  28. $arr = explode('、',$v);
  29. $keyword = array_merge($keyword, $arr);
  30. }
  31. $keyword = array_unique($keyword);
  32. return app('json')->success($keyword);
  33. }
  34. /**
  35. * 获取产品列表
  36. */
  37. public function productList(Request $request){
  38. $data = UtilService::getMore([
  39. ['cid', 0],
  40. ['sort', ''],
  41. ['keyword', ''],
  42. ['page', 1],
  43. ['limit', 24]
  44. ], $request);
  45. $list = Product::getProductList($data);
  46. foreach ($list as $k => $v) {
  47. if($v['price'] == 0) {
  48. $list[$k]['price'] = round($v['ot_price'] * 1.15, 2);
  49. }
  50. $list[$k]['market_price'] = round($list[$k]['price'] * 1.3, 2);
  51. $score = Db::name("product_score")->where('product_id', $v['id'])->find();
  52. if($score) {
  53. $attr = json_decode($score['attribute'], true);
  54. $max = $attr[2]['taobao'];
  55. if($attr[2]['t1688'] > $max)
  56. $max = $attr[2]['t1688'];
  57. if($attr[2]['pdd'] > $max)
  58. $max = $attr[2]['pdd'];
  59. if($attr[2]['dy'] > $max)
  60. $max = $attr[2]['dy'];
  61. if($attr[2]['ks'] > $max)
  62. $max = $attr[2]['ks'];
  63. if($attr[2]['other'] > $max)
  64. $max = $attr[2]['other'];
  65. if($max)
  66. $list[$k]['market_price'] = $max;
  67. }
  68. if($v['label'] != ""){
  69. $list[$k]['label'] = explode('、',$v['label']);
  70. }else{
  71. $list[$k]['label'] = [];
  72. }
  73. $list[$k]['saler'] = random_int(1, 100);
  74. }
  75. return app('json')->success($list);
  76. }
  77. /**
  78. * 商品详情
  79. * @param Request $request
  80. * @param $id
  81. * @param int $type
  82. * @return mixed
  83. */
  84. public function productDetail(Request $request, $id)
  85. {
  86. if (!$id || !($storeInfo = Product::getValidProduct($id))) return app('json')->fail('商品不存在或已下架');
  87. $storeInfo['slider_image'] = json_decode($storeInfo['slider_image'], true);
  88. $storeInfo['detail_image'] = json_decode($storeInfo['detail_image'], true);
  89. $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/h5/pages/product/detail?id=' . $id);
  90. //替换windows服务器下正反斜杠问题导致图片无法显示
  91. $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
  92. return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
  93. }, $storeInfo['description']);
  94. if($storeInfo['price'] == 0){
  95. $storeInfo['price'] = round($storeInfo['ot_price'] * 1.15, 2);
  96. }
  97. $storeInfo['market_price'] = round($storeInfo['price'] * 1.3, 2);
  98. $score = Db::name("product_score")->where('product_id', $id)->find();
  99. if($score) {
  100. $attr = json_decode($score['attribute'], true);
  101. $max = $attr[2]['taobao'];
  102. if($attr[2]['t1688'] > $max)
  103. $max = $attr[2]['t1688'];
  104. if($attr[2]['pdd'] > $max)
  105. $max = $attr[2]['pdd'];
  106. if($attr[2]['dy'] > $max)
  107. $max = $attr[2]['dy'];
  108. if($attr[2]['ks'] > $max)
  109. $max = $attr[2]['ks'];
  110. if($attr[2]['other'] > $max)
  111. $max = $attr[2]['other'];
  112. if($max)
  113. $storeInfo['market_price'] = $max;
  114. }
  115. $data['storeInfo'] = $storeInfo;
  116. list($productAttr, $productValue, $sku) = ProductAttr::getProductAttrDetail($id, 0, 0);
  117. $data['productAttr'] = $productAttr;
  118. $prices = array_column($productValue, 'price');
  119. array_multisort($prices, SORT_ASC, SORT_NUMERIC, $productValue);
  120. $keys = array_keys($productValue);
  121. $productValue = array_combine($keys, $productValue);
  122. $data['productValue'] = $productValue;
  123. $data['sku'] = $sku;
  124. $data['priceName'] = 0;
  125. $data['good_list'] = Product::getGoodList(18, 'image,store_name,price,id,ot_price,stock,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,unit_name');
  126. return app('json')->successful($data);
  127. }
  128. }