<?php

function parse_qq_callback($response){
	if(strpos($response, "callback") !== false){
		$lpos = strpos($response, "(");
		$rpos = strrpos($response, ")");
		$response  = substr($response, $lpos + 1, $rpos - $lpos -1);
		
		$msg = json_decode($response,true);

		if(isset($msg['error'])){
			Redirect::to('/main');
		}
		return $msg;
	}
	Redirect::to('/main');
}

if(Request::get('uid') && Request::get('upwd')){
	$u = new Model('user');
	$u->find(Request::get('uid'));
	// if($u->has_id() && md5($u->pwd)==Request::get('upwd')){
	if($u->has_id() && $u->pwd==Request::get('upwd')){
		set_user_login($u);
	}
}

$gkw = "企微魔盒";
$gde = "企微魔盒";
$gti = "企微魔盒";

$u = new SampleModel();
function set_user_login($mu){
	Auth::im_user();
	Session::set('uid',$mu->id);
	Session::set('mainuid',$mu->id);
	$uid = Session::get('uid');
	user_upgrade($uid);
	if(!Session::has('level_id')){		
		Session::set('level_id',$mu->level_id);
	}	
	Session::set('un',$mu->un);
	Session::set('ue',$mu->email);
	Session::set('ut',$mu->telephone);
	Session::set('upc',$mu->ispwdcg);
		
	if($mu->isadmin=='a'){
		Auth::im_admin('admin');
	}
	Session::set('upath','/ups/'.date('Y/m',strtotime($mu->rtime)).'/'.$mu->id.'/');
	File::creat_dir(DOXCX_FRAME_PATH.DOXCX_PUB.'/'.Session::get('upath'));
	Session::set('headpic',trim($mu->headpic));
	Cookie::set('uid',$mu->id);
	Cookie::set('uuid',md5($mu->un.Request::ip().$mu->pwd));
	Redirect::to('/start');
}


if($u->try_post()){
		$mu = new Model('user');
		$mu->un = $u->un;
		$mu->pwd = md5($u->pwd);
		$mu->email = $u->un;
		$mu->telephone = $u->un;
		if(!empty($u->un) && !empty($u->pwd) && ($mu->is_real(array('un','pwd')) || $mu->is_real(array('email','pwd')) || $mu->is_real(array('telephone','pwd')))){			
			if($mu->isstop != '0'){
				Page::view('login');
				tusi('账户已经被停用或作废');				
			}else{
				if('1'== Request::post('remme')){
					//记住登录
					Cookie::set('weixinid',$mu->id);
					Cookie::set('weixinuuid',md5(Conf::$management_center_password.$mu->id));
				}		
				set_user_login($mu);
			}
			
		}else{
			$u->pwd = '';
			Page::view('index');
			tusi('登录信息不正确');
		}
}else if(Cookie::has('weixinid')){
	$uid = Cookie::get('weixinid');
	$uuid = Cookie::get('weixinuuid');
	if($uuid==md5(Conf::$management_center_password.$uid)){
		$mu = new Model('user');
		$mu->find($uid);
		set_user_login($mu);
	}
}
if($_SERVER['IS_OEM']){
	Page::view('index');
}
//会员升级或降级操作
function user_upgrade($uid){
	$user = new Model('user');
	$user->find($uid);
	Session::remove('level_id');
	/**
	// uesr upgrade
	if((strtotime($user->mendtime) < time() || $user->level_id == 1) && $user->next_level_id != 0){ 
		$user->level_id = $user->next_level_id;
		Session::set('level_id',$user->next_level_id);
		$user->mendtime = $user->next_mendtime;
		if($user->save()){
			$user->next_level_id = '';
			$user->next_mendtime = '';
			$user->save();
			return true;
		}
	}
	//user donwgrading
	if(strtotime($user->mendtime)<time() && $user->next_level_id == 0){
		$user->level_id = 1;
		Session::set('level_id',1);
		$user->mendtime = '';
		$user->save();
		return true;
	}	
	*/
	if(strtotime($user->mendtime)<time()){
		$user->level_id = 1;
		Session::set('level_id',1);
		$user->mendtime = '';
		$user->save();
	}else{
		Session::set('level_id',$user->level_id);
	}
}
