<?php
// +----------------------------------------------------------------------
// | yershop网店管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
// | 版权申明：yershop网店管理系统不是一个自由软件，是贝云网络官方推出的商业源码，严禁在未经许可的情况下
// | 拷贝、复制、传播、使用yershop网店管理系统的任意代码，如有违反，请立即删除，否则您将面临承担相应
// | 法律责任的风险。如果需要取得官方授权，请联系官方http://www.yershop.com
// +----------------------------------------------------------------------
namespace app\index\controller;
use think\Controller;
use think\Db;
class Home  extends Controller
{
	
	 protected function initialize(){
		 updateConfig();
        /* 读取站点配置 */
        $controller=request()->controller();
		$action=request()->action();	
	
        if(!api('WEB_SITE_CLOSE')){
           $this->error('站点已经关闭，请稍后访问~');
        }
	   
		$category=model("category")->getCategory() ;
		$this->assign('category', $category);
	  
		if(session('cart'))
		 {
			 $cartlist=session('cart');
		}else{
			$cartlist=array();}
		/**购物车调用**/
		$sum = 0;$price = 0.00;
		if($cartlist){
		    foreach ($cartlist as $item) {		
		         $sum += $item['num'];
				 $price += $item['num'] * $item['price'];
		    }
		}
		if( is_login()>0 ){
			$Cart=new \app\index\model\Cart;
		       //$cartlist=$Cart->get_data();
		/*查询购物车*/
		
			$uid=is_login();
		      $user=db("User")->find($uid);          	
			 
			 $map["uid"]=$uid;
        $c=db("collect")->where($map)->select();
		if($c){
			foreach ($c as &$v) {
                $v['image']= get_goods_cover($v["goods_id"]); 
				$v['price']= get_goods($v["goods_id"],'price');
             }
		  $this->assign( 'c' ,$c);
		}
		
		} else{
			$user['cover_id']=1;
			
		}
		$this->assign( 'user',$user );
		$this->assign( 'cartlist',$cartlist );
		if(input('linkid')){
		  $linkid=input('linkid');
		  session('link',$linkid);
		}
       $this->assign( 'list','' );
		$current_url=strtolower($controller."/".$action);
		$this->assign ( 'current_url',$current_url);
		$this->assign ( 'action',$current_url);
        if($current_url=="order/index"||$current_url=="order/pay"){
	       $this->assign ( 'showsidebar',true);
	   }

	 
	   $cart["total_num"]=$sum;
	   $cart["total_money"]=$price;
	   if($cartlist&&is_array($cartlist)){
		    $cart["total_count"]=count($cartlist);
	   }else{
		  $cart["total_count"]=0; 
	   }
	  
       $this->assign( 'cart',$cart);
	   $Channel=new \app\index\model\Channel;
	   $channel=$Channel->getList();
	   $this->assign( 'channel',$channel);
	    $data ['ip'] =$_SERVER['REMOTE_ADDR'];;
	
		$history=db("history")->where($data)->select();
		if($history){
			foreach ($history as &$v) {
                $v['image']= get_goods_cover($v["goods_id"]); 
				$v['price']= get_goods($v["goods_id"],'price');
             }
		   $historynum=db("history")->where($data)->count();
           $this->assign( 'historynum' ,$historynum);
		  $this->assign( 'history' ,$history);
		}
        $insert["url"]='http://'.safe_replace($_SERVER['HTTP_HOST']).safe_replace($_SERVER['REQUEST_URI']);
		$insert ['ip'] =safe_replace($_SERVER['REMOTE_ADDR']);
		$insert ['create_time'] =time();
		$insert ['time'] =time_format(time());
		//DB::name('flow')->insert($insert);
		  
		
        /* 热门搜索 */
		$str=db( 'config' )->where( 'name="REKEWORD"' )->find();	
		$hotsearch=explode("|",$str['value']);
		$this->assign( 'hotsearch' , $hotsearch );
        
		$category =  new \app\common\model\Category;
	    $footer=$category ->getfooter() ;
	    $this->assign( 'footer',$footer );
        $systems=array();
	    if(api('pin')){
           $systems["pin"]=1;

	    }
         if(api('fenxiao')){
           $systems["fenxiao"]=1;

	    }
		$this->assign( 'systems' , $systems);

    }

 
}