Files
youlegames/codes/games/server/youle/third.php
2026-02-04 23:47:45 +08:00

193 lines
5.8 KiB
PHP

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Authorization, Content-Length, X-Requested-With");
header("Access-Control-Allow-Credentials: true");
header("Content-Type: textml; charset=utf-8");
///////////////////////////// 全局常量 /////////////////////////////
//mysql数据库连接
$G_MySql = array(
"host" => "localhost",
"name" => "youle_games",
"user" => "root",
"pwd" => "root",
"port" => "3333"
);
//错误编码及提示
$G_Error = array(
"condb" => array("code" => 81, "msg" => "database connection failed."),
"execsql" => array("code" => 82, "msg" => "failed to operate database."),
"wrong_a" => array("code" => 83, "msg" => "a parameter error."),
"wrong_c" => array("code" => 84, "msg" => "c parameter error."),
"wrong_g" => array("code" => 85, "msg" => "g parameter error."),
"wrong_do" => array("code" => 86, "msg" => "do parameter error."),
"wrong_data" => array("code" => 87, "msg" => "data parameter error."),
"wrong_openid" => array("code" => 88, "msg" => "openid is null."),
"wrong_unionid" => array("code" => 89, "msg" => "unionid is null.")
);
///////////////////////////// 全局变量 /////////////////////////////
//接口函数返回的json对象
$G_Result = array(
"state" => 0, //0:成功 <>0:错误编码
"error" => "", //错误时的描述
"data" => array() //成功时的数据
);
//返回结果$G_G_Result
function do_return(){
global $G_Result;
if (count($G_Result["data"]) == 0) {
$G_Result["data"] = new stdClass;
}
echo json_encode($G_Result);
exit();
}
////////////////////////// 连接MYSQL数据库 /////////////////////////
$PDO = null;
try
{
$PDO = new PDO("mysql:host=".$G_MySql["host"].";port=".$G_MySql["port"].";dbname=".$G_MySql["name"], $G_MySql["user"], $G_MySql["pwd"]);
$PDO->exec("set names utf8;");
$PDO->exec("use ".$G_MySql["name"].";");
}
catch (Exception $e)
{
$G_Result['state'] = $G_Error["condb"]["code"];
$G_Result['error'] = $G_Error["condb"]["msg"];
do_return();
}
//////////////////////////// 读取参数 ////////////////////////////
$a = GetRequest("a");
if ($a == null || $a == "") {
$G_Result['state'] = $G_Error["wrong_a"]["code"];
$G_Result['error'] = $G_Error["wrong_a"]["msg"];
do_return();
}
$c = GetRequest("c");
if ($c == null || $c == "") {
$G_Result['state'] = $G_Error["wrong_c"]["code"];
$G_Result['error'] = $G_Error["wrong_c"]["msg"];
do_return();
}
$g = GetRequest("g");
if ($g == null || $g == "") {
$G_Result['state'] = $G_Error["wrong_g"]["code"];
$G_Result['error'] = $G_Error["wrong_g"]["msg"];
do_return();
}
$do = GetRequest("do");
if ($do != 1 && $do != 2) {
$G_Result['state'] = $G_Error["wrong_do"]["code"];
$G_Result['error'] = $G_Error["wrong_do"]["msg"];
do_return();
}
//读取data参数
$str_data = GetRequest("data");
//php收到后将^号替换为=号,替换回来,,以避免=号POST传参会截断的问题
//$str_data = str_replace("^","=",$str_data);
//检查data是否为空
if ($str_data == null || $str_data == "") {
$G_Result['state'] = $G_Error["wrong_data"]["code"];
$G_Result['error'] = $G_Error["wrong_data"]["msg"];
do_return();
}
//检查data是否能转成json
$json_data = json_decode($str_data);
if ($json_data == null) {
$G_Result['state'] = $G_Error["wrong_data"]["code"];
$G_Result['error'] = $G_Error["wrong_data"]["msg"];
do_return();
}
switch ($do){
case 1:
get_roomcard($a, $c, $g, $json_data);
do_return();
break;
case 2:
change_roomcard($a, $c, $g, $json_data);
do_return();
break;
}
//根据参数名获取参数
function GetRequest($name) {
return isset($_REQUEST[$name])?$_REQUEST[$name]:'';
}
//获取房卡数量
function get_roomcard($agentid, $channelid, $gameid, $data) {
global $G_Result, $G_Error, $PDO;
$openid = $data->openid;
$unionid = $data->unionid;
$nickname = $data->nickname;
$avatar = $data->avatar;
$sex = $data->sex;
$province = $data->province;
$city = $data->city;
if ($openid == null || $openid == "") {
$G_Result['state'] = $G_Error["wrong_openid"]["code"];
$G_Result['error'] = $G_Error["wrong_openid"]["msg"];
do_return();
}
if ($unionid == null || $unionid == "") {
$G_Result['state'] = $G_Error["wrong_unionid"]["code"];
$G_Result['error'] = $G_Error["wrong_unionid"]["msg"];
do_return();
}
$stmt = $PDO->prepare("call cp_third_get_roomcard(?,?,?,?,?,?,?,?,?,?)");
if ($stmt->execute(array($agentid, $channelid, $gameid, $openid, $unionid, $nickname, $avatar, $sex, $province, $city))){
$aryData = $stmt->fetch(PDO::FETCH_NAMED);
if ($aryData["result"] != 0){
$G_Result['state'] = $aryData["result"];
$G_Result['error'] = $aryData["error"];
} else {
$G_Result['data']["playerid"] = (int)$aryData["playerid"];
$G_Result['data']["roomcard"] = (int)$aryData["roomcard"];
}
} else {
$G_Result['state'] = $G_Error["execsql"]["code"];
$G_Result['error'] = $G_Error["execsql"]["msg"];
}
}
//修改房卡数量
function change_roomcard($agentid, $channelid, $gameid, $data) {
global $G_Result, $G_Error, $PDO;
$openid = $data->openid;
$unionid = $data->unionid;
$change = $data->change;
$stmt = $PDO->prepare("call cp_third_change_roomcard(?,?,?,?,?,?)");
if ($stmt->execute(array($agentid, $channelid, $gameid, $openid, $unionid, $change))){
$aryData = $stmt->fetch(PDO::FETCH_NAMED);
if ($aryData["result"] != 0){
$G_Result['state'] = $aryData["result"];
$G_Result['error'] = $aryData["error"];
} else {
$G_Result['data']["playerid"] = (int)$aryData["playerid"];
$G_Result['data']["roomcard"] = (int)$aryData["roomcard"];
$G_Result['data']["change"] = (int)$aryData["amount"];
}
} else {
$G_Result['state'] = $G_Error["execsql"]["code"];
$G_Result['error'] = $G_Error["execsql"]["msg"];
}
}
?>