Category.php 4.1 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Tree;

/**
 * 分类
 */
class Category extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\common\model\Category');
    }

    /**
     * 分类-首页
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/category/index)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
    public function index()
    {
        $list = $this->model->where('type','goods')->order('weigh desc,id desc')->select();
        foreach($list as $v){
            $v->visible(['id','pid','name','image']);
        }
        $tree = Tree::instance();
        $tree->init(collection($list)->toArray(), 'pid');
        $list = $this->getTreeList($tree->getTreeArray(0));
        // 拼接全部分类,方便前端展示
        $all_list = [];
        foreach($list as $v){
            if($v['pid'] == 0){
                $v['childlist'] = [];
                $all_list[] = $v;
            }
        }
        $all = [
            "id" => 0,
            "pid" => 0,
            "name" => "全部",
            "image" => cdnurl('/assets/img/qrcode.png',true),
            "type_text" => "商品分类",
            "flag_text" => "",
            "spacer" => "",
            "childlist" => [
                [
                    "id" => 0,
                    "pid" => 0,
                    "name" => "全部",
                    "image" => cdnurl('/assets/img/qrcode.png',true),
                    "type_text" => "商品分类",
                    "flag_text" => "",
                    "spacer" => "",
                    "childlist" => $all_list
                ]
            ],
            "haschild" => 1,
            "level" => 1
        ];
        array_unshift($list,$all);
        $this->success(__('成功'),compact('list'));
    }

    /**
     * 分类-商品
     * @ApiMethod   (GET)
     * @ApiParams   (name="rows", type="integer", required=true, description="每页条数")
     * @ApiParams   (name="page", type="integer", required=true, description="页码")
     * @ApiParams   (name="category_id", type="integer", required=true, description="分类ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
    public function goods()
    {
        $rows = $this->request->get('rows',null);// 每页条数
        $page = $this->request->get('page',1);// 页码
        $category_id = $this->request->get('category_id');
        $category = $this->model->get($category_id);
        empty($category) && $this->error(__('分类信息不存在'));
        $has_parent = $this->model->where('pid','>','0')->where('id',$category['pid'])->find();
        $where = "FIND_IN_SET('{$category_id}', category_ids)";
        if($has_parent){
            $where .= " OR FIND_IN_SET('{$has_parent['id']}', category_ids)";
        }
        $list = \app\common\model\Goods::getList($where,$page,$rows);
        $this->success(__('成功'),compact('list'));
    }

    // 判断等级和是否有下级
    public function getTreeList($data = [], $level = 1)
    {
        $arr = [];
        $n = 0;
        foreach ($data as $k => $v) {
            $childlist = isset($v['childlist']) ? $v['childlist'] : [];
            $v['haschild'] = $childlist ? 1 : 0;
            $v['level'] = $v['pid'] == 0 ? 1 : $level+1;
            $arr[$n] = $v;
            if ($childlist) {
                $arr[$n]['childlist'] = $this->getTreeList($childlist, $v['level']);
            }
            $n++;
        }
        return $arr;
    }
}