<?php



if(!defined('IN_DISCUZ')) {

	exit('Access Denied');

}



//$bind = DB::query("DELETE FROM ".DB::table('haodaibao_bind')." WHERE uid=".$_G['uid']);

$haodaibao_index = 'http://mm.haodaibao.com/?access_is_app=1';

//$haodaibao_buy = 'http://mm.haodaibao.com/f/memberBidInfo/dqrxbDetail_buy?access_is_app=1&mbid=';



if($_GET['action'] == 'login'){



	if(!$_G['uid']){

		header('Location:'.$haodaibao_index);

		exit;

	}



	$bind = DB::fetch_first("SELECT * FROM ".DB::table('haodaibao_bind')." WHERE uid=".$_G['uid']);



	//没有绑定登录好贷宝 

	if( (empty($bind) || $_GET['rebind'] == 1) && !empty($_GET['jump_id'])){

		$auth = urlencode(trim($_SERVER['HTTP_MOCUZAUTH']));

		$jump_id = urlencode($_GET['jump_id']);

		$url_str = "&auth={$auth}&jump_id={$jump_id}";



		include './template/haodaibao/login.php';

		exit;

	}



	if(empty($bind)){

		header('Location:'.$haodaibao_index);

		exit;

	}

	

	//绑定后取出token跳转

	$access_token = urlencode($bind['access_token']);



	//两种跳转模式

	if(empty($_GET['jump_id'])){ //首页跳转

		$jump = $haodaibao_index."&access_token={$access_token}";

	}else{

		$_GET['jump_id'] = urldecode($_GET['jump_id']);

		$url_info = parse_url($_GET['jump_id']);

		parse_str($url_info['query'], $out);

		$out['access_token'] = $access_token;

		$jump = $url_info['scheme'].'://'.$url_info['host'].$url_info['path'].'?'.http_build_query($out);

	}



	header('Location:'.$jump);	

	exit;

}



if($_GET['action'] == 'reg_html'){

	//两种跳转模式

	if(empty($_GET['jump_id'])){ //首页跳转

		$jump_url = $haodaibao_index;

	}else{

		$_GET['jump_id'] = urldecode($_GET['jump_id']);

		$url_info = parse_url($_GET['jump_id']);

		parse_str($url_info['query'], $out);

		if(isset($out['access_token'])){

			unset($out['access_token']);

		}

		$jump_url = $url_info['scheme'].'://'.$url_info['host'].$url_info['path'].'?'.http_build_query($out);

	}





	include './template/haodaibao/reg.php';

	exit;

}







require './includes/library/Requests.php';



Requests::register_autoloader();

//根据配置选择curl或者socket

$options['verify'] = false;

$options['timeout'] = 2;

$options['transport'] = isset($_G['mocuz_config']['transport']) && $_G['mocuz_config']['transport'] == 'fsockopen' ? 'Requests_Transport_fsockopen' : '';

$base_url = 'http://api.51kahui.com/kahui3-api-web';

$header = array();



if($_GET['action'] == 'login_ajax'){



	if($_G['uid'] == 0){

		exit('no login');

	}

	$result = haodaibao_login($_GET['mobile_phone'], $_GET['pwd']);

	$result = json_encode($result);

	echo $data = mb_convert_encoding($result  , 'gbk' , 'utf8');

	exit;

}



if($_GET['action'] == 'send_code'){

	$data['phone'] = $_GET['phone'];

	$data['type'] = 2;

	$response = Requests::post($base_url.'/smscode/send', $header, $data, $options);

	$encode = mb_detect_encoding($response->body, array('ASCII','GB2312','GBK','UTF-8')); 

	echo $response->body;

	exit;

}



if($_GET['action'] == 'register'){



	if($_G['uid'] == 0){

		exit('no login');

	}



	$data['account'] = $_GET['account'];

	$data['check_code'] = $_GET['check_code'];

	$data['pwd'] = $_GET['pwd'];

	$data['channel'] = 1;

	$response = Requests::post($base_url.'/api/kh/register', $header, $data, $options);

	$result = json_decode($response->body);



	if($result->code != 1){



		echo $response->body;

		exit;

	}else{

		$result = haodaibao_login($data['account'], $data['pwd']);

		$result = json_encode($result);

		echo $result;

	}



	



}







function haodaibao_login($mobile_phone, $pwd){

	global $_G;

	$data['mobile_phone'] = $mobile_phone;

	$data['pwd'] = $pwd;

	$options['verify'] = false;

	$options['timeout'] = 2;

	$options['transport'] = isset($_G['mocuz_config']['transport']) && $_G['mocuz_config']['transport'] == 'fsockopen' ? 'Requests_Transport_fsockopen' : '';

	$base_url = 'http://api.51kahui.com/kahui3-api-web';

	$header = array();

	$response = Requests::post($base_url.'/api/kh/login', $header, $data, $options);

	$result = json_decode($response->body);



	if($result->code == 1){

		$bind = DB::result_first("SELECT * FROM ".DB::table('haodaibao_bind')." WHERE uid=".$_G['uid']);

		if(!empty($bind)){

			$fields['access_token'] = $result->result->access_token;

			DB::update('haodaibao_bind', $fields, array('uid'=>$_G['uid']));

		}else{

			$data = array(

				'uid' => $_G['uid'],

				'access_token' => $result->result->access_token,

				'timestamp' => TIMESTAMP

			);

			DB::insert('haodaibao_bind', $data);

		}

	}



	return $result;

}



   