Index.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. class Index extends BaseController
  11. {
  12. /**
  13. * 获取产品分类
  14. */
  15. public function getCategory(){
  16. $category = new CategoryModel();
  17. $menus = $category->getArMenu('',0,1);
  18. return app('json')->success($menus);
  19. }
  20. /**
  21. * 获取产品标签
  22. */
  23. public function getKeyword($cid){
  24. $data = Product::where('is_del', 0)->where('is_show', 1)->where('cate_id',$cid)->where('keyword','<>',"")->group('keyword')->column('keyword');
  25. $keyword = [];
  26. foreach ($data as $k => $v) {
  27. $arr = explode('、',$v);
  28. $keyword = array_merge($keyword, $arr);
  29. }
  30. $keyword = array_unique($keyword);
  31. return app('json')->success($keyword);
  32. }
  33. /**
  34. * 获取产品列表
  35. */
  36. public function productList(Request $request){
  37. $data = UtilService::getMore([
  38. ['cid', 0],
  39. ['sort', ''],
  40. ['keyword', ''],
  41. ['page', 1],
  42. ['limit', 24]
  43. ], $request);
  44. $list = Product::getProductList($data);
  45. foreach ($list as $k => $v) {
  46. if($v['price'] == 0) {
  47. $list[$k]['price'] = round($v['ot_price'] * 1.15, 2);
  48. }
  49. $list[$k]['market_price'] = round($list[$k]['price'] * 1.15, 2);
  50. if($v['label'] != ""){
  51. $list[$k]['label'] = explode('、',$v['label']);
  52. }else{
  53. $list[$k]['label'] = [];
  54. }
  55. $list[$k]['saler'] = random_int(1, 100);
  56. }
  57. return app('json')->success($list);
  58. }
  59. /**
  60. * 商品详情
  61. * @param Request $request
  62. * @param $id
  63. * @param int $type
  64. * @return mixed
  65. */
  66. public function productDetail(Request $request, $id)
  67. {
  68. if (!$id || !($storeInfo = Product::getValidProduct($id))) return app('json')->fail('商品不存在或已下架');
  69. $storeInfo['slider_image'] = json_decode($storeInfo['slider_image'], true);
  70. $storeInfo['detail_image'] = json_decode($storeInfo['detail_image'], true);
  71. $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/h5/pages/product/detail?id=' . $id);
  72. //替换windows服务器下正反斜杠问题导致图片无法显示
  73. $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
  74. return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
  75. }, $storeInfo['description']);
  76. if($storeInfo['price'] == 0){
  77. $storeInfo['price'] = round($storeInfo['ot_price'] * 1.15, 2);
  78. }
  79. $data['storeInfo'] = $storeInfo;
  80. list($productAttr, $productValue) = ProductAttr::getProductAttrDetail($id, 0, 0);
  81. $data['productAttr'] = $productAttr;
  82. $prices = array_column($productValue, 'price');
  83. array_multisort($prices, SORT_ASC, SORT_NUMERIC, $productValue);
  84. $keys = array_keys($productValue);
  85. $productValue = array_combine($keys, $productValue);
  86. $data['productValue'] = $productValue;
  87. $data['priceName'] = 0;
  88. $data['good_list'] = Product::getGoodList(18, 'image,store_name,price,id,ot_price,stock,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,unit_name');
  89. return app('json')->successful($data);
  90. }
  91. }