<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use addons\unishop\model\Product as productModel;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     */
    public function index()
    {

        //一行标签
        $tag = Db::name('index_server')->order('id desc')->select();
        $tag = list_domain_image($tag,['image']);

        //多个头条
        $sys = Db::name('index_sys')->order('id desc')->select();

        //默认不弹窗，登录了但是没有地址，就弹窗
        $add_address = 0;
        if($this->auth->isLogin()){
            $address = Db::name('unishop_address')->where('user_id',$this->auth->id)->find();
            if(empty($address)){
                $add_address = 1;
            }
        }

        //首页弹窗商品
        $index_show_product = null;
        $map = [
            'id' => config('site.index_show_product'),
            'switch' => 1,
            'deletetime' => null,
        ];
        $productModel = new productModel();
        $index_show_product = $productModel->field('id,image,title')->where($map)->find();
        $index_show_product = info_domain_image($index_show_product,['image']);

        //登录之后，首页商品一天只弹一次
        if($this->auth->isLogin()){

            $key = date('Y-m-d').'-'.$this->auth->id;
            if(cache($key) == 1){
                $index_show_product = null;
            }else{
                cache($key,1,86400);
            }

        }

        //
        $result = [
            'tag' => $tag,
            'sys' => $sys,
            'index_add_address' => $add_address,        //首页是否添加地址
            'index_show_product' => $index_show_product,//首页弹窗商品

        ];

        $this->success(1, $result);

    }

    public function test(){
        $key = date('Y-m-d').'-'.$this->auth->id;
        dump(cache($key));
    }

    public function clean(){
        $key = date('Y-m-d').'-'.$this->auth->id;
        cache($key,null);
    }


}
