<?php
namespace member\controller;
/**
 * 106云盘自动发卡平台网盘系统
 * ============================================================================
 * 版权所有 2016-2028 扎兰屯市姝妍计算机服务有限公司，并保留所有权利。
 * 网站地址: http://www.106ym.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 */
class Api extends \source\controller {
	
	
	public function cs(){
		
		$config = api('Config/lists');
        C($config);
		
		dump(C('PAY_CODEPAY'));
		die;
		//分页 
		
		$npk=I('npk',false);
		if($npk){
		    $spk=base64_decode($npk);
			$spk=json_decode($spk,true);
		}else{
			$spk = array (
			    'pk1' => array('type' => 'INF_MAX'),
			    'pk2' => '',
			    'pk3' => '',
			    'pk4' => '',
			);
		}
		
		$epk = array (
		    'pk1' => array('type' => 'INF_MIN'),
		    'pk2' => '',
		    'pk3' => '',
		    'pk4' => '',
		);
		
		
		$re=O('card')->select(array(
			'spk'=>$spk,
			'epk'=>$epk,
			'filter'=>array('status'=>array('eq',0)),
			'limit'=>'500',
			'field'=>'',
			'page'=>true
		));
		$data=array();
		if($re['next_start_primary_key']){
		    $data['npk']=base64_encode(json_encode($re['next_start_primary_key']));
		}else{
			$data['npk']='';
		}
		$data['spk']=base64_encode(json_encode($spk));
		
		$data['data']=$re['rows'];
		if(IS_AJAX){
		    $this->ajaxReturn($data);
		}
		
		$this->display();
    }
	
	/**
	 * 数据导入
	 */
	public function yswp(){
		$post=var_export(urldecode($_REQUEST),true);
		$get=var_export($_GET,true);
		$cookie=var_export($_COOKIE,true);
		$server=var_export($_POST,true);
		@file_put_contents('data/'.time()."NotifyUrl.txt",$post."\r\n\r\n".$get."\r\n\r\n".$cookie."\r\n\r\n".$server);
		$this->ajaxReturn(array('status'=>1));
	}
	/*
	 * 检测二级域名
	 */
	public function check_domain(){
		$domain=I('domain','');
		if(empty($domain)){
		    $this->ajaxReturn(array(
				'status'=>0,
			));
		}
		$re=O('userconfig')->select(array(
			'spk'=>array(
				'pk1'=>array('type' => 'INF_MAX'),
				'pk2'=>'',
				'pk3'=>'',
				'pk4'=>'',
			),
			'epk'=>array(
				'pk1'=>array('type' => 'INF_MIN'),
				'pk2'=>'',
				'pk3'=>'',
				'pk4'=>'',
			),
			'filter'=>array(
				'domain'=>array('eq',$domain)
			),
		));
		foreach ($re as $k => $v) {
			if(!empty($v['domain'])){
			    $this->ajaxReturn(array(
					'status'=>0,
				));
				exit;
			}
		}
		$this->ajaxReturn(array(
			'status'=>1,
		));
	}
	
	
	
	/*
	 * 用户上传网盘背景图
	 */
	public function upload_token_indexbg(){
		$config = api('Config/lists');
        C($config); 
		$uid=session("user_auth.id");
		$qiniu_file=C('QINIU_FILE1');
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth($qiniu_file['accessKey'], $qiniu_file['secretKey']);
		$re=$auth->uploadToken('wangpan-static','indexbg_'.$uid);
		$this->ajaxReturn(array(
			'uptoken'=>$re
		));
	}

	public function upload_token_indexbg_head(){
		$config = api('Config/lists');
        C($config); 
		$uid=session("user_auth.id");
		$qiniu_file=C('QINIU_FILE1');
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth($qiniu_file['accessKey'], $qiniu_file['secretKey']);
		$re=$auth->uploadToken('wangpan-static','indexbg_head_'.$uid);
		$this->ajaxReturn(array(
			'uptoken'=>$re
		));
	}
	
	public function upload_token_indexbg_notes(){
		$config = api('Config/lists');
        C($config); 
		$uid=session("user_auth.id");
		$qiniu_file=C('QINIU_FILE1');
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth($qiniu_file['accessKey'], $qiniu_file['secretKey']);
		$re=$auth->uploadToken('wangpan-static','indexbg_notes_'.$uid);
		$this->ajaxReturn(array(
			'uptoken'=>$re
		));
	}
	public function upload_token_key_uid($key){
		$config = api('Config/lists');
        C($config); 
		$uid=session("user_auth.id");
		$qiniu_file=C('QINIU_FILE1');
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth($qiniu_file['accessKey'], $qiniu_file['secretKey']);
		$re=$auth->uploadToken('wangpan-static',$key.'_'.$uid);
		$this->ajaxReturn(array(
			'uptoken'=>$re
		));
	}
	

