牟新芬 4 years ago
parent
commit
a1d8e37cca
3 changed files with 51 additions and 153 deletions
  1. 40 107
      app/api/controller/v1/Index.php
  2. 6 31
      app/api/controller/v1/Pub.php
  3. 5 15
      app/api/route/index.php

+ 40 - 107
app/api/controller/v1/Index.php

@@ -1,133 +1,66 @@
 <?php
-// +----------------------------------------------------------------------
-// | [ WE CAN DO IT MORE SIMPLE  ]
-// +----------------------------------------------------------------------
-// | Copyright (c) 2018-2020 rights reserved.
-// +----------------------------------------------------------------------
-// | Author: TABLE ME
-// +----------------------------------------------------------------------
-// | Date: 2020-08-25 17:23
-// +----------------------------------------------------------------------
-declare (strict_types = 1);
+
 namespace app\api\controller\v1;
 
 use app\BaseController;
+use app\model\api\Product;
 use app\model\api\SiteProduct;
+use app\model\system\Category as CategoryModel;
 use app\model\system\News;
+use app\model\system\ProductAttr;
 use app\Request;
 use library\services\UtilService;
 use library\utils\AdvertUtils;
 
 class Index extends BaseController
 {
-
     /**
-     * 获取首页数据
+     * 获取产品分类
      */
-    public function getIndex(Request $request) {
-        $sassid = $request->site['sassid'];
-        $siteProduct = new SiteProduct;
-        $siteProduct->setSass($sassid);
-        $banner = (new AdvertUtils($sassid))->getData('62');
-        $data['banner'] = $banner;
-        $data['sort_product'] =  $siteProduct->SortList();//排行榜
-        foreach ($data['sort_product'] as $k=>$v) {
-            $imgAr =  explode(',',$v['img']);
-            $data['sort_product'][$k]['img'] = empty($imgAr) ? '' : $imgAr[0];
-        }
-        $newList = $siteProduct->NewsList();//新品上市
-        foreach ($newList as $k=>$v) {
-            $imgAr =  explode(',',$v['img']);
-            $newList[$k]['img'] = empty($imgAr) ? '' : $imgAr[0];
-        }
-        shuffle($newList);
-        $data['new_product'] = $newList;
-        shuffle($newList);
-        $data['hot_product'] = $newList;
-       return app('json')->success($data);
+    public function getCategory(){
+        $category = new CategoryModel();
+        $menus = $category->getArMenu('',0,1);
+        return app('json')->success($menus);
     }
 
-
-
-
     /**
-     * 获取产品列表数据
-     * @param Request $request
+     * 获取产品列表
      */
-    public function proList(Request $request){
-        $post =  UtilService::getMore([
-                ['page',1],
-                ['warehouse',0],
-                ['countType','0'],
-                ['type','all'],
-                ['pageSize',20]
-            ],$request);
-        $pageSize = $post['pageSize'];
-        $sassid = $request->site['sassid'];
-        $siteProduct = new SiteProduct;
-        $siteProduct->setSass($sassid);
-        list($pageCount,$data) = $siteProduct->getList($post['page'],$post,$pageSize);
-        foreach ($data as $k=>$v) {
-            $imgAr =  explode(',',$v['img']);
-            $data[$k]['img'] = empty($imgAr) ? '' : $imgAr[0];
-        }
-
-        return app('json')->success([
-            'list'      => $data,
-            'pageCount' => $pageCount,
-            'pageSize'  => $pageSize,
-            'page'      => $post['page']
-        ]);
+    public function productList(Request $request){
+        $data = UtilService::getMore([
+            ['cid', 0],
+            ['page', 1],
+            ['limit', 24]
+        ], $request);
+        $list = Product::getProductList($data);
+        return app('json')->success($list);
     }
 
     /**
-     * 产品详情数据
+     * 商品详情
      * @param Request $request
+     * @param $id
+     * @param int $type
      * @return mixed
      */
-    public function proItem(Request $request) {
-        $post =  UtilService::getMore([['id',0,'empty','请输入产品ID']],$request);
-        $sassid = $request->site['sassid'];
-        $siteProduct = new SiteProduct;
-        $siteProduct->setSass($sassid);
-        $data = $siteProduct->getItem($post['id']);
-        if(empty($data)) {
-            return app('json')->fail("找不到产品数据");
-        } else {
-            return app('json')->success($data);
-        }
-    }
-
-    /**
-     * 随机产品
-     */
-    public function proRand(Request $request) {
-        $sassid = $request->site['sassid'];
-        $siteProduct = new SiteProduct;
-        $siteProduct->setSass($sassid);
-        $data = $siteProduct->getRand(5);
-        return app('json')->success($data);
+    public function productDetail(Request $request, $id)
+    {
+        if (!$id || !($storeInfo = Product::getValidProduct($id))) return app('json')->fail('商品不存在或已下架');
+        $storeInfo['slider_image'] = json_decode($storeInfo['slider_image'], true);
+        //$storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/pages/goods_details/index?id=' . $id);
+        //替换windows服务器下正反斜杠问题导致图片无法显示
+        $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
+            return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
+        }, $storeInfo['description']);
+        list($productAttr, $productValue) = ProductAttr::getProductAttrDetail($id, 0, 0);
+        $data['productAttr'] = $productAttr;
+        $prices = array_column($productValue, 'price');
+        array_multisort($prices, SORT_ASC, SORT_NUMERIC, $productValue);
+        $keys = array_keys($productValue);
+        $productValue = array_combine($keys, $productValue);
+        $data['productValue'] = $productValue;
+        $data['priceName'] = 0;
+        return app('json')->successful($data);
     }
 
-    /**
-     * 新闻数据
-     * @param Request $request
-     */
-    public function news(Request $request) {
-        $post =  UtilService::getMore([['id',0,'empty','新闻数据']],$request);
-        $data = (new News)->where('id',$post['id'])->find()->toArray();
-        if(empty($data)) {
-            return app('json')->fail('请输入正确新闻数据');
-        }
-        return app('json')->success($data);
-    }
-
-
-
-    public function newsList(Request $request) {
-        [$page,$data] = (new News)->getList(0,['cate_id'=>2,'is_show'=>1],'6','*','time desc');
-        return app('json')->success($data);
-    }
-
-
-}
+}

