hxp
2025-06-09 6c3f6335c70859ded94a1ad8d218acb0ac34239c
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
<?php
include_once "../../../Common/Logging.php";
include_once '../../../Common/CommFunc.php';
include_once '../../../Common/MongoDB7.php';
include_once "wxfunc.php";
include_once "GamePlayer.php";
 
// 该文件名虽然命名为 set ,实际逻辑是处理不为0的互动数据,并设置为0
\Logging\CreateLogging("wx.setUserInteractiveData.php");
 
$openid = $_GET['openid'];
$gamename = $_GET['gamename'];
$key = $_GET['key'];
$value = $_GET['value'];
Logging\LogInfo("_GET: " . print_r($_GET, true));
 
if ($value <= 0) {
    return;
}
 
$gameconfig = parse_ini_file("config/" . $gamename . ".ini", true);
$appid = $gameconfig["app"]["appid"];
$secret = $gameconfig["app"]["appSecret"];
 
$dbOpt = new \MongoDB7\MongoDb("session", $gameconfig["db"]);
$where = ['openid' => $openid];
$result = $dbOpt->query($where);
if (!$result) {
    Logging\LogError("找不到玩家session_key!" . $openid);
    return;
}
$sessionKey = $result[0]["session_key"];
 
$wx = new WXFunc();
$kv_list = [[
    'key' => $key,
    'value' => 0 // 默认设置为0
]];
$ret = $wx->setUserInteractiveData($appid, $secret, $openid, $sessionKey, $kv_list);
 
$ret = json_decode($ret, true);
// 先改托管数据,成功后再处理游戏业务逻辑
// 修改成功
if ($ret['errmsg'] == "ok") {
    $gamePlayer = new GamePlayer($openid, $gameconfig["db"]);
    // 赠送钻石
    if ($key == "1") {
        $giveGold = 5; // 每次给钻石数,需与前端配置一致
        $giveGoldTotal = $giveGold * $value;
        $gsRet = $gamePlayer->AddGold($giveGoldTotal);
        $ret['gameServerRet'] = $gsRet;
    }
}
 
echo json_encode($ret);