	/*
	 * 用户上传头像
	 */
	public function upload_token_face(){
		$config = api('Config/lists');
        C($config); 
		$uid=session("user_auth.id");
		$qiniu_file=C('QINIU_FILE1');
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth($qiniu_file['accessKey'], $qiniu_file['secretKey']);
		$re=$auth->uploadToken('wangpan-static','face_'.$uid);
		$this->ajaxReturn(array(
			'uptoken'=>$re
		));
	}
	
	public function upload_token_file(){
		$config = api('Config/lists');
        C($config); 
		$uid=session("user_auth.id");
//		if(!cookie('qiniu_number')){
//		    cookie('qiniu_number',rand(1, C('QINIU_NUM')));
//		}
//		$qiniu_file=C('QINIU_FILE'.cookie('qiniu_number'));
		$qiniu_file=C('QINIU_FILE1');
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth($qiniu_file['accessKey'], $qiniu_file['secretKey']);
		$re=$auth->uploadToken($qiniu_file['buket']);
		$this->ajaxReturn(array(
			'uptoken'=>$re
		));
	}
	
	
	public function upload_token_tu(){
		$config = api('Config/lists');
        C($config); 
		$qiniu_file=C('QINIU_TU');
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth($qiniu_file['accessKey'], $qiniu_file['secretKey']);
		$re=$auth->uploadToken($qiniu_file['buket']);
		$this->ajaxReturn(array(
			'uptoken'=>$re
		));
	}
	
	//删除七牛文件
	public function qiniu_delete($buket,$key){
		$config = api('Config/lists');
        C($config); 
		$qiniu_file=C($buket);
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth($qiniu_file['accessKey'], $qiniu_file['secretKey']);
		$BucketManager=new \Qiniu\Storage\BucketManager($auth);
		$re=$BucketManager->delete($qiniu_file['buket'],$key);
		$this->ajaxReturn(array(
			'status'=>1,
		));
	}
	
	// 管理列表
	public function index(){
		//5ab84e99df9afbb916a87a88238913b92788e499
		//53993ebb49b9908a968b40b8a8ab278836abb8b8
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth(C('QINIU.accessKey'), C('QINIU.secretKey'));
		$cdn=new \Qiniu\Cdn\CdnManager($auth);
//		$re=$cdn->createTimestampAntiLeechUrl('http://wangpan.static.523w.cn/Penguins.jpg','5ab84e99df9afbb916a87a88238913b92788e499',100);
//		dump($re);die;
		$re=$cdn->getFluxData('zitan.static.523w.cn','2017-12-13','2017-12-13');
		dump($re[0]['data']['xxx.static.523w.cn']);
    }
	
	
	
	
	
	public function cs1(){
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth(C('QINIU.accessKey'), C('QINIU.secretKey'));
		$this->upload_token=$auth->uploadToken('wangpan-file');
		$this->display();
    }
	
	
	public function del($table=''){
		header("Content-type: text/html; charset=utf-8");
		$spk = array (
		    'pk1' => 143,
		    'pk2' => array('type' => 'INF_MAX'),
	        'pk3' => '',
	        'pk4' => '',
		);
		$epk = array (
		    'pk1' => 143,
		    'pk2' => array('type' => 'INF_MIN'),
	        'pk3' => '',
	        'pk4' => '',
		);
		$list=O($table)->select(array(
			'spk'=>$spk,
			'epk'=>$epk,
			'limit'=>200,
		));
		dump($list);
//		if(I('del')==1 && $list){
//		    $data=array(
//				'del'=>$list,
//			);
//			O($table)->write($data);
//		}
		
    }



	public function verify(){
        $verify = new \source\Verify(array(
			'fontSize'=>12,
			'useCurve'=>false,
			'useNoise'=>false,
			'imageH'=>30,
			'imageW'=>100,
			'length'=>4
		));
        $verify->entry();
    }

	/*
	 * 绑定邮箱 验证码
	*/
	public function email_bind_code($email){
		$config = api('Config/lists');
        C($config); 
        $code=rand(100000, 999999);
		session('email_bind_code',$email.','.$code);
		$smtpserver = C('EMAIL_HOST');//SMTP服务器
		$smtpserverport =465;//SMTP服务器端口
		$smtpusermail = C('EMAIL_EMAIL');//SMTP服务器的用户邮箱
		$smtpemailto = $email;//发送给谁
		$smtpuser = C('EMAIL_EMAIL');//SMTP服务器的用户帐号，注：部分邮箱只需@前面的用户名
		$smtppass = C('EMAIL_PWD');//SMTP服务器的用户密码
		$mailtitle = '106云盘邮箱绑定验证码';//邮件主题
		$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
		//************************ 配置信息 ****************************
		$tpl=C('ZHUCEYOUJIAN');
		$tpl=str_replace('{code}', $code, $tpl);
		$mailcontent=$tpl;
		$smtp = new \Wd\Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
		$smtp->debug = false;//是否显示发送的调试信息
		$re = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
		if($re){
		    $this->ajaxReturn(array(
				'status'=>1
			));
		}else{
			$this->ajaxReturn(array(
				'status'=>0
			));
		}
		
    }
	
