<?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 { protected $noNeedLogin = ['*']; 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(); $list = $model->field('id,name')->order('weigh desc')->select(); foreach ($list as $key => &$value){ $value['is_myself'] = $value['id']==$id?'1':'0'; } $this->success('分类列表',$list); } /** * @ApiTitle (分类列表商品) * @ApiSummary (分类按钮页面右侧商品列表 初次点击分类页面可不传分类id值) * @ApiMethod (POST) * @ApiHeaders (name=token, type=string, required=false, description="token") * @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",划线价 "image_text": "图片路径", "stock_num": 68, 商品总库存 } ] }, "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){ $list = $model ->where('is_delete','0') ->where('category_id',$sort_id) ->where('goods_status','10') ->field('goods_id,goods_name,image') ->paginate(10,false,['page'=>$page]) ->each(function ($item,$key){ 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; } $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']; // 总库存 $item->append(['stock_num']); }); }else{ $list = $model ->where('is_delete','0') ->where('goods_status','10') ->field('goods_id,goods_name,image') ->paginate(10,false,['page'=>$page]) ->each(function ($item,$key){ 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; } $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']; // 总库存 $item->append(['stock_num']); }); } $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": [ { "goods_spec_id": 103, // 规格列表id "goods_id": 22, "goods_no": "SNHW001", "goods_price": "4499.00", "line_price": "0.00", "stock_num": 941, // 库存 "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 = []; $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 ]; } $array = array_values($array); } $goods_spec = GoodsSpec::all(['goods_id'=>$goods_id]); $this->success('商品规格',['list'=>$goods_spec,'sku'=>$array]); } }