<?php
// +----------------------------------------------------------------------
// | 74CMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.74cms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 
// +----------------------------------------------------------------------
// | ActionName: 系统第三方回调
// +----------------------------------------------------------------------
namespace Home\Controller;
use Think\Controller;
class CallbackController extends Controller{
	/**
     * 第三方帐号登陆和绑定
     * @mod qq sina taobao  weixin
     * @type 操作类型 login bind unbind
     */
    public function index() {
    	$mod = I('get.mod','','trim');
    	$type = I('get.type','login','trim');
    	!$mod && $this->error('请选择正确的第三方服务！');
        if ('unbind' == $type) {
            !D('User')->is_login() && $this->redirect('user/index');
            if(false === M('UserBind')->where(array('uid'=>session('uu.uid'), 'type'=>$mod))->delete()) $this->error('解除绑定失败，请重新操作！');
            $this->redirect('user/set_basic');
        }else{
            $oauth = new \Common\Lib\oauth($mod);
            cookie('callback_type', $type);
            return $oauth->authorize();
        }
    }
	/**
	 * 第三方登录和绑定回调
	 */
	public function oauth(){
		$error_uri=I('get.error_uri','','trim');
		$error=I('get.error','','trim');
		$error_code=I('get.error_code','','trim');
        if($error_uri || $error || $error_code){
            $this->error("配置出错，错误码:{$error}",__ROOT__.'/');
        }
		$mod = I('get.mod','','trim');
        !$mod && $this->error('请选择正确的第三方服务！');
        $callback_type = cookie('callback_type');
        $oauth = new \Common\Lib\oauth($mod);
        $rk = $oauth->NeedRequest();
        $request_args = array();
        foreach ($rk as $v) {
            $request_args[$v] =I('get.'.$v);
        }
        switch ($callback_type) {
            case 'login':
                $url = $oauth->callbackLogin($request_args);
                break;
            case 'bind':
                $url = $oauth->callbackbind($request_args);
                break;
            default:
                $url = __ROOT__;
                break;
        }
        cookie('callback_type', null);
        if(false === $url) $this->error($oauth->getError(),__ROOT__);
        redirect($url);
	}
}
?>