<?php
if (!defined('ROOT')) exit('Can\'t Access !'); 
class mod_member extends module_class{
 	function main(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $this->xdzs = $this->mysql->counts("select id from {$this->pre}store where cid='{$this->member[uid]}'");
	  $symoney = $this->mysql->select_one("select sum(symoney) as i from {$this->pre}res where uid='{$this->member[uid]}'");
	  $money = $this->mysql->select_one("select sum(money) as i from {$this->pre}res where uid='{$this->member[uid]}'");
	  $txze = $this->mysql->select_one("select sum(lognum) as i from {$this->pre}atmlog where uid='{$this->member[uid]}'");
	  $tcje = $this->mysql->select_one("select sum(lognum) as i from {$this->pre}reslog where uid='{$this->member[uid]}' and type='2'"); 
      $this->shzs = $this->mysql->counts("select uid from {$this->pre}user where groupid='3' and dls='{$this->member[username]}'");
	  $this->tcje = fnum($tcje['i']);
	  $this->txze = fnum($txze['i']);
	  $this->qbfl = fnum($money['i']);
	  $this->dfye = fnum($symoney['i']);
	  $this->ljfl = fnum($this->qbfl - $this->dfye);
	}

	function password(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $this->type = $_GET['type'] ? $_GET['type'] : "one";
	  $this->twourl = rewrite::request("?mod=member&act=password&type=two");
	  if(submit()){ 
		 if($this->type == 'one'){
		    $cpassword = sintrim($_POST['cpassword']);
		    $oldpassword = sintrim($_POST['oldpassword']);
		    $arr['password'] = sintrim($_POST['newpassword']);
		    if($oldpassword=='') $this->message('go_back','对不起，旧密码不能为空'); 
		    if($this->user->password($oldpassword,$this->member[salt]) != $this->member[password]) $this->message('go_back','对不起，旧密码不正确'); 
	 	    if($arr['password']=='') $this->message('go_back','对不起，新密码不能为空'); 
		    if($arr['password']!=$cpassword) $this->message('go_back','对不起，两次输入密码不一致'); 
		    $this->user->update($arr,$this->member[uid]);
			$this->user->dropuser();
		    $this->message('member_login','登录密码已修改，请重新登录');
		 }
		 if($this->type == 'two'){
		    $cpassword = sintrim($_POST['cpassword']);
		    $oldpassword = sintrim($_POST['oldpassword']);
		    $arr['repass'] = sintrim($_POST['newpassword']);
		    if($oldpassword=='') $this->message('go_back','对不起，旧提现密码不能为空'); 
		    if($this->user->password($oldpassword,$this->member[salt]) != $this->member[repass]) $this->message('go_back','对不起，旧提现密码不正确'); 
	 	    if($arr['repass']=='') $this->message('go_back','对不起，新提现密码不能为空'); 
		    if($arr['repass']!=$cpassword) $this->message('go_back','对不起，两次输入密码不一致'); 
		    $this->user->update($arr,$this->member[uid]);
		    $this->message('member_password','恭喜您，提现密码修改成功');
 		 }  
	  }
    }
	
	
	function auth(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $_GET['type'] = $_GET['type'] ? $_GET['type'] : "email";
	  $this->type = $_GET['type'];
	  $this->mobileurl = rewrite::request("?mod=member&act=auth&type=mobile");
	  $this->realnameurl = rewrite::request("?mod=member&act=auth&type=realname");
	  if($this->type=='mobile'){
		if($_GET['get']=='checkcode'){
		   $mtime = $this->member['mtime'];
		   $this->resendtime = 179 - (time()-$mtime);
		}
	  }
	  if(submit()){
		 if($this->type=='email'){
			if($this->member['echeck']) $this->message('go_back','对不起，你已经完成邮箱认证！');
			$arr['email'] = $_POST['email'];
			$username = $this->user->authemail($arr['email'],$this->member[uid]);
		    if($username!='') $this->message('go_back','对不起，该邮箱已被 '.$username.' 注册！');
		    $salt = $this->user->salt();
		    $arr['authemail'] = $this->user->password($salt,$this->member['salt']);
            $this->mysql->update("{$this->pre}user",$arr,"uid='{$this->member[uid]}'");
		    $authemailurl = config::get("sitedomain").Purl("?mod=member&act=authemail&uid={$this->member[uid]}&salt={$salt}");
		    $this->emailurl = 'http://mail.'.substr($arr['email'],strpos($arr['email'],'@')+1);
	        $subject = config::get('sitename').'邮箱认证';		
		    $body = "<div style='line-height:1.5;font-size:14px;margin-bottom:15px;color:#4d4d4d;'>";
		    $body .= "<strong style='display:block;margin-bottom:15px;'>";
		    $body .= "亲爱的会员：{$this->member['username']} 您好！</strong><p>您已于 ".formattime(time())." 进行邮箱认证操作。</p></div>";
		    $body .= "<div style='margin-bottom:20px;'><strong style='display:block;margin-bottom:20px;font-size:14px;'>";
		    $body .= "<a target='_blank' style='color:#f60;' href='{$authemailurl}'>点此进行认证</a></strong>";
		    $body .= "<p style='color:#666;'><small style='display:block;font-size:12px;margin-bottom:5px;'>";
		    $body .= "如果上述文字点击无效，请把下面网页地址复制到浏览器地址栏中打开：  </small><span style='color:#666;'>";
		    $body .= "<a target='_blank' href='{$authemailurl}'> {$authemailurl} </a></span></p></div>";		
	        sendmail($arr['email'],$subject,$body,$arr['username']); 
		 }
		 if($this->type=='mobile'){
		   $checkcode = $_POST['checkcode'];
		   if($checkcode){
             if($this->member['msalt']!=$checkcode) $this->message("go_back",'对不起，你输入的验证码有误。');
			 $arr['mcheck'] = 1;
			 $this->user->update($arr,$this->member[uid]);
			 $this->message("?mod=member&act=auth&type=mobile",'恭喜你，手机认证成功');
		   }else{
			 if($this->user->checkuserphone($_POST['userphone'])) $this->message("go_back",'该手机号码已经存在');
		     if(time()-$this->member['mtime']>180){
			   $arr['msalt'] = mt_rand(1000,9999);
			   $arr['mtime'] = time();
			   $arr['userphone'] = $_POST['userphone'];
			   $this->user->update($arr,$this->member['uid']);
			   $mtime = $arr['mtime'];
               @sendsms("你获得的手机验证码：".$arr['msalt']."。【".config::get("sitename")."】",$arr['userphone']);
			 }else{
			   if($this->member['userphone']!=$_POST['userphone']){
			     $arr['msalt'] = mt_rand(1000,9999);
			     $arr['mtime'] = time();
			     $arr['userphone'] = $_POST['userphone'];
			     $this->user->update($arr,$this->member['uid']);
			     $mtime = $arr['mtime'];
                 @sendsms("你获得的手机验证码：".$arr['msalt']."。【".config::get("sitename")."】",$arr['userphone']);
			   }
		     }
			 location("?mod=member&act=auth&type=mobile&get=checkcode");
		   }
		 }
		 if($this->type=='realname'){
            if($_POST['truename']=='') $this->message("go_back",'真实姓名不能为空');
			if($_POST['idcard']=='') $this->message("go_back",'身份证号码不能为空');
            //if($this->user->checkidcard($_POST['idcard'])) $this->message("go_back",'该身份证号码已经存在');
			$this->user->update($_POST,$this->member[uid]);
			$this->message("?mod=member&act=auth&type=realname",'恭喜你，实名认证成功');
		 }
	  }
    }
	
