<?php
/**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class UserAction{
	function __construct(){
		if(ACTION_NAME!="login" && ACTION_NAME!="register" && ACTION_NAME!="forget" && ACTION_NAME!="logout" && ACTION_NAME!="quicklogin"  && ACTION_NAME!='activating' && ACTION_NAME!='activation' && ACTION_NAME!='hello'){
			BaseAction::checkLogin();
			//if(!$_SESSION['email'] && ACTION_NAME!="info"){ Msg::show(array('msg'=>'您还未补充电子邮箱，请先填写邮箱。','url'=>'/user/info/','time'=>'100')); }
		}

	}
	function index(){
		$pageInfo['title'] = "用户管理中心 ".$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		load_class("user");
		$um = new UserModel();
		$userId = $_SESSION['userId'];
		$userInfo = $um->getInfo($userId);

		$act = getgpc("act","P");
		$inajax = getgpc("inajax","P");
		if($inajax==1){
			$ret = array('success'=>false,'message'=>'参数错误');
			if($act=="sendActivationEmail"){
				if($um->sendActivationEmail($_SESSION['userId'],getgpc('email','P'))){
					$ret = array('success'=>true,'message'=>'邮件发送成功');
				}else{
					$ret = array('success'=>false,'message'=>'邮件发送失败');
				}
			}else if($act=='sendMobileCode'){
				if($um->sendMobileCode($_SESSION['userId'])){
					$ret = array('success'=>true,'message'=>'验证码发送成功');
				}else{
					$ret = array('success'=>false,'message'=>'验证码发送失败');
				}		
			}else if($act=='verifyMobileCode'){
				if($um->verifyMobileCode($_SESSION['userId'],getgpc('code','P'))){
					$ret = array('success'=>true,'message'=>'手机号码验证成功');
				}else{
					$ret = array('success'=>false,'message'=>'手机号码验证失败');
				}			
			}
			if($um->msg){ $ret['message'] = $um->msg; }
			echo json_encode($ret);
			exit();
		}

		assign("userInfo",$userInfo);
		display();
	}
	function forget(){
		global $_G;
		load_class("user");
		$um = new UserModel();
		$act = getgpc("act","P");
		$inajax = getgpc("inajax","P");
		if($inajax==1){
			$ret = array('success'=>false,'message'=>$_G['lang']['admin']['failure']);
			if($act=="forget"){
				if($um->forget($_POST)){
					$ret = array('success'=>true,'message'=>$_G['lang']['user']['forget']['success']);
				}
			}else if($act=="getemail"){
				$loginName = getgpc("loginName","P");
				$email = $um->getEmail($loginName);
				if($email){
						$email = explode("@",$email);
						$email = substr($email[0],0,3).'****@'.$email[1];
						exit('{"success":true,"message":"成功","data":{"email":"'.$email.'"}}');			
				}else{
						exit('{"success":false,"message":"您没有注册邮箱"}');			
				}
			}
			if($um->msg){ $ret['message']=$um->msg; }
			echo json_encode($ret);exit;
		}
		$sm = new SeoModel();
		$pageInfo = $sm->pageInfo(10);
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']." - ".$GLOBALS['siteConf']['siteName']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/forget");	
	}
	function login(){
		global $db,$baseUrl;
		$userConfig=getConfig('user');if($userConfig['login']!=1){Msg::show('已关闭会员登录功能');}
		$inajax = getgpc("inajax","P");
		if(BaseAction::userLogin()){
			if($inajax==1){
				//exit('{"success":true,"message":"用户已经登录"}');
			}else{
				header('Location:'.$baseUrl.'user/');
			}
		}
		$act=getgpc("act","P");
		if($act=="login"){
			$loginId = getgpc("loginName");
			$loginPwd = getgpc("loginPwd");
			if($loginId && $loginPwd){

		$next = true;
		$where ='';
		if(strlen($loginId)==11 && is_numeric($loginId)){
			$where = "mobile='$loginId'";
		}else if(preg_match("/^([a-z0-9A-Z]+)$/",$loginId)){
			$where = "loginName='$loginId'";
		}else if(strpos($loginId,'@')){
			$where = "email='$loginId'";
		}else{
			$ret['message']='登录名不正确';
			$next = false;
		}
		if($next){
				$sql = "select id,loginPwd from qk_user where $where";
				$res = $db->getRow($sql);
		}
				if($next && $res){
					if($res['loginPwd']==md5($loginPwd)){
						load_class("user");
						$user = new UserModel();
						if($user->quickLogin($res['id'],false)){
							//print_R($_COOKIE);print_R($_SESSION);
							$msg  = array("success"=>true,"message"=>"登录成功");
						}else{
							$msg  = array("success"=>false,"message"=>"登录失败");
						}
					}else{
						$msg  = array("success"=>false,"message"=>"用户不存在或密码错误");
					}
				}else{
					$msg  = array("success"=>false,"message"=>"用户不存在或密码错误");
				}
			}else{
				$msg  = array("success"=>false,"message"=>"用户名和密码必填");
			}
			if($inajax==1){
				echo json_encode($msg);
			}else{
				Msg::show($msg['message']);
			}
			exit();
		}
		$cb=getgpc('cb','GP');
		if($cb){
			if(!preg_match("/:\/\/$host\//",$cb) && !preg_match("/^\//",$cb)){
				$cb='';
			}
		}
		if(!$cb){
			$referer = $_SERVER['HTTP_REFERER'];
			$host = $_SERVER['HTTP_HOST'];
			if(preg_match("/:\/\/$host\//",$referer)){
				$cb=$referer;
			}
		}
		assign('cb',$cb);
		$sm = new SeoModel();
		$pageInfo = $sm->pageInfo(7);
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		$style=getgpc('style');
		$tpl = "user/login.html";
		if($style=='dialog'){
			$tpl = "user/login_dialog.html";
		}
		$config=getConfig('user');
		assign('config',$user);
		$mod=getgpc('mod');
		if($mod!='login'){$mod='register';}
		assign('mod',$mod);
		display($tpl);
	}

	function quicklogin(){
		$type = getgpc("type");
		if($type){
			require(ROOT_PATH."plugins/quicklogin/".$type."/".$type.".inc.php");
		}else{
			header("Location:/user/login");
		}
	}

	function register(){
		global $db,$_G;
		$userConfig=getConfig('user');if($userConfig['register']!=1){Msg::show('已关闭会员注册功能');}
		load_class("user");
		$um = new UserModel();
		$act = getgpc("act","P");
		$inajax=getgpc('inajax','P');
		if($inajax==1){
			$ret = array('success'=>false,'message'=>'验证失败');
			if($act=="register"){
				if($userId = $um->register($_POST)){
					$um->quickLogin($userId,false);
					$ret = array('success'=>true,'message'=>$_G['lang']['register']['success'],'data'=>array('dispatchUrl'=>$GLOBALS['baseUrl'].'user'));
				}else{
					$ret['message'] = $um->msg?$um->msg:'用户注册失败';
				}
			}else if($act=='checkLoginName'){
				$loginName = getgpc('loginName','P');
				$ret['success'] = $um->checkLoginName($loginName);
				$ret['message'] = $um->msg?$um->msg:'账户名验证失败';
			}else if($act=='checkMobile'){
				$mobile = getgpc('mobile','P');
				$ret['success'] = $um->checkMobile($mobile);
				$ret['message'] = $um->msg?$um->msg:'手机号码验证失败';
			}else if($act=='checkEmail'){
				$email = getgpc('email','P');
				$ret['success'] = $um->checkEmail($email);
				$ret['message'] = $um->msg?$um->msg:'电子邮箱验证失败';
			}else if($act=='sendMobileCode'){
				$mobile = getgpc('mobile','P');
				if($um->sendMobileReg($mobile)){
					$ret = array('success'=>true,'message'=>'验证码发送成功','data'=>array('id'=>$um->mobileRegCodeId));
				}else{
					$ret = array('success'=>false,'message'=>$um->msg);
				}		
			}
			echo json_encode($ret);exit();
		}
		$sm = new SeoModel();
		$pageInfo = $sm->pageInfo(6);
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/register");
	}

	function regsuccess(){
		$loginName = getgpc("loginName","P");
		assign("loginName","$loginName");
		$sm = new SeoModel();
		$pageInfo = $sm->pageInfo(10);
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/regsuccess");
	}

	function logout(){
		$_SESSION['loginName'] = "";
		$_SESSION['userId'] = "";
		$_SESSION['nickname'] = "";
		setcookie("userId",null,time()-3600,"/");
		setcookie("loginName",null,time()-3600,"/");
		setcookie("account",null,time()-3600,"/");
		setcookie("nickname",null,time()-3600,"/");
		setcookie("ueidc_uid",null,time()-3600,"/");
		Msg::show(array("msg"=>"退出成功，即将带您返回网站首页","url"=>"/"));
	}

	function order(){
		global $db;
		load_class("order");
		$om = new OrderModel();
		$userId = $_SESSION['userId'];
		$act = getgpc("act","P");
		$inajax = getgpc("inajax","P");
		if($inajax==1){
			$id = getgpc("id","P");
			if($act=="pay" && $id && settype($id,"integer")){
				$order = $om->getInfo($id);
				if(!$order){
					exit('{"success":false,"message":"订单不存在或已经被删除'.$userId.$id.'","type":2}');
				}else{
					$iStatus = $order['iStatus'];
					$payStatusId = $order['payStatusId'];
					$amount = getgpc('amount','P');
					$status = $db->getOne("select name from qk_order_status where id='$iStatus'");
					$amountNow = sprintf("%01.2f",$order['amountNow']);
					if(($payStatusId==4 || $payStatusId==21)&&$amountNow>0&&$amount&&is_numeric($amount)){//
						if($amount!=$amountNow){ exit('{"success":false,"message":"支付金额 '.$amount.' 与订单当前所需支付金额 '.$amountNow.' 不一致","type":2}'); }
						$balance = $db->getOne("select sum(amount) from qk_finance where userId='$userId' and iStatus=1");
						if($balance>=$amount){
							$sql = "insert into qk_finance set amount='-$amount',typeId=2,userId='$userId',about='为订单 ".$order['id']." 支付',orderId='".$order['id']."',iStatus=1,effectiveTime=UNIX_TIMESTAMP(),posttime=UNIX_TIMESTAMP()";
							if($db->query($sql)){
								if(($payStatusId==4 || $payStatusId==21) && $amount==$amountNow){
									if($order['divideNum']>1){
										$payedAmount = $db->getOne("select sum(amount) from qk_finance where userId='$userId' and iStatus=1 and typeId=2 and orderId='".$order['id']."'");
										//echo $payedAmount.$order['amount'];exit;
										$cha=sprintf("%01.2f",$payedAmount)+sprintf("%01.2f",$order['amount']);
										//echo $cha;
										if($cha<0.01 && $cha>-0.01){
											$db->query("update `qk_order` set payStatusId=22 where id='".$order['id']."'");
										}else{
											$db->query("update `qk_order` set payStatusId=21 where id='".$order['id']."'");
										}
									}else{
										$db->query("update `qk_order` set payStatusId=22 where id='".$order['id']."'");
									}

								}
								//$sql = "update qk_order set iStatus=3 where id='".$order['id']."'";
								//$db->query($sql);
								exit('{"success":true,"message":"支付成功","type":3}');
							}else{
								exit('{"success":false,"message":"支付失败，数据存取失败","type":2}');
							}
						}else{
							exit('{"success":false,"message":"支付失败，账户余额不足","type":2}');
						}
					}else{
						exit('{"success":false,"message":"支付失败，订单失效或已经支付","type":2}');
					}
				}
			}else{
				exit('{"success":false,"message":"支付失败，缺少必需参数","type":2}');
			}
			exit();
		}//inajax
		$act = getgpc("act","G");
		if($act=="pay"){
			$id = getgpc("id","G");
			if($id && settype($id,"integer")){
				$orderInfo = $om->getInfo($id);
				assign("orderInfo",$orderInfo);
				/*
				if($orderInfo['iStatus']==4 || $orderInfo['amountNow']>0){
					$pay = new Pay();
					$paybankId = $orderInfo['paybankId'];
					$amount = $orderInfo['amountNow'];
					$orderId = $orderInfo['id'];
					$pay->order($paybankId,$amount,$orderId);exit();
				}
				*/
			}
			
		}

		$pageInfo['title'] = $GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		if($act=='pay'){
			display('user/order_pay');
		}else{
			display("user/order");
		}
	}

	function pay(){
		global $db,$baseUrl;
		$act = getgpc("act");
		if($act=="return"){
			$pay = new Pay();
			$outTradeNo = getgpc("out_trade_no");
			if($pay->verifyReturn($outTradeNo)){
				header("Location:".$baseUrl."pay/result/status/success");
			}else{
				header("Location:".$baseUrl."pay/result/status/failed");
			}
			exit();
		}
		if($act=="notify"){
			$pay = new Pay();
			$outTradeNo = getgpc("out_trade_no","P");
			$pay->verifyNotify($outTradeNo);
			exit();
		}

		$act = getgpc("act","P");
		if($act=="pay"){
			$loginName = getgpc("loginName","P");
			$amount = getgpc("amount","P");
			$payment =  getgpc("payment","P");
			$bank =  getgpc("bank","P");
			if($amount && settype($amount,"float") && $amount>0 && $payment && settype($payment,"float") && $payment>0){
				$pay = new Pay();
				$pay->order($payment,$amount,0,$loginName,$bank);
				exit();
			}
		}
		$sql = "select * from qk_payment where iStatus=1 and typeId in (select id from qk_paytype where parentId=1)";
		$paybank = $db->getAll($sql);
		assign("paybank",$paybank);
		$pageInfo['title'] = "在线支付 - ".$GLOBALS['siteConf']['siteName'];
		$payBanks = array(
			array('name'=>'银联在线支付 支持所有银联银行卡','bank'=>'UPOP','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/UPOP.gif'),
			array('name'=>'农业银行','bank'=>'ABC','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/ABC.gif'),
			array('name'=>'北京银行','bank'=>'BCCB','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/BCCB.gif'),
			array('name'=>'中国银行','bank'=>'BOC','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/BOC.gif'),
			array('name'=>'上海银行','bank'=>'BOS','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/BOS.gif'),
			array('name'=>'建设银行','bank'=>'CCB','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/CCB.gif'),
			array('name'=>'光大银行','bank'=>'CEB','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/CEB.gif'),
			array('name'=>'兴业银行','bank'=>'CIB','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/CIB.gif'),
			array('name'=>'中信银行','bank'=>'CNCB','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/CITIC.gif'),
			array('name'=>'招商银行','bank'=>'CMB','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/CMB.gif'),
			array('name'=>'民生银行','bank'=>'CMBC','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/CMBC.gif'),
			array('name'=>'交通银行','bank'=>'BOCOM','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/COMM.gif'),
			array('name'=>'广东发展银行','bank'=>'GDB','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/GDB.gif'),
			array('name'=>'华夏银行','bank'=>'HXB','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/HXB.gif'),
			array('name'=>'工商银行','bank'=>'ICBC','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/ICBC.gif'),
			array('name'=>'中国邮储银行','bank'=>'PSBC','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/PSBC.gif'),
			array('name'=>'浦东发展银行','bank'=>'SPDB','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/SPDB.gif'),
			array('name'=>'平安银行','bank'=>'PAB','img'=>'https://static.pay.sina.com.cn/cashier/resource/20130311/img/bank_logo/SZPAB.gif'),
		);
		assign("payBanks",$payBanks);
		assign("pageInfo",$pageInfo);
		display("user/pay");		
	}
	
	function finance(){	
		$pageInfo['title'] = "财务记录 - ".$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		load_class("user");
		$um = new UserModel();
		$userId = $_SESSION['userId'];
		$userInfo = $um->getInfo($userId);
		assign("userInfo",$userInfo);
		display("user/finance");
	}

	function info(){	
		load_class("user");
		$um = new UserModel();
		global $db;
		$userId = $_SESSION['userId'];
		$act = getgpc("act","P");
		$inajax = getgpc("inajax","P");
		$id = getgpc("id","P");
		if($inajax==1){
			$ret = array('success'=>false,'message'=>'参数错误');
			if($act=="edit" && $userId && settype($userId,"integer")){
				$_POST['id'] = $userId;//echo $userId;
				$ret['success'] = $um->edit($_POST,$userId);
			}
			if($um->msg){$ret['message']=$um->msg;}
			echo json_encode($ret);
			exit();
		}//echo $userId;exit;
		$userInfo = $um->getInfo($_SESSION['userId']);
		assign("userInfo",$userInfo);
		$pageInfo['title'] = "修改资料 - ".$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/info");
	}

	function password(){
		global $db;
		load_class("user");
		$um = new UserModel();
		$userId = $_SESSION['userId'];
		
		$act = getgpc("act","P");
		$inajax = getgpc("inajax","P");
		$id = getgpc("id","P");
		if($inajax==1){
			$ret=array('success'=>false,'message'=>'缺少参数');
			if($act=="edit" && $userId && settype($userId,"integer")){
				$ret['success'] = $um->password($_POST,$userId);
			}
			if($um->msg)$ret['message']=$um->msg;
			echo json_encode($ret);
			exit();
		}

		$userInfo = $db->getRow("select * from qk_user where id='$userId'");
		assign("userInfo",$userInfo);
		$pageInfo['title'] = "修改密码 - ".$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/password");
	}

	function address(){
		$userId = $_SESSION["userId"];
		load_class("user");
		$um = new UserModel();
		$inajax = getgpc("inajax");
		$act = getgpc("act","P");
		$id = getgpc("id","P");
		if($inajax==1){
			if($act=="edit" && $id && settype($id,"integer")){
				if($um->editAddr($_POST,$_SESSION['userId'])){
						exit('{"success":true,"message":"success","type":3,"errno":0}');			
				}
			}else if($act=="add"){
				if($um->addAddr($_POST,$_SESSION['userId'])){
						exit('{"success":true,"message":"success","type":3}');				
				}
			}else if($act=="del" && $id && settype($id,"integer")){
				if($um->delAddr($id,$_SESSION['userId'])){
						exit('{"success":true,"message":"success","type":3}');				
				}
			}
			$act=getgpc("act");
			if($act=="data"){
				echo $um->addrData();
				exit();
			}
			exit('{"success":false,"message":"error","type":1}');
		}
		$act = getgpc("act");
		$id = getgpc("id");
		if($act=="edit" && $id && settype($id,"integer")){
			$addrInfo = $um->getAddrInfo($id,$_SESSION['userId']);
			assign("addrInfo",$addrInfo);
		}
		$pageInfo['title'] = "My Personal Address Book - ".$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/address");
	}

	function profile(){
		//exit('{"error":0,"data":[]}');

		//exit('["errno":0,"data":{"icsonid":"Login_QQ__E87AC2B4F4BFF84B704593885134742B","email":"admin@qkweb.net","mobile":"15903611504","qq":0,"name":"","nick":"","face":0,"sex":"n","year":0,"month":0,"day":0,"identity":0,"level":1,"total_point":0,"idcard":"","status":0,"phone":"","fax":"","city":0,"address":"","zipcode":"","updatetime":1342643824,"regsrc":0,"regtime":1322267071,"note":"","is_manual_level":0,"type":0,"regIP":"218.29.87.101","exp_point":1336,"reg_warehouse_id":1,"recomend_score":-999999,"refer_uid":-999999,"vip_rank":-999999,"web_power_group":-999999,"status_bits":0,"retailerLevel":0,"cqq":0,"promotion_point":0,"cash_point":0,"uid":14853568,"point":0,"levelDesc":"铜盾会员","nextLevel":3001,"nextLevelDesc":"银盾会员","bindEmail":0,"bindMobile":1}]');
		/*
		exit('{"errno":0,"data":{"icsonid":"Login_QQ__E87AC2B4F4BFF84B704593885134742B","email":"admin@qkweb.net","mobile":"15903611504","qq":0,"name":"","nick":"","face":0,"sex":"n","year":0,"month":0,"day":0,"identity":0,"level":1,"total_point":0,"idcard":"","status":0,"phone":"","fax":"","city":0,"address":"","zipcode":"","updatetime":1342643824,"regsrc":0,"regtime":1322267071,"note":"","is_manual_level":0,"type":0,"regIP":"218.29.87.101","exp_point":1336,"reg_warehouse_id":1,"recomend_score":-999999,"refer_uid":-999999,"vip_rank":-999999,"web_power_group":-999999,"status_bits":0,"retailerLevel":0,"cqq":0,"promotion_point":0,"cash_point":0,"uid":14853568,"point":0,"levelDesc":"铜盾会员","nextLevel":3001,"nextLevelDesc":"银盾会员","bindEmail":0,"bindMobile":1}}');
		*/
		//
		load_class("user");
		$um = new UserModel();
		$userId = $_SESSION['userId'];

		$act = getgpc("act","P");
		$inajax = getgpc("inajax","P");
		$id = getgpc("id","P");
		if($inajax==1){
			if($act=="edit" && $userId && settype($userId,"integer")){
				echo $um->edit($_POST,$userId);
			}else{
				exit('{"success":false,"message":"缺少参数","type":1}');
			}
			exit();
		}

		$userInfo = $um->getInfo($userId);
		assign("userInfo",$userInfo);

		display("user/profile");
	}

	function coupon(){
		
		display("user/coupon");
	}

	function addr(){
		display("user/addr");
	}

	function prize(){
		global $baseUrl;
		$act = getgpc("act");
		$id = getgpc("id");
		if($act=="get" && $id && settype($id,"integer")){
			global $db;
			$userId = $_SESSION['userId'];
			$sql = "select eventGoodsId,iStatus from qk_user_prize where id='$id' and userId='$userId'";
			$prizeInfo = $db->getRow($sql);
			$eventGoodsId = $prizeInfo['eventGoodsId'];
			$iStatus = $prizeInfo['iStatus'];
			if($eventGoodsId && $iStatus==0){
				$sql = "select goodsId from qk_event_goods where id='$eventGoodsId'";
				$goodsId = $db->getOne($sql);
				load_class("cart");
				$cm = new CartModel();
				$params = array("goodsId"=>$goodsId,"pnum"=>1,"prizeId"=>$id);
				$cm->add($params);
				header("Location:".$baseUrl."purchase/shoppingcart");
			}else{
				$result = "奖品已经被领";
				assign("result",$result);
			}
		}
		display("user/prize");
	}

	function child(){
		display("user/child");
	}

	function score(){
		load_class("user");
		$userId = $_SESSION['userId'];
		$um = new UserModel();
		$userscore = $um->getScore($userId);
		assign("userscore",$userscore);
		display("user/score");
	}

	function favorite(){
		load_class("user");
		$um = new UserModel();
		$userId = $_SESSION['userId'];

		$inajax = getgpc("inajax","P");
		$act = getgpc("act","P");
		$id = getgpc("id","P");
		if($inajax==1){
			if($act=="add"){
				echo $um->addFavorite($_POST,$userId);
			}else if($act=="del" && $id && intval($id)){
				echo $um->delFavorite($id,$userId);
			}
			exit();
		}
		display("user/favorite");
	}

	function myorder(){
		$userId = $_SESSION['userId'];
		load_class("user");
		$um = new UserModel();
		$act = getgpc("act","P");
		$inajax = getgpc("inajax","P");
		if($inajax==1){
			$msg = array("success"=>false,"message"=>"操作失败");
			if($act=="add"){
				if($um->addMyOrder($_POST)){
					$msg = array("success"=>true,"message"=>"添加成功");
				}
			}else if($act=="audit"){
				$col = "iStatus";
				$val = getgpc("iStatus","P");
				$id = getgpc("id","P");
			}
			$msg = json_encode($msg);
			exit($msg);
		}
		display("user/myorder");
	}

	function getinfo(){
		load_class("user");
		$um = new UserModel();
		$userId = $_SESSION['userId'];
		$userInfo = $um->getInfo($userId);
		if(!$userInfo['nickname']){$userInfo['nickname']=$userInfo['loginName'];}
		if(!$userInfo['score']){$userInfo['score']=0;}
		echo '{"iUin":228759217,"errorCode":0,"errorMessage":"ok","sNick":"'.$userInfo['nickname'].'","isClub":1,"isLightIcon":"1\r\n","clubLevel":6,"jifenBalance":'.$userInfo['score'].',"isOpenCFT":1,"jifenPeriod":"2013-12-31"}';
	}
	function goods(){
		$pageInfo['title'] = "我推荐的商品 - ".$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/goods");
	}
	function status(){
		load_class("user");
		$um = new UserModel();
		$userId = $_SESSION['userId'];
		$userInfo = $um->getInfo($userId);
		$ret = array("success"=>true,"message"=>"","data"=>array("loginName"=>$userInfo['loginName']));
		echo json_encode($ret);
	}
	function domain(){
		$pageInfo['title'] = "域名管理 - 用户管理中心";
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/domain.html");
	}
	function hosting(){
		$pageInfo['title'] = "空间管理 - 用户管理中心";
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/hosting.html");
	}
	function sms(){
		$pageInfo['title'] = "短信平台管理 - 用户管理中心";
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/sms.html");
	}
	function tel(){
		$pageInfo['title'] = "400电话管理 - 用户管理中心";
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/tel.html");
	}
	function cloudserver(){
		$pageInfo['title'] = "云主机管理 - 用户管理中心";
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/cloudserver.html");
	}
	function com(){
		load_class("user");
		$user = new UserModel();
		load_class("com");
		$com = new ComModel();
		$userInfo = $user->getInfo($_SESSION['userId']);
		$act = getgpc("act","P");
		$inajax = getgpc("inajax","P");
		if($inajax==1){
			$ret=array("success"=>false,"message"=>"参数错误");
			if($act=="edit"){
				if(!($_POST['id']=$userInfo['comId'])){$act="add";}
				$_POST['userId'] = $_SESSION['userId'];
				if($comId = $com->$act($_POST)){
					if($act=="add"){ global $db;$db->query("update qk_user set comId='$comId' where id='".$userInfo['id']."'"); }
					$ret['success'] = true;
				}
				if($com->msg){$ret['message']=$com->msg;}
			}
			echo json_encode($ret);
			exit();
		}
		if($userInfo['comId']){
			assign("comInfo",$com->getInfo($userInfo['comId']));
		}
		$pageInfo['title'] = "商家信息管理 - 用户管理中心";
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display("user/com");
	}

	function activating(){
		$username = getgpc('username');
		$rand = getgpc('rand');
		load_class("user");
		$um = new UserModel();
		if(!$username || !$rand){return false;}
		if($um->activatingEmail($username,$rand)){
			Msg::show('邮件激活成功');
		}else{
			Msg::show('邮件激活失败');
		}
	}

	function prepaidcard(){
		$act=getgpc('act','P');
		$inajax=getgpc('inajax','P');
		if($inajax=='1'){
			$ret=array('success'=>false,'message'=>'参数错误');
			if($act=='pay'){
				$c=true;
				$number=getgpc('number','P');
				$password=getgpc('password','P');
				if(!preg_match("/^([a-zA-Z0-9]+)$/",$number)){ $ret['message']='充值卡账号需为字母或数字'; $c=false; }
				if($c && !preg_match("/^([a-zA-Z0-9]+)$/",$password)){ $ret['message']='充值卡密码需为字母或数字'; $c=false; }
				if($c){
					$sql = "select * from `qk_prepaidcard` where iStatus=1 and userId=0 and useoftime=0 and number='$number'";
					$info = $GLOBALS['db']->getRow($sql);
					if(!$info){ $ret['message']='充值卡账号不存在'; $c=false; }
				}
				if($c){
					if($info['password']!=$password){$ret['message']='充值卡密码错误';$c=false;}else{
						$amount=$info['amount'];
						$sql = "insert into `qk_finance` set userId='".$_SESSION['userId']."',amount='$amount',iStatus=1,typeId=1,about='使用充值卡".$number."充值',orderId=0,effectiveTime=UNIX_TIMESTAMP(),outTradeNo='',posttime=UNIX_TIMESTAMP()";
						$GLOBALS['db']->query($sql);
						$sql="update `qk_prepaidcard` set iStatus=0,userId='".$_SESSION['userId']."',useoftime=UNIX_TIMESTAMP(),ip='".getIp()."' where id='".$info['id']."'";
						$GLOBALS['db']->query($sql);
						$ret=array('success'=>true,'message'=>'使用充值卡充值 '.$amount.' 元成功');
					}
				}
			}
			echo json_encode($ret);exit;
		}
		$pageInfo['title'] = "充值卡充值 - 用户管理中心";
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display('user/prepaidcard');
	}
	function withdraw(){
		load_class("user");
		$um = new UserModel();
		$userId = $_SESSION['userId'];
		$userInfo = $um->getInfo($userId);
		assign("userInfo",$userInfo);

		$act=getgpc('act','P');
		$inajax=getgpc('inajax','P');
		if($inajax=='1'){
			$ret=array('success'=>false,'message'=>'参数错误');
			if($act=='withdraw'){
				$c=true;
				$amount=getgpc('amount','P');
				$bank=getgpc('bank','P');
				$account=getgpc('account','P');
				$name=getgpc('name','P');
				$mobile=getgpc('mobile','P');

				if(!preg_match("/^([0-9]+)$/",$amount)){ $ret['message']='请填写提现金额'; $c=false; }
				if($c && $amount<=0){$ret['message']='提现金额需大于0'; $c=false;}
				if($c && $amount>$userInfo['amount']){$ret['message']='提现金额需小于当前余额'; $c=false;}
				if($c && !preg_match("/^([0-9]+)$/",$account)){ $ret['message']='银行卡账号数字'; $c=false; }
				if($c && !$bank){$ret['message']='请填写银行名称'; $c=false;}
				if($c && !$account){$ret['message']='请填写银行卡账号'; $c=false;}
				if($c && !$name){$ret['message']='请填写开户人姓名'; $c=false;}
				if($c && !preg_match("/^1([0-9]{10})$/",$mobile)){$ret['message']='请填写联系手机'; $c=false;}
				if($c){
						$about = '银行：'.$bank."\r\n卡号：".$account."\r\n户名".$name."\r\n手机：".$mobile;
						$sql = "insert into `qk_finance` set userId='".$_SESSION['userId']."',amount='-$amount',iStatus=1,typeId=4,about='".$about."',orderId=0,effectiveTime=0,outTradeNo='',posttime=UNIX_TIMESTAMP()";
						$GLOBALS['db']->query($sql);
						$ret=array('success'=>true,'message'=>'申请提现 '.$amount.' 元成功');	
				}
			}
			echo json_encode($ret);exit;
		}

		$pageInfo['title'] = "申请提现 - 用户管理中心";
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display('user/withdraw');
	}

	function activation(){
		load_class("user");
		$um = new UserModel();
		//$userId = $_SESSION['userId'];
		//$userInfo = $um->getInfo($userId);
		//assign("userInfo",$userInfo);
		$act=getgpc('act','P');
		$inajax=getgpc('inajax','P');
		if($inajax=='1'){
			$ret=array('success'=>false,'message'=>'参数错误');
			if($act=='activation'){
				$number=getgpc('number','P');
				$password=getgpc('password','P');
				$c=true;
				if(!preg_match("/^(\d+)$/",$number)){$ret['message']='激活码格式错误';$c=false;}
				if($c && !preg_match("/^([a-z0-9]+)$/",$password)){$ret['message']='密码格式错误';$c=false;}
				if($c){
					global $db;
					$sql="select id from qk_user where id='".$number."' and loginPwd='".md5($password)."' and activated=0";//
					if($db->getOne($sql)){
						$code=randCoupon();
						$sql = "insert into qk_user_coupon set userId='$number',couponId='3',money=30,iStatus=1,endtime=0,posttime=UNIX_TIMESTAMP(),number='$code'";
						$db->query($sql);
						$um->quickLogin($number,false);
						$ret=array('success'=>true,'message'=>'激活成功，正在登录，请稍候...');
						$db->query("update qk_user set activated=1,activationTime=UNIX_TIMESTAMP() where id='$number'");
					}else{
						$ret['message']='激活失败：已激活或密码错误';
					}
				}
			}
			echo json_encode($ret);exit;
		}
		$pageInfo['title'] = "会员激活 - 用户管理中心";
		$pageInfo['title'] = $pageInfo['title']?$pageInfo['title']:$GLOBALS['siteConf']['siteName'];
		assign("pageInfo",$pageInfo);
		display('user/activation');
	}

	function appraise(){
		$id=getgpc('id');
		$inajax=getgpc('inajax','P');
		$ret=array('success'=>false,'message'=>'参数错误');
		if(!$id || !is_numeric($id)){if($inajax==1){$ret['message']='订单不存在';echo json_encode($ret);exit;}else{Msg::show('订单不存在');}}
		load_class('order');
		$order = new OrderModel();
		$orderInfo = $order->getInfo(array('id'=>$id,'userId'=>$_SESSION['userId']));
		if(!$orderInfo){if($inajax==1){$ret['message']='订单不存在';echo json_encode($ret);exit;}else{Msg::show('订单不存在');}}
		if($inajax==1){
			$c=true;
			$act=getgpc('act','P');
			if($act=='appraise'){
				$aid=getgpc('aid','P');
				$point=getgpc('point','P');
				$appraise=getgpc('appraise','P');
				if(!$point || !is_numeric($point) || $point<1 || $point>5){$ret['message']='请给商品选择评分';$c=false;}
				if($c && !$appraise){ $ret['message']='请填写评价内容';$c=false; }
				if($c && strlen($appraise)>1500){$ret['message']='评价内容不得超过500字';$c=false; }
				global $db;
				$orderGoodsInfo=$db->getRow("select * from `qk_order_goods` where `id`='$aid' and `orderId`='".$id."'");
				if($c && !$orderGoodsInfo){$ret['message']='您没有购买此商品';$c=false;}
				//if($c && $orderGoodsInfo['point']){ $ret['message']='您已经进行过评价';$c=false; }
				if($c){
					$sql = "update `qk_order_goods` set `point`='$point',`appraise`='$appraise',`appraisetime`=UNIX_TIMESTAMP() where `id`='$aid'";$db->query($sql);
					$ret=array('success'=>true,'message'=>'评价成功，感谢您对我们的支持！');
				}
			}
			
			echo json_encode($ret);exit;
		}
		//print_r($orderInfo['goods']);
		$commentNum=0;
		foreach($orderInfo['goods'] as $goods){if($goods['appraisetime']==0 && $goods['point']==0){$commentNum++;}}
		assign('commentNum',$commentNum);
		assign('orderInfo',$orderInfo);
		display('user/appraise');
	}

	function hello(){
		$callback = getgpc('callback');
		if(!preg_match("/^([a-zA-Z0-9_]+)$/",$callback)){ header("HTTP/1.1 404 Not Found");exit('Error 404'); }
		if($_SESSION['userId'] && $_COOKIE['account']){
			$ret = array('info'=>'您好，'.$_COOKIE['account'].'！<a href="/user/">[用户中心]</a> <a href="/user/logout" class="link-logout">[退出]</a>');
		}else{
			$ret = array('info'=>'欢迎来到'.$GLOBALS['siteConf']['shortName'].'！<span><a href="javascript:login();" class="link-login">亲，请登录</a> <a href="javascript:regist();"  class="link-regist">免费注册</a></span>');	
		}
		echo $callback.'('.json_encode($ret).')';
	}

	function service(){
		display('user/service');
	}
}
?>