<?php
/**
 *
 */

include_once(dirname(dirname(__FILE__)) . "/library/publicBase.php");

class callback extends publicBase {

    public function wxlogin() {
        if (empty($this->get('code'))) $this->show_message('登录失败,请重新登录', 'goback');

        $state = explode('-', $this->get('state'));

        //知识库登陆
        if ($state[0] == CALLBACK_KNOWLEDGE_LOGIN) {
            $obj = new mManage();
            $res = $obj->weixinLogin($this->get('code'), $this->get('state'));
            if (!$res) $this->show_message($obj->getError(), 'goback');

            session_start();
            $_SESSION['nickname'] = $res['nickname'];
            $_SESSION['openid'] = $res['openid'];
            $_SESSION['token'] = $res['jwttoken'];
            session_write_close();

            header('Location:' . urldecode($state[1]));
            exit;
        }

        //知识库绑定
        if ($state[0] == CALLBACK_KNOWLEDGE_BIND) {
            $obj = new mManage();
            $sub_user_id = $obj->weixinBind($this->get('code'), $this->get('state'));
            if (!$sub_user_id) $this->show_message($obj->getError(), 'goback');

            $obj = new mUser();
            $rdobj = $obj->initRedis();
            $rdobj->setex(sprintf(_RC_KNOWLEDGE_BIND_USER_INFO, $state[1], $state[2]), 6*60*60, $sub_user_id);
            header('Location:http://zhishiku.yizherenxin.cn/index/user_list?pageid='.$state[2]);
            exit;
        }
        exit;
    }
}