<?php

/**
 * 三星 accesstoken换取用户信息
 * @version
 *          1.0.0 Created at 2018-3-9. by --gwang
 * @author gwang (mail@wanggangzero.cn)
 * @copyright ? 2018-3-9, SJZ LoyalSoft Corporation & gwang. All rights reserved.
 */
include_once __DIR__ . '/../../../main.php';             # 导入game utils
include_once __DIR__ . '/../Mo/payResp.php';                # 导入通用返回值结构
include_once __DIR__ . '/../Mo/payRequest.php';          # 导入通用订单请求结构
require_once __DIR__ . '/config_sanxing.php';               # 配置文件

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

HttpUtil::PostOnly();                                                           # 要求客户端必须以post方式提交数据
//参数: access_token
$str = HttpUtil::getQueryString();                                              # 提取参数字符串
//$str = \gzinflate($str);                                                      # 解压
$req = JsonUtil::decode($str);                                                  # 反序列化到对象
$token = $req->signValue;
try {
    $cfg = config_sanxing::Inst();
    $data = "appid=" . $cfg->appId . "&token=" . urldecode($token);
    $sign = $cfg->getSign($data);
    $reqData = "appid=" . $cfg->appId . "&token=" . urlencode($token) . "&sign=" . urlencode($sign);
    $ret = HttpUtil::makeRequest($cfg->sanxing_api_login, $reqData);
    if ($ret['result']) {
        $resp = JsonUtil::decode($ret['msg']);                                  # 三星返回JSON
        if ($resp->code == 0) {                                                  # 登录校验通过
            echo payResp::ok(JsonUtil::decode($resp->data));
        } else {
            echo payResp::err($resp->code, '校验失败');                              # 错误信息
        }
    } else {
        echo payResp::err($result['errno'], $result['msg']);                       # 通讯失败
    }
} catch (\Exception $e) {                                                       # 其他异常
    echo payResp::err($e->getCode(), $e->getMessage());
}
