-
Notifications
You must be signed in to change notification settings - Fork 2
/
AppTool.php
76 lines (67 loc) · 1.59 KB
/
AppTool.php
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
<?php
namespace App\Providers;
class AppTool
{
private static $requestId;
/**
* 获取8位 http请求唯一标识
* @return mixed
*/
public static function getRequestId()
{
if (empty(static::$requestId)){
static::$requestId = sprintf("%08x", abs(crc32(
rand(0, 999999) . static::getRequestIp() . static::getRequestTime() . static::getRequestPort()
)));
}
return static::$requestId;
}
/**
* 获取请求端口
* @return int|mixed
*/
public static function getRequestPort()
{
return $_SERVER['REMOTE_PORT'] ?? 80;
}
/**
* 获取请求IP
* @return string|null
*/
public static function getRequestIp()
{
return request()->getClientIp();
}
/**
* 获取请求开始时间
* @return mixed
*/
public static function getRequestTime()
{
return $_SERVER['REQUEST_TIME'] ?? static::getAppStartTime();
}
/**
* 获取laravel启动时间, 秒
* @return mixed
*/
public static function getAppStartTime()
{
return LARAVEL_START ?? microtime(true);
}
/**
* 获取请求方式 host和路径
* @return string
*/
public static function getRequestRoute()
{
return request()->getMethod() . request()->getUri();
}
/**
* 获取当前时间与laravel启动时间的间隔, 秒
* @return mixed
*/
public static function getCurrentTimesSinceAppStart()
{
return microtime(true) - static::getAppStartTime();
}
}