CollarController.php 10.8 KB
<?php
class Home_CollarController extends QLib_Controller_Home_Action
{
	public function init()
	{
		$this->QLayoutTitle()->headTitle('优惠券领取');
		
	}
	
	public function indexAction()
	{
		$this->QLayoutScript()->offsetSetFile(61, $this->_js('jqueryui'));
		$this->QLayoutLink()->offsetSetStylesheet(30, $this->_css('common_new'))->offsetSetStylesheet(40, $this->_css('common'))->offsetSetStylesheet(31, $this->_css('jqueryuicss'));
		$code = $this->helpGquery('code');
		$this->view->collar = $collar = QINCoupons_Models_Collar_Client::getOneCollarByCode($code);
		empty($collar) ? $this->helpGo('/', '没有需要领取的优惠券') : true;
		
		//获取用户vip信息
		$this->view->userVipInfo = QINPassport_Models_Vip_Client::getVipDetailInfo($this->uid);
		
		try{
			$obj = QINCoupons_Collar_Factory::factory((int)$collar['collar_type']);
			$status = $obj->check($collar, $this->uid);
		}catch (Exception $e){
			$this->helpJsRedirect($e->getMessage(),'window.location="/home/coupons?t=' . microtime(true).'";');
		}
	}
	
	public function emailAction()
	{
		
		$code = Q_Utils_Function::base64_url_decode($this->helpGquery('code'));
		$info = $this->checkCode($code);
		if(empty($info['code']))
		{
			$this->helpJsRedirect('邮箱验证失败', 'window.location.href="/";');
		}
		$verif = QINPassport_Models_Verification_Client::getVerificationByUser($this->uid);
		$url = '/home/collar?code=' . $info['code'];
		if(!empty($verif[1])){
			$this->helpGo($url);
		}
		$ref = '/';
		(empty($info) || empty($info['email']) || empty($info['code'])) ? $this->helpRefresh($ref,'内容无效') : true;
		
		$status = QINPassport_Models_Verification_Client::setVerification($this->uid, $info['email'], 1);
		$status == true ? $this->helpGo($url) : $this->helpRefresh($ref, '邮箱验证失败,请稍后再试');
	}
	
	public function checkCode($code)
	{
		if (empty($code))
		{
			return array();
		}
		$info = json_decode(Q_Utils_AuthCode::decode($code, QCPassport_Configs_Site::$secret_key), true);
		if (empty($info['create_time']) || $info['create_time'] < 1 || (time() - $info['create_time']) > 86400)
		{
			return array();
		}
		return $info;
	}
	
