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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
 
include_once '/Common/CommFunc.php';
include_once '/Common/Signature.php';
include_once '/Common/Logging.php';
include_once "/Common/ServerOPS.php";
include_once "/ProjComm/CfgReader.php";
include_once "/db/DBOper.php";
 
function processCommfromx7($appid)
{
    \Logging\CreateLogging("x7.commfrom" . $appid . ".php");
    \Logging\LogInfo("_POST: " . print_r($_POST, true));
 
    // 小7手游 通用接口 https://api.x7sy.com/vendorApiHelper/index
    // Request字段    类型    必选    说明
    // bizParams    String    是    业务参数,值为每个接口对应业务参数的JSON字符串
    // apiMethod    String    是    接口名称
    // reqTime        String    是    请求时间,格式使用ISO8601规范,示例:2022-06-29T15:08:44+0800
    // appkey        String    是    游戏appkey,如果双端使用相同appkey接入,osType字段必传
    // gameType        String    是    游戏端类型,网游为client H5游戏为 h5
    // signature    String    是    请求签名(签名方式参见下文)
    // osType        String    否    系统类型,ios或android
 
    $bizParams = $_POST["bizParams"];
    $apiMethod = $_POST["apiMethod"];
    $reqTime = $_POST["reqTime"];
    $appkey = $_POST["appkey"];
    $gameType = $_POST["gameType"];
    $signature = $_POST["signature"];
    $osType = $_POST["osType"];
 
    $FAILED = "FAILED";
 
    if (!$bizParams || !$apiMethod || !$reqTime || !$appkey || !$gameType || !$signature) {
        $ret = $FAILED . ":ParamError";
        \Logging\LogError($ret);
        echo $ret;
        exit;
    }
 
    if (
        !\CfgReader\ReadConfig()
        || !\CfgReader\GetConfigData("x7", "Appkey_" . $appid, $appkey)
        || !\CfgReader\GetConfigData("x7", "Pubkey_" . $appid, $pubkey)
        || !\CfgReader\GetConfigData("sign", "Prikey_" . $appid, $prikey)
    ) {
        $ret = $FAILED . ":CfgError";
        \Logging\LogError($ret);
        echo $ret;
        exit;
    }
 
    $bizResp = array("respCode" => $FAILED, "respMsg" => "");
    if ($apiMethod == "common.roleQuery") {
        $bizResp["role"] = array();
        $bizResp["guidRoles"] = array();
    } else if ($apiMethod == "common.serverQuery") {
        $bizResp["serverList"] = array();
    } else {
        $ret = $FAILED . ":apiMethodError";
        \Logging\LogError($ret);
        echo $ret;
        exit;
    }
 
    // 验签
    $payload = Signature::genPayload($apiMethod, $appkey, $reqTime, $bizParams, $gameType);
    $verifyRet = Signature::verify($payload, $signature, $pubkey);
    if ($verifyRet != 1) {
        $ret = $FAILED . ":sign_data_verify_failed";
        \Logging\LogError($ret . " verifyRet:" . $verifyRet . " payload:" . $payload);
        echo $ret;
        exit;
    }
 
    if ($apiMethod == "common.roleQuery") {
        $ok = roleQuery($appid, $bizParams, $bizResp);
    } else if ($apiMethod == "common.serverQuery") {
        $ok = serverQuery($appid, $bizParams, $bizResp);
    } else {
        exit;
    }
 
    if ($ok) {
        $bizResp["respCode"] = "SUCCESS";
        $bizResp["respMsg"] = "";
    }
 
    // Response字段    类型    必选    说明
    // bizResp        String    是    响应参数,值为每个接口对应响应参数的JSON字符串
    // apiMethod    String    是    接口名称
    // respTime        String    是    响应时间,格式使用ISO8601规范,示例:2022-06-29T15:08:44+0800
    // appkey        String    是    游戏appkey
    // gameType        String    是    游戏端类型,网游为client H5游戏为 h5
    // signature    String    是    响应签名(签名方式参见下文)
    // osType        String    否    系统类型,ios或android  
    $respTime = \CommFunc\StrDateTimeFormatConver("", DATE_ISO8601);
    $bizResp = \CommFunc\MyJson_encode($bizResp);
    $payload = Signature::genPayload($apiMethod, $appkey, $respTime, $bizResp, $gameType);
    \Logging\LogInfo("Response payload:" . print_r($payload, true));
    $signature = Signature::sign($payload, $prikey);
    $responseData = array(
        "bizResp" => $bizResp,
        "apiMethod" => $apiMethod,
        "respTime" => $respTime,
        "appkey" => $appkey,
        "gameType" => $gameType,
        "signature" => $signature,
        "osType" => $osType,
    );
    echo json_encode($responseData);
}
 
