src/Controller/Api/ProudctController.php line 102

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Controller\Api\ApiController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use App\Entity\Product;
  6. use App\Entity\ProductCategory;
  7. use App\Entity\UserProductStar;
  8. /**
  9.  * @Route("/api", name="api_")
  10.  * 
  11.  * @IgnoreAnnotation("api")
  12.  * @IgnoreAnnotation("apiName")
  13.  * @IgnoreAnnotation("apiGroup")
  14.  * @IgnoreAnnotation("apiVersion")
  15.  * @IgnoreAnnotation("apiParam")
  16.  * @IgnoreAnnotation("apiSuccessExample")
  17.  * @IgnoreAnnotation("apiErrorExample")
  18.  */
  19. class ProudctController extends ApiController
  20. {       
  21.     /**
  22.      * @Route("/home/category", name="categoryHome", options={"ignore"=true})
  23.      * 
  24.      * @api {post} /api/home/category 金刚区分类
  25.      * @apiName 金刚区分类
  26.      * @apiGroup product
  27.      * @apiVersion 1.0.0
  28.      * 
  29.      * 
  30.      * @apiSuccessExample {json} Success-Response:
  31.      *     HTTP/1.1 200 OK
  32.      *     {
  33.      *       "code": "0",
  34.      *       "message": "",
  35.      *       "result": 
  36.                 [{
  37.                     id: 分类id
  38.                     name: 名称
  39.                     image: 图片   
  40.                 },...]
  41.      * 
  42.      *     }
  43.      * 
  44.      * @apiErrorExample {json} Error-Response:
  45.      *     HTTP/1.1 200 
  46.      *     {
  47.      *       "code": "1",
  48.      *       "message": "",
  49.      *       "result": {}
  50.      *     }
  51.      */
  52.     public function category()
  53.     {    
  54.         $categorys $this->entityManager->getRepository(ProductCategory::class)->createQueryBuilder('u')
  55.             ->select('u.id, u.name, u.image')
  56.             ->andWhere('u.level = 1 and u.isHome = 1')->getQuery()->getResult();
  57.         
  58.         foreach ($categorys as &$category) {
  59.             $category['image'] = $this->request->getSchemeAndHttpHost() . $category['image'];
  60.         }
  61.         return $this->json(['code'=>0'message'=>'success''result'=> $categorys]);
  62.     }
  63.     /**
  64.      * @Route("/home", name="home", options={"ignore"=true})
  65.      * 
  66.      * @api {post} /api/home 首页商品
  67.      * @apiName 首页商品
  68.      * @apiGroup product
  69.      * @apiVersion 1.0.0  
  70.      * 
  71.      * @apiParam {NUmber} [page]    分页 默认1
  72.      * @apiParam {NUmber} [rows]    每页条数 默认10个 
  73.      * 
  74.      * @apiSuccessExample {json} Success-Response:
  75.      *     HTTP/1.1 200 OK
  76.      *     {
  77.      *       "code": "0",
  78.      *       "message": "",
  79.      *       "result": 
  80.                 [{   
  81.                     id:           商品id
  82.                     oldPrice:     原始价格
  83.                     price:        当前价格                
  84.                     name:         商品名称
  85.                     image:        http://domain.com/图片
  86.                 }, ...]
  87.      * 
  88.      *     }
  89.      * 
  90.      * @apiErrorExample {json} Error-Response:
  91.      *     HTTP/1.1 200 
  92.      *     {
  93.      *       "code": "1",
  94.      *       "message": "",
  95.      *       "result": {}
  96.      *     }
  97.      */
  98.     public function home()
  99.     {    
  100.         $page $this->request->request->get('page'1);     
  101.         $rows $this->request->request->get('rows'10);
  102.         $queryBuilder $this->entityManager->getRepository(Product::class)->createQueryBuilder('u')
  103.             ->select('u.id, u.name, u.price, u.oldPrice, u.image')
  104.             ->andWhere('u.isHome = 1 and u.isEnabled = 1');
  105.         
  106.         $pagination $this->paginator->paginate($queryBuilder$page$rows); 
  107.         $products $pagination->getItems();
  108.         
  109.         foreach ($products as &$product) {
  110.             $product['image'] = $this->request->getSchemeAndHttpHost() . $product['image'];
  111.         }
  112.         return $this->json(['code'=>0'message'=>'success''result'=> $products]);
  113.     }
  114.      /**
  115.      * @Route("/category", name="category", options={"ignore"=true})
  116.      * 
  117.      * @api {post} /api/category 分类
  118.      * @apiName 分类
  119.      * @apiGroup product
  120.      * @apiVersion 1.0.0
  121.      * 
  122.      * 
  123.      * @apiSuccessExample {json} Success-Response:
  124.      *     HTTP/1.1 200 OK
  125.         {
  126.         "code": 0,
  127.         "message": "success",
  128.         "result": [
  129.             {
  130.                 "id": 1,
  131.                 "image": "https://laiju.hbpt.xyz/uploads/category/6322cc61907a2.jpg",
  132.                 "name": "猪肉专区",
  133.                 "erji": [  //二级
  134.                     {
  135.                         "id": 4,
  136.                         "image": "https://laiju.hbpt.xyz/uploads/category/6322ccd6b5902.jpg",
  137.                         "name": "猪肉1"
  138.                     },
  139.                     {
  140.                         "id": 5,
  141.                         "image": "https://laiju.hbpt.xyz/uploads/category/6322cce9c3e79.jpg",
  142.                         "name": "猪肉2"
  143.                     }
  144.                 ]
  145.             },
  146.             {
  147.                 "id": 2,
  148.                 "image": "https://laiju.hbpt.xyz/uploads/category/6322cc8b78d64.jpg",
  149.                 "name": "牛肉专区",
  150.                 "erji": []
  151.             },
  152.             {
  153.                 "id": 3,
  154.                 "image": "https://laiju.hbpt.xyz/uploads/category/6322cc95b1da3.jpg",
  155.                 "name": "羊肉专区",
  156.                 "erji": []
  157.             }
  158.         ]
  159.     }
  160.      * 
  161.      * @apiErrorExample {json} Error-Response:
  162.      *     HTTP/1.1 200 
  163.      *     {
  164.      *       "code": "1",
  165.      *       "message": "",
  166.      *       "result": {}
  167.      *     }
  168.      */
  169.     public function categorys()
  170.     {           
  171.         //
  172.         $categorys $this->entityManager->getRepository(ProductCategory::class)->createQueryBuilder('u')
  173.                 ->select('u.id, u.image, u.name')->andWhere('u.level = 1')
  174.                 ->getQuery()->getResult();
  175.         $queryBuilder $this->entityManager->getRepository(ProductCategory::class)->createQueryBuilder('u')
  176.             ->select('u.id, u.image, u.name')->andWhere('u.parent = :category');
  177.         
  178.         foreach ($categorys as &$category) {
  179.             $category['image'] = $this->request->getSchemeAndHttpHost() . $category['image'];
  180.             $erji $queryBuilder->setParameter('category'$category['id'])->getQuery()->getResult();
  181.             foreach ($erji as &$v) {
  182.                 $v['image'] = $this->request->getSchemeAndHttpHost() . $v['image'];                
  183.             }
  184.             $category['erji'] = $erji;
  185.         }      
  186.         return $this->json(['code'=>0'message'=>'success''result'=> $categorys]);
  187.     }
  188.     /**
  189.      * @Route("/category/erji", name="categoryerji", options={"ignore"=true})
  190.      * 
  191.      * @api {post} /api/category/erji 二级分类
  192.      * @apiName 二级分类
  193.      * @apiGroup product
  194.      * @apiVersion 1.0.0
  195.      * 
  196.      * @apiParam {String} category_id  一级分类id
  197.      * 
  198.      * @apiSuccessExample {json} Success-Response:
  199.      *     HTTP/1.1 200 OK
  200.         {
  201.             "code": 0,
  202.             "message": "success",
  203.             "result": [
  204.                 {
  205.                     "id": 4,
  206.                     "image": "https://laiju.hbpt.xyz/uploads/category/6322ccd6b5902.jpg",
  207.                     "name": "猪肉1"
  208.                 },
  209.                 {
  210.                     "id": 5,
  211.                     "image": "https://laiju.hbpt.xyz/uploads/category/6322cce9c3e79.jpg",
  212.                     "name": "猪肉2"
  213.                 }
  214.             ]
  215.         }
  216.      * 
  217.      * @apiErrorExample {json} Error-Response:
  218.      *     HTTP/1.1 200 
  219.      *     {
  220.      *       "code": "1",
  221.      *       "message": "",
  222.      *       "result": {}
  223.      *     }
  224.      */
  225.     public function categoryErji()
  226.     {    
  227.         
  228.         $category_id $this->request->request->get('category_id');
  229.         if (!$category_id) return $this->json(['code'=>1'message'=>'参数不完整']);
  230.         $categorys $this->entityManager->getRepository(ProductCategory::class)->createQueryBuilder('u')
  231.                 ->select('u.id, u.image, u.name')->andWhere('u.parent = :category')->setParameter('category'$category_id)
  232.                 ->getQuery()->getResult();
  233.         
  234.         foreach ($categorys as &$category) {
  235.             $category['image'] = $this->request->getSchemeAndHttpHost() . $category['image'];
  236.         }
  237.        
  238.         return $this->json(['code'=>0'message'=>'success''result'=> $categorys]);
  239.     }
  240.     /**
  241.      * @Route("/category/product", name="categoryProduct", options={"ignore"=true})
  242.      * 
  243.      * @api {post} /api/category/product 分类商品
  244.      * @apiName 分类商品
  245.      * @apiGroup product
  246.      * @apiVersion 1.0.0
  247.      * 
  248.      * @apiParam {String} [category_id]  分类id
  249.      * @apiParam {String} [price]   0从价格由低到高 1由高到低
  250.      * @apiParam {String} [sales]   销量     升序 0 或倒序 1
  251.      * @apiParam {String} [lastest] 最新     0从近到远 1从远到近
  252.      * @apiParam {NUmber} [page]    分页 默认1
  253.      * @apiParam {NUmber} [rows]    每页条数 默认10个
  254.      * 
  255.      * @apiSuccessExample {json} Success-Response:
  256.      *     HTTP/1.1 200 OK
  257.         {
  258.         "code": 0,
  259.         "message": "success",
  260.         "result": [
  261.             {
  262.                 "id": 3,
  263.                 "oldPrice": "400.00",
  264.                 "price": "300.00",
  265.                 "name": "土猪肉3",
  266.                 "image": "https://laiju.hbpt.xyz/uploads/product/6322d06d538ca.avif",
  267.                 "sales": 2999   //销量
  268.                 "comments": 10  //评论数
  269.             },
  270.             {
  271.                 "id": 2,
  272.                 "oldPrice": "300.00",
  273.                 "price": "200.00",
  274.                 "name": "土猪肉2",
  275.                 "image": "https://laiju.hbpt.xyz/uploads/product/6322cf4ddbe6e.avif",
  276.                 "sales": 2999   //销量
  277.                 "comments": 10  //评论数
  278.             },
  279.             {
  280.                 "id": 1,
  281.                 "oldPrice": "200.00",
  282.                 "price": "100.00",
  283.                 "name": "土猪肉",
  284.                 "image": "https://laiju.hbpt.xyz/uploads/product/6322cf18381b1.avif",
  285.                 "sales": 2999   //销量
  286.                 "comments": 10  //评论数
  287.             }
  288.         ]
  289.     }
  290.      * @apiErrorExample {json} Error-Response:
  291.      *     HTTP/1.1 200 
  292.      *     {
  293.      *       "code": "1",
  294.      *       "message": "",
  295.      *       "result": {}
  296.      *     }
  297.      */
  298.     public function categoryProduct()
  299.     {    
  300.         $page $this->request->request->get('page'1);     
  301.         $rows $this->request->request->get('rows'10);
  302.         $price $this->request->request->get('price''');  
  303.         $sales $this->request->request->get('sales''');
  304.         $lastest $this->request->request->get('lastest'0); //默认
  305.         $category_id $this->request->request->get('category_id');
  306.         $queryBuilder $this->entityManager->getRepository(Product::class)->createQueryBuilder('u')
  307.             ->leftJoin('u.category''c');
  308.         
  309.         if ($sales !== '') {
  310.             $queryBuilder->orderBy('u.sales'$sales == 'desc' 'asc');
  311.         }
  312.         if ($lastest !== '') {
  313.             $queryBuilder->orderBy('u.id'$lastest == 'desc' 'asc');
  314.         }
  315.         if ($price !== '') {
  316.             $queryBuilder->orderBy('u.price'$price == 'asc' 'desc');
  317.         }   
  318.         if (!$category_id) {
  319.             $queryBuilder->andWhere('u.isHome = 1');
  320.         } else {
  321.             $queryBuilder->andWhere('c.id = :category or c.parent = :category')->setParameter('category'$category_id);
  322.         }
  323.         
  324.         $total $queryBuilder->select('count(u.id) as count')->getQuery()->getOneOrNullResult();
  325.         
  326.         $queryBuilder->select('u.id, u.oldPrice, u.price, u.name, u.image, u.sales');        
  327.         $pagination $this->paginator->paginate($queryBuilder$page$rows); 
  328.         $products $pagination->getItems();
  329.         
  330.         $qb $this->entityManager->getRepository('App\Entity\FinanceTradeProductComment')->createQueryBuilder('u')
  331.             ->select('count(u.id) as count')->andWhere('u.product = :product');
  332.         foreach ($products as &$product) {
  333.             $product['image'] = $this->request->getSchemeAndHttpHost() . $product['image'];
  334.             //评论数
  335.             $comments $qb->setParameter('product'$product['id'])->getQuery()->getOneOrNullResult();
  336.             $product['comments'] = $comments['count'] ?: 0;
  337.         }
  338.         
  339.         return $this->json(['code'=>0'message'=>'success''result'=> $products]);
  340.     }
  341.     /**
  342.      * @Route("/product/detail", name="auth_product_detail", options={"ignore"=true})
  343.      * 
  344.      * @api {post} /api/product/detail 商品详情
  345.      * @apiName 商品详情
  346.      * @apiGroup product
  347.      * @apiVersion 1.0.0
  348.      * 
  349.      * @apiParam {String} id        商品id
  350.      * 
  351.      * @apiSuccessExample {json} Success-Response:
  352.      *     HTTP/1.1 200 OK
  353.      *     {
  354.      *       "code": "0",
  355.      *       "message": "",
  356.      *       "result": {
  357.         id:             商品id
  358.         price:          当前价格    
  359.         shipping:       运费            
  360.         name:           商品名称
  361.         image:          http://domain.com/图片
  362.         imageList:      [http://domain.com/图片, http://domain.com/图片]  图片数组
  363.         stock:          库存
  364.         sales:          销量
  365.         content:        商品详情
  366.         comments:       评论条数
  367.         stars:          点赞数
  368.         star:           是否点赞 1是 0否
  369.         descript:       商品描述
  370.     }        
  371.      * 
  372.      *     }
  373.      * 
  374.      * @apiErrorExample {json} Error-Response:
  375.      *     HTTP/1.1 200 
  376.      *     {
  377.      *       "code": "1",
  378.      *       "message": "",
  379.      *       "result": {}
  380.      *     }
  381.      */
  382.     public function productDetail()
  383.     {
  384.         $id $this->request->request->get('id');
  385.                
  386.         if (!$id) return $this->json(['code'=>1'message'=> 'id不能为空']);
  387.         $product $this->entityManager->getRepository(Product::class)->createQueryBuilder('u')
  388.             ->select('u.id, u.price, u.shipping, u.name, u.image, u.stock, u.sales, u.content, u.imageList, u.descript')
  389.             ->andWhere('u.id = :id and u.isEnabled = 1 ')
  390.             ->setParameter('id'$id)
  391.             ->getQuery()->getOneOrNullResult();
  392.         if (!$product) return $this->json(['code'=>1'message'=>'商品信息不存在']);
  393.         
  394.         $product['image'] = $this->request->getSchemeAndHttpHost() . $product['image'];
  395.         foreach ($product['imageList'] as &$v) {
  396.             $v $this->request->getSchemeAndHttpHost() . $v;
  397.         } 
  398.         $comments $this->entityManager->getRepository('App\Entity\FinanceTradeProductComment')->createQueryBuilder('u')->select('count(u.id) as count')
  399.             ->andWhere('u.product = :id')->setParameter('id'$id)->getQuery()->getOneOrNullResult();
  400.         $product['comments'] = $comments['count'] ?: 0;
  401.         $stars $this->entityManager->getRepository('App\Entity\UserProductStar')->createQueryBuilder('u')->select('count(u.id) as count')
  402.             ->andWhere('u.product = :id')->setParameter('id'$id)->getQuery()->getOneOrNullResult();
  403.         $product['stars'] = $stars['count'] ?: 0;
  404.         $productStar $this->entityManager->getRepository('App\Entity\UserProductStar')->findOneBy(['user'=>$this->getUser(), 'product'=>$id]);
  405.         //var_dump($this->getUser());var_dump($id);
  406.         $product['star'] = $productStar 0;
  407.         $pattern "/ style=\".*?\"/";
  408.         $replacement "";
  409.         $product['content'] = preg_replace($pattern$replacement$product['content']); 
  410.         
  411.         $exp= ["/height=.{0,5}\s/i""/width=.{0,5}\s/i"];
  412.         $exp_o= [''''];
  413.         $product['content'] = preg_replace($exp$exp_o$product['content']);
  414.         $product['content'] = str_replace('<img ''<img style="width:100%" '$product['content']);              
  415.         return $this->json(['code'=>0'message'=>'success''result'=> $product]);
  416.     }
  417.     /**
  418.      * @Route("/product/comment", name="product_comment", options={"ignore"=true})
  419.      * 
  420.      * @api {post} /api/product/comment 商品评价列表
  421.      * @apiName 商品评价列表
  422.      * @apiGroup product
  423.      * @apiVersion 1.0.0
  424.      * 
  425.      * @apiParam {String} id        商品id
  426.      * 
  427.      * @apiSuccessExample {json} Success-Response:
  428.      *     HTTP/1.1 200 OK
  429.      *     {
  430.      *       "code": "0",
  431.      *       "message": "",
  432.      *       "result": {
  433.             count: 1000 总评论数
  434.             comments: [{
  435.                 headimg:    用户头像
  436.                 nickname:   用户昵称
  437.                 star:       好评情况 1差评2中评3好评
  438.                 content:    评论内容
  439.                 images:     评论图片 数组
  440.             }, ...]      
  441.      }
  442.      * 
  443.      *     }
  444.      * 
  445.      * @apiErrorExample {json} Error-Response:
  446.      *     HTTP/1.1 200 
  447.      *     {
  448.      *       "code": "1",
  449.      *       "message": "",
  450.      *       "result": {}
  451.      *     }
  452.      */
  453.     public function productComment()
  454.     {
  455.         $id $this->request->request->get('id');
  456.         if (!$id) return $this->json(['code'=>1'message'=> 'id不能为空']);
  457.         $queryBuilder $this->entityManager->getRepository('App\Entity\FinanceTradeProductComment')->createQueryBuilder('u')
  458.             ->leftJoin('u.comment''c')->leftJoin('u.user''uu')
  459.             ->andWhere('u.product = :id')
  460.             ->setParameter('id'$id);
  461.         $count $queryBuilder->select('count(u.id) as count')->getQuery()->getOneOrNullResult();
  462.         $comments $queryBuilder->select('uu.nickname, uu.inviteCode, uu.headimg, c.star, c.content, c.images')
  463.             ->getQuery()->getResult();        
  464.         
  465.         foreach ($comments as &$item) {
  466.             foreach ($item['images'] as $k => $image) {
  467.                 $items[$k] = $this->request->getSchemeAndHttpHost() . $image;
  468.             }
  469.         } 
  470.         return $this->json(['code'=>0'message'=>'success''result'=> [
  471.             'count' => $count['count'] ?: 0,
  472.             'comments' => $comments,
  473.         ]]);
  474.     }
  475.     /**
  476.      * @Route("/product/star", name="auth_productstar", options={"ignore"=true})
  477.      * 
  478.      * @api {post} /api/product/star 商品点赞
  479.      * @apiName 商品点赞
  480.      * @apiGroup product
  481.      * @apiVersion 1.0.0
  482.      * 
  483.      * @apiParam {String} id        商品id
  484.      * @apiParam {String} star      1是点赞 0是取消
  485.      * 
  486.      * @apiSuccessExample {json} Success-Response:
  487.      *     HTTP/1.1 200 OK
  488.      *     {
  489.      *       "code": "0",
  490.      *       "message": "",
  491.      *       "result": {  
  492.                 star: 1 已点赞  0已取消
  493.      }
  494.      * 
  495.      *     }
  496.      * 
  497.      * @apiErrorExample {json} Error-Response:
  498.      *     HTTP/1.1 200 
  499.      *     {
  500.      *       "code": "1",
  501.      *       "message": "",
  502.      *       "result": {}
  503.      *     }
  504.      */
  505.     public function star()
  506.     {        
  507.         $id $this->request->request->get('id');
  508.         $star $this->request->request->get('star'0);
  509.         if (!$id) return $this->json(['code'=>1'message'=> 'id不能为空']);
  510.         $product $this->entityManager->getRepository(Product::class)->find($id);
  511.         if (!$product) return $this->json(['code'=>1'message'=> 'id不能为空']);
  512.         $productStar $this->entityManager->getRepository('App\Entity\UserProductStar')->findOneBy(['user'=>$this->getUser(), 'product'=>$product]);
  513.         if ($star == && $productStar) {
  514.             $this->entityManager->remove($productStar);
  515.             $this->entityManager->flush();
  516.         } 
  517.         if ($star == && !$productStar) {
  518.             $productStar = new UserProductStar();
  519.             $productStar->setUser($this->getUser());
  520.             $productStar->setProduct($product);
  521.             $this->entityManager->persist($productStar);
  522.             $this->entityManager->flush();
  523.         } 
  524.         
  525.         return $this->json(['code'=>0'message'=>'success''result'=>[
  526.             'star' => $star
  527.         ]]);
  528.     }
  529. }