<?php
namespace member\controller;
use source\Controller;

/**
 * 106云盘自动发卡平台网盘系统
 * ============================================================================
 * 版权所有 2016-2028 扎兰屯市姝妍计算机服务有限公司，并保留所有权利。
 * 网站地址: http://www.106ym.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 */
class Member extends Controller {
	
	public function _empty(){
		$this->display('Msg_404');
	}

    protected function _initialize(){
    	
    	if(I('from')){
		    cookie('from',I('from'),864000);
		}
		if( !session('user_auth') && (!in_array(CONTROLLER_NAME, array('Member')) || (in_array(CONTROLLER_NAME, array('Member')) && !in_array(ACTION_NAME, array('login','reg','sms'))) ) ){
		    header('location:/login');
			exit;
		}
		$uid=session("user_auth.id");
		if($uid){
		    $userconfig=userconfig_get($uid);
			if($userconfig['member_status']==-1){
			    $this->display('Msg_dongjie');
				exit;
			}
		}
		
		
		/* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置
        
        C('TMPL_PARSE_STRING.__STATIC__',C('WEB_SITE_STATIC_DOAMIN').'');
		C('TMPL_PARSE_STRING.__TPL__','/static/'.MODULE_NAME);
		
		//上传token
		$qiniu_file=C('QINIU_FILE1');
		Vendor('Qiniu.autoload');
		$auth = new \Qiniu\Auth($qiniu_file['accessKey'], $qiniu_file['secretKey']);
		$this->upload_token_static=$auth->uploadToken('wangpan-static');
		$this->upload_token_face=$auth->uploadToken('wangpan-static',$uid.'_face');
		//$this->upload_token_indexbg=$auth->uploadToken('wangpan-static',$uid.'_indexbg');
		$this->upload_token_file=$auth->uploadToken($qiniu_file['buket']);
		
		
		//静态资源域名
		if(!S('STATIC')){
			S('STATIC','http://'.C('QINIU_STATIC.domain'));
		}
    }
    
	
    
    public function reg(){
    	if(IS_POST) {
    		$code_sms=I('code_sms',false);
			if($code_sms!=session('code_sms_reg') || !$code_sms){
			    $this->error('短信验证码错误');
				exit;
			}
    		$m = D("pub/Member");
			if($m->create()){
				$uid=$m->reg(I('post.'));
				if($uid>0){
				    $this->ajaxReturn(array(
						'status'=>1,
						'uid'=>$uid,
					));
				}else{
					$this->error('注册失败');
				}
			}else{
				$this->error($m->geterror());
			}
		}else{
			$this->display();
		}
    }
    
    public function mod(){
    	
    	if(IS_POST) {
    		$m = D("pub/Member");
			if($m->create()){
			   	$m->save($data);
				$this->succes('修改成功'); 
			}else{
				$this->error($m->getError());
			}
		}else{
			$this->display();
		}
    }
    public function mods(){
    	$this->display();if(IS_POST) {
    		$m = D("pub/Member");
			$data = $_POST;
			$re = $m->save($data);
			if($re){
				$auth = M('member')->find(session('user_auth.id'));
				session('user_auth', $auth);
//				header("Location:http://wangpanuser.523w.cn/member/mods");
			}
		}else{
			$this->display();
		}
    }
    
    public function login() {
		if (IS_POST) {
			$m = D("pub/Member");
			$uid=$m->login(I('post.'));
			if(IS_AJAX){
			    if($uid){
				    $this->ajaxReturn(array(
						'status'=>1,
						'uid'=>$uid,
					));
				}else{
					$this->ajaxReturn(array(
						'status'=>0,
						'info'=>'用户或密码错误'
					));
				}
			}else{
				if($uid){
				    header("location:/");
				}else{
					echo '用户或密码错误';
					exit;
				}
			}
			
		}else{
			if(I('tpl')){
				$tpl=I('tpl');
			    $this->display(ucfirst($tpl));
			}else{
				$this->display();
			}
		}
	}
	
	public function logout(){
        session('user_auth', null);
        session('user_auth_sign', null);
		header('location:/login');
    }
	
	public function nickname_mod(){
		$nickname=I('nickname','');
		if(!empty($nickname)){
			$uid=session("user_auth.id");
			$m=D('pub/Member');
		    $m->save(array(
				'id'=>$uid,
				'nickname'=>$nickname,
			));
			$user=$m->find($uid);
			$m->autologin($user);
			$this->ajaxReturn(array(
				'status'=>1,
				'nickname'=>$nickname,
			));
		}
    }
	
	public function face_mod(){
		$face=I('face','');
		if(!empty($face)){
			$uid=session("user_auth.id");
			$m=D('pub/Member');
		    $m->save(array(
				'id'=>$uid,
				'face'=>$face,
			));
			$user=$m->find($uid);
			$m->autologin($user);
			$this->ajaxReturn(array(
				'status'=>1,
			));
		}
    }
	
	public function sms($code_img,$mobile){
		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('code_sms_reg',$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_REG'),
				'tplparam'=>array(
					"code" => $code,
				)
			));
			$this->success('ok');
		}
    }
	
	public function findpwd($mobile){
		$isemail = preg_match('/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/', $mobile, $matches);
		$ismobile = preg_match('/^0?(1)[0-9]{10}$/', $mobile, $matches);
		if(!$isemail && !$ismobile){
		    $this->error('邮箱或手机不正确');
		}
		if($ismobile){
		    $uid=M('member')->where(array('mobile'=>$mobile))->getField('id');
			if(!$uid){
				$this->error('手机号对应的用户不存在');
			}
			$code=(string)rand(100000, 999999);
			$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_FINDPWD'),
				'tplparam'=>array(
					"code" => $code,
				)
			));
			if($re['Message']=='OK'){
			   	M('member')->save(array(
					'id'=>$uid,
					'password'=>md5(sha1($code)),
				)); 
				$this->success('新密码已发送到您手机');
			}else{
				$this->error($re['Message']);
			}
		}else if($isemail){
			$uid=M('member')->where(array('email'=>$mobile))->getField('id');
			if(!$uid){
				$this->error('邮箱对应的用户不存在');
			}
			$code=(string)rand(100000, 999999);
			
			$sms=new \Wd\Sms();
			$smtpserver = C('EMAIL_HOST');//SMTP服务器
			$smtpserverport =465;//SMTP服务器端口
			$smtpusermail = C('EMAIL_EMAIL');//SMTP服务器的用户邮箱
			$smtpemailto = $mobile;//发送给谁
			$smtpuser = C('EMAIL_EMAIL');//SMTP服务器的用户帐号，注：部分邮箱只需@前面的用户名
			$smtppass = C('EMAIL_PWD');//SMTP服务器的用户密码
			$mailtitle = '密码找回';//邮件主题
			$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
			//************************ 配置信息 ****************************
			$tpl=C('ZHAOHUIMIMYOUJIAN');
			$tpl=str_replace('{password}', $code, $tpl);
			$mailcontent=$tpl;
			$smtp = new \Wd\Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
			$smtp->debug = false;//是否显示发送的调试信息
			$state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
			
			M('member')->save(array(
				'id'=>$uid,
				'password'=>md5(sha1($code)),
			));
			
			$this->success('新密码已发送到您邮箱，请查收');
		}
		
		
	}
	
	//找回二级密码
	public function findpwd2($mobile){
		$isemail = preg_match('/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/', $mobile, $matches);
		$ismobile = preg_match('/^0?(1)[0-9]{10}$/', $mobile, $matches);
		if(!$isemail && !$ismobile){
		    $this->error('邮箱或手机不正确');
		}
		if($ismobile){
		    $uid=M('member')->where(array('mobile'=>$mobile))->getField('id');
			if(!$uid){
				$this->error('手机号对应的用户不存在');
			}
			$code=(string)rand(100000, 999999);
			$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_FINDPWD'),
				'tplparam'=>array(
					"code" => $code,
				)
			));
			if($re['Message']=='OK'){
			   	M('member')->save(array(
					'id'=>$uid,
					'password2'=>md5(sha1($code)),
				)); 
				$this->success('新密码已发送到您手机');
			}else{
				$this->error($re['Message']);
			}
		}else if($isemail){
			$uid=M('member')->where(array('email'=>$mobile))->getField('id');
			if(!$uid){
				$this->error('邮箱对应的用户不存在');
			}
			$code=(string)rand(100000, 999999);
			
			$sms=new \Wd\Sms();
			$smtpserver = C('EMAIL_HOST');//SMTP服务器
			$smtpserverport =465;//SMTP服务器端口
			$smtpusermail = C('EMAIL_EMAIL');//SMTP服务器的用户邮箱
			$smtpemailto = $mobile;//发送给谁
			$smtpuser = C('EMAIL_EMAIL');//SMTP服务器的用户帐号，注：部分邮箱只需@前面的用户名
			$smtppass = C('EMAIL_PWD');//SMTP服务器的用户密码
			$mailtitle = '二级密码找回';//邮件主题
			$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
			//************************ 配置信息 ****************************
			$tpl=C('ZHAOHUIMIMYOUJIAN');
			$tpl=str_replace('{password}', $code, $tpl);
			$mailcontent=$tpl;
			$smtp = new \Wd\Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
			$smtp->debug = false;//是否显示发送的调试信息
			$state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
			
			M('member')->save(array(
				'id'=>$uid,
				'password2'=>md5(sha1($code)),
			)); 
			
			$this->success('新密码已发送到您邮箱，请查收');
		}
		
		
	}

	
	//修改密码
	public function safe(){
		if(IS_POST){
			$verify = new \source\Verify();
			if(!$verify->check(I('code'))){
			    $this->error('图像验证码错误！');
			}
			$uid=session("user_auth.id");
		    $oldpassword=I('oldpassword','');
			$repassword=I('repassword','');
			if(!$oldpassword){
			   	$this->error('原密码不能为空'); 
			}
			$password=I('password','');
			$uid=M('member')->where(array('password'=>md5(sha1($oldpassword)),'id'=>$uid))->getField('id');
			if($uid){
			    $m=D('pub/Member');
				if($m->create()){
				    $m->save(array(
						'id'=>$uid,
						'password'=>md5(sha1($password)),
					));
					$this->success('密码修改成功'); 
				}else{
					$this->error($m->getError());
				}
			}else{
				$this->error('原密码错误'); 
			}
			
		}else{
			$this->display();
		}
		
	}
	
	//修改二级密码
	public function safe2(){
		if(IS_POST){
			$verify = new \source\Verify();
			if(!$verify->check(I('code'))){
			    $this->error('图像验证码错误！');
			}
			$uid=session("user_auth.id");
		    $oldpassword2=I('oldpassword2','');
			$repassword2=I('repassword2','');
			$password2=I('password2','');
			if($repassword2!=$password2){
			    $this->error('两次输入密码不符'); 
			}
			if(!$oldpassword2){
			    $uid=M('member')->where(array('password2'=>'','id'=>$uid))->getField('id');
			}else{
				$uid=M('member')->where(array('password2'=>md5(sha1($oldpassword2)),'id'=>$uid))->getField('id');
			}
			if($uid){
			    $m=D('pub/Member');
				if($m->create()){
				    $m->save(array(
						'id'=>$uid,
						'password2'=>md5(sha1($password2)),
					));
					$this->success('密码修改成功'); 
				}else{
					$this->error($m->getError());
				}
			}else{
				$this->error('原密码错误'); 
			}
		}else{
			$this->display();
		}
	}
	
	public function mobile(){
		if(IS_POST){
			$uid=session("user_auth.id");
		    $mobileold=I('mobileold');
			$sms_code_old=I('sms_code_old');
			$sms_code=I('sms_code');
			$mobile=I('mobile');
			$sms_unbind_mobile=explode(',', session('sms_unbind_mobile'));
			$sms_bind_mobile=explode(',', session('sms_bind_mobile'));
			if($sms_unbind_mobile[0]!=$mobileold || $sms_unbind_mobile[1]!=$sms_code_old){
			    $this->error('原有手机验证码错误');
			}
			if($sms_bind_mobile[0]!=$mobile || $sms_bind_mobile[1]!=$sms_code){
			    $this->error('现有手机验证码错误');
			}
			D('pub/Member')->save(array(
				'id'=>$uid,
				'mobile'=>$mobile,
			));
			$this->success('修改成功');
		}else{
			$this->modval = M('member')->find(session('user_auth.id'));
			$this->display();
		}
	}
	
	public function mobileold(){
		$this->modval = M('member')->find(session('user_auth.id'));
		$this->display();
	}
	
	public function email(){
		if(IS_POST){
			$uid=session("user_auth.id");
		    $emailold=I('emailold');
			$email_code_old=I('email_code_old');
			$email=I('email');
			$email_code=I('email_code');
			$email_unbind_code=explode(',', session('email_unbind_code'));
			$email_bind_code=explode(',', session('email_bind_code'));
			if($email_unbind_code[0]!=$emailold || $email_unbind_code[1]!=$email_code_old){
			    $this->error('原有邮箱验证码错误');
			}
			if($email_bind_code[0]!=$email || $email_bind_code[1]!=$email_code){
			    $this->error('现有邮箱验证码错误');
			}
			D('pub/Member')->save(array(
				'id'=>$uid,
				'email'=>$email,
			));
			$this->success('修改成功');
		}else{
			$this->modval = M('member')->find(session('user_auth.id'));
			$this->display();
		}
	}
	
	public function emailold(){
		$this->modval = M('member')->find(session('user_auth.id'));
		$this->display();
	}
	
	
	
	public function alipay(){
		$this->display();
	}
	
	public function safeinfo(){
		$uid=session("user_auth.id");
		$config = api('Config/lists');
		$wx=new \weixin\Wxfw(array(
			'appid'=>$config['WX_APPID'],
			'secret'=>$config['WX_APPSECRET'],
		));
		$data=array(
			'action_name'=>'QR_LIMIT_SCENE',
			'action_info'=>array(
				'scene'=>array(
					'scene_id'=>(int)$uid
				)
			)
		);
		$re=$wx->api('qrcode/create','',$data,'POST');
		$this->ewm='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$re['ticket'];
		
		$this->modval = M('member')->find(session('user_auth.id'));
		$this->display();
	}
	
	public function is_weixin_bind($time){
		$uid=session("user_auth.id");
		$userconfig=userconfig_get($uid);
		if($userconfig['member_weixin_bind_time']>$time){
		    $this->success();
		}else{
			$this->error();
		}
	}
	
	public function weixin_bind_time_update($time){
		$uid=session("user_auth.id");
		D('pub/Member')->save(array(
			'id'=>$uid,
			'weixin_bind_time'=>$time
		));
	}
	
	
	
	
	
}
