审查视图

application/api/controller/Classification.php 7.3 KB
李忠强 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php


namespace app\api\controller;

use app\api\model\Category;
use app\api\model\Goods;
use app\api\model\GoodsSpec;
use app\api\model\GoodsSpecRel;
use app\common\controller\Api;
use think\Config;
use think\Db;

/**
 * 分类页面
 */
class Classification extends Api
{
李忠强 authored
19
    protected $noNeedLogin = ['*'];
李忠强 authored
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (分类列表)
     * @ApiSummary  (分类按钮页面左侧分类列表 其他页面切换到分类页面id不传值 分类页面点击分类id传值 后台返回高亮显示)
     * @ApiMethod   (POST)
     * @ApiParams   (name=id, type=integer, required=false, description="分类id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':[
        {
            "id": 4,
            "name": "电子产品",
            "is_myself": "0", 0不高亮1高亮
            "image_text": ""
        },
        {
            "id": 6,
            "name": "水果",
            "is_myself": "0",
            "image_text": ""
        }
    ]
    })
     */
    public function sort()
    {
        $id = $this->request->post('id',0);
        $model = new Category();
何书鹏 authored
50
        $list = $model->field('id,name')->order('weigh desc')->select();
李忠强 authored
51 52 53 54 55 56 57 58 59 60
        foreach ($list as $key => &$value){
            $value['is_myself'] = $value['id']==$id?'1':'0';
        }
        $this->success('分类列表',$list);
    }

    /**
     * @ApiTitle    (分类列表商品)
     * @ApiSummary  (分类按钮页面右侧商品列表 初次点击分类页面可不传分类id值)
     * @ApiMethod   (POST)
李忠强 authored
61
     * @ApiHeaders  (name=token, type=string, required=false, description="token")
李忠强 authored
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
     * @ApiParams   (name=sort_id, type=integer, required=false, description="分类id")
     * @ApiParams   (name=page, type=integer, required=false, description="页数")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':{
    "list": {
        "total": 4, 总条数
        "per_page": 10,每页数量
        "current_page": 1,当前页
        "last_page": 1,最后一页
        "data": [
            {
            "goods_id": 21,
            "goods_name": "小米Mix3",
            "price": "100.00",
            "line_price": "1000.00",划线价
何书鹏 authored
79 80
            "image_text": "图片路径",
            "stock_num": 68, 商品总库存
李忠强 authored
81 82 83 84 85 86 87 88 89 90 91 92 93
            }
        ]
    },
    "image": "广告图"
    }
    })
     */
    public function sortGoodsList()
    {
        $sort_id = $this->request->post('sort_id',0);
        $page = $this->request->post('page',1);
        $model = new Goods();
        if ($sort_id > 0){
何书鹏 authored
94
            $list = $model
李忠强 authored
95
                ->where('is_delete','0')
何书鹏 authored
96
                ->where('category_id',$sort_id)
李忠强 authored
97 98 99 100
                ->where('goods_status','10')
                ->field('goods_id,goods_name,image')
                ->paginate(10,false,['page'=>$page])
                ->each(function ($item,$key){
李忠强 authored
101 102 103 104 105 106 107 108
                    if ($this->auth->isLogin()){
                        $item['cart_number'] = Db::name('cart')
                            ->where('user_id',$this->auth->id)
                            ->where('goods_id',$item['goods_id'])
                            ->sum('number');
                    }else{
                        $item['cart_number'] = 0;
                    }
李忠强 authored
109 110 111 112 113
                    $goods_spec = Db::name('litestore_goods_spec')
                        ->where('goods_id',$item['goods_id'])
                        ->find();
                    $item['price'] = $goods_spec['goods_price'];
                    $item['line_price'] = $goods_spec['line_price'];
何书鹏 authored
114 115
                    // 总库存
                    $item->append(['stock_num']);
李忠强 authored
116 117 118
                });
        }else{
            $list = $model
何书鹏 authored
119 120
                ->where('is_delete','0')
                ->where('goods_status','10')
李忠强 authored
121 122 123
                ->field('goods_id,goods_name,image')
                ->paginate(10,false,['page'=>$page])
                ->each(function ($item,$key){
李忠强 authored
124 125 126 127 128 129 130 131
                    if ($this->auth->isLogin()){
                        $item['cart_number'] = Db::name('cart')
                            ->where('user_id',$this->auth->id)
                            ->where('goods_id',$item['goods_id'])
                            ->sum('number');
                    }else{
                        $item['cart_number'] = 0;
                    }
李忠强 authored
132 133 134 135 136
                    $goods_spec = Db::name('litestore_goods_spec')
                        ->where('goods_id',$item['goods_id'])
                        ->find();
                    $item['price'] = $goods_spec['goods_price'];
                    $item['line_price'] = $goods_spec['line_price'];
何书鹏 authored
137 138
                    // 总库存
                    $item->append(['stock_num']);
李忠强 authored
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                });
        }
        $this->success('分类商品列表',['list'=>$list,'image'=>cdnurl(Config::get('site.advert'),true)]);
    }

    /**
     * @ApiTitle    (商品规格)
     * @ApiMethod   (POST)
     * @ApiParams   (name=goods_id, type=integer, required=true, description="商品id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':{
        // 规格组合完毕的列表
        // 如果sku是空数组 为单规格商品 只需要用list的值
        "list": [
            {
李忠强 authored
156
            "goods_spec_id": 103,  // 规格列表id
李忠强 authored
157 158 159 160
            "goods_id": 22,
            "goods_no": "SNHW001",
            "goods_price": "4499.00",
            "line_price": "0.00",
李忠强 authored
161
            "stock_num": 941,  // 库存
李忠强 authored
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
            "goods_sales": 58,
            "goods_weight": 500,
            "spec_sku_id": "44_46", // 搜索字段 组合sku里面的id搜索 从小到大排序
            "spec_image": "",
            "create_time": 1542784591,
            "update_time": 1543242861
            }
        ],
        // 规格展示的列表
        "sku": [
            {
            "name": "颜色",
            "second": [
                {
                "id": 44,
                "name": "亮黑色"
                }
            ]
            },
            {
            "name": "内存",
            "second": [
                {
                "id": 46,
                "name": "6GB+64GB"
                }
            ]
            }
        ]
    }
    })
     */
    public function goodsSku()
    {
        $goods_id = $this->request->post('goods_id');
        $goodsspecrelmodel = new GoodsSpecRel();
        $list = $goodsspecrelmodel
            ->where('goods_id',$goods_id)
            ->select();
        $array = [];
李忠强 authored
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        $goods = \app\api\model\Goods::get($goods_id);
        if ($goods['spec_type'] == 20){
            foreach ($list as $key => $value){
                if (!isset($array[$value['spec_id']])){
                    $array[$value['spec_id']]['name'] = Db::name('litestore_spec')
                        ->where('id',$value['spec_id'])
                        ->value('spec_name');
                }
                $spec_value =Db::name('litestore_spec_value')
                    ->where('id',$value['spec_value_id'])
                    ->value('spec_value');
                $array[$value['spec_id']]['second'][] = [
                    'id' => $value['spec_value_id'],
                    'name' => $spec_value
                ];
李忠强 authored
217
            }
李忠强 authored
218
            $array = array_values($array);
李忠强 authored
219 220 221 222 223 224
        }
        $goods_spec = GoodsSpec::all(['goods_id'=>$goods_id]);
        $this->success('商品规格',['list'=>$goods_spec,'sku'=>$array]);
    }

}