Index.php 13.8 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\Coupon;
use app\common\model\Category;
use app\common\model\Goods;
use app\common\model\GoodsStyle;
use app\common\model\UserSize;


/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['index','searchGoodsList','categoryList','goodsList','memberGoodsList','goodsInfo','appraiseList','styleList','specData'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user = $this->auth->getUser();
    }

    /**
     * 首页
     * @ApiMethod   (GET)
     * @ApiHeaders  (name=token, type=string, description="请求的Token")
     * @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()
    {
        // 轮播图
        $banner_list = Banner::banner_list();
        $coupon_list = Coupon::couponList($this->user);
        // 私人定制(一级分类)
        $category_list = Category::where(['type'=>'goods','pid'=>0])->select();
        foreach($category_list as $v){
            $v->visible(['id','name','image']);
        }
        $category_list = collection($category_list)->toArray();
        // 会员专区
        $member_goods_list = Goods::getList(['ismember'=>'1'],1,2)['list'];
        // 热卖爆品
        $hot_goods_list = Goods::getList(['ishot'=>'1'],1,1000)['list'];
        // 返回结果
        $this->success(__('成功'),compact(
            'banner_list',
            'coupon_list',
            'category_list',
            'member_goods_list',
            'hot_goods_list'
        ));
    }

    /**
     * 领取优惠券
     * @ApiMethod   (GET)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="coupon_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 receiveCoupon($coupon_id)
    {
        $coupon = \app\common\model\Coupon::get($coupon_id);
        empty($coupon) && $this->error(__('优惠券不存在'));
        $coupon['expiretime'] < time() && $this->error(__('优惠券已到期'));
        $data = [
            'user_id' => $this->user['id'],
            'coupon_id' => $coupon_id
        ];
        $UserCoupon = new \app\common\model\UserCoupon;
        $user_coupon = $UserCoupon->get($data);
        !empty($user_coupon) && $this->error(__('已领取,请勿重复领取'));
        $UserCoupon->save($data);
        $this->success(__('领取成功'));
    }

    /**
     * 搜索商品
     * @ApiMethod   (GET)
     * @ApiParams   (name="rows", type="integer", required=true, description="每页条数")
     * @ApiParams   (name="page", type="integer", required=true, description="页码")
     * @ApiParams   (name="keywords", type="string", required=true, description="关键字")
     * @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 searchGoodsList()
    {
        $rows = $this->request->request('rows',null);// 每页条数
        $page = $this->request->request('page',1);// 页码
        $keywords = $this->request->request('keywords','');
        $list = Goods::getList(['goods_name'=>['like','%'.$keywords.'%']],$page,$rows);
        $this->success(__('成功'),compact('list'));
    }

    /**
     * 首页-一级分类-二级分类
     * @ApiMethod   (GET)
     * @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 categoryList($category_id)
    {
        empty($category_id) && $this->error(__('参数不合法!'));
        $list = Category::where('pid',$category_id)
            ->where('type','goods')
            ->select();
        foreach ($list as $k => $v) {
            $v->visible(['id','name']);
        }
        $list = collection($list)->toArray();
        $this->success(__('成功'),compact('list'));
    }

    /**
     * 二级分类下的商品
     * @ApiMethod   (GET)
     * @ApiParams   (name="rows", type="integer", 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 goodsList()
    {
        $rows = $this->request->request('rows',null);// 每页条数
        $page = $this->request->request('page',1);// 页码
        $category_id = $this->request->request('category_id');// 分类id
        empty($category_id) && $this->error(__('参数不合法!'));
        $three_category_id_arr = Category::where('pid',$category_id)->column('id');
        $where = "FIND_IN_SET('{$category_id}', category_ids)";
        if(!empty($three_category_id_arr)){
            foreach($three_category_id_arr as $v){
                $where .= " OR FIND_IN_SET('{$v}', category_ids)";
            }
        }
        $list = Goods::getList($where,$page,$rows);
        $this->success(__('成功'),compact('list'));
    }

    /**
     * 会员专区(首页-会员专区-查看更多)
     * @ApiMethod   (GET)
     * @ApiParams   (name="rows", type="integer", description="每页条数")
     * @ApiParams   (name="page", type="integer", required=true, description="页码")
     * @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 memberGoodsList()
    {
        $rows = $this->request->request('rows',null);// 每页条数
        $page = $this->request->request('page',1);// 页码
        $list = Goods::getList(['ismember'=>'1'],$page,$rows);
        // 返回结果
        $this->success(__('成功'),compact('list'));
    }

    /**
     * 热卖爆品(首页-热卖爆品-查看更多)
     * @ApiMethod   (GET)
     * @ApiParams   (name="rows", type="integer", description="每页条数")
     * @ApiParams   (name="page", type="integer", required=true, description="页码")
     * @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 hotGoodsList()
    {
        $rows = $this->request->request('rows',null);// 每页条数
        $page = $this->request->request('page',1);// 页码
        $list = Goods::getList(['ishot'=>'1'],$page,$rows);
        // 返回结果
        $this->success(__('成功'),compact('list'));
    }

    /**
     * 商品详情
     * @ApiMethod   (GET)
     * @ApiParams   (name="goods_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 goodsInfo()
    {
        // 商品id
        $goods_id = $this->request->request('goods_id');
        $goods = Goods::get($goods_id);
        empty($goods) && $this->error(__('商品不存在'));
        $goods['issale'] == '0' && $this->error(__('商品已下架'));
        $goods['goods_style'] = '';
        $goods['size'] = [];
        if($goods['ismake'] == '1'){
            $goods_style = GoodsStyle::get(['goods_id'=>$goods_id]);
            if($goods_style){
                $goods['goods_price'] = $goods['goods_price'] + $goods_style['goods_style_price'];
                $goods['goods_style'] = $goods_style['goods_style'];
            }
            $user_size = UserSize::get(['user_id'=>$this->user['id'],'isdefault'=>'1']);
            if($user_size){
                $goods['size'] = $user_size->visible(['id','name']);
            }
        }
        $goods['appraise'] = \app\common\model\GoodsAppraise::getList(['goods_id'=>$goods_id],1,1);
        $ismember = $this->user['mobile'] ? '1' : '0';
        // 返回结果
        $this->success(__('成功'),compact('goods','ismember'));
    }

    /**
     * 全部评价
     * @ApiMethod   (GET)
     * @ApiParams   (name="rows", type="integer", description="每页条数")
     * @ApiParams   (name="page", type="integer", required=true, description="页码")
     * @ApiParams   (name="goods_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 appraiseList()
    {
        // 商品id
        $rows = $this->request->request('rows',null);
        $page = $this->request->request('page',1);
        $goods_id = $this->request->request('goods_id');
        $list = \app\common\model\GoodsAppraise::getList(['goods_id'=>$goods_id],$page,$rows);
        // 返回结果
        $this->success(__('成功'),compact('list'));
    }

    /**
     * 定制风格
     * @ApiMethod   (GET)
     * @ApiParams   (name="goods_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 styleList()
    {
        $goods_id = $this->request->request('goods_id');
        $goods = Goods::get($goods_id);
        empty($goods) && $this->error(__('商品信息不存在'));
        $goods_style = GoodsStyle::get(['goods_id'=>$goods_id]);
        empty($goods_style) && $this->error(__('该商品还未设置模特款'));
        $list = \app\common\model\Style::styleList($goods_style['style_template_id']);
        $this->success(__('成功'),compact('list'));
    }

    /**
     * 规格
     * @ApiMethod   (GET)
     * @ApiParams   (name="goods_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 specData()
    {
        $goods_id = $this->request->request('goods_id');
        $goods = Goods::get($goods_id);
        empty($goods) && $this->error(__('商品信息不存在'));
        $spec_data = $goods['spec_type'] == '2' ? $goods->getManySpecData($goods['spec_rel'], $goods['spec']) : null;
        $this->success(__('成功'),compact('spec_data'));
    }

    /**
     * 规格-获取商品sku
     * @ApiMethod   (GET)
     * @ApiParams   (name="goods_id", type="integer", required=true, description="商品ID")
     * @ApiParams   (name="spec_sku_id", type="string", description="商品sku")
     * @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 getGoodsSku()
    {
        $goods_id = $this->request->request('goods_id');
        $spec_sku_id = $this->request->request('spec_sku_id');
        $goods = Goods::get($goods_id,['spec_rel.spec']);
        empty($goods) && $this->error(__('商品信息不存在'));
        empty($spec_sku_id) && $this->error(__('请选择规格'));
        $this->success(__('成功'),$goods->getGoodsSku($spec_sku_id));
    }
}