function roleQuery($appid, $bizParams, &$bizResp)
{
    \Logging\LogInfo("roleQuery bizParams:" . $bizParams);
    $bizParams = json_decode($bizParams, true);
    // bizParams字段    类型    必选    说明
    // roleId        String        是    游戏角色ID
    // guid            String        是    单个小7小号ID
    // guids        String[]    否    多个小7小号ID
    // serverId        String        否    区服ID,限定guid或guids参数对应的区服
    // 注意:
    // 0. (重要)对于roleId需要保证游戏内的唯一性,如果角色id会重复,可通过拼接其他参数等方式实现唯一性。
    // 1. roleId和guid(s)参数一般不会同时传递,但需要保证同时传递时接口也可正常使用。
    // 2. guid和guids参数不会同时传递,但建议将guid参数和guids参数合并查询角色数据并在guidRoles字段中返回。
    // 3. 按guid(s)查询时,如果传递了serverId,则只需要返回小号下对应区服的角色数据。
    // 4. 请求参数中的roleId来源于SDK接口获取的角色信息或使用guid参数调用此接口查询返回的角色信息。
 
    // bizResp字段        类型    必选    说明
    // respCode            String    是    响应码,SUCCESS代表成功
    // respMsg            String    是    响应提示信息
    // role                Role    是    角色信息,返回请求参数roleId对应的角色信息,未传递roleId请求参数查询时可返回空对象 {}
    // guidRoles        Role[]    是    角色信息数组,返回请求参数guid或guids对应小号拥有的所有角色信息,若传递了serverId,则只需返回对应区服的角色数据;未传递guid或guids请求参数查询或没有角色数据时可返回空数组[]
 
    $roleId = $bizParams["roleId"]; // 这里传的是完整游戏账号,由前端登录后汇报给对方
    $guid = $bizParams["guid"];
    $guids = $bizParams["guids"];
    $serverId = $bizParams["serverId"];
 
    if (!$roleId && !$guid && !$guids) {
        $bizResp["respMsg"] = "not roleId guid guids";
        return;
    }
 
    $gameServers = \CommFunc\GetGameServers($appid);
 
    // 指定游戏账号查询
    if ($roleId) {
        $accIDInfoList = explode("@", $roleId);
        $infoCount = count($accIDInfoList);
        if ($infoCount < 3) {
            $bizResp["respMsg"] = "roleId error";
            \Logging\LogError("roleQuery roleId:" . $roleId . " error -> explode:" . print_r($accIDInfoList, true));
            return;
        }
        $sid = $accIDInfoList[$infoCount  - 1];
        $platform = $accIDInfoList[$infoCount  - 2];
        if ($appid != $platform) {
            $bizResp["respMsg"] = "roleId appid error";
            \Logging\LogError("roleQuery roleId:" . $roleId . " error -> platform:" . $platform . " != appid:" . $appid);
            return;
        }
 
        $AccountID = join("@", array_slice($accIDInfoList, 0, $infoCount  - 2));
        $find = array("Channel" => $appid, "AccountID" => $AccountID);
        if (!\DBOper\FindOne("GameRoles", $find, $findData) || !isset($findData)) {
            $bizResp["respMsg"] = "roleId not found";
            \Logging\LogError("roleQuery roleId:" . $roleId . " not found. find:" . print_r($find, true));
            return;
        }
 
        if (!array_key_exists($sid, $findData)) {
            $bizResp["respMsg"] = "roleId not found serverId";
            \Logging\LogError("roleQuery roleId:" . $roleId . " not role. sid:" . $sid);
            return;
        }
        $roleInfo = $findData[$sid];
        $bizResp["role"] = getRoleObj($roleId, $AccountID, substr($sid, 1), $roleInfo, $gameServers);
    }
    // 多平台账号查询
    else {
        $findDataList = array();
        if ($guid && !$guids) {
            $find = array("Channel" => $appid, "AccountID" => $guid);
            if (!\DBOper\FindOne("GameRoles", $find, $findData) || !isset($findData)) {
                \Logging\LogInfo("roleQuery guid not found:" . print_r($find, true));
                return true;
            }
            array_push($findDataList, $findData);
        } else {
            $queryguids = array();
            if ($guids) {
                $queryguids = array_merge($queryguids, $guids);
            }
            if ($guid) {
                array_push($queryguids, $guid);
            }
            $find = array("Channel" => $appid, "AccountID" => array('$in' => $queryguids));
            if (!\DBOper\Find("GameRoles", $find, $findDataList) || !isset($findDataList)) {
                \Logging\LogInfo("roleQuery queryguids not found:" . print_r($find, true));
                return true;
            }
        }
 
        $guidRoles = array();
        foreach ($findDataList as $findData) {
            if ($serverId) {
                $sid = "s" . $serverId;
                if (!array_key_exists($sid, $findData)) {
                    continue;
                }
                $AccountID = $findData["AccountID"];
                $roleInfo = $findData[$sid];
                array_push($guidRoles, getRoleObj($AccountID . "@" . $appid . "@" . $sid, $AccountID, $serverId, $roleInfo, $gameServers));
            } else {
                foreach ($findData as $field => $value) {
                    $serverID = \CommFunc\GetServerIDBySid($field);
                    if ($serverID <= 0) {
                        continue;
                    }
                    $sid = $field;
                    $AccountID = $findData["AccountID"];
                    $roleInfo = $value;
                    array_push($guidRoles, getRoleObj($AccountID . "@" . $appid . "@" . $sid, $AccountID, $serverID, $roleInfo, $gameServers));
                }
            }
        }
        $bizResp["guidRoles"] = $guidRoles;
    }
    return true;
}
 