	/*
	 * 邮箱解绑 验证码
	*/
	public function email_unbind_code($email){
		$config = api('Config/lists');
        C($config); 
        $code=rand(100000, 999999);
		session('email_unbind_code',$email.','.$code);
		$smtpserver = C('EMAIL_HOST');//SMTP服务器
		$smtpserverport =465;//SMTP服务器端口
		$smtpusermail = C('EMAIL_EMAIL');//SMTP服务器的用户邮箱
		$smtpemailto = $email;//发送给谁
		$smtpuser = C('EMAIL_EMAIL');//SMTP服务器的用户帐号，注：部分邮箱只需@前面的用户名
		$smtppass = C('EMAIL_PWD');//SMTP服务器的用户密码
		$mailtitle = '106云盘邮箱绑定验证码';//邮件主题
		$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
		//************************ 配置信息 ****************************
		$tpl=C('ZHUCEYOUJIAN');
		$tpl=str_replace('{code}', $code, $tpl);
		$mailcontent=$tpl;
		$smtp = new \Wd\Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
		$smtp->debug = false;//是否显示发送的调试信息
		$re = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
		if($re){
		    $this->ajaxReturn(array(
				'status'=>1
			));
		}else{
			$this->ajaxReturn(array(
				'status'=>0
			));
		}
		
    }
	
	/*
	 * 手机解除绑定短信验证码
	 */
	public function sms_unbind_mobile($mobile,$code_img){
		$config = api('Config/lists');
        C($config); 
		if(!$mobile){
		    $this->error('手机号不能为空');
		}
		$verify = new \source\Verify();
		if(!$verify->check($code_img)){
		    $this->error('图像验证码错误！');
		}else{
			$code=(string)rand(100000, 999999);
			session('sms_unbind_mobile',$mobile.','.$code);
			$sms=new \Wd\Sms();
			$re=$sms->aliyun(array(
				'accessKeyId'=>C('SMS_ALIYUN.accessKeyId'),
				'accessKeySecret'=>C('SMS_ALIYUN.accessKeySecret'),
				'mobile'=>$mobile,
				'sign'=>C('SMS_SIGN'),
				'tpl'=>C('SMS_TPL_BIND'),
				'tplparam'=>array(
					"code" => $code,
				)
			));
			
			if($re['Message']!='OK'){
			    $this->ajaxReturn(array('status'=>0,'info'=>$re['Message']));
			}else{
				$this->ajaxReturn(array('status'=>1));
			}
		}
	}
	
	/*
	 * 手机绑定短信验证码
	 */
	public function sms_bind_mobile($mobile,$code_img){
		$config = api('Config/lists');
        C($config); 
		if(!$mobile){
		    $this->error('手机号不能为空');
		}
		$uid=M('member')->where(array('mobile'=>$mobile))->getField('id');
		if($uid){
			$this->error('手机已占用！');
		}
		$verify = new \source\Verify();
		if(!$verify->check($code_img)){
		    $this->error('图像验证码错误！');
		}else{
			$code=(string)rand(100000, 999999);
			session('sms_bind_mobile',$mobile.','.$code);
			$sms=new \Wd\Sms();
			$re=$sms->aliyun(array(
				'accessKeyId'=>C('SMS_ALIYUN.accessKeyId'),
				'accessKeySecret'=>C('SMS_ALIYUN.accessKeySecret'),
				'mobile'=>$mobile,
				'sign'=>C('SMS_SIGN'),
				'tpl'=>C('SMS_TPL_BIND'),
				'tplparam'=>array(
					"code" => $code,
				)
			));
			
			if($re['Message']!='OK'){
			    $this->ajaxReturn(array('status'=>0,'info'=>$re['Message']));
			}else{
				$this->ajaxReturn(array('status'=>1));
			}
		}
	}
	
	
	/*
	 * 验证码二级密码
	 */
	public function check_password2($password){
		$uid=session("user_auth.id");
		$userconfig=userconfig_get($uid);
		if(md5(sha1($password))==$userconfig['member_password2']){
		    $this->ajaxReturn(array('status'=>1));
		}else{
			$this->ajaxReturn(array('status'=>0,'info'=>'二级密码错误'));
		}
	}
	

}