175 lines
5.0 KiB
PHP
175 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace server\components;
|
|
|
|
use server\components\ApiException;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
|
|
|
|
class Utility{
|
|
|
|
/**
|
|
* 调试输出
|
|
* @param $msg
|
|
*/
|
|
public static function dump($msg){
|
|
if(is_array($msg)){
|
|
$msg = json_encode($msg);
|
|
}
|
|
echo $msg."\n";
|
|
}
|
|
|
|
//获取毫秒级时间戳
|
|
public static function getMsectime(){
|
|
return (float)sprintf('%.0f', microtime(true) * 1000);
|
|
}
|
|
|
|
/**
|
|
* 获取GET请求
|
|
* @param $url
|
|
* @param array $params
|
|
* @param int $timeout
|
|
*/
|
|
public static function curl_get($url, array $params = array(), $timeout = 5)
|
|
{
|
|
if($params){
|
|
$attr = strpos($url,'?')?'&':'?';
|
|
foreach ($params as $k=>$v){
|
|
$attr .= $k.'='.$v;
|
|
}
|
|
$url .= $attr;
|
|
}
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
|
$file_contents = curl_exec($ch);
|
|
curl_close($ch);
|
|
return $file_contents;
|
|
}
|
|
|
|
/**
|
|
* 获取POST请求
|
|
* @param $url
|
|
* @param array $params
|
|
* @param $timeout
|
|
* @return mixed
|
|
*/
|
|
public static function curl_post($url, $data, $timeout = 5) {
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
"Content-Type: application/json; charset=utf-8",
|
|
"Content-Length: " . strlen($data))
|
|
);
|
|
$res = curl_exec($ch);
|
|
curl_close($ch);
|
|
return $res;
|
|
|
|
}
|
|
|
|
/**
|
|
* 处理请求.
|
|
*/
|
|
public static function dealRequest($requestParam){
|
|
try {
|
|
if(empty($requestParam['path'])){
|
|
throw new ApiException(404,'path路径不能为空.');
|
|
}
|
|
$parms = explode('/',$requestParam['path']);
|
|
list($module, $method) = $parms;
|
|
$urlManager = [
|
|
'module' => $module,
|
|
'method' => $method
|
|
];
|
|
|
|
$className = "\\server\\modules\\".$urlManager['module']."\\service\\Service";
|
|
$method = 'action' . ucwords($urlManager['method']);
|
|
|
|
if(!class_exists($className) || !method_exists($className,$method)){
|
|
throw new ApiException(404,'请求路径错误');
|
|
}
|
|
return (new $className($requestParam))->$method();
|
|
} catch (ApiException $e) {
|
|
throw new ApiException($e->getCode(),$e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 远程游戏api调用
|
|
*/
|
|
public static function callRemoteApi($api,$token,$msg = []){
|
|
global $configFile;
|
|
$data = ['Param'=>['Token'=>$token]];
|
|
if(!empty($msg)){
|
|
$itemData = [
|
|
'code' => 200,
|
|
'msg' => 'ok',
|
|
'message_type' => $msg['msg_type'],
|
|
'data' => $msg['data'],
|
|
];
|
|
$data = [
|
|
'Param'=> [
|
|
"SnId" => $token,
|
|
"Plateform" => 1,
|
|
"Data" => json_encode($itemData)
|
|
]
|
|
];
|
|
}
|
|
|
|
$data = json_encode($data);
|
|
$AppId = $configFile['app_id'];
|
|
$msectime = self::getMsectime();
|
|
$sign = md5($AppId.';'.$api.';'.$data.';'.$msectime);
|
|
|
|
$url = $configFile['game_host'].$api.'?ts='.$msectime.'&sign='.$sign;
|
|
var_dump($url);
|
|
$res = self::curl_post($url,$data);
|
|
var_dump($res);
|
|
$res = json_decode($res,true);
|
|
|
|
return $res;
|
|
}
|
|
|
|
public static function makeSnowFlake($dataCenterID=0,$workerID=0){
|
|
// 41bit timestamp + 5bit dataCenter + 5bit worker + 12bit
|
|
$lastTimestamp = 0;
|
|
$lastSequence = 0;
|
|
$sequenceMask = 4095;
|
|
$twepoch = 1508945092000;
|
|
$timestamp = self::getMsectime();
|
|
if ($lastTimestamp == $timestamp) {
|
|
$lastSequence = ($lastSequence + 1) & $sequenceMask;
|
|
if ($lastSequence == 0) $timestamp = self::tilNextMillis($lastTimestamp);
|
|
} else {
|
|
$lastSequence = 0;
|
|
}
|
|
$lastTimestamp = $timestamp;
|
|
$snowFlakeId = (($timestamp - $twepoch) << 22) | ($dataCenterID << 17) | ($workerID << 12) | $lastSequence;
|
|
return $snowFlakeId;
|
|
}
|
|
|
|
//等待下一毫秒的时间戳
|
|
public static function tilNextMillis($lastTimestamp){
|
|
$timestamp = self::getMsectime();
|
|
while ($timestamp <= $lastTimestamp) {
|
|
$timestamp = self::getMsectime();
|
|
}
|
|
return $timestamp;
|
|
}
|
|
|
|
/**
|
|
* UUID生成
|
|
*/
|
|
public static function makeUuid(){
|
|
$uuid4 = Uuid::uuid4();
|
|
return $uuid4->toString();
|
|
}
|
|
|
|
|
|
} |