hxp
2025-06-04 f4a514d5ac952110da846636ecbb9de951eaf3d2
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
<?php
include_once "../../../Common/CommFunc.php";
include_once "../../../Common/Logging.php";
include_once '../../../Common/MongoDB7.php';
include_once "GamePlayer.php";
 
\Logging\CreateLogging("wx.login.php");
 
$jsCode = $_GET['code'];
$gamename = $_GET['gamename'];
$syncData = $_GET['syncData'];
$gameconfig = parse_ini_file("config/" . $gamename . ".ini", true);
 
// https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/login/auth.code2Session.html
//GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
$url = "https://api.weixin.qq.com/sns/jscode2session";
$get = [
    'appid' => $gameconfig["app"]["appid"],
    'secret' => $gameconfig["app"]["appSecret"],
    'js_code' => $jsCode,
    'grant_type' => 'authorization_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"];
    $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;