	function authemail(){
	  $uid = $_GET['uid'];
	  $salt = $_GET['salt'];
	  $user = $this->mysql->select_one("select * from {$this->pre}user where uid='{$uid}'");
	  $this->username = $user['username'];
	  $authemail = $this->user->password($salt,$user['salt']);
	  if($authemail!=$user['authemail']||$user['echeck']=='1'){
		  $this->message("member_auth",'对不起，你的认证地址已经失效');
	  }
	  $arr['authemail'] = '';
      $arr['echeck'] = 1;
      $this->mysql->update("{$this->pre}user",$arr,"uid='{$user[uid]}'");
	  $this->message('member_auth','恭喜你，邮箱认证成功');
	}
	
	
	
		
	function profile(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $this->type = $_GET['type'] ? $_GET['type'] : "basic";
	  $this->step = $_POST['step'];
	  $this->bankurl = rewrite::request("?mod=member&act=profile&type=bank");
	  $this->companyurl = rewrite::request("?mod=member&act=profile&type=company");
	  if($this->type=='company'){
		 $this->company = $this->mysql->select_one("select * from {$this->pre}company where uid='{$this->member[uid]}'");
	     $typequery = $this->mysql->query("select * from {$this->pre}companytype order by `typeorder` asc");
	     while($typers=$this->mysql->assoc($typequery)){
		  $this->typelist .= "[<$typers[id]><$typers[typename]>]";;
	     }
		 if(!is_array($this->company)){
		   $arr['uid'] = $this->member['uid'];
		   $this->mysql->insert("{$this->pre}company",$arr); 
	     }
	  }
	  if(submit()){
		 if($this->type == 'basic'){
		    $arr['address'] = sintrim($_POST['address']);
		    $this->user->update($arr,$this->member[uid]);
		    $this->message('member_profile','基本资料修改成功');
		 }
		 if($this->type == 'company'){
		    $arr['name'] = sintrim($_POST['name']);
		    $arr['address'] = $_POST['address'];
		    $arr['person'] = $_POST['person'];
		    $arr['telphone'] = $_POST['telphone'];
		    $arr['pics'] = $_POST['pics'];
		    $arr['typeid'] = $_POST['typeid'];
		    $arr['addtime'] = time();
		    $arr['content'] = $_POST['content'];
		    $arr['checked'] = 1; 
		    $arr['ishot'] = 0; 
		    $arr['s_province'] = $_POST['s_province'];
		    $arr['s_city'] = $_POST['s_city']; 
		    $arr['s_county'] = $_POST['s_county'];
			$this->mysql->update("{$this->pre}company",$arr,"uid='{$this->member[uid]}'"); 
		    $this->message('?mod=member&act=profile&type=company','修改商户资料成功');
 		 }
		 
	  }
    }
	
	
	function money(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $this->type = $_GET['type'] ? $_GET['type'] : "monlog";
	  $this->monlogurl = rewrite::request("?mod=member&act=money&type=monlog");
	  $this->reslogurl = rewrite::request("?mod=member&act=money&type=reslog");
	  $this->conlogurl = rewrite::request("?mod=member&act=money&type=conlog");
	  $this->atmlogurl = rewrite::request("?mod=member&act=money&type=atmlog");
	  $this->paylogurl = rewrite::request("?mod=member&act=money&type=paylog");
	  $where =  "where uid='{$this->member['uid']}'";
	  
	  if($this->type=='monlog'){
	     $pagetotal = $this->pagetotal = $this->mysql->counts("select id from {$this->pre}log $where");
	     $this->pageclass($pagetotal);
	     $this->monlog = $this->mysql->getarr("select * from {$this->pre}log  $where order by id desc limit {$this->page->start},{$this->page->size}");
	  }
	  
	  if($this->type=='reslog'){
	     $pagetotal = $this->pagetotal = $this->mysql->counts("select id from {$this->pre}reslog $where");
	     $this->pageclass($pagetotal);
	     $this->reslog = $this->mysql->getarr("select * from {$this->pre}reslog  $where order by id desc limit {$this->page->start},{$this->page->size}");
	  }
	  
	  if($this->type=='conlog'){
   	     $pagetotal = $this->pagetotal = $this->mysql->counts("select id from {$this->pre}store $where");
	     $this->pageclass($pagetotal);
	     $this->conlog = $this->mysql->getarr("select * from {$this->pre}store $where order by addtime desc limit {$this->page->start},{$this->page->size}"); 
	  }
	  
	  if($this->type=='atmlog'){
	    $pagetotal = $this->pagetotal = $this->mysql->counts("select id from {$this->pre}atmlog $where");
	    $this->pageclass($pagetotal);
	    $this->atmlog = $this->mysql->getarr("select * from {$this->pre}atmlog $where  order by id desc limit {$this->page->start},{$this->page->size}");  
	  }
	  
	  if($this->type=='paylog'){
	    $pagetotal = $this->pagetotal = $this->mysql->counts("select id from {$this->pre}payorder $where");
	    $this->pageclass($pagetotal);
	    $this->paylog = $this->mysql->getarr("select * from {$this->pre}payorder $where order by id desc limit {$this->page->start},{$this->page->size}");        
	  }
	  
	}
	
	function myatm(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $this->bank = $this->mysql->getarr("select * from {$this->pre}atmbank where uid='{$this->member[uid]}'");
	  if($this->member['idcard']=='') $this->message('?mod=member&act=auth&type=realname','请通过实名认证后进行提现！');
	  $this->moatm = '';
	  if($this->member['groupid']=='2'||$this->member['groupid']=='27'){
	     if($this->member['keti']<1000) $this->moatm = '可提现金额不足1000元，不能提现！'; 
      }elseif($this->member['groupid']=='3'){
	     if($this->member['keti']<500) $this->moatm = '可提现金额不足500元，不能提现！'; 
      }else{
	     if($this->member['keti']<100) $this->moatm = '可提现金额不足100元，不能提现！';
      }
	  if(submit()){
		 if($_POST['bigin'] == 'money'){
			if(!$this->user->isrepass()) $this->message('member_myatm','请通过提现密码验证后操作！');
		    $money = fnum($_POST['howmoney']);
	        if($this->member['groupid']=='2'||$this->member['groupid']=='27'){
	          if($this->member['keti']<1000) $this->message('go_back','可提现金额不足1000元，不能提现！'); 
			  if($money%500!=0) $this->message('go_back','提现金额必须是1000的倍数');
	        }elseif($this->member['groupid']=='3'){
	          if($this->member['keti']<500) $this->message('go_back','可提现金额不足500元，不能提现！'); 
			  if($money%500!=0) $this->message('go_back','提现金额必须是500的倍数');
	        }else{
	          if($this->member['keti']<100) $this->message('go_back','可提现金额不足100元，不能提现！');
			  if($money%100!=0) $this->message('go_back','提现金额必须是整百');
	        }
		    if($this->member['keti']<$money) $this->message('member_myatm','对不起，您提现金额超出可提现金额！');
			$sql = "select * from {$this->pre}atmbank where id='{$_POST['bankid']}' and uid='{$this->member[uid]}'";
			$bank = $this->mysql->select_one($sql);
			if(!is_array($bank)) $this->message('member_myatm','对不起，请正确选择提现银行！');
	        $where =  "where uid='{$this->member['uid']}'";
		    $symoney = fnum($this->member['money']-$money);
		    $syketi = fnum($this->member['keti']-$money);
		    $this->mysql->query("update {$this->pre}user set money = $symoney,keti = $syketi $where");
		    $arr['uid'] = $this->member['uid'];
		    $arr['lognum'] = $money;
		    $arr['bankname'] = $bank['bankadd'].$bank['bankname'];
		    $arr['bankcard'] = $bank['bankcard'];
		    $arr['addtime'] = time();
		    $arr['checked'] = 0;
		    $log['uid'] = $this->member['uid'];
		    $log['lognum'] = "-".$money;
		    $log['addtime'] = time();
		    $log['content'] = "申请提现".$money."元成功！";
            $this->mysql->insert("{$this->pre}atmlog",$arr);
            $this->mysql->insert("{$this->pre}log",$log);
            $this->message('?mod=member&act=money&type=atmlog','提现申请成功！');
		 }
		 if($_POST['bigin'] == 'repass'){
			$this->user->relogin($_POST['repass']);
			location("member_myatm");
 		 } 
	  } 
	}

    function addbank(){
	  $id = $_GET['id'];
	  if($this->member['idcard']=='') $this->javascript('?mod=member&act=myatm&type=realname','通过实名认证后才可以添加银行信息！');
	  if($_GET['get']=='del'){
		 $this->mysql->delete("{$this->pre}atmbank","id='{$_GET['id']}' and uid='{$this->member['uid']}'");  
		 location("member_myatm");
	  }
      if(submit()){
		$arr['bankadd'] = sintrim($_POST['bankadd']);
		$arr['bankname'] = sintrim($_POST['bankname']);
		$arr['bankcard'] = sintrim($_POST['bankcard']);
		$arr['uid'] = $this->member['uid'];
		if($id){
          $this->mysql->update("{$this->pre}atmbank",$arr,"id='$id'"); 
		}else{
          $this->mysql->insert("{$this->pre}atmbank",$arr); 
		}
	  }
      $this->bank = $this->mysql->select_one("select * from {$this->pre}atmbank where id='$id'");
	  if(!is_array($this->bank)&&$id) $this->message('go_back','对不起，该银行信息不存在');	  
	}

