Index.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/h5/pages/product/detail?id=' . $id);
  64. //替换windows服务器下正反斜杠问题导致图片无法显示
  65. $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
  66. return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
  67. }, $storeInfo['description']);
  68. if($storeInfo['price'] == 0){
  69. $storeInfo['price'] = round($storeInfo['ot_price'] * 1.15, 2);
  70. }
  71. $data['storeInfo'] = $storeInfo;
  72. list($productAttr, $productValue) = ProductAttr::getProductAttrDetail($id, 0, 0);
  73. $data['productAttr'] = $productAttr;
  74. $prices = array_column($productValue, 'price');
  75. array_multisort($prices, SORT_ASC, SORT_NUMERIC, $productValue);
  76. $keys = array_keys($productValue);
  77. $productValue = array_combine($keys, $productValue);
  78. $data['productValue'] = $productValue;
  79. $data['priceName'] = 0;
  80. $data['good_list'] = Product::getGoodList(18, 'image,store_name,price,id,ot_price');
  81. return app('json')->successful($data);
  82. }
  83. }