<?php
namespace uhome\controller;

/**
 * 控制器
 * 
 */
class Api extends Uhome {
	
	public function down(){
		$email=I('email');
		$code=I('code');
		$pk=M('file_log')->where(array('email'=>$email,'code'=>$code))->getField('file_pk');
		$pk=json_decode($pk,true);
		if(!$pk){
			header("Content-type: text/html; charset=utf-8");
		    echo '提取码错误';die;
		}
		
		$info=O('file')->find(array(
			'pk'=>$pk
		));
		
		//下载链接
		$qiniu_file=C($info['qiniu']);
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth($qiniu_file['accessKey'], $qiniu_file['secretKey']);
		$info['downurl']=$auth->privateDownloadUrl('http://'.$qiniu_file['domain'].'/'.$info['key']).'&attname='.$info['name'];
		header("location:".$info['downurl']);
	}
	
	
	
	// 文件柜
	public function files(){
		if(IS_AJAX){
			$m = M('file');
			$rows = I('rows');
			$list = $m->where(array('uid'=>I('uid')))->limit(0,$rows)->select();
			$data = array(
				'list'=>$list,
				'sql'=>$m->getLastSql()
			);
			$this->ajaxReturn($data);
		}
    }
	
	
	public function banner(){
		$uid=S(I('domain'));
		$m = M('banner');
		$imgs = $m->where(array('uid'=>$uid))->getfield('imgs');
		$imgs_arr=preg_split("/[,]+/s", $imgs,-1,PREG_SPLIT_NO_EMPTY);
		    
		$where=array();
		$where['id']=array('in',$imgs_arr);
		$list=M('Picture')->where($where)->select();
		foreach ($list as $k => &$v) {
			$v['path']=C('WEB_SITE_STATIC_DOAMIN').$v['path'];
		}
		$data = array(
			'list'=>$list,
			'sql'=>$m->getLastSql()
		);
		$this->ajaxReturn($data);
    }
	

	// 新闻
	public function news(){
		if(IS_AJAX){
			$m = M('news');
			$rows = I('rows');
			$list = $m->where(array('uid'=>I('uid')))->limit(0,$rows)->select();
			$data = array(
				'list'=>$list,
				'sql'=>$m->getLastSql()
			);
			$this->ajaxReturn($data);
		}
    }
	
	// 教学视频
	public function video(){
		if(IS_AJAX){
			$m = M('video');
			$rows = I('rows');
			$list = $m->where(array('uid'=>I('uid')))->limit(0,$rows)->select();
			$data = array(
				'list'=>$list,
				'sql'=>$m->getLastSql()
			);
			$this->ajaxReturn($data);
		}
		$this->display();
    }
	
	// 管理列表
	public function lists(){
		// 记录当前列表页的cookie
		cookie('lists',U('lists',$_REQUEST));
		
		$m=D($this->model);
		$where=array();
		$order=$this->pk." desc";
		$count=$m->where($where)->count();
		$page       = new \source\Page($count,20); //3.2
		$nowPage = I("p",1);
		$this->page  = $page->show();
		$this->list=$m->order($order)->where($where)->page($nowPage.','.$page->listRows)->select();
		$this->display();
		
		
    }
	
	
	/*
	 * 广告
	 */
	public function advert(){
		$list=M('advert')->where(array('stime'=>array('lt',time()),'etime'=>array('gt',time())))->order('sort')->select();
		foreach ($list as $k => &$v) {
			$v['content']=wd_content_img_qiniu($v['content'],'');
		}
		unset($v);
		$list=array_group($list, 'position');
		$this->list=json_encode($list);
		$this->display();
	}
	
}