Category.php 3.9 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 分类接口
 */
class Category extends Api
{
    protected $categoryModel;
    protected $categoryGroupModel;

    public function _initialize()
    {
        parent::_initialize();
        $this->categoryModel = new \app\api\model\Category;
        $this->categoryGroupModel = new \app\api\model\CategoryGroup;
    }

    /**
     * @ApiTitle    (首页分类)
     * @ApiSummary  (首页分类)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/category/getCategoryIndex)
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    "id": "id",//幻灯片id
    "goods_id": "shop_id",//商品id(可跳转商品详情)
    "title": "title",//标题
    "image": "image",//图片
    },
    })
     */
    public function getCategoryIndex(){

        $data['category1'] = $this->categoryGroupModel->selectData(['sitelist'=>['like','%' . 1 . '%']],4,$this->lang);
        $data['category2'] = $this->categoryGroupModel->selectData(['sitelist'=>['like','%' . 2 . '%']],3,$this->lang);
        $data['category3'] = $this->categoryModel->selectData(['pid'=>0],null,$this->lang);
        $this->success('SUCCESS',$data);
    }

    /**
     * @ApiTitle    (分类页列表)
     * @ApiSummary  (分类页列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/category/getCategory)
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587524078",
    "data": {
    "category1": [   顶部分组列表
    {
    "id": 2,  分组id
    "ch_name": "素食",  中文名称
    "en_name": "vegetarian",  英文名称
    "ch_index_name": "素食",   中文首页名称
    "en_index_name": "vegetarian",  英文首页名称
    "index_image": "http://q7s0a1rb4.bkt.clouddn.com/assets/img/qrcode.png",    首页图片
    "image": ""  图片
    }
    ],
    "category2": [  左侧一级分类列表
    {
    "id": 8,  分类id
    "ch_name": "乳品蛋类",    中文名称
    "en_name": "Eggs in the dairy",   英文名称
    "image": ""   图片
    },
    {
    "id": 1,
    "ch_name": "生鲜产品",
    "en_name": "fresh product",
    "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png"
    }
    ],
    "category3": [   右侧二级分类列表
    {
    "ch_name": "乳品蛋类",  一级分类中文名称
    "en_name": "Eggs in the dairy",  一级分类英文名称
    "list": [  二级分类列表
    {
    "id": 11, 分类id
    "ch_name": "酸奶/甜点", 二级分类中文名称
    "en_name": "Yogurt/dessert",  二级分类英文名称
    "image": ""
    },
    {
    "id": 10,
    "ch_name": "鸡蛋",
    "en_name": "henapple",
    "image": ""
    },
    {
    "id": 9,
    "ch_name": "牛奶",
    "en_name": "milk",
    "image": ""
    }
    ]
    },
    {
    "ch_name": "生鲜产品",
    "en_name": "fresh product",
    "list": [
    {
    "id": 5,
    "ch_name": "水产海鲜",
    "en_name": "aquaculture seafood",
    "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png"
    },
    {
    "id": 2,
    "ch_name": "肉类",
    "en_name": "meat",
    "image": ""
    }
    ]
    }
    ]
    }
    })
     */
    public function getCategory(){

        $data['category1'] = $this->categoryGroupModel->selectData(['sitelist'=>['like','%' . 3 . '%']],4,$this->lang);
        $data['category2'] = $this->categoryModel->selectData(['pid'=>0],null,$this->lang);
        $data['category3'] = [];
        foreach ($data['category2'] as $k=>$v){
            $data['category3'][$k]['name'] = $v['name'];
            $data['category3'][$k]['list'] = $this->categoryModel->selectData(['pid'=>$v['id']],null,$this->lang);
            if (!$data['category3'][$k]['list']){
                unset($data['category2'][$k]);
                unset($data['category3'][$k]);
            }
        }
        $this->success('SUCCESS',$data);
    }
}