	function order(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $where =  "where uid='{$this->member['uid']}'";
	  $pagetotal = $this->pagetotal = $this->mysql->counts("select id from {$this->pre}order $where");
	  $this->pageclass($pagetotal);
	  $query = $this->mysql->query("select * from {$this->pre}order $where order by checked asc,id desc limit {$this->page->start},{$this->page->size}");
	  while($rs=$this->mysql->assoc($query)){
		$rs['shopname'] = explode('|##|',$rs['shopname']);
	    $this->order[] = $rs;
	  } 
	}
	
	
	function shopcart(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  if($_GET['get']=='del'){
		$this->mysql->delete("{$this->pre}shopcart","id='{$_GET[id]}' and uid='{$this->member['uid']}'");
		$this->message('member_shopcart','删除购物车产品成功');
	  }
	  if($_GET['get']=='empty'){
		$this->mysql->delete("{$this->pre}shopcart","uid='{$this->member['uid']}'");
		$this->message('member_shopcart','清空购物车成功');
	  }
	  if(submit()){
		if($this->member['money']<$_POST['koumoneyall']) $this->message('go_back','对不起，您的现金不足');
		if(!is_array($_POST['id'])) $this->message('go_back','对不起，购物车为空');
		$user['money'] = fnum($this->member['money']-$_POST['koumoneyall']);
		if($user['money']<$this->member['keti']) $user['keti'] = $user['money'];
		$this->mysql->update("{$this->pre}user",$user,"uid='{$this->member[uid]}'"); //扣除现金以及积分
		$order['orderid'] = date('Ymdhis').$this->member['uid'];
		$order['message'] = "{$_POST['koumoneyall']}元现金";
		$order['shopid'] = implode("|##|",$_POST['id']);
		$order['shopname'] = implode("|##|",$_POST['name']);
		$order['checked'] = 0;
		$order['uid'] = $this->member[uid];
		$order['addtime'] = time();
		$order['yourname'] = $_POST['yourname'];
		$order['dianhua'] = $_POST['dianhua'];
		$order['shouhuodizhi'] = $_POST['shouhuodizhi'];
        $this->mysql->insert("{$this->pre}order",$order); //写入订单
		$moneylog['uid'] = $this->member['uid'];
		$moneylog['addtime'] = time();
		$moneylog['content'] = "购买商品扣除{$_POST['koumoneyall']}元现金，订单号{$order['orderid']}";
		$moneylog['lognum'] = "-".$_POST['koumoneyall'];
        $this->mysql->insert("{$this->pre}log",$moneylog);
		$this->mysql->delete("{$this->pre}shopcart","uid='{$this->member['uid']}'");
		$this->message('go_back','商品订购成功，请等待发货');
	  }
	  $query = $this->mysql->query("select * from {$this->pre}shopcart where uid='{$this->member['uid']}' order by id desc");
	  while($rs=$this->mysql->assoc($query)){
		 $rs['shop'] = $this->mysql->select_one("select * from {$this->pre}shop where id='{$rs[shopid]}'");
		 $rs['delurl'] = rewrite::request("?mod=member&act=shopcart&get=del&id=".$rs['id']);
		 $this->cart[] = $rs;
	  }
	}
	
	
	function payment(){
	  $_GET['get'] = $_GET['get'] ? $_GET['get'] : "main";
	  $this->title = "用户中心 - ".config::get("sitename");
	  if($_GET['get']=='main'&&submit()){
		 if(!$_POST['paytype']) $this->message('go_back','请正确选择支付方式！');
         if($_POST['paytype']=='alipay'){
           $aliapy_config['partner']      = config::get("partner");
           $aliapy_config['key']          = config::get("alipaykey");
           $aliapy_config['seller_email'] = config::get("seller_email");
           $aliapy_config['return_url']   = config::get("sitedomain").Purl('?mod=member&act=payment&get=return&type=alipay');
           $aliapy_config['notify_url']   = config::get("sitedomain").Purl('?mod=member&act=payment&get=notify');
           $aliapy_config['sign_type']    = 'MD5';
           $aliapy_config['input_charset']= 'utf-8';
           $aliapy_config['transport']    = 'http';
           require_once(PATH."library/alipay/lib/alipay_service.class.php");
           $out_trade_no = date('Ymdhis').$this->member['uid'];
           $subject      = config::get('sitename')."现金充值";
           $body         = config::get('sitename')."现金充值";
           $total_fee    = fnum($_POST['howmoney']);
           $paymethod    = '';
           $defaultbank  = '';
           $anti_phishing_key  = '';
           $exter_invoke_ip = '';
           $show_url = config::get("sitedomain").Purl("?mod=shop");
           $extra_common_param = '';
           $royalty_type	= "";
           $royalty_parameters = "";
		   $arr['orderid'] = $out_trade_no;
		   $arr['total_fee'] = $total_fee;
		   $arr['checked'] = 0;
		   $arr['uid'] = $this->member['uid'];
		   $arr['addtime'] = time();
		   $arr['paytype'] = "支付宝";
		   $this->mysql->insert("{$this->pre}payorder",$arr);
           $parameter = array(
		     "service"			=> "create_direct_pay_by_user",
		     "payment_type"		=> "1",		
		     "partner"			=> trim($aliapy_config['partner']),
		     "_input_charset"	=> trim(strtolower($aliapy_config['input_charset'])),
             "seller_email"		=> trim($aliapy_config['seller_email']),
             "return_url"		=> trim($aliapy_config['return_url']),
             "notify_url"		=> trim($aliapy_config['notify_url']),		
		     "out_trade_no"		=> $out_trade_no,
		     "subject"			=> $subject,
		     "body"				=> $body,
		     "total_fee"			=> $total_fee,		
		     "paymethod"			=> $paymethod,
		     "defaultbank"		=> $defaultbank,		
		     "anti_phishing_key"	=> $anti_phishing_key,
		     "exter_invoke_ip"	=> $exter_invoke_ip,		
		     "show_url"			=> $show_url,
		     "extra_common_param"=> $extra_common_param,		
		     "royalty_type"		=> $royalty_type,
		     "royalty_parameters"=> $royalty_parameters
           );
           $alipayService = new AlipayService($aliapy_config);
           $html_text = $alipayService->create_direct_pay_by_user($parameter);
           echo $html_text;	 
		 }elseif($_POST['paytype']=='chinabank'){
	       $v_mid = config::get("v_mid");
	       $key   = config::get("chinabankkey"); 
	       $v_url = config::get("sitedomain").Purl('?mod=member&act=payment&get=return&type=chinabank');
	       $v_oid = date('Ymdhis').$this->member['uid']; 
	       $v_amount = fnum($_POST['howmoney']);            
           $v_moneytype = "CNY";
	       $text = $v_amount.$v_moneytype.$v_oid.$v_mid.$v_url.$key;
           $v_md5info = strtoupper(md5($text));
	       $remark1 = config::get('sitename')."现金充值";	
	       $remark2 = config::get('sitename')."现金充值";
		   $arr['orderid'] = $v_oid;
		   $arr['total_fee'] = $v_amount;
		   $arr['checked'] = 0;
		   $arr['uid'] = $this->member['uid'];
		   $arr['addtime'] = time();
		   $arr['paytype'] = "网银在线";
		   $this->mysql->insert("{$this->pre}payorder",$arr);
           require_once(PATH."library/chinabank/bank_lib.php");
           exit();	 
		 }elseif($_POST['paytype']=='yeepay'){
           require_once(PATH."library/yeepay/yeepayCommon.php");		
           $p2_Order = date('Ymdhis').$this->member['uid'];
           $p3_Amt = fnum($_POST['howmoney']);
           $p4_Cur = "CNY";
           $p5_Pid =  iconv("GB2312","UTF-8","现金充值");
           $p6_Pcat	= $p5_Pid;
           $p7_Pdesc = $p5_Pid;
           $p8_Url = config::get("sitedomain").Purl('?mod=member&act=payment&get=return&type=yeepay');
           $pa_MP  = $this->member['uid'];
           $pd_FrpId = "";
           $pr_NeedResponse	= "1";
           $hmac = getReqHmacString($p2_Order,$p3_Amt,$p4_Cur,$p5_Pid,$p6_Pcat,$p7_Pdesc,$p8_Url,$pa_MP,$pd_FrpId,$pr_NeedResponse);
		   $arr['orderid'] = $p2_Order;
		   $arr['total_fee'] = $p3_Amt;
		   $arr['checked'] = 0;
		   $arr['uid'] = $this->member['uid'];
		   $arr['addtime'] = time();
		   $arr['paytype'] = "易宝支付";
		   $this->mysql->insert("{$this->pre}payorder",$arr);
           require_once(PATH."library/yeepay/yeepay.php");
           exit();	 	 
		 }elseif($_POST['paytype']=='allinpay'){
	       $serverUrl="";
	       $inputCharset=1;
	       $pickupUrl = config::get("sitedomain").Purl('?mod=member&act=payment&get=return&type=allinpay');
	       $receiveUrl = config::get("sitedomain").Purl('?mod=member&act=payment&get=return&type=allinpay');
	       $version = "v1.0";
	       $language = 1;
	       $signType = 1;
	       $merchantId = config::get("allinpay");
	       $payerName = "";
	       $payerEmail = $this->member['email'];	
	       $payerTelephone = "";
	       $payerIDCard = "";
	       $pid = "";
	       $orderNo = date('Ymdhis').$this->member['uid'];
		   $howmoney = $_POST['howmoney'];
	       $orderAmount = fnum($howmoney*100);
	       $orderDatetime = date('Ymdhis');
	       $orderCurrency = "0";
	       $orderExpireDatetime = "";
	       $productName = config::get('sitename')."现金充值";
	       $productId = $this->member['uid'];
	       $productPrice = "";
	       $productNum = "1";
	       $productDescription = "";
	       $ext1 = "";
	       $ext2 = "";
	       $payType = "0"; //payType   不能为空，必须放在表单中提交。
	       $issuerId = ""; //issueId 直联时不为空，必须放在表单中提交。
	       $pan = "";	
	       $key = config::get("allinpaykey");

	       $bufSignSrc=""; 
	       if($inputCharset != "")
	       $bufSignSrc=$bufSignSrc."inputCharset=".$inputCharset."&";		
	       if($pickupUrl != "")
	       $bufSignSrc=$bufSignSrc."pickupUrl=".$pickupUrl."&";		
	       if($receiveUrl != "")
	       $bufSignSrc=$bufSignSrc."receiveUrl=".$receiveUrl."&";		
	       if($version != "")
	       $bufSignSrc=$bufSignSrc."version=".$version."&";		
	       if($language != "")
	       $bufSignSrc=$bufSignSrc."language=".$language."&";		
	       if($signType != "")
	       $bufSignSrc=$bufSignSrc."signType=".$signType."&";		
	       if($merchantId != "")
	       $bufSignSrc=$bufSignSrc."merchantId=".$merchantId."&";		
	       if($payerName != "")
	       $bufSignSrc=$bufSignSrc."payerName=".$payerName."&";		
	       if($payerEmail != "")
	       $bufSignSrc=$bufSignSrc."payerEmail=".$payerEmail."&";		
	       if($payerTelephone != "")
	       $bufSignSrc=$bufSignSrc."payerTelephone=".$payerTelephone."&";			
	       if($payerIDCard != "")
	       $bufSignSrc=$bufSignSrc."payerIDCard=".$payerIDCard."&";			
	       if($pid != "")
	       $bufSignSrc=$bufSignSrc."pid=".$pid."&";		
	       if($orderNo != "")
	       $bufSignSrc=$bufSignSrc."orderNo=".$orderNo."&";
	       if($orderAmount != "")
	       $bufSignSrc=$bufSignSrc."orderAmount=".$orderAmount."&";
	       if($orderCurrency != "")
	       $bufSignSrc=$bufSignSrc."orderCurrency=".$orderCurrency."&";
	       if($orderDatetime != "")
	       $bufSignSrc=$bufSignSrc."orderDatetime=".$orderDatetime."&";
	       if($orderExpireDatetime != "")
	       $bufSignSrc=$bufSignSrc."orderExpireDatetime=".$orderExpireDatetime."&";
	       if($productName != "")
	       $bufSignSrc=$bufSignSrc."productName=".$productName."&";
	       if($productPrice != "")
	       $bufSignSrc=$bufSignSrc."productPrice=".$productPrice."&";
	       if($productNum != "")
	       $bufSignSrc=$bufSignSrc."productNum=".$productNum."&";
	       if($productId != "")
	       $bufSignSrc=$bufSignSrc."productId=".$productId."&";
	       if($productDescription != "")
	       $bufSignSrc=$bufSignSrc."productDescription=".$productDescription."&";
	       if($ext1 != "")
	       $bufSignSrc=$bufSignSrc."ext1=".$ext1."&";
	       if($ext2 != "")
	       $bufSignSrc=$bufSignSrc."ext2=".$ext2."&";	
	       if($payType != "")
	       $bufSignSrc=$bufSignSrc."payType=".$payType."&";		
	       if($issuerId != "")
	       $bufSignSrc=$bufSignSrc."issuerId=".$issuerId."&";
	       if($pan != "")
	       $bufSignSrc=$bufSignSrc."pan=".$pan."&";	
	       $bufSignSrc=$bufSignSrc."key=".$key; //key为MD5密钥，密钥是在通联支付网关会员服务网站上设置。
	
	       $signMsg = strtoupper(md5($bufSignSrc));

		   $arr['orderid'] = $orderNo;
		   $arr['total_fee'] = $howmoney;
		   $arr['checked'] = 0;
		   $arr['uid'] = $this->member['uid'];
		   $arr['addtime'] = time();
		   $arr['paytype'] = "通联支付";
		   $this->mysql->insert("{$this->pre}payorder",$arr);
           require_once(PATH."library/allinpay/allinpay.php");
           exit();	 	 
		 }else{
           require_once (PATH."library/tenpay/classes/PayRequestHandler.class.php");
           /* 商户号 */
           $bargainor_id = config::get("tenpay");;
           /* 密钥 */
           $key = config::get("tenpaykey");
           /* 返回处理地址 */
           $return_url = config::get("sitedomain").Purl('?mod=member&act=payment&get=return&type=tenpay');
           //date_default_timezone_set(PRC);
           $strDate = date("Ymd");
           $strTime = date("His");
           //4位随机数
           $randNum = rand(1000, 9999);
           //10位序列号,可以自行调整。
           $strReq = $strTime . $randNum;
           /* 商家订单号,长度若超过32位，取前32位。财付通只记录商家订单号，不保证唯一。 */
           $sp_billno = $strReq;
           /* 财付通交易单号，规则为：10位商户号+8位时间（YYYYmmdd)+10位流水号 */
           $transaction_id = $bargainor_id . $strDate . $strReq;
           /* 商品价格（包含运费），以分为单位 */
           $total_fee = fnum($_POST['howmoney']);
           /* 商品名称 */
           $desc = Sitize(config::get('sitename')."现金充值");
		   
		   $arr['orderid'] = $strDate . $strReq;
		   $arr['total_fee'] = $total_fee;
		   $arr['checked'] = 0;
		   $arr['uid'] = $this->member['uid'];
		   $arr['addtime'] = time();
		   if($_POST['paytype']!='tenpay'){
		      $arr['paytype'] = "网银直充";
		   }else{
		      $arr['paytype'] = "财付通";
		   }
		   $this->mysql->insert("{$this->pre}payorder",$arr);		   
           $reqHandler = new PayRequestHandler();
           $reqHandler->init();
           $reqHandler->setKey($key);
           $reqHandler->setParameter("bargainor_id", $bargainor_id);			//商户号
           $reqHandler->setParameter("sp_billno", $sp_billno);					//商户订单号
           $reqHandler->setParameter("transaction_id", $transaction_id);		//财付通交易单号
           $reqHandler->setParameter("total_fee", $total_fee*100);					//商品总金额,以分为单位
           $reqHandler->setParameter("return_url", $return_url);				//返回处理地址
           $reqHandler->setParameter("desc",$desc);	//商品名称
		   if($_POST['paytype']!='tenpay') $reqHandler->setParameter("bank_type",$_POST['paytype']);
           $reqHandler->setParameter("spbill_create_ip", $_SERVER['REMOTE_ADDR']);
           $reqUrl = $reqHandler->getRequestURL(); 
		   location($reqUrl,1);
		 }
		 exit;
	  }
	  
	  if($_GET['get']=='pay'&&$_GET['orderid']){
		$uid = $this->member['uid'];
		$orderid = $_GET['orderid'];
		$sql = "select * from {$this->pre}payorder where orderid='{$orderid}' and uid='{$uid}' and checked<>1";
		$order =  $this->mysql->select_one($sql);
	    if(is_array($order)){
          $this->mysql->delete("{$this->pre}payorder","orderid='{$orderid}' and uid='{$uid}' and checked<>1");
		  $this->message('?mod=member&act=money&type=paylog','取消在线支付成功！');	
		}else{
		  $this->message('?mod=member&act=money&type=paylog','对不起，请不要非法操作！');			
		}
      }

	  
	  if($_GET['get']=='return'){
		 $type = $_GET['type'];
		 unset($_GET['mod'],$_GET['act'],$_GET['get'],$_GET['type']);
		 if($type=='alipay'){
           $aliapy_config['partner']      = config::get("partner");
           $aliapy_config['key']          = config::get("alipaykey");
           $aliapy_config['seller_email'] = config::get("seller_email");
           $aliapy_config['return_url']   = config::get("sitedomain").Purl('?mod=member&act=payment&get=return&type=alipay');
           $aliapy_config['notify_url']   = config::get("sitedomain").Purl('?mod=member&act=payment&get=notify');
           $aliapy_config['sign_type']    = 'MD5';
           $aliapy_config['input_charset']= 'utf-8';
           $aliapy_config['transport']    = 'http';
		   require_once(PATH."library/alipay/lib/alipay_notify.class.php");
		   $alipayNotify = new AlipayNotify($aliapy_config);
           $verify_result = $alipayNotify->verifyReturn();
           if($verify_result){
              $out_trade_no	= $_GET['out_trade_no'];
              $trade_no		= $_GET['trade_no'];
              $total_fee		= $_GET['total_fee'];
              if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS'){
				 $uid = $this->member['uid'];
		         $order =  $this->mysql->select_one("select * from {$this->pre}payorder where orderid='{$out_trade_no}' and uid='{$uid}' and checked=0");
			     if(is_array($order)){
                   $this->mysql->update("{$this->pre}payorder",array('checked'=>1),"orderid='{$out_trade_no}' and uid='{$this->member[uid]}'");
                   $this->mysql->query("update {$this->pre}user set `money`=`money`+{$total_fee} where uid='{$this->member[uid]}'");
		           $log['uid'] = $this->member['uid'];
		           $log['addtime'] = time();
		           $log['content'] = "在线充值{$total_fee}元现金，订单号{$out_trade_no}";
		           $log['lognum'] = "+".$total_fee;
                   $this->mysql->insert("{$this->pre}log",$log);
                   $this->message('member_main','恭喜您，支付成功！');  
			     }else{
                   $this->message('member_main','对不起，支付失败请联系管理员！'); 
			     }
              }else{
                 $this->message('member_main','对不起，支付失败请联系管理员！');
			  }
           }else{
              $this->message('member_main','对不起，支付失败请联系管理员！');
           }
		 }elseif($type=='chinabank'){
           $key = config::get("chinabankkey");
           $v_oid = trim($_POST['v_oid']);       // 商户发送的v_oid定单编号   
           $v_pmode = trim($_POST['v_pmode']);    // 支付方式（字符串）   
           $v_pstatus =trim($_POST['v_pstatus']);   //  支付状态 ：20（支付成功）；30（支付失败）
           $v_pstring =trim($_POST['v_pstring']);   // 支付结果信息 ： 支付完成（当v_pstatus=20时）；失败原因（当v_pstatus=30时,字符串）； 
           $v_amount  =trim($_POST['v_amount']);     // 订单实际支付金额
           $v_moneytype  =trim($_POST['v_moneytype']); //订单实际支付币种    
           $remark1   =trim($_POST['remark1' ]);      //备注字段1
           $remark2   =trim($_POST['remark2' ]);     //备注字段2
           $v_md5str  =trim($_POST['v_md5str' ]);   //拼凑后的MD5校验值                   
           $md5string=strtoupper(md5($v_oid.$v_pstatus.$v_amount.$v_moneytype.$key));
           if($v_md5str==$md5string){
	          if($v_pstatus=="20"){
				 $uid = $this->member['uid'];
		         $order =  $this->mysql->select_one("select * from {$this->pre}payorder where orderid='{$v_oid}' and uid='{$uid}' and checked=0");
			     if(is_array($order)){
                   $this->mysql->update("{$this->pre}payorder",array('checked'=>1),"orderid='{$v_oid}' and uid='{$uid}'");
                   $this->mysql->query("update {$this->pre}user set `money`=`money`+{$v_amount} where uid='{$uid}'");
		           $log['uid'] = $this->member['uid'];
		           $log['addtime'] = time();
		           $log['content'] = "在线充值{$v_amount}元现金，订单号{$v_oid}";
		           $log['lognum'] = "+".$v_amount;
                   $this->mysql->insert("{$this->pre}log",$log);
                   $this->message('member_main','恭喜您，支付成功！');  
			     }else{
                   $this->message('member_main','对不起，支付失败请联系管理员！'); 
			     }
	          }else{
		        exit("支付失败");
	          }
           }else{
	          exit("<br>校验失败,数据可疑");
           }
		 }elseif($type=='yeepay'){
            require_once(PATH."library/yeepay/yeepayCommon.php");	
            $return = getCallBackValue($r0_Cmd,$r1_Code,$r2_TrxId,$r3_Amt,$r4_Cur,$r5_Pid,$r6_Order,$r7_Uid,$r8_MP,$r9_BType,$hmac);
            $bRet = CheckHmac($r0_Cmd,$r1_Code,$r2_TrxId,$r3_Amt,$r4_Cur,$r5_Pid,$r6_Order,$r7_Uid,$r8_MP,$r9_BType,$hmac);
            if($bRet){
	           if($r1_Code=="1"){  	 	
		            if($r9_BType=="1"){						
				      $uid = $this->member['uid'];
					  $sql = "select * from {$this->pre}payorder where orderid='{$r6_Order}' and uid='{$uid}' and checked=0";
		              $order =  $this->mysql->select_one($sql);
			          if(is_array($order)){
                         $this->mysql->update("{$this->pre}payorder",array('checked'=>1),"orderid='{$r6_Order}' and uid='{$uid}'");
                         $this->mysql->query("update {$this->pre}user set `money`=`money`+{$r3_Amt} where uid='{$uid}'");
		                 $log['uid'] = $this->member['uid'];
		                 $log['addtime'] = time();
		                 $log['content'] = "在线充值{$r3_Amt}元现金，订单号{$r6_Order}";
		                 $log['lognum'] = "+".$r3_Amt;
                         $this->mysql->insert("{$this->pre}log",$log);
                         $this->message('member_main','恭喜您，支付成功！');  
			          }else{
                         $this->message('member_main','对不起，支付失败请联系管理员！'); 
			          }
		            }elseif($r9_BType=="2"){		 
		            }
	            }
	  
            }else{
	           exit("交易信息被篡改");
            }
		 }elseif($type=='allinpay'){
            require_once(PATH."library/allinpay/php_rsa.php");
            require_once(PATH."library/allinpay/php_sax.php");
            $merchantId=$_POST["merchantId"];
            $version=$_POST['version'];
            $language=$_POST['language'];
            $signType=$_POST['signType'];
            $payType=$_POST['payType'];
            $issuerId=$_POST['issuerId'];
            $paymentOrderId=$_POST['paymentOrderId'];
            $orderNo=$_POST['orderNo'];
            $orderDatetime=$_POST['orderDatetime'];
            $orderAmount=$_POST['orderAmount'];
            $payDatetime=$_POST['payDatetime'];
            $payAmount=$_POST['payAmount'];
            $ext1=$_POST['ext1'];
            $ext2=$_POST['ext2'];
            $payResult=$_POST['payResult'];
            $errorCode=$_POST['errorCode'];
            $returnDatetime=$_POST['returnDatetime'];
            $signMsg=$_POST["signMsg"];
			
            $bufSignSrc="";
            if($merchantId != "")
            $bufSignSrc=$bufSignSrc."merchantId=".$merchantId."&";		
            if($version != "")
            $bufSignSrc=$bufSignSrc."version=".$version."&";		
            if($language != "")
            $bufSignSrc=$bufSignSrc."language=".$language."&";		
            if($signType != "")
            $bufSignSrc=$bufSignSrc."signType=".$signType."&";		
            if($payType != "")
            $bufSignSrc=$bufSignSrc."payType=".$payType."&";
            if($issuerId != "")
            $bufSignSrc=$bufSignSrc."issuerId=".$issuerId."&";
            if($paymentOrderId != "")
            $bufSignSrc=$bufSignSrc."paymentOrderId=".$paymentOrderId."&";
            if($orderNo != "")
            $bufSignSrc=$bufSignSrc."orderNo=".$orderNo."&";
            if($orderDatetime != "")
            $bufSignSrc=$bufSignSrc."orderDatetime=".$orderDatetime."&";
            if($orderAmount != "")
            $bufSignSrc=$bufSignSrc."orderAmount=".$orderAmount."&";
            if($payDatetime != "")
            $bufSignSrc=$bufSignSrc."payDatetime=".$payDatetime."&";
            if($payAmount != "")
            $bufSignSrc=$bufSignSrc."payAmount=".$payAmount."&";
            if($ext1 != "")
            $bufSignSrc=$bufSignSrc."ext1=".$ext1."&";
            if($ext2 != "")
            $bufSignSrc=$bufSignSrc."ext2=".$ext2."&";
            if($payResult != "")
            $bufSignSrc=$bufSignSrc."payResult=".$payResult."&";
            if($errorCode != "")
            $bufSignSrc=$bufSignSrc."errorCode=".$errorCode."&";
            if($returnDatetime != "")
            $bufSignSrc=$bufSignSrc."returnDatetime=".$returnDatetime;
            $key = initPublicKey(PATH."library/allinpay/publickey.xml");
            $keylength = 1024;
            $verify_result = rsa_verify($bufSignSrc,$signMsg, $key[1][0], $key[2][0], $keylength,"sha1");
	
            $value = null;
            if($verify_result){
		       $value = "victory";
            }else{
               $value = "failed";
            }
            $payvalue = null;
	        $pay_result = false;
	        if($verify_result and $payResult == 1){
		       $pay_result = true;
		       $payvalue = "victory";
	        }else{
	           $payvalue = "failed";
         	}

	        if($pay_result){
				      $orderAmount = $orderAmount/100;
				      $uid = $this->member['uid'];
					  $sql = "select * from {$this->pre}payorder where orderid='{$orderNo}' and uid='{$uid}' and checked=0";
		              $order =  $this->mysql->select_one($sql);
			          if(is_array($order)){
                         $this->mysql->update("{$this->pre}payorder",array('checked'=>1),"orderid='{$orderNo}' and uid='{$uid}'");
                         $this->mysql->query("update {$this->pre}user set `money`=`money`+{$orderAmount} where uid='{$uid}'");
		                 $log['uid'] = $this->member['uid'];
		                 $log['addtime'] = time();
		                 $log['content'] = "在线充值{$orderAmount}元现金，订单号{$orderNo}";
		                 $log['lognum'] = "+".$orderAmount;
                         $this->mysql->insert("{$this->pre}log",$log);
                         $this->message('member_main','恭喜您，支付成功！');  
			          }else{
                         $this->message('member_main','对不起，支付失败请联系管理员！'); 
			          }	
	        }else{
			  exit('支付失败');	
			}

		 }elseif($type=='tenpay'){
           require_once (PATH."library/tenpay/classes/PayResponseHandler.class.php");
           $key = config::get("tenpaykey");
           $resHandler = new PayResponseHandler();
           $resHandler->setKey($key);
           if($resHandler->isTenpaySign()) {
	          $transaction_id = $resHandler->getParameter("transaction_id");
              $total_fee = $resHandler->getParameter("total_fee");
              $total_fee = $total_fee/100;
              $pay_result = $resHandler->getParameter("pay_result");
              if("0"==$pay_result ) {
				 $uid = $this->member['uid'];
				 $transaction_id = str_replace(config::get("tenpay"),"",$transaction_id);
		         $order =  $this->mysql->select_one("select * from {$this->pre}payorder where orderid='{$transaction_id}' and uid='{$uid}' and checked=0");
			     if(is_array($order)){
                   $this->mysql->update("{$this->pre}payorder",array('checked'=>1),"orderid='{$transaction_id}' and uid='{$uid}'");
                   $this->mysql->query("update {$this->pre}user set `money`=`money`+{$total_fee} where uid='{$uid}'");
		           $log['uid'] = $this->member['uid'];
		           $log['addtime'] = time();
		           $log['content'] = "在线充值{$total_fee}元现金，订单号{$transaction_id}";
		           $log['lognum'] = "+".$total_fee;
                   $this->mysql->insert("{$this->pre}log",$log);
                   $this->message('member_main','恭喜您，支付成功！');  
			     }else{
                   $this->message('member_main','对不起，支付失败请联系管理员！'); 
			     } 
	          }else{
		        $this->message('member_main','对不起，支付失败请联系管理员！');
	          }
            }else{
	          $this->message('member_main','对不起，支付失败请联系管理员！');
            }
 
		 }else{
		   echo 'success';
		   exit;
		 }


	  }
	  
