1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
include_once "../../../Common/CommFunc.php";
include_once "../../../Common/Logging.php";
include_once '../../../Common/MongoDB7.php';
include_once "GamePlayer.php";
 
\Logging\CreateLogging("tt.login.php");
 
$jsCode = $_GET['code'];
$gamename = $_GET['gamename'];
$syncData = $_GET['syncData'];
$anonymous_code = $_GET['anonymous_code']; // 需支持匿名登录
$gameconfig = parse_ini_file("config/" . $gamename . ".ini", true);
 
// https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/server/log-in/code-2-session
//GET https://developer.toutiao.com/api/apps/jscode2session?appid=APPID&secret=SECRET&code=JSCODE&anonymous_code=anonymous_code
$url = "https://developer.toutiao.com/api/apps/jscode2session";
$get = [
    'appid' => $gameconfig["app"]["appid"],
    'secret' => $gameconfig["app"]["appSecret"],
    'code' => $jsCode,
    'anonymous_code' => $anonymous_code
];
 
\Logging\LogInfo("DoGet url: " . $url);
\Logging\LogInfo("get Data: " . print_r($get, true));
$ret = \CommFunc\curl_get($url, $get);
\Logging\LogInfo("ret: " . print_r($ret, true));
 
// 1. 平台api返回
$responseInfo = json_decode($ret, true);
if ($responseInfo['errcode'] != 0) {
    echo $ret;
    return;
}
 
$openid = "";
$gamePlayer;
if (array_key_exists("openid", $responseInfo)) {
    $openid = $responseInfo["openid"];
    // 支持匿名登录,匿名登录玩家使用 anonymous_openid ,前端处理,后端不存库
    if ($openid != "") {
        $gamePlayer = new GamePlayer($openid, $gameconfig["db"]);
        $playerData = $gamePlayer->onPlayerLogin($syncData);
        if ($playerData) {
            // 2. 玩家数据
            $responseInfo['playerData'] = $playerData;
        }
    }
}
 
// 会话密钥 session_key 是对用户数据进行 加密签名 的密钥。为了应用自身的数据安全,开发者服务器不应该把会话密钥下发到小程序,也不应该对外提供这个密钥。
$session_key = "";
if (array_key_exists("session_key", $responseInfo)) {
    $session_key = $responseInfo["session_key"];
    \Logging\LogInfo("session_key: " . $session_key);
    unset($responseInfo["session_key"]);
    if ($gamePlayer) {
        $gamePlayer->saveSessionkey($session_key);
    }
}
 
// 3. 服务器数据
$responseInfo['serverData'] = ['serverTime' => time()];
 
$responseInfo = json_encode($responseInfo);
\Logging\LogInfo("responseInfo: " . print_r($responseInfo, true));
echo $responseInfo;