<?php

/**
 * 百度 accesstoken换取用户信息
 * @version
 *          1.0.0 Created at 2018-3-19. by --gwang
 * @author gwang (mail@wanggangzero.cn)
 * @copyright ? 2018-3-19, SJZ LoyalSoft Corporation & gwang. All rights reserved.
 */
include_once __DIR__ . '/../../../main.php';             # 导入game utils
include_once __DIR__ . '/../Mo/resp.php';                # 导入通用返回值结构
include_once __DIR__ . '/../Mo/payRequest.php';          # 导入通用订单请求结构
require_once __DIR__ . '/lib/Sdk.php';                       # 百度SDK

use loyalsoft\CLog;
use loyalsoft\JsonUtil;
use loyalsoft\HttpUtil;

HttpUtil::PostOnly();                                                           # 要求客户端必须以post方式提交数据

try {
//参数: access_token
    $str = HttpUtil::getQueryString();                                          # 提取参数字符串
//$str = \gzinflate($str);                                                      # 解压
    $req = JsonUtil::decode($str);                                              # 反序列化到对象

    $sdk = new Sdk();                                                           # sdk
    $accessToken = $req->accessToken; //                                        //客户端SDK返回的登陆令牌
    $Res = $sdk->login_state_result($accessToken);
    # 结果
    if ($Res['ResultCode'] == "1" && $Res['Sign'] == $sdk->SignMd5($Res['ResultCode'], urldecode($Res['Content']))) {
        //Content参数需要urldecode后再进行base64解码
        $result = base64_decode(urldecode($Res['Content']));
        $Item = extract(json_decode($result, true));
        echo Resp::ok($Item);
    } else {
        echo Resp::err($Res['ResultCode'], '校验失败');                          # 错误信息
    }
} catch (\Exception $e) {                                                       # 其他异常
    echo Resp::err($e->getCode(), $e->getMessage());
}
