147 lines
4.1 KiB
PHP
147 lines
4.1 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" => "rm-bp117l606l220e69e.mysql.rds.aliyuncs.com",
|
||
"name" => "game_db",
|
||
"user" => "games",
|
||
"pwd" => "Games2017@)!&",
|
||
"port" => "3306"
|
||
|
||
// "host" => "localhost",
|
||
// // "host" => "120.25.60.74",
|
||
// "name" => "youle_games",
|
||
// "user" => "root",
|
||
// "pwd" => "root",
|
||
// "port" => "3333"
|
||
);
|
||
|
||
//错误编码及提示
|
||
$G_Error = array(
|
||
"condb" => array("code" => 81, "msg" => "连接数据库失败"),
|
||
"execsql" => array("code" => 82, "msg" => "操作数据库失败"),
|
||
"d_wrong" => array("code" => 83, "msg" => "data参数错误"),
|
||
"m_wrong" => array("code" => 84, "msg" => "m参数错误"),
|
||
"s_wrong" => array("code" => 85, "msg" => "s参数错误"),
|
||
"p_wrong" => array("code" => 86, "msg" => "p参数错误")
|
||
);
|
||
|
||
///////////////////////////// 全局变量 /////////////////////////////
|
||
//接口函数返回的json对象
|
||
$G_Result = array(
|
||
"state" => 0, //0:成功 <>0:错误编码
|
||
"error" => "", //错误时的描述
|
||
"param" => "", //接收的参数
|
||
"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();
|
||
}
|
||
|
||
//////////////////////////// 读取参数 ////////////////////////////
|
||
//读取data参数
|
||
$str_data = GetRequest("data");
|
||
// $str_data = stripslashes($str_data); //解决表单POST传参数时,自动加转义字符的问题
|
||
$str_data = str_replace("^","=",$str_data);
|
||
$G_Result['param'] = $str_data;
|
||
|
||
//检查data是否为空
|
||
if ($str_data == "") {
|
||
$G_Result['state'] = $G_Error["d_wrong"]["code"];
|
||
$G_Result['error'] = $G_Error["d_wrong"]["msg"];
|
||
do_return();
|
||
}
|
||
|
||
//检查data是否能转成json
|
||
$json_para = json_decode($str_data);
|
||
if ($json_para == null) {
|
||
$G_Result['state'] = $G_Error["d_wrong"]["code"];
|
||
$G_Result['error'] = $G_Error["d_wrong"]["msg"];
|
||
do_return();
|
||
}
|
||
|
||
$m = $json_para->m;
|
||
$s = $json_para->s;
|
||
$p = $json_para->p;
|
||
|
||
//检查m值
|
||
if (($m == null) || ($m == "") || (!function_exists($m))) {
|
||
$G_Result['state'] = $G_Error["m_wrong"]["code"];
|
||
$G_Result['error'] = $G_Error["m_wrong"]["msg"];
|
||
do_return();
|
||
}
|
||
//检查s值
|
||
if (($s == null) || ($s == "")) {
|
||
$G_Result['state'] = $G_Error["s_wrong"]["code"];
|
||
$G_Result['error'] = $G_Error["s_wrong"]["msg"];
|
||
do_return();
|
||
}
|
||
//检查p值
|
||
if (($p != null) && (!is_array($p))) {
|
||
$G_Result['state'] = $G_Error["p_wrong"]["code"];
|
||
$G_Result['error'] = $G_Error["p_wrong"]["msg"];
|
||
do_return();
|
||
}
|
||
|
||
$m($s, $p);
|
||
do_return();
|
||
|
||
//根据参数名获取参数
|
||
function GetRequest($name) {
|
||
return isset($_REQUEST[$name])?$_REQUEST[$name]:'';
|
||
}
|
||
|
||
//执行sql语句返回结果
|
||
function opensql($sql, $para) {
|
||
global $G_Result, $G_Error, $PDO;
|
||
$stmt = $PDO->prepare($sql.";");
|
||
if ($stmt->execute($para))
|
||
{
|
||
while ($aryData = $stmt->fetch(PDO::FETCH_NAMED)) {
|
||
$G_Result["data"][] = $aryData;
|
||
}
|
||
} else {
|
||
$G_Result['state'] = $G_Error["execsql"]["code"];
|
||
$G_Result['error'] = $G_Error["execsql"]["msg"];
|
||
}
|
||
}
|
||
|
||
//执行sql语句无返回结果
|
||
function execsql($sql, $para){
|
||
global $G_Result, $G_Error, $PDO;
|
||
$stmt = $PDO->prepare($sql.";");
|
||
if ($stmt->execute($para))
|
||
{} else {
|
||
$G_Result['state'] = $G_Error["execsql"]["code"];
|
||
$G_Result['error'] = $G_Error["execsql"]["msg"];
|
||
}
|
||
}
|
||
?>
|