|
@@ -3,8 +3,7 @@
|
|
|
* 添加自定义辅助函数
|
|
|
*/
|
|
|
|
|
|
-if ( ! function_exists('config_path'))
|
|
|
-{
|
|
|
+if (!function_exists('config_path')) {
|
|
|
/**
|
|
|
* Get the configuration path.
|
|
|
*
|
|
@@ -65,7 +64,7 @@ function http($url, $param, $method = 'post')
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
$response = $client->request($method, $url, $param);
|
|
|
$result = json_decode($response->getBody()->getContents(), true);
|
|
|
- \Illuminate\Support\Facades\Log::debug('url:'.$url.'param:'.json_encode($param).'response:'.json_encode($result));
|
|
|
+ \Illuminate\Support\Facades\Log::debug('url:' . $url . 'param:' . json_encode($param) . 'response:' . json_encode($result));
|
|
|
if (isset($result['code']) && $result['code'] == 0) {
|
|
|
return $result['data'];
|
|
|
} else {
|
|
@@ -77,7 +76,7 @@ function http($url, $param, $method = 'post')
|
|
|
|
|
|
}
|
|
|
|
|
|
- function jsonSuccess($data = [], $msg = "成功", $extra = [])
|
|
|
+function jsonSuccess($data = [], $msg = "成功", $extra = [])
|
|
|
{
|
|
|
$response = array(
|
|
|
'code' => 0,
|
|
@@ -95,20 +94,20 @@ function http($url, $param, $method = 'post')
|
|
|
'pagination' => $data['meta']['pagination']
|
|
|
)
|
|
|
);
|
|
|
- if($extra){
|
|
|
+ if ($extra) {
|
|
|
$temp['data'] = array_merge($temp['data'], $extra);
|
|
|
}
|
|
|
$response = array_merge($response, $temp);
|
|
|
- } elseif(isset($data['data'])) {
|
|
|
- if($extra){
|
|
|
+ } elseif (isset($data['data'])) {
|
|
|
+ if ($extra) {
|
|
|
$data['data'] = array_merge($data['data'], $extra);
|
|
|
}
|
|
|
$response = array_merge($response, $data);
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
$temp = array(
|
|
|
'data' => $data
|
|
|
);
|
|
|
- if($extra){
|
|
|
+ if ($extra) {
|
|
|
$temp['data'] = array_merge($temp['data'], $extra);
|
|
|
}
|
|
|
$response = array_merge($response, $temp);
|
|
@@ -120,7 +119,7 @@ function http($url, $param, $method = 'post')
|
|
|
return $response;
|
|
|
}
|
|
|
|
|
|
- function jsonError($msg)
|
|
|
+function jsonError($msg)
|
|
|
{
|
|
|
$response = array(
|
|
|
'code' => 1,
|
|
@@ -132,8 +131,8 @@ function http($url, $param, $method = 'post')
|
|
|
|
|
|
function subtext($text, $length)
|
|
|
{
|
|
|
- if(mb_strlen($text, 'utf8') > $length) {
|
|
|
- return mb_substr($text, 0, $length - 1, 'utf8').'...';
|
|
|
+ if (mb_strlen($text, 'utf8') > $length) {
|
|
|
+ return mb_substr($text, 0, $length - 1, 'utf8') . '...';
|
|
|
} else {
|
|
|
return $text;
|
|
|
}
|
|
@@ -158,9 +157,9 @@ function success($data = [], $msg = "成功")
|
|
|
)
|
|
|
);
|
|
|
$response = array_merge($response, $temp);
|
|
|
- } elseif(isset($data['data'])) {
|
|
|
+ } elseif (isset($data['data'])) {
|
|
|
$response = $data;
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
$temp = array(
|
|
|
'data' => $data
|
|
|
);
|
|
@@ -170,6 +169,7 @@ function success($data = [], $msg = "成功")
|
|
|
}
|
|
|
return $response;
|
|
|
}
|
|
|
+
|
|
|
//转换数字
|
|
|
function getNumber($number)
|
|
|
{
|
|
@@ -186,3 +186,27 @@ function getNumber($number)
|
|
|
}
|
|
|
return (string)$number . $unit;
|
|
|
}
|
|
|
+
|
|
|
+function getClientIp()
|
|
|
+{
|
|
|
+ $ip = '';
|
|
|
+ $unknown = '';
|
|
|
+
|
|
|
+ if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
|
|
|
+ // 使用透明代理、欺骗性代理的情况
|
|
|
+ $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
|
+
|
|
|
+ } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
|
|
|
+ // 没有代理、使用普通匿名代理和高匿代理的情况
|
|
|
+ $ip = $_SERVER['REMOTE_ADDR'];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理多层代理的情况
|
|
|
+ if (strpos($ip, ',') !== false) {
|
|
|
+ // 输出第一个IP
|
|
|
+ $ips = explode(',', $ip);
|
|
|
+ $ip = $ips[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $ip;
|
|
|
+}
|