Tool.php
5.6 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace tinymeng\tools;
/**
* Name: Tool.php.
* Author: JiaMeng <666@majiameng.com>
* Date: 2018/8/17 14:20
* Description: Tool.php.
*/
class Tool{
/**
* Description: 对象到数组转换
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param $obj
* @return array
*/
private function objectToArray($obj){
if(!is_object($obj) && !is_array($obj)) {
return $obj;
}
$arr = array();
foreach($obj as $k => $v){
$arr[$k] = $this->objectToArray($v);
}
return $arr;
}
/**
* Description: 获取ip
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @return string
*/
public static function getIp(){
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$cip = $_SERVER["HTTP_CLIENT_IP"];
}else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}else if(!empty($_SERVER["REMOTE_ADDR"])){
$cip = $_SERVER["REMOTE_ADDR"];
}else{
$cip = '';
}
preg_match("/[\d\.]{7,15}/", $cip, $cips);
$cip = isset($cips[0]) ? $cips[0] : 'unknown';
unset($cips);
return $cip;
}
/**
* Description: 是否是移动端
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @return bool
*/
public static function isMobile()
{
// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
return true;
}
// 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset ($_SERVER['HTTP_VIA'])) {
// 找不到为flase,否则为true
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
}
// 脑残法,判断手机发送的客户端标志,兼容性有待提高
if (isset ($_SERVER['HTTP_USER_AGENT'])) {
$clientKeyWords = array(
'nokia',
'sony',
'ericsson',
'mot',
'samsung',
'htc',
'sgh',
'lg',
'sharp',
'sie-',
'philips',
'panasonic',
'alcatel',
'lenovo',
'iphone',
'ipod',
'blackberry',
'meizu',
'android',
'netfront',
'symbian',
'ucweb',
'windowsce',
'palm',
'operamini',
'operamobi',
'openwave',
'nexusone',
'cldc',
'midp',
'wap',
'mobile'
);
// 从HTTP_USER_AGENT中查找手机浏览器的关键字
if (preg_match("/(" . implode('|', $clientKeyWords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
return true;
}
}
// 协议法,因为有可能不准确,放到最后判断
if (isset ($_SERVER['HTTP_ACCEPT'])) {
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
return true;
}
}
return false;
}
/**
* Description: 判断是否微信内置浏览器访问
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @return bool
*/
public static function isWeiXin() {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
return true;
}
return false;
}
/**
* Description: 判断是否支付宝内置浏览器访问
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @return bool
*/
public static function isAliPay() {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Alipay') !== false) {
return true;
}
return false;
}
/**
* Description: 获取菜单树结构
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param array $list 数据list
* @param string $filed 主键字段
* @param string $parent_filed 父节点字段
* @param int $parent_id 父id值
* @return array
*/
public static function getTreeStructure($list,$filed='id',$parent_filed='pid',$parent_id=0){
$result = array();
if(!empty($list)){
foreach($list as $key=>$val){
if($val[$parent_filed] == $parent_id){
$val['child'] = self::getTreeStructure($list,$filed,$parent_filed,$val[$filed]);
if(empty($val['child'])){
unset($val['child']);
}
$result[] = $val;
}
}
}
return $result;
}
/**
* 空数组转为object
* 给安卓出接口时部分要求[]返回{}(数组返回字典类型)
* @Author: TinyMeng <666@majiameng.com>
* @param $data
*/
static public function nullArrayToObject(&$data){
foreach ($data as $key=>&$val){
if(is_array($val)){
if(empty($val)){
settype($val,'object');
}else{
self::nullArrayToObject($val);
}
}
}
}
}