Index.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $where = UtilService::getMore([
  39. ['cid', 0],
  40. ['sort', ''],
  41. ['keyword', ''],
  42. ['page', 1],
  43. ['limit', 24]
  44. ], $request);
  45. $data = Product::getProductList($where);
  46. foreach ($data['list'] as $k => $v) {
  47. if($v['price'] == 0) {
  48. $data['list'][$k]['price'] = round($v['ot_price'] * 1.15, 2);
  49. }
  50. $data['list'][$k]['market_price'] = round($data['list'][$k]['price'] * 1.2, 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 && $max > $data['list'][$k]['market_price'])
  66. $data['list'][$k]['market_price'] = $max;
  67. }
  68. if($v['label'] != ""){
  69. $data['list'][$k]['label'] = explode('、',$v['label']);
  70. }else{
  71. $data['list'][$k]['label'] = [];
  72. }
  73. $data['list'][$k]['saler'] = random_int(1, 100);
  74. }
  75. return app('json')->success($data);
  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['saler'] = random_int(1, 100);
  88. $storeInfo['slider_image'] = json_decode($storeInfo['slider_image'], true);
  89. $storeInfo['detail_image'] = json_decode($storeInfo['detail_image'], true);
  90. $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/h5/pages/product/detail?id=' . $id);
  91. //替换windows服务器下正反斜杠问题导致图片无法显示
  92. $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
  93. return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
  94. }, $storeInfo['description']);
  95. if($storeInfo['price'] == 0){
  96. $storeInfo['price'] = round($storeInfo['ot_price'] * 1.15, 2);
  97. }
  98. $storeInfo['market_price'] = round($storeInfo['price'] * 1.3, 2);
  99. $score = Db::name("product_score")->where('product_id', $id)->find();
  100. if($score) {
  101. $attr = json_decode($score['attribute'], true);
  102. $max = $attr[2]['taobao'];
  103. if($attr[2]['t1688'] > $max)
  104. $max = $attr[2]['t1688'];
  105. if($attr[2]['pdd'] > $max)
  106. $max = $attr[2]['pdd'];
  107. if($attr[2]['dy'] > $max)
  108. $max = $attr[2]['dy'];
  109. if($attr[2]['ks'] > $max)
  110. $max = $attr[2]['ks'];
  111. if($attr[2]['other'] > $max)
  112. $max = $attr[2]['other'];
  113. if($max && $max > $storeInfo['market_price'])
  114. $storeInfo['market_price'] = $max;
  115. }
  116. $data['storeInfo'] = $storeInfo;
  117. list($productAttr, $productValue, $sku) = ProductAttr::getProductAttrDetail($id, 0, 0);
  118. $data['productAttr'] = $productAttr;
  119. $prices = array_column($productValue, 'price');
  120. array_multisort($prices, SORT_ASC, SORT_NUMERIC, $productValue);
  121. $keys = array_keys($productValue);
  122. $productValue = array_combine($keys, $productValue);
  123. $data['productValue'] = $productValue;
  124. $data['sku'] = $sku;
  125. $data['priceName'] = 0;
  126. $data['good_list'] = Product::getGoodList(18, 'image,store_name,price,id,ot_price,stock,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,unit_name');
  127. return app('json')->successful($data);
  128. }
  129. }