+ 6 - 31
app/api/controller/v1/Pub.php

@@ -1,20 +1,10 @@
 <?php
-// +----------------------------------------------------------------------
-// | [ WE CAN DO IT MORE SIMPLE  ]
-// +----------------------------------------------------------------------
-// | Copyright (c) 2018-2020 rights reserved.
-// +----------------------------------------------------------------------
-// | Author: TABLE ME
-// +----------------------------------------------------------------------
-// | Date: 2020-08-25 17:23
-// +----------------------------------------------------------------------
-declare (strict_types = 1);
+
 namespace app\api\controller\v1;
 
 use app\BaseController;
-use app\model\api\Recharge;
-use app\model\api\Site;
 use app\model\api\Warehouse;
+use app\model\system\Sys;
 use app\Request;
 use library\services\UtilService;
 
@@ -24,24 +14,9 @@ class Pub extends BaseController
     /**
      * 站点信息
      */
-    public function siteResouce(Request $request) {
-        $stie = UtilService::getParam([
-            'custom_qq',
-            'custom_tel',
-            'domain',
-            'expire_time',
-            'logo',
-            'record',
-            'name',
-            'sub_domain',
-            'sys_seo_description',
-            'sys_seo_keyword',
-            'sys_seo_title',
-            'weixin_logo',
-            'mono',
-            "tag"
-        ],[$request->site]);
-        return app('json')->success($stie[0]);
+    public function siteResouce() {
+        $info = Sys::where("id", 1)->find()->toArray();
+        return app('json')->success($info);
     }
 
 
@@ -78,4 +53,4 @@ class Pub extends BaseController
 
 
 
-}
+}

+ 5 - 15
app/api/route/index.php

@@ -2,28 +2,18 @@
 namespace app\api\route;
 
 use app\api\middleware\AllowOriginMiddleware;
-use app\api\middleware\SeretKeyMiddleware;
-use think\facade\Config;
 use think\facade\Route;
-use think\Response;
 
 /**
- * 首页栏目
+ * 首页接口
  */
 Route::group('index',function () {
-    //首页基本数据
-    Route::rule('getIndex', 'v1.index/getIndex');
+    //产品分类
+    Route::rule('getCategory', 'v1.index/getCategory');
     //产品列表
-    Route::rule('proList','v1.index/proList');
+    Route::rule('productList', 'v1.index/productList');
     //产品详情
-    Route::rule('proItem','v1.index/proItem');
-    //产品随机
-    Route::rule('proRand','v1.index/proRand');
-    //新闻中心-列表
-    Route::rule('news','v1.index/news');
-
-    Route::rule('newsList','v1.index/newsList');
-
+    Route::get('product/:id', 'v1.index/productDetail');
 
 })->middleware([
     AllowOriginMiddleware::class