Activity.php 2.8 KB
<?php


namespace app\api\controller;


use app\common\controller\Api;
use think\Config;
use think\Db;

/**
 * 首页活动
 */
class Activity extends Api
{
    protected $noNeedRight = ['*'];
//    protected $noNeedLogin = ['*'];


    /**
     * @ApiTitle    (活动商品列表)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="活动ID")
     * @ApiParams   (name="page", type="integer", required=true, 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, 商品总库存
        }
        ]
        },
        "timeslot": "秒杀时间段"
        {
            "time": "16:41",
            "status": "not" not 未开始 will即将开始 begin 抢购中 over 已结束
        },
        {
            "time": "17:24",
            "status": "not"
        },
        "image": "广告图"
    })
     */
    public function listGoods()
    {
        $id = $this->request->post('id');
        $page = $this->request->post('page',1);
        if (!is_numeric($page) || !is_numeric($id)) $this->error('参数不合法');
        $model = new \app\api\model\Goods();

        $list = $model
            ->where('is_delete','0')
            ->where('goods_status','10')
            ->where('activity_id',$id)
            ->field('goods_id,goods_name,image')
            ->paginate(10,false,['page'=>$page])
            ->each(function ($item,$key){
                $goods_spec = Db::name('litestore_goods_spec')
                    ->where('goods_id',$item['goods_id'])
                    ->find();
                $item['cart_number'] = Db::name('cart')
                    ->where('user_id',$this->auth->id)
                    ->where('goods_id',$item['goods_id'])
                    ->sum('number');
                $item['price'] = $goods_spec['goods_price'];
                $item['line_price'] = $goods_spec['line_price'];
                // 总库存
                $item->append(['stock_num']);
            });
        $timeslot = $this->timeplot();
        foreach ($timeslot as $key => $value){
            if ($value['int'] > time()){
                $next_time = $value['int'];
                break;
            }
        }
        $image = cdnurl(Config::get('site.activity_image'),true);
        $this->success('活动商品列表',['list'=>$list,'timeslot'=>$timeslot,'image'=>$image,'next_time'=>$next_time]);
    }
}