	public function checkemailAction()
	{
		$this->_helper->layout()->disableLayout();
		$validator = new Zend_Validate_EmailAddress();

		$email = $this->helpGquery('email');
		(empty($email) || !preg_match('/^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/si', $email)) ? $this->helpJsonResult(401,'您输入的邮件地址不正确') : true;
		
		$verif = QINPassport_Models_Verification_Client::getVerificationByAccount($email);
		!empty($verif) ? $this->helpJsonResult(401, '该邮箱地址已经被验证') : true;
		
		$code = $this->helpGquery('code');
		empty($code) ? $this->helpJsonResult(401, '参数错误') : true;
		
		list( $name,$domain ) = explode('@', $email);
		$domain = 'http://' . (($domain == 'gmail.com') ? 'mail.google.com' : 'mail.' . $domain);
		
		$server = QConfigs_Mail::$mailConfigs[RELEASE_ENV];
		$mailTransport = new Zend_Mail_Transport_Smtp($server);
		
		$mail = new Zend_Mail('utf-8');
		$str = json_encode(array(
				'email' => $email,
				'code' => $code,
				'create_time' => time()
		));
		$secret_key = Q_Utils_AuthCode::encode($str, QCPassport_Configs_Site::$secret_key);
		$url = 'http://www.yohobuy.com/home/collar/email?code=' . Q_Utils_Function::base64_url_encode($secret_key);
		
		$html = '<table width="692" height="434" border="0" cellspacing="0" cellpadding="0" background="http://static.yohobuy.com/images/email_bg.png" style="width:692px;height:434px;overflow:hidden;line-height:22px;font-family:微软雅黑;" align="center"><tr><td height="105" style="height:105px;" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="270" height="95" style="width:270px;"><a href="http://www.yohobuy.com" title="YOHO!有货 www.yohobuy.com-全球年轻人流行商品购物平台" target="_blank" style="width:270px;height:95px; text-decoration:none;float:left;">&nbsp;</a></td><td align="right" valign="bottom"  style="line-height:20px;font-size:12px;font-family:微软雅黑;text-decoration:none;color:#333;"><a href="http://www.yohobuy.com/help" target="_blank" style="line-height:20px;font-size:12px;font-family:微软雅黑;text-decoration:none;color:#333;">帮助中心</a></td><td width="160" style="width:160px;">&nbsp;</td></tr></table></td></tr><tr><td style="font-size:12px;font-family:微软雅黑;color:#333;word-wrap:break-word; overflow:hidden;" valign="top"><table width="592" border="0" cellspacing="0" cellpadding="0" align="center"><tr><td><br /><br />尊敬的用户,您好!<br />您在'.date('Y年m月d日 H时i分').'申请验证邮箱,点击以下链接,即可完成验证。<br /><br /><span>为保障您的帐户安全,请在24小时内点击该链接,您也可以将链接复制到浏览器地址栏访问</span><br /><br /><div style="width:592px;word-wrap:break-word; overflow:hidden; line-height:14px;"><a href="'.$url.'" target="_blank" style="color:#e8044f;text-decoration:none;">'.$url.'</a></div><br />如果您有任何疑问欢迎查阅 <a href="http://www.yohobuy.com/help" target="_blank" style="color:#e8044f;text-decoration:none;">帮助中心</a> 或者 联系我们:<br />客服热线:400-889-9646(9:00-22:30,周一至周日) </td></tr></table></td></tr></table>';
		$mail->setBodyHtml($html);
		$mail->setSubject('邮箱验证');
		$mail->setFrom('do_replay@yoho.cn', 'YOHO!有货');
		
		$info = QINPassport_Models_Profile_Base_Client::getBaseInfoByUid($this->uid);
		$toname = empty($info['username']) ? '' : $info['username'];
		$mail->addTo($email, $toname);
		$status = $mail->send($mailTransport);
		
		$data = array(
			'url' => $domain	
		);
		$status == true ? $this->helpJsonResult(200,'发送成功', $data) : $this->helpJsonResult(400, '发送失败');
	}
	
	public function checkverficationAction()
	{
		$account = $this->helpGquery('account');
		empty($account) ? $this->helpJsonResult(400, '账号为空') : true;
		$verif = QINPassport_Models_Verification_Client::getVerificationByAccount($account);
		empty($verif) ? $this->helpJsonResult(200) : $this->helpJsonResult(402, '该账号已经被验证过');
	}
	
	public function checkmobileAction()
	{
		$mobile_code = (int)$this->helpGpost('mobile_code', 0);
		empty($mobile_code) ? $this->helpJsRedirect('手机校验码为空') : true;
		
		$mobile = $this->helpGpost('mobile_num');
		$code = (int)$this->helpSession('passport_site')->__get('check_mobile_' . $mobile);
		($code != $mobile_code) ? $this->helpJsRedirect('手机校验码错误') : true;
		
		$verif = QINPassport_Models_Verification_Client::getVerificationByAccount($mobile);
		(!empty($verif)) ? $this->helpJsRedirect('该手机号已经被验证过') : true;
		
		$status = QINPassport_Models_Verification_Client::setVerification($this->uid, $mobile, 2);
		
		$status == true ? $this->helpRefresh($_SERVER['HTTP_REFERER']) : $this->helpJsRedirect('验证失败');
	}
	
