审查视图

application/api/controller/Category.php 4.1 KB
1  
何书鹏 authored
1 2 3 4 5 6 7
<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Tree;
何书鹏 authored
8 9 10
/**
 * 分类
 */
1  
何书鹏 authored
11 12 13 14 15 16 17 18 19 20 21 22 23 24
class Category extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

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

    /**
     * 分类-首页
     * @ApiMethod   (GET)
何书鹏 authored
25
     * @ApiRoute    (/api/category/index)
1  
何书鹏 authored
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
     * @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');
何书鹏 authored
42
        $list = $this->getTreeList($tree->getTreeArray(0));
何书鹏 authored
43
        // 拼接全部一级分类,方便前端展示
何书鹏 authored
44 45
        $all_list = [];
        foreach($list as $v){
何书鹏 authored
46 47
            $v['childlist'] = [];
            $all_list[] = $v;
何书鹏 authored
48 49 50 51 52
        }
        $all = [
            "id" => 0,
            "pid" => 0,
            "name" => "全部",
何书鹏 authored
53
            "image" => cdnurl('/assets/img/qrcode.png',true),
何书鹏 authored
54 55 56 57 58 59 60 61
            "type_text" => "商品分类",
            "flag_text" => "",
            "spacer" => "",
            "childlist" => [
                [
                    "id" => 0,
                    "pid" => 0,
                    "name" => "全部",
何书鹏 authored
62
                    "image" => cdnurl('/assets/img/qrcode.png',true),
何书鹏 authored
63 64 65 66 67
                    "type_text" => "商品分类",
                    "flag_text" => "",
                    "spacer" => "",
                    "childlist" => $all_list
                ]
何书鹏 authored
68 69 70
            ],
            "haschild" => 1,
            "level" => 1
何书鹏 authored
71 72
        ];
        array_unshift($list,$all);
1  
何书鹏 authored
73 74 75 76 77 78
        $this->success(__('成功'),compact('list'));
    }

    /**
     * 分类-商品
     * @ApiMethod   (GET)
何书鹏 authored
79 80 81
     * @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")
1  
何书鹏 authored
82 83 84 85 86 87 88 89 90 91
     * @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()
    {
何书鹏 authored
92 93 94
        $rows = $this->request->get('rows',null);// 每页条数
        $page = $this->request->get('page',1);// 页码
        $category_id = $this->request->get('category_id');
何书鹏 authored
95
        $category = $this->model->get($category_id);
何书鹏 authored
96 97 98
        empty($category) && $this->error(__('分类信息不存在'));
        $has_parent = $this->model->where('pid','>','0')->where('id',$category['pid'])->find();
        $where = "FIND_IN_SET('{$category_id}', category_ids)";
何书鹏 authored
99
        if($has_parent){
何书鹏 authored
100
            $where .= " OR FIND_IN_SET('{$has_parent['id']}', category_ids)";
1  
何书鹏 authored
101
        }
何书鹏 authored
102
        $list = \app\common\model\Goods::getList($where,$page,$rows);
1  
何书鹏 authored
103 104
        $this->success(__('成功'),compact('list'));
    }
何书鹏 authored
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    // 判断等级和是否有下级
    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;
    }
1  
何书鹏 authored
123
}