<?php 
class weixin{

	function login($appid,$appkey,$code){
		if($_REQUEST ['state'] == $_SESSION ['state']){
		
			$token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appkey."&code=".$code."&grant_type=authorization_code";
			$response = get_url_contents ( $token_url );
			$msg = json_decode ( $response );
			if (isset ( $msg->errcode )) {
				echo "<h3>error:</h3>" . $msg->errcode;
				echo "<h3>msg  :</h3>" . $msg->errmsg;
				exit ();
			}else{
				
				$_SESSION ['weixin_access_token'] = $msg->access_token;
				$_SESSION ['weixin_open_id'] = $msg->openid;
			}
		}else{
			echo ("The state does not match. You may be a victim of CSRF.");
			exit;
		}
	}
	
	function weixin_cb(){
		global $wpdb;
		if(isset($_SESSION ['weixin_open_id'])){
			$user_fb = get_users(array("meta_key "=>"weixin_openid","meta_value"=>$_SESSION['weixin_open_id']));
			if(is_wp_error($user_fb) || !count($user_fb)){
				$arr=range(0,9);
				shuffle($arr);
				$value = '';
				foreach($arr as $values)
				{
				   $value .= $values;
				}
			
				$uinfo = $this->wx_oauth2_get_user_info($_SESSION ['weixin_access_token'],$_SESSION ['weixin_open_id']);
				$pass = wp_create_nonce(rand(10,1000));
				$login_name = "u".$value;
				$username = $uinfo->nickname;
				$userdata=array(
				  'user_login' => $login_name,
				  'display_name' => $username,
				  'nickname' => $username,
				  'user_pass' => $pass,
				  'role' => get_option('default_role'),
				  'first_name' => $username
				);
				$user_id = wp_insert_user( $userdata );
				if ( is_wp_error( $user_id ) ) {
					echo $user_id->get_error_message();
				}else{
	
					update_user_meta($user_id ,"weixin_openid",$_SESSION['weixin_open_id']);
					update_user_meta($user_id, 'photo', $uinfo->headimgurl);
					wp_set_auth_cookie($user_id,true,false);
					wp_redirect(get_bloginfo('url'));
		   
				}
				exit();
			}else{
				$user_ID = $user_fb[0]->ID;
				wp_set_auth_cookie($user_ID,true,false);
				wp_redirect(get_bloginfo('url'));
				exit();
			}
			
		}
	}
	
	function weixin_bd(){
		global $wpdb;
		if(isset($_SESSION ['weixin_open_id'])){
			if(is_user_logged_in()){
				$user_fb = get_users(array("meta_key "=>"weixin_openid","meta_value"=>$_SESSION['weixin_open_id']));
				if(is_wp_error($user_fb) || !count($user_fb)){
					$userinfo=get_userdata(get_current_user_id());
					$userid = $userinfo->id;
					update_user_meta($userid ,"weixin_openid",$_SESSION['weixin_open_id']);
					//update_user_meta($userid, 'avatar', $uinfo->headimgurl);
					wp_redirect(get_permalink(get_page_id_from_template('page-user.php')).'?account=networks');
				}else{
					echo '已有其他账号绑定了此微信，请先使用此微信登录后解绑！';
					exit();
				}	
			}
		}
	}
	
	function wx_oauth2_get_user_info($access_token, $openid){
		$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
		$res = get_url_contents($url);
		return json_decode($res);
	}

}

add_action('wp_ajax_nopriv_weixin_remove_bind', 'weixin_remove_bind');
add_action('wp_ajax_weixin_remove_bind', 'weixin_remove_bind');
function weixin_remove_bind(){
	if(is_user_logged_in()){
		global $wpdb;
		delete_user_meta(get_current_user_id(),'weixin_openid');
		return true;
	}
}
	
?>