function getRoleObj($roleId, $guid, $serverId, $roleInfo, $gameServers)
{
    // Role字段                类型 (长度)        必选    说明
    // roleId                String (64)        是    游戏角色ID
    // guid                    String (64)        是    小7小号ID
    // roleName                String (100)    是    角色名称
    // serverId                String (64)        是    角色所属区服ID
    // serverName            String (64)        是    角色所属区服名称
    // roleLevel            String (100)    是    角色等级, 示例:100
    // roleCE                String (100)    是    角色战力,示例:20000
    // roleStage            String (100)    是    角色关卡,示例:2-3
    // roleRechargeAmount    Float (10,2)    是    角色总充值,精度为小数点后2位
    // roleGuild            String (100)    否    角色所属公会
    $serverId .= "";
    $serverName = "";
    if (array_key_exists($serverId, $gameServers)) {
        $serverName = $gameServers[$serverId]["Name"];
    }
    $FightPower = array_key_exists("FightPower", $roleInfo) ? $roleInfo["FightPower"] : 0;
    $AllCoinTotal = array_key_exists("AllCoinTotal", $roleInfo) ? round($roleInfo["AllCoinTotal"] / 100.0, 2) : 0;
    $FamilyName = array_key_exists("FamilyName", $roleInfo) ? $roleInfo["FamilyName"] : "";
    if ($FamilyName == null) {
        $FamilyName = "";
    }
    return array(
        "roleId" => $roleId,
        "guid" => $guid,
        "roleName" => $roleInfo["PlayerName"],
        "serverId" => $serverId,
        "serverName" => $serverName,
        "roleLevel" => $roleInfo["LV"] . "",
        "roleCE" => $FightPower . "",
        "roleStage" => "",
        "roleRechargeAmount" => $AllCoinTotal,
        "roleGuild" => $FamilyName,
    );
}
 
function serverQuery($appid, $bizParams, &$bizResp)
{
    \Logging\LogInfo("serverQuery bizParams:" . $bizParams);
    $bizParams = json_decode($bizParams, true);
    // bizParams字段    类型    必选    说明
    // startTime        String    是    开始时间,为空时表示不限制开始时间,格式使用ISO8601规范,示例:2022-06-29T15:08:44+0800
    // endTime            String    是    截止时间,为空时表示不限制截止时间,格式使用ISO8601规范
 
    $startTime = $bizParams["startTime"];
    $endTime = $bizParams["endTime"];
 
    // $appid = "x7bt5";
    // $startTime = "2022-06-29T15:08:44+0800";
    // $endTime = "2022-07-10T17:25:50+0800";
 
    $find = array("Channel" => $appid, "Statue" => \ServerOPS\ServerStatue::Open);
 
    $StartDateCond = array();
    if ($startTime) {
        $startTime = \CommFunc\StrDateTimeFormatConver($startTime);
        $StartDateCond['$gte'] = $startTime;
    }
 
    if ($endTime) {
        $endTime = \CommFunc\StrDateTimeFormatConver($endTime);
        $StartDateCond['$lte'] = $endTime;
    }
 
    if (count($StartDateCond) > 0) {
        $find["StartDate"] = $StartDateCond;
    }
 
    \DBOper\Find("GameServers", $find, $serverDataList);
 
    $serverList = array();
    if (isset($serverDataList)) {
        foreach ($serverDataList as $serverData) {
            // Server字段    类型    必选    说明
            // serverId        String    是    区服ID(区服编号)
            // serverTime    String    是    开服时间,格式使用ISO8601规范,示例:2022-06-29T15:08:44+0800
            // serverName    String    否    区服名称,可为空
            // apiServer    String    否    api区服,如不为空,角色信息查询等接口会优先使用此值作为serverId进行调用查询
            array_push($serverList, array(
                "serverId" => $serverData["ServerID"] . "",
                "serverTime" => \CommFunc\StrDateTimeFormatConver($serverData["StartDate"], DATE_ISO8601),
                "serverName" => $serverData["ServerName"],
                "apiServer" => "",
            ));
        }
    }
    $bizResp["serverList"] = $serverList;
    return true;
}