	public function sendsmsAction()
	{
		$this->_helper->layout()->disableLayout();
        $mobile = intval($this->helpGquery('mobile'));
        (empty($mobile) || !preg_match('/^1[3|4|5|8][0-9]\\d{8}$/si', $mobile)) ? $this->helpJsonResult(402, '手机号码不正确') : true;
        
        $code = $this->helpSession('passport_site')->__get('check_mobile_' . $mobile);
        if (empty($code)) {
            $code = $this->getCode(4);
            $this->helpSession('passport_site')->__set('check_mobile_' . $mobile, $code);
        }
        
        $sms = QCLib_Sms_Factory::factory();
        $message = '欢迎验证手机号码, 您申请的短信校验码为:' . $code . ',请将收到的校验码填写在网页上完成校验。';
        $status = $sms->sendSms(array($mobile), $message);
        $status == true ? $this->helpJsonResult(200, '发送成功') : $this->helpJsonResult(403, '发送失败');
	}
	
	/**
	 * 生成手机校验码
	 *
	 * @param Integer $length
	 * @return Interger
	 */
	private function getCode($length = 4)
	{
		$code = mt_rand();
		return substr($code, 0, 4);
	}
	
	public function collarcouponAction()
	{
		$this->_helper->layout()->disableLayout();
		$code = $this->helpGquery('code');
		(empty($code)) ? $this->helpJsonResult(400,'参数错误') : true;
		
		$collar = QINCoupons_Models_Collar_Client::getOneCollarByCode($code);
		empty($collar) ? $this->helpJsonResult(401,'您要领的优惠券不存在') : true;
		
		//验证优惠券可以领券
		try {
			$obj = QINCoupons_Collar_Factory::factory($collar['collar_type']);
			$obj->check($collar, $this->uid);
			if($obj->checkAccount($this->uid) == false){
				$this->helpJsonResult(402, '对不起,你的账号尚未全部验证,无法领券。');
			}
		}catch (Exception $e){
			$this->helpJsRedirect($e->getMessage());
		}
		if($collar['is_suit'] == 'Y')
		{
			$result = $this->sendSuitCoupons($collar['id']);
		}else{
			$couponIds = $this->helpGquery('coupon_ids');
			(count($couponIds) > $collar['collar_num']) ? $this->helpJsonResult(402, '领取失败。领取的数量超过最大领取数量') : true;
			$result = $this->sendCoupons($collar['id']);
		}
		$result == true ? $this->helpJsonResult(200, '领取成功') : $this->helpJsonResult(400, '领取失败.请联系客服');
	}
	
	
	private function sendCoupons($collar_id)
	{
		if((int)$collar_id < 1)
		{
			return false;
		}
		$key = $this->uid . '_' . $collar_id;
		$mc = QINLib_Utils::Mc();
		$status = $mc->add($key, 1, 60);
		($status == false) ? $this->helpJsonResult(403, '请不要重复提交') : true;
		$couponIds = $this->helpGquery('coupon_ids');
		(empty($couponIds)) ? $this->helpJsonResult(403, '请选择要领的优惠券') : true;
		$error = array();
		foreach ($couponIds as $coupon_id)
		{
			$res = $this->send($collar_id, $coupon_id);
			if(!$res){
				$error[] = $coupon_id;
			}
		}
		return empty($error) ? true : false;
	}
	
	private function sendSuitCoupons($collar_id)
	{
		if((int)$collar_id < 1){
			return false;
		}
		$coupons = QINCoupons_Models_Collar_Relation_Client::getRelation($collar_id);
		$error = array();
		foreach ($coupons as $c)
		{
			$res = $this->send($collar_id, $c['coupon_id']);
			if(!$res){
				$error[] = $c['coupon_id'];
			}
		}
		return empty($error) ? true : false;
	}
	
	private function send($collar_id, $coupon_id)
	{
		if((int)$collar_id < 1 || (int)$coupon_id < 1){
			return false;
		}
		try {
			//发券
			$coupon_code = QINPromotion_Utils_Coupons::sendCoupon($coupon_id, $this->uid);
			if(empty($coupon_code)){
				return false;
			}
			$logStatus = QINCoupons_Models_Collar_Logs_Client::setLogs($this->uid, $collar_id, $coupon_id, $coupon_code);
		}catch (Exception $e){
			$data = array(
				'collar_id' => $collar_id,
				'coupon_id' => $coupon_id,
				'message' => $e->getMessage()
			);
			QINLogs_Sys::logger('collar_coupons', 'collar', $data, QINLogs_Sys::ERROR_ERROR);
			return false;
		}
		return true;
	}
}