<?php
/**
 * Created by PhpStorm.
 * User: xmy 280564871@qq.com
 * Date: 2017/4/1
 * Time: 10:14
 */

namespace App\Controller;

use App\Logic\UserLogic;

class ShareController extends BaseController{


	public function down(){
		$this->display();
	}

	public function register(){

		$this->display();
	}

	/**
	 * 获取分享链接
	 * @param $token
	 * author: xmy 280564871@qq.com
	 */
	public function get_share_url($token){
		$this->auth($token);
		$url = U("Share/register",['invite_account'=>USER_ACCOUNT],true,true);
		$this->set_message(200,1,$url);
	}

	/**
	 * 邀请好友注册
	 * @param $phone
	 * @param $password
	 * @param $v_code
	 * @param string $invite_account
	 * author: xmy 280564871@qq.com
	 */
	
	public function share_register($phone,$password,$v_code,$invite_account="",$game_id='')
	{
		
		if(!preg_match('/^1[3456789][0-9]{9}$/',$phone)) {
			$this->set_message(-1,'手机格式不正确');
		}
		
		#验证短信验证码
		$result = R('Common/Sms/verify_sms_code',array($phone,$v_code,false));
		
		if($result['code']==200) {
			
			if(!$password) {$this->set_message(-1,'密码不能为空');}
			
			if(strlen($password)<6 || strlen($password) > 32) {$this->set_message(-1,'密码格式不正确');}
			
			session($phone,null);
			
			$user['account'] = $phone;
			$user['password'] = $password;
			$result = 1;
			if ($result > 0) {
				$result = D('User', 'Logic')->userRegisterByApp($user);
			}
			if($result < 0){
				$this->set_message(-1, $result);
			}

			if(!empty($invite_account)) {

				//添加邀请人记录
				D("ShareRecord")->addShareRecord($invite_account, $phone,$game_id);

				//添加邀请好友注册积分
				D("PointRecord")->addPointByType("invite_friend", get_user_id($invite_account),$result,$game_id);
			}

			$this->set_message(1,1,$user);
		}else{
			switch ($result['code']){
					case 1020:
							$this->set_message(1020,"请先获取验证码");
							break;
					case 1021:
							$this->set_message(1021,"验证码超时");
							break;
					case 1022:
							$this->set_message(1022,"验证码错误");
			}
		}
	}
	
	public function share_register2($phone,$password,$v_code,$invite_account="",$game_id='')
	{
		
		if(!preg_match('/^1[3456789][0-9]{9}$/',$phone)) {
			$this->set_message(-1,'手机格式不正确');
		}
		
		#验证短信验证码
		$code_result = UserLogic::smsVerify2($phone,$v_code,0);
		if($code_result == UserLogic::RETURN_SUCCESS) {
			
			if(!$password) {$this->set_message(-1,'密码不能为空');}
			
			if(strlen($password)<6 || strlen($password) > 32) {$this->set_message(-1,'密码格式不正确');}
			
			session($phone,null);
			
			$user['account'] = $phone;
			$user['password'] = $password;
			$result = 1;
			if ($result > 0) {
				$result = D('User', 'Logic')->userRegisterByApp($user);
			}
			if($result < 0){
				$this->set_message(-1, $result);
			}

			if(!empty($invite_account)) {

				//添加邀请人记录
				D("ShareRecord")->addShareRecord($invite_account, $phone,$game_id);

				//添加邀请好友注册积分
				D("PointRecord")->addPointByType("invite_friend", get_user_id($invite_account),$result,$game_id);
			}

			$this->set_message(1,1,$user);
		}else{
			switch($code_result) {
				case 2:{$this->set_message(-1,'请先获取验证码');};break;
				case -98:{$this->set_message(-1,'验证码已失效,请重新获取');};break;
				case -97:{$this->set_message(-1,'验证码不正确');};break;
				default:$this->set_message(-1,$code_result);
			}
		}
	}

	/**
	 * 获取邀请记录
	 * @param $token
	 * author: xmy 280564871@qq.com
	 */
	public function get_my_invite_record($token){
		$this->auth($token);
		$invite_id = get_user_id(USER_ACCOUNT);
		$data = D("ShareRecord")->getMyInviteRecord($invite_id);
		if (empty($data)){
			$this->set_message(1064,"暂无记录");
		}else{
			$this->set_message(200,1,$data);
		}
	}


	/**
	 * 获取用户邀请统计
	 * @param $token
	 * author: xmy 280564871@qq.com
	 */
	public function get_user_invite_info($token){
		$this->auth($token);
		$invite_id = get_user_id(USER_ACCOUNT);
		$data = D("ShareRecord")->getUserInviteInfo($invite_id);
		if (empty($data)){
			$this->set_message(1064,"暂无记录");
		}else{
			$this->set_message(200,1,$data);
		}
	}

}