	  if($_GET['get']=='notify'){
         echo "success";	
	  }
	  
	}
	
	function addjoin(){
	    $this->title = "用户中心 - ".config::get("sitename");
		if(submit()){
		  $arr['username'] = sintrim($_POST['username']);
		  $arr['password'] = sintrim($_POST['password']);
		  $arr['email'] = sintrim($_POST['email']);
		  $arr['truename'] = sintrim($_POST['truename']);
		  $arr['userphone'] = $_POST['userphone'];
		  $arr['address'] = $_POST['address'];	
		  $arr['s_province'] = $_POST['s_province'];
		  $arr['s_city'] = $_POST['s_city'];
		  $arr['s_county'] = $_POST['s_county'];
		  $cpassword = sintrim($_POST['cpassword']); 
		  $arr['groupid'] = '4';
		  $arr['status'] = '0';
		  $arr['dls'] = $this->member['username'];
		  $arr['tjdls'] = $this->member['username'];
		  $arr['jsr'] = $this->member['username'];
          if($arr['username']=='') $this->message("go_back",'用户名不能为空');
          if($this->user->checkusername($arr['username'])) $this->message("go_back",'该用户名已经存在');
		  if($arr['password']=='') $this->message("go_back",'登录密码不能为空');
		  if(strlen($arr['password'])<6) $this->message("go_back",'密码长度不能小于6');
		  if($cpassword=='') $this->message("go_back",'重复密码不能为空');
	      if($cpassword!=$arr['password']) $this->message("go_back",'两次输入密码不一致');
          if($arr['email']=='') $this->message("go_back",'请输入免费注册邮箱');  
		  if(!checkemail($arr['email'])) $this->message("go_back",'不正确的邮箱格式'); 
          if($_POST['company']=='') $this->message("go_back",'请输入商户名称'); 
          if($arr['truename']=='') $this->message("go_back",'请输入商户的真实姓名');  
          if(strlen($arr['userphone'])<8) $this->message("go_back",'请正确填写商户的联系电话'); 
		  $this->user->insert($arr);
		  $company['person'] = sintrim($_POST['truename']);
		  $company['name'] = sintrim($_POST['company']);
		  $company['telphone'] = $_POST['userphone'];
		  $company['address'] = $_POST['address'];	
		  $company['s_province'] = $_POST['s_province'];
		  $company['s_city'] = $_POST['s_city'];
		  $company['s_county'] = $_POST['s_county'];
		  $company['addtime'] = time();
		  $company['checked'] = 0; 
		  $company['ishot'] = 0; 
		  $company['uid'] = $this->mysql->insertid(); 
		  $company['pics'] = $_POST['pics'];
		  $this->mysql->insert("{$this->pre}company",$company); 
		  $this->message('member_joins','商户申请成功，请等待管理员审核！');
		}
	}	
	
	function joins(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $where =  "where dls='{$this->member['username']}' and groupid='4'";
	  $pagetotal = $this->pagetotal = $this->mysql->counts("select uid from {$this->pre}user $where");
	  $this->pageclass($pagetotal);
	  $this->joins = $this->mysql->getarr("select * from {$this->pre}user $where order by status asc limit {$this->page->start},{$this->page->size}");
	}
	
	
	function qjoins(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $where = "where s_province='{$this->member[s_province]}' and s_city='{$this->member[s_city]}' and s_county='{$this->member[s_county]}' and groupid='4'";
	  $pagetotal = $this->pagetotal = $this->mysql->counts("select uid from {$this->pre}user $where");
	  $this->pageclass($pagetotal);
	  $this->joins = $this->mysql->getarr("select * from {$this->pre}user $where order by status asc limit {$this->page->start},{$this->page->size}");
	}
	
	
	function addagent(){
	    $this->title = "用户中心 - ".config::get("sitename");
		if(submit()){
		  $arr['username'] = sintrim($_POST['username']);
		  $arr['password'] = sintrim($_POST['password']);
		  $arr['email'] = sintrim($_POST['email']);
		  $arr['truename'] = sintrim($_POST['truename']);
		  $arr['userphone'] = $_POST['userphone'];
		  $arr['address'] = $_POST['address'];	
		  $arr['s_province'] = $_POST['s_province'];
		  $arr['s_city'] = $_POST['s_city'];
		  $arr['s_county'] = $_POST['s_county'];
		  $cpassword = sintrim($_POST['cpassword']); 
		  $arr['groupid'] = 5;
		  $arr['status'] = 0;
		  $arr['dls'] = $this->member['username'];
		  $arr['jsr'] = $this->member['username'];
		  $arr['tjdls'] = $this->member['username'];
          if($arr['username']=='') $this->message("go_back",'用户名不能为空');
          if($this->user->checkusername($arr['username'])) $this->message("go_back",'该用户名已经存在');
		  if($arr['password']=='') $this->message("go_back",'登录密码不能为空');
		  if(strlen($arr['password'])<6) $this->message("go_back",'密码长度不能小于6');
		  if($cpassword=='') $this->message("go_back",'重复密码不能为空');
	      if($cpassword!=$arr['password']) $this->message("go_back",'两次输入密码不一致');
          if($arr['email']=='') $this->message("go_back",'请输入免费注册邮箱'); 
		  if($arr['s_county']=='市、县级市') $this->message("go_back",'请正确填写你的区域信息');  
		  $checkdl = $this->mysql->select_one("select * from {$this->pre}user where groupid='5' and s_province='$arr[s_province]' and s_city='$arr[s_city]' and s_county='$arr[s_county]' and status>0");
		  if(is_array($checkdl)) $this->message("go_back",'该区域已经有独家代理了');  
		  if(!checkemail($arr['email'])) $this->message("go_back",'不正确的邮箱格式'); 
          if($arr['truename']=='') $this->message("go_back",'请输入代理商的真实姓名');  
          if(strlen($arr['userphone'])<8) $this->message("go_back",'请正确填写代理商的联系电话'); 
		  $this->user->insert($arr);
		  $this->message('member_agents','代理商申请成功，请等待管理员审核！');
		}
	}	
	
	function agents(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $where =  "where tjdls='{$this->member['username']}' and groupid='5'";
	  $pagetotal = $this->pagetotal = $this->mysql->counts("select uid from {$this->pre}user $where");
	  $this->pageclass($pagetotal);
	  $this->joins = $this->mysql->getarr("select * from {$this->pre}user $where order by status asc limit {$this->page->start},{$this->page->size}");
	}
	
	
	function upywy(){
	    $this->title = "用户中心 - ".config::get("sitename");
		if(submit()){
		  if($this->member['money']<config::get("ywyfy")) $this->message("go_back",'你的余额不足升级');
		  $this->up_money($this->member['uid'],config::get("ywyfy"),"-","升级业务员");
		  $arr['groupid'] = sintrim($_POST['username']);
		  $this->mysql->query("update {$this->pre}user set groupid ='3' where uid='{$this->member['uid']}'");
		  
	
             $jsr = $this->mysql->select_one("select * from {$this->pre}user where status>0 and username='{$this->member['jsr']}'  and groupid>'2'");
			 if(is_array($jsr)){ 
		        $ywytc = formatnum(config::get("ywyfy")*config::get("ywytc"));
		        $this->up_money($jsr['uid'],$ywytc,"+","推荐会员升级业务员",'8');
			 }
	
		  $this->message('member_main','升级业务员成功');
		}
	}	
	
	function openywy(){
	    $this->title = "用户中心 - ".config::get("sitename");
		if(submit()){
		  if($this->member['money']<config::get("ywyfy")) $this->message("go_back",'你的余额不足开通');
		  $u = $this->user->sql($_POST['username'],"username");
		  if(!is_array($u)) $this->message('go_back','要开通的业务员不存在');
		  if($u['groupid']!='2') $this->message('go_back','该会员已经是业务员');
		  $this->up_money($this->member['uid'],config::get("ywyfy"),"-","开通业务员");
		  $this->mysql->query("update {$this->pre}user set groupid ='3' where uid='{$u['uid']}'");
		  
             $jsr = $this->mysql->select_one("select * from {$this->pre}user where status>0 and username='{$u['jsr']}' and groupid>'2'");
			 if(is_array($jsr)){ 
		        $ywytc = formatnum(config::get("ywyfy")*config::get("ywytc"));
		        $this->up_money($jsr['uid'],$ywytc,"+","推荐会员升级业务员",'8');
			 }
		  
		  $this->message('member_openywy','开通业务员成功');
		  
		  
		  
		  
		}
	}
	
	
	function addywy(){
	    $this->title = "用户中心 - ".config::get("sitename");
		if(submit()){
		  $arr['username'] = sintrim($_POST['username']);
		  $arr['password'] = sintrim($_POST['password']);
		  $arr['email'] = sintrim($_POST['email']);
		  $arr['truename'] = sintrim($_POST['truename']);
		  $arr['userphone'] = $_POST['userphone'];
		  $arr['address'] = $_POST['address'];	
		  $arr['s_province'] = $_POST['s_province'];
		  $arr['s_city'] = $_POST['s_city'];
		  $arr['s_county'] = $_POST['s_county'];
		  $cpassword = sintrim($_POST['cpassword']); 
		  $arr['groupid'] = 3;
		  $arr['status'] = 1;
		  $arr['dls'] = $_POST['jsr'];
		  $arr['jsr'] = $_POST['jsr'];
		  $arr['tjdls'] = $_POST['jsr'];
          if($arr['username']=='') $this->message("go_back",'用户名不能为空');
          if($this->user->checkusername($arr['username'])) $this->message("go_back",'该用户名已经存在');
		  if($arr['password']=='') $this->message("go_back",'登录密码不能为空');
		  if(strlen($arr['password'])<6) $this->message("go_back",'密码长度不能小于6');
		  if($cpassword=='') $this->message("go_back",'重复密码不能为空');
	      if($cpassword!=$arr['password']) $this->message("go_back",'两次输入密码不一致');
          if($arr['email']=='') $this->message("go_back",'请输入免费注册邮箱'); 
		  if($arr['s_county']=='市、县级市') $this->message("go_back",'请正确填写你的区域信息');  
		  if(!checkemail($arr['email'])) $this->message("go_back",'不正确的邮箱格式'); 
          if($arr['truename']=='') $this->message("go_back",'请输入真实姓名');  
          if(strlen($arr['userphone'])<8) $this->message("go_back",'请正确填写联系电话'); 
		  if($this->member['money']<config::get("ywyfy")) $this->message("go_back",'你的余额不足开通业务员');
		  $this->up_money($this->member['uid'],config::get("ywyfy"),"-","开通业务员");

             $jsr = $this->mysql->select_one("select * from {$this->pre}user where status>0 and username='{$_POST['jsr']}'  and groupid>'2'");
			 if(is_array($jsr)){ 
		        $ywytc = formatnum(config::get("ywyfy")*config::get("ywytc"));
		        $this->up_money($jsr['uid'],$ywytc,"+","推荐会员升级业务员",'8');
			 }
		  
		  $this->user->insert($arr);
		  $this->message('member_ywys','业务员申请并开通成功');
		}
	}	
	
	function ywys(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $where =  "where tjdls='{$this->member['username']}' and groupid='3'";
	  $pagetotal = $this->pagetotal = $this->mysql->counts("select uid from {$this->pre}user $where");
	  $this->pageclass($pagetotal);
	  $this->joins = $this->mysql->getarr("select * from {$this->pre}user $where order by status asc limit {$this->page->start},{$this->page->size}");
	}
	
	
	
	
	
	
	
	function addstore(){
	    $this->title = "用户中心 - ".config::get("sitename");
		$company = $this->mysql->select_one("select * from {$this->pre}company where uid='{$this->member['uid']}'");
	    if($company['name']==''||$company['address']==''||$company['telphone']==''||$company['person']==''){
			$this->message('?mod=member&act=profile&type=company','请完善你的商户资料后进行报单！');
		}
		$this->payment =  rewrite::request("?mod=member&act=payment");
		if(submit()){
		  $username = $_POST['username'];
		  $user = $this->mysql->select_one("select * from {$this->pre}user where username='{$username}'");
		  if(!is_array($user)) $this->message("go_back",'消费会员不存在，请核实');	 
		  if(config::get("xfxe")){
			$time = time();
			$btm = mktime(0,0,0,date("m",$time),date("d",$time),date("Y",$time));
			$etm = mktime(23,59,59,date("m",$time),date("d",$time),date("Y",$time));
			$sql = "select sum(money) as num from {$this->pre}store where uid='{$user[uid]}' and addtime>'{$btm}' and addtime<'{$etm}'";
			$daymoney =  $this->mysql->select_one($sql);
			$dmoney = formatnum($daymoney['num']);
			if($dmoney>=config::get("xfxe")) $this->message('go_back','每天每个用户最多消费'.config::get("xfxe")."元！");
		  }
		  $store['money'] = formatnum($_POST['money']);
          $store['shopname'] = $_POST['shopname'];
		  $store['shopnum'] = $_POST['shopnum'];
		  $store['yfmoney'] = fnum($store['money']*config::get("shsf"));
		  $store['cid'] = $this->member['uid'];
		  $store['uid'] = $user['uid'];
		  $store['orderid'] = date('Yd').$this->member['uid'].date('is');
		  $store['addtime'] = time();
          if($this->member['money']<$store['yfmoney']) $this->message("go_back",'对不起你的余额不足请充值后操作。');
		  
		  //{商家信息处理
		     $ymoney = $this->member['money']-$store['yfmoney'];
		  	 $set = "money={$ymoney},yyze=yyze+{$store[money]}";
		     if($ymoney<$this->member['keti']) $set.=",keti={$ymoney}";
             $this->mysql->query("update {$this->pre}user set $set where uid='{$this->member[uid]}'");
	         $log['uid'] = $this->member['uid'];
		     $log['addtime'] = time();
		     $log['content'] = "下单{$store['money']}元扣除{$store['yfmoney']}元,订单号：{$store['orderid']}";
		     $log['lognum'] = "-{$store['yfmoney']}";
             $this->mysql->insert("{$this->pre}log",$log);
		  //}
		  
		  
		  
		  
		  //{市级代理
		     $where = "s_province='{$this->member[s_province]}'";
             $sjdls = $this->mysql->select_one("select * from {$this->pre}user where groupid='7' and status>0 and $where");
			 if(is_array($sjdls)){ 
		        $sjfyje = formatnum($store['money']*config::get("shengjiticheng"));
		        $this->up_money($sjdls['uid'],$sjfyje,"+",$sjdls[s_county]."的“{$this->member['company']['name']}”下单{$store['money']}元，给你返利{$sjfyje}元",'7');
			 }
		  //}
		  

		  
		  
		  //{市级代理
		     $where = "s_province='{$this->member[s_province]}' and s_city='{$this->member[s_city]}'";
             $qsjdls = $this->mysql->select_one("select * from {$this->pre}user where groupid='6' and status>0 and $where");
			 if(is_array($qsjdls)){ 
		        $qsfyje = formatnum($store['money']*config::get("sjqytcbl"));
		        $this->up_money($qsjdls['uid'],$qsfyje,"+",$qsjdls[s_county]."的“{$this->member['company']['name']}”下单{$store['money']}元，给你返利{$qsfyje}元",'6');
			 }
		  //}
		    
		  //{区县代理
		     $where = "s_province='{$this->member[s_province]}' and s_city='{$this->member[s_city]}' and s_county='{$this->member[s_county]}'";
             $dls = $this->mysql->select_one("select * from {$this->pre}user where groupid='5' and status>0 and $where");
			 if(is_array($dls)){ 
		        $fyje = formatnum($store['money']*config::get("qytcbl"));
		        $this->up_money($dls['uid'],$fyje,"+",$dls['s_county']."的“{$this->member['company']['name']}”下单{$store['money']}元，返利{$fyje}元",'2');
			 }
		  //}
		  


		  
		  //{代理推荐人
             $tdls = $this->mysql->select_one("select * from {$this->pre}user where status>0 and username='{$dls['tjr']}'");
			 if(is_array($tdls)){ 
		        $tdlse = formatnum($store['money']*config::get("tjqydl"));
		        $this->up_money($tdls['uid'],$tdlse,"+",$dls['s_county']."区域内下单{$store['money']}元，返利{$tdlse}元",'4');
			 }
		  //}
		  

		  
		  //{给商户推荐人返利
             $tjr = $this->mysql->select_one("select * from {$this->pre}user where status>0 and username='{$this->member['dls']}'");
			 if(is_array($tjr)){ 
		        $tjrfl = formatnum($store['money']*config::get("tjshtc"));
		        $this->up_money($tjr['uid'],$tjrfl,"+","推荐商户 {$this->member['company']['name']} 下单{$store['money']}元，返利{$tjrfl}元",'5');
			 }
		  //}
		  
		$floor = 0;
		$referee = $user;
	    while(is_array($referee = $this->user->sql($referee['jsr'],'username'))){			
		  $jsrfl = getval(config::get("tjxffl"),$floor,$store['money']);
		  if($referee['groupid']>'2'){
		    $this->up_money($referee['uid'],$jsrfl,"+","会员消费{$store['money']}元，给你推荐返利{$jsrfl}元",'3');
		  }
	      $floor++;
		  if($floor>1) break;
	    }

		  
		  

		  
		  //{写入消费明细
             $this->mysql->insert("{$this->pre}store",$store);
		  //}
		  
		  
		  //{更新用户消费信息
			 $balance = fnum($user['balance']+$store['money']);
			 $number = floor($balance/config::get('onefh'));
			 
		  if($number>=1){	
		    $res = $this->mysql->counts("select * from {$this->pre}res where uid='{$user['uid']}' and checked='1'");
		    for($i=0;$i<$number;$i++){
			  $arr['checked'] = $res>=config::get("hownumber") ? "0" : "1";
			  $arr['uid'] = $user['uid'];
			  $arr['addtime'] = time();
			  $arr['restime'] = untime(formattime(time(),"Y-m-d"))+3600*24;
			  $arr['money'] = config::get("onefh");
			  $arr["symoney"] = $arr['money'];
              $this->mysql->insert("{$this->pre}res",$arr);
			  $res++;
		    }
	      }	
			 
			 
			 
		     $my['consume'] = fnum($user['consume']+$store['money']);
			 $my['balance'] = fnum($balance - $number*config::get("onefh"));
             $this->mysql->update("{$this->pre}user",$my,"uid='{$user[uid]}'");
		  //}
		  $this->message("member_stores",'恭喜你，下单成功');	
		  
		}
	}	
	function stores(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $where =  "where cid='{$this->member['uid']}'";
	  $pagetotal = $this->pagetotal = $this->mysql->counts("select id from {$this->pre}store $where");
	  $this->pageclass($pagetotal);
	  $this->stores = $this->mysql->getarr("select * from {$this->pre}store $where order by addtime desc limit {$this->page->start},{$this->page->size}");
	}
	
	function record(){
	  $this->title = "用户中心 - ".config::get("sitename");
	  $this->turl = config::get("sitedomain").Purl('?mod=member&act=register&username='.$this->member['username']);
	  $where =  "where jsr='{$this->member['username']}'";
	  $pagetotal = $this->pagetotal = $this->mysql->counts("select uid from {$this->pre}user $where");
	  $this->pageclass($pagetotal);
	  $this->record = $this->mysql->getarr("select * from {$this->pre}user $where order by uid desc limit {$this->page->start},{$this->page->size}");
      $record = $this->mysql->select_one("select sum(consume) as good from {$this->pre}user $where");
	  $this->good = fnum($record['good']*$this->member['xftc']);
	}
	

	function login(){
	  if(is_array($this->member)) location('member');
	  if($_POST['logintype']=='消费卡号'){
		 $user =   $this->mysql->select_one("select username from {$this->pre}user where kahao='{$_POST[username]}'");
		 if(is_array($user)){
		   $_POST['username'] = $user['username'];
	     }else{
		   $this->message("go_back",'对不起，该消费卡号不存在'); 
		 }
	  }
	  if(submit()) $this->user->login($_POST['username'],$_POST['password'],$_POST['seccode']);
	}	
	
	function register(){
		if(submit()){
		  $arr['username'] = sintrim($_POST['username']);
		  $arr['password'] = sintrim($_POST['password']);
		  $arr['email'] = sintrim($_POST['email']);
		  $arr['jsr'] = $_POST['jsr'];	
		  $arr['dls'] = $_POST['jsr'];
		  $arr['tjdls'] = $_POST['jsr'];
		  $arr['s_province'] = $_POST['s_province'];
		  $arr['s_city'] = $_POST['s_city'];
		  $arr['s_county'] = $_POST['s_county'];
          $arr['status'] = "1";
		  $cpassword = sintrim($_POST['cpassword']); 
		  $seccode = sintrim($_POST['seccode']);
          if($arr['username']=='') $this->message("go_back",'用户名不能为空');
          if($this->user->checkusername($arr['username'])) $this->message("go_back",'该用户名已经存在');
		  if($arr['password']=='') $this->message("go_back",'登录密码不能为空');
		  if(strlen($arr['password'])<6) $this->message("go_back",'密码长度不能小于6');
		  if($cpassword=='') $this->message("go_back",'重复密码不能为空');
	      if($cpassword!=$arr['password']) $this->message("go_back",'两次输入密码不一致');
          if($arr['email']=='') $this->message("go_back",'请输入您的免费注册邮箱');  
		  if(!checkemail($arr['email'])) $this->message("go_back",'不正确的邮箱格式');  
		  if(config::get("mustcode")){
			  if($seccode=='') $this->message("go_back",'验证码不能为空');  
			  if($seccode!=$_SESSION['seccode']) $this->message("go_back",'验证码不正确');
		  }
		  $this->user->insert($arr);
	      $subject = config::get('sitename').'免费注册欢迎信';		
		  $body = "<div style='line-height:1.5;font-size:14px;margin-bottom:15px;color:#4d4d4d;'>";
		  $body .= "<strong style='display:block;margin-bottom:15px;'>";
		  $body .= "亲爱的会员：{$arr['username']}您好！</strong><p>您已于 ".formattime(time())." 成功免费注册为本站会员。</p></div>";
		  $body .= "<div style='margin-bottom:20px;'><strong style='display:block;margin-bottom:20px;font-size:14px;'>";
		  $body .= "<a target='_blank' style='color:#f60;' href='".config::get('sitedomain').Purl('member_login')."'>马上登录网站</a></strong>";
		  $body .= "<p style='color:#666;'><small style='display:block;font-size:12px;margin-bottom:5px;'>";
		  $body .= "如果上述文字点击无效，请把下面网页地址复制到浏览器地址栏中打开：  </small>";
		  $body .= "<span style='color:#666;'><a target='_blank' href='".config::get('sitedomain').Purl('member_login')."'> ".config::get('sitedomain').Purl('member_login')." </a></span></p></div>";		
	      sendmail($arr['email'],$subject,$body,$arr['username']);
		  $this->message('member_login','注册成功，请录完善个人信息');
		}
	}
	
	function logout(){
	    $this->user->logout('index',"恭喜您，用户注销成功");	
	}
	
	function forgotpassword(){
	  if(is_array($this->member)) location('member');
	  if(submit()){
		$username = $_POST['username'];
		$email = $_POST['email'];
		$seccode = $_POST['seccode'];
		$user = $this->mysql->select_one("select * from {$this->pre}user where username='{$username}' and email='{$email}'");
		if(config::get("mustcode")){
		  if($seccode=='') $this->message("go_back",'验证码不能为空');  
		  if($seccode!=$_SESSION['seccode']) $this->message("go_back",'验证码不正确');
		}
		if(!is_array($user)) $this->message('go_back','对不起，用户名或邮箱不匹配');
		$salt = $this->user->salt();
		$arr['forgotpassword'] = $this->user->password($salt,$user['salt']);
        $this->mysql->update("{$this->pre}user",$arr,"uid='{$user[uid]}'");
		$forgotpasswordurl = config::get("sitedomain").Purl("?mod=member&act=resetpassword&uid={$user[uid]}&salt={$salt}");
		$this->emailurl = 'http://mail.'.substr($email,strpos($email,'@')+1);
	    $subject = config::get('sitename').'密码找回';		
		$body = "<div style='line-height:1.5;font-size:14px;margin-bottom:15px;color:#4d4d4d;'>";
		$body .= "<strong style='display:block;margin-bottom:15px;'>";
		$body .= "亲爱的会员：{$user['username']}您好！</strong><p>您已于 ".formattime(time())." 进行密码找回操作。</p></div>";
		$body .= "<div style='margin-bottom:20px;'><strong style='display:block;margin-bottom:20px;font-size:14px;'>";
		$body .= "<a target='_blank' style='color:#f60;' href='{$forgotpasswordurl}'>点此找回密码</a></strong>";
		$body .= "<p style='color:#666;'><small style='display:block;font-size:12px;margin-bottom:5px;'>";
		$body .= "如果上述文字点击无效，请把下面网页地址复制到浏览器地址栏中打开：  </small><span style='color:#666;'>";
		$body .= "<a target='_blank' href='{$forgotpasswordurl}'> {$forgotpasswordurl} </a></span></p></div>";		
	    sendmail($user['email'],$subject,$body,$user['username']);
		$this->submit = true;
	  }
	}
	
	function resetpassword(){
	  if(is_array($this->member)) location('member');
	  $uid = $_GET['uid'];
	  $salt = $_GET['salt'];
	  $user = $this->mysql->select_one("select * from {$this->pre}user where uid='{$uid}'");
	  $this->username = $user['username'];
	  $forgotpassword = $this->user->password($salt,$user['salt']);
	  if($forgotpassword!=$user['forgotpassword']) location('member_forgotpassword');
	  if(submit()){
		$arr['forgotpassword'] = '';
		$arr['password'] = $this->user->password($_POST['newpassword'],$user['salt']);
        $this->mysql->update("{$this->pre}user",$arr,"uid='{$user[uid]}'");
		$this->message('member_login','密码重置成功');
	  }
	}
	
}
?>