增加docke部署
This commit is contained in:
265
codes/agent/game-docker/api/source/login/common.php
Normal file
265
codes/agent/game-docker/api/source/login/common.php
Normal file
@@ -0,0 +1,265 @@
|
||||
<?php
|
||||
define('USEDCHARSET', 'utf-8');
|
||||
|
||||
// 加载环境变量配置
|
||||
require_once dirname(dirname(dirname(__DIR__))) . '/env_config.php';
|
||||
// 域名配置(若 pay/common.php 已先定义则跳过)
|
||||
if (!defined('SITE_API_DOMAIN')) define('SITE_API_DOMAIN', env('SITE_API_URL', 'https://api.tscce.cn'));
|
||||
if (!defined('SITE_API2_DOMAIN')) define('SITE_API2_DOMAIN', env('SITE_API2_URL', 'https://api2.tscce.cn'));
|
||||
|
||||
/// 接口返回用的信息类
|
||||
class ResultObject
|
||||
{
|
||||
public $error; /// 返回值: 0成功; 非0失败;
|
||||
public $error_code; /// 错误号
|
||||
public $msg; /// 错误信息
|
||||
public $data; /// 返回的数据
|
||||
|
||||
public function ResultObject($string)
|
||||
{
|
||||
$this->error = 0;
|
||||
$this->error_code = 0;
|
||||
$this->msg = null;
|
||||
$this->data = null;
|
||||
|
||||
$this->from_string($string);
|
||||
}
|
||||
|
||||
|
||||
public function to_array()
|
||||
{
|
||||
return (array)$this;
|
||||
}
|
||||
|
||||
public function to_string()
|
||||
{
|
||||
return json_encode($this, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
public function from_array($array)
|
||||
{
|
||||
foreach ($array as $key => $value)
|
||||
{
|
||||
if (property_exists($this, $key))
|
||||
{
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function from_string($string)
|
||||
{
|
||||
return $this->from_array((array)json_decode($string));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @note 获取当前页面的完整连接
|
||||
* @param $always_http bool
|
||||
* @return string
|
||||
*/
|
||||
function getLocaleUrl($always_http = false)
|
||||
{
|
||||
$is_https =
|
||||
(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ||
|
||||
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ||
|
||||
(isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https');
|
||||
|
||||
$request_scheme = $is_https ? 'https://' : 'http://';
|
||||
$hostname = $_SERVER['SERVER_NAME'];
|
||||
$hostport = (($is_https && '443' == $_SERVER['SERVER_PORT']) || (!$is_https && '80' == $_SERVER['SERVER_PORT'])) ? '' : ':' . intval($_SERVER['SERVER_PORT']);
|
||||
|
||||
if ($always_http)
|
||||
return 'http://' . $hostname . $hostport;
|
||||
else
|
||||
return $request_scheme . $hostname . $hostport;
|
||||
}
|
||||
|
||||
function getFullUrl($relatively_url, $always_http = false)
|
||||
{
|
||||
if (mb_strstr($relatively_url, '/', false, USEDCHARSET) == $relatively_url)
|
||||
return getLocaleUrl($always_http) . $relatively_url;
|
||||
else
|
||||
return getLocaleUrl($always_http) . '/' . $relatively_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @date 2017-03-04
|
||||
* @note 给参数按key的顺序排序。(支持递归)
|
||||
* @param mixed $parameter 要排序的参数
|
||||
* @return array
|
||||
* @auth 应俊
|
||||
*/
|
||||
function SortParam($parameter)
|
||||
{
|
||||
$parameter = (array)$parameter;
|
||||
foreach ($parameter as $k => $v)
|
||||
{
|
||||
if (is_array($v) || is_object($v))
|
||||
{
|
||||
$parameter[$k] = SortParam($v);
|
||||
}
|
||||
}
|
||||
|
||||
// 调用strcmp函数来排序,该函数区分大小写。
|
||||
uksort($parameter, 'strcmp');
|
||||
return $parameter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @date 2017-03-06
|
||||
* @note 转换参数成字符串形式按key=value的形式,用&分隔)。
|
||||
* @param mixed $parameter 要转换的参数
|
||||
* @return string
|
||||
* @auth 应俊
|
||||
*/
|
||||
function ConvertParam($parameter)
|
||||
{
|
||||
$parameter = (array)$parameter;
|
||||
$return = '';
|
||||
foreach ($parameter as $k => $v)
|
||||
{
|
||||
if (is_array($v) || is_object($v))
|
||||
{
|
||||
$return .= sprintf('&%s={%s}', $k, ConvertParam($v));
|
||||
}
|
||||
else
|
||||
{
|
||||
$return .= sprintf('&%s=%s', $k, $v);
|
||||
}
|
||||
}
|
||||
|
||||
$sublen = mb_strlen('&', USEDCHARSET);
|
||||
$retlen = mb_strlen($return, USEDCHARSET);
|
||||
$return = mb_substr($return, $sublen, $retlen - $sublen, USEDCHARSET);
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @date 2017-03-04
|
||||
* @note 为参数生成签名
|
||||
* @param mixed $parameter 要签名的参数
|
||||
* @param string $signkey 签名key
|
||||
* @return string
|
||||
* @auth 应俊
|
||||
*/
|
||||
function SignParameter($parameter, $signkey = '')
|
||||
{
|
||||
// 1:先把参数按参数名(key)从小到大排序
|
||||
$parameter = SortParam($parameter);
|
||||
|
||||
// 2:连接参数成一个字符串(按key=value的形式,用&分隔)。
|
||||
$return = ConvertParam($parameter);
|
||||
|
||||
// 3:结尾加上key=签名key
|
||||
$return .= '&key=' . $signkey;
|
||||
|
||||
// 4:md5加密这个字符串
|
||||
return md5($return);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @return array|mixed
|
||||
*/
|
||||
function ChangePostData($data)
|
||||
{
|
||||
switch (gettype($data))
|
||||
{
|
||||
case 'array':
|
||||
{
|
||||
foreach ($data as $key => $value)
|
||||
{
|
||||
$data[$key] = ChangePostData($value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'object':
|
||||
{
|
||||
$array = (array)$data;
|
||||
foreach ($array as $key => $value)
|
||||
{
|
||||
$data->$key = ChangePostData($value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
$data = preg_replace_callback('/\+/', function ($r) { return '%2B'; }, $data);
|
||||
$data = preg_replace_callback('/\&/', function ($r) { return '%26'; }, $data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @note 发送post请求
|
||||
* @param string $url
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
function SendPost($url, $data)
|
||||
{
|
||||
$data = http_build_query(ChangePostData($data));
|
||||
|
||||
$opts = array(
|
||||
'http' => array(
|
||||
'method' => 'POST',
|
||||
'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n",
|
||||
'content' => $data,
|
||||
),
|
||||
);
|
||||
|
||||
$context = stream_context_create($opts);
|
||||
|
||||
// 抑制警告并捕获错误
|
||||
$ret = @file_get_contents($url, false, $context);
|
||||
|
||||
// 如果请求失败,记录错误并返回空字符串
|
||||
if ($ret === false) {
|
||||
// 记录错误到日志文件用于调试
|
||||
$error_msg = "SendPost failed: URL=$url, Data=" . print_r($data, true);
|
||||
if (isset($http_response_header)) {
|
||||
$error_msg .= ", Headers=" . print_r($http_response_header, true);
|
||||
}
|
||||
error_log($error_msg);
|
||||
return '';
|
||||
}
|
||||
|
||||
$ret = trim($ret, "\xEF\xBB\xBF");
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @note 从参数中过滤不需要的参数
|
||||
* @param array|null $ignores 要过滤的参数列表(参数名)
|
||||
* @param array|null $parameters 目标参数列表
|
||||
* @return array
|
||||
*/
|
||||
function GetAttachParameters($ignores = null, $parameters = null)
|
||||
{
|
||||
$return = (array)(empty($parameters) ? $_REQUEST : $parameters);
|
||||
if (!empty($ignores))
|
||||
{
|
||||
foreach ($return as $k => $v)
|
||||
{
|
||||
if (in_array($k, $ignores))
|
||||
unset($return[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
Reference in New Issue
Block a user