Index.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  50. return app('json')->success($list);
  51. }
  52. /**
  53. * 商品详情
  54. * @param Request $request
  55. * @param $id
  56. * @param int $type
  57. * @return mixed
  58. */
  59. public function productDetail(Request $request, $id)
  60. {
  61. if (!$id || !($storeInfo = Product::getValidProduct($id))) return app('json')->fail('商品不存在或已下架');
  62. $storeInfo['slider_image'] = json_decode($storeInfo['slider_image'], true);
  63. $storeInfo['detail_image'] = json_decode($storeInfo['detail_image'], true);
  64. $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/h5/pages/product/detail?id=' . $id);
  65. //替换windows服务器下正反斜杠问题导致图片无法显示
  66. $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
  67. return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
  68. }, $storeInfo['description']);
  69. if($storeInfo['price'] == 0){
  70. $storeInfo['price'] = round($storeInfo['ot_price'] * 1.15, 2);
  71. }
  72. $data['storeInfo'] = $storeInfo;
  73. list($productAttr, $productValue) = ProductAttr::getProductAttrDetail($id, 0, 0);
  74. $data['productAttr'] = $productAttr;
  75. $prices = array_column($productValue, 'price');
  76. array_multisort($prices, SORT_ASC, SORT_NUMERIC, $productValue);
  77. $keys = array_keys($productValue);
  78. $productValue = array_combine($keys, $productValue);
  79. $data['productValue'] = $productValue;
  80. $data['priceName'] = 0;
  81. $data['good_list'] = Product::getGoodList(18, 'image,store_name,price,id,ot_price,stock,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,unit_name');
  82. return app('json')->successful($data);
  83. }
  84. }