forked from gongfuxiang/shopxo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.php
executable file
·3187 lines (2963 loc) · 88.6 KB
/
common.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://opensource.org/licenses/mit-license.php )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
// 应用公共文件
use app\service\SystemBaseService;
use app\service\ResourcesService;
use app\service\PluginsService;
use app\service\ConstService;
use app\service\AdminService;
use app\service\AdminPowerService;
use app\service\MultilingualService;
/**
* 图片转base64
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-03-08
* @desc description
* @param [string] $image [图片地址]
*/
function ImageToBase64($image)
{
return 'data:image/jpg/png/gif;base64,'.chunk_split(base64_encode(RequestGet($image)));
}
/**
* 两个数组字段对比处理、arr1不存在arr2中的字段则移除
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-11-12
* @desc description
* @param [array] $arr1 [待处理的数据]
* @param [array] $arr2 [参考对比的数据]
*/
function ArrayFieldContrastHandle($arr1, $arr2)
{
if(!empty($arr1) && is_array($arr1) && is_array($arr2))
{
foreach($arr1 as $fk=>$fv)
{
if(!array_key_exists($fk, $arr2))
{
unset($arr1[$fk]);
}
}
}
return $arr1;
}
/**
* 获取汉字拼音、默认返回数组
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-10-09
* @desc description
* @param [string] $string [汉字]
* @param [boolean] $is_string [返回字符串]
* @param [string] $join [字符串连接符号]
*/
function ChinesePinyin($string, $is_string = false, $join = '')
{
$value = (new \Overtrue\Pinyin\Pinyin())->convert($string);
return ($is_string && is_array($value)) ? implode($join, $value) : $value;
}
/**
* 获取汉字首字母
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-10-09
* @desc description
* @param [string] $string [汉字]
*/
function ChineseLetter($string)
{
$value = (new \Overtrue\Pinyin\Pinyin())->abbr($string);
return empty($value) ? '' : $value;
}
/**
* 弹出内容处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-09-26
* @desc description
* @param [string] $content [展示的内容]
*/
function PopoverContentHandle($content)
{
return str_replace(["\n", "\r", "'", '"'], ['<br />', '', '', ''], $content);
}
/**
* 生成uuid
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-10-30
* @desc description
*/
function UUId()
{
$chars = md5(uniqid(mt_rand(), true));
$uuid = substr($chars, 0, 8) . '-'
. substr($chars, 8, 4) . '-'
. substr($chars, 12, 4) . '-'
. substr($chars, 16, 4) . '-'
. substr($chars, 20, 12);
return $uuid;
}
/**
* 获取常量数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-08-14
* @desc description
* @param [string] $key [数据key]
* @param [mixed] $default [默认值]
*/
function MyConst($key = '', $default = null)
{
return ConstService::Run($key, $default);
}
/**
* session管理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-17
* @desc description
* @param [string] $name [session名称]
* @param [mixed] $value [session值]
*/
function MySession($name = '', $value = '')
{
// 调用框架session统一方法
$res = session($name, $value);
// 调用框架session数据保存、避免页面退出导致session保存失败
// 框架是页面return才自动执行这个方法的
if($value !== '' && $value !== null)
{
\think\facade\Session::save();
}
return $res;
}
/**
* 获取语言
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-08-19
* @desc 框架默认仅支持二级分组数据、这里做了支持N级处理(由于参数可能存在数组解析原因)这里单独处理不使用框架处理
* @param [string] $key [语言key(支持 . 多级)]
* @param [array] $vars [替换参数]
* @param [string] $lang [指定语言]
* @param [string] $plugins [指定插件]
*/
function MyLang($key, $vars = [], $lang = '', $plugins = '')
{
$value = '';
if(!empty($key))
{
// 当前语言
$current_lang = empty($lang) ? MultilingualService::GetUserMultilingualValue() : $lang;
// key使用 . 分隔
$key_arr = explode('.', $key);
// 语言数据容器
static $lang_data = [];
// 系统语言
$request_module = RequestModule();
$arr_file = [
APP_PATH.$request_module.DS.'lang'.DS.$current_lang.'.php',
APP_PATH.'lang'.DS.$current_lang.'.php',
];
// 是否插件语言
if(!empty($plugins) || RequestController() == 'plugins')
{
$pluginsname = empty($plugins) ? MyInput('pluginsname') : $plugins;
$plugins_dir = APP_PATH.'plugins'.DS.$pluginsname.DS.'lang'.DS;
array_unshift($arr_file, $plugins_dir.$current_lang.'.php');
array_unshift($arr_file, $plugins_dir.$request_module.DS.$current_lang.'.php');
}
foreach($arr_file as $file)
{
$md5_key = md5($file);
if(!array_key_exists($md5_key, $lang_data) && file_exists($file))
{
$lang_data[$md5_key] = require $file;
}
if(!empty($lang_data[$md5_key]) && is_array($lang_data[$md5_key]))
{
$temp_lang_data = $lang_data[$md5_key];
// 仅一级则直接读取
if(count($key_arr) == 1)
{
if(array_key_exists($key, $temp_lang_data))
{
$value = $temp_lang_data[$key];
}
} else {
// 默认先读取第一级
if(array_key_exists($key_arr[0], $temp_lang_data))
{
$value = $temp_lang_data[$key_arr[0]];
if(!empty($value) && is_array($value))
{
// 移除第一级
array_shift($key_arr);
// 循环后面级别的数据
foreach($key_arr as $v)
{
if(array_key_exists($v, $value))
{
$value = $value[$v];
} else {
// 未匹配到则赋空值
$value = $key;
break;
}
}
}
}
}
}
}
// 未找到对应语言
if($value == '' || $key == $value)
{
// 非默认语言则读取默认语言
$default_lang = MyConfig('lang.default_lang');
if($default_lang != $lang)
{
$value = MyLang($key, $vars, $default_lang);
}
}
// 变量解析
if(is_string($value) && !empty($value) && !empty($vars) && is_array($vars))
{
// 为了检测的方便,数字索引的判断仅仅是参数数组的第一个元素的key为数字0
// 数字索引采用的是系统的 sprintf 函数替换,用法请参考 sprintf 函数
if(key($vars) === 0)
{
// 数字索引解析
array_unshift($vars, $value);
$value = call_user_func_array('sprintf', $vars);
} else {
// 关联索引解析
$replace = array_keys($vars);
foreach($replace as &$v)
{
$v = "{:{$v}}";
}
$value = str_replace($replace, $vars, $value);
}
}
}
return $value;
}
/**
* cookie管理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-17
* @desc description
* @param [string] $name [cookie名称]
* @param [mixed] $value [cookie值]
* @param [boolean] $is_encryption [是否需要加密存储]
*/
function MyCookie($name = '', $value = '', $is_encryption = true)
{
// 非空则转换数据
if($value !== null && $value !== '' && $is_encryption)
{
$value = urlencode(Authcode(base64_encode(json_encode($value)), 'ENCODE'));
}
$res = cookie($name, $value);
return ($res === null || $res === '' || !$is_encryption) ? $res : json_decode(base64_decode(Authcode(urldecode($res), 'DECODE')), true);
}
/**
* 缓存管理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-17
* @desc description
* @param [string] $name [缓存名称]
* @param [mixed] $value [缓存值]
* @param [mixed] $options [缓存参数]
* @param [string] $tag [缓存标签]
*/
function MyCache($name = null, $value = '', $options = null, $tag = null)
{
// 静态存储、不用每次都从磁盘读取
static $object_cache = [];
// 读取数据
if($value === '')
{
if(!array_key_exists($name, $object_cache))
{
$object_cache[$name] = cache($name, $value, $options, $tag);
}
return $object_cache[$name];
}
// 设置数据
return cache($name, $value, $options, $tag);
}
/**
* 环境变量配置
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-17
* @desc description
* @param [string] $key [key名称、支持.连接子数据,比如 shopxo.hello]
* @param [mixed] $val [默认值]
*/
function MyEnv($key, $val = null)
{
// 静态存储、不用每次都从磁盘读取
static $object_env = [];
if(!array_key_exists($key, $object_env))
{
$object_env[$key] = env($key, $val);
}
return $object_env[$key];
}
/**
* 配置读取
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-17
* @desc description
* @param [string] $key [key名称、支持.连接子数据,比如 shopxo.hello ]
*/
function MyConfig($key)
{
// 静态存储、不用每次都从磁盘读取
static $object_config = [];
if(!array_key_exists($key, $object_config))
{
$object_config[$key] = config($key);
}
return $object_config[$key];
}
/**
* 重定向
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-16
* @desc description
* @param [string] $url [url地址 或 模块地址]
* @param [boolean] $is_exit [是否需要结束运行、默认 false]
*/
function MyRedirect($url, $is_exit = false)
{
if(!IsUrl($url))
{
$url = MyUrl($url);
}
if($is_exit)
{
exit(header('location:'.$url));
} else {
return redirect($url);
}
}
/**
* 钩子事件定义
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-16
* @desc description
* @param [string] $key [钩子名称]
* @param [array] $params [输入参数]
*/
function MyEventTrigger($key, $params = [])
{
return event($key, $params);
}
/**
* 视图赋值
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-16
* @desc description
* @param [string|array] $data [key名称|批量赋值(数组)]
* @param [mixed] $value [数据值]
*/
function MyViewAssign($data, $value = '')
{
// 模板引擎数据渲染分配钩子
$hook_name = 'plugins_view_assign_data';
MyEventTrigger($hook_name,
[
'hook_name' => $hook_name,
'is_backend' => true,
'data' => &$data,
'value' => &$value,
]);
\think\facade\View::assign($data, $value);
}
/**
* 视图调用
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-16
* @desc description
* @param [string] $view [视图地址]
* @param [array] $data [模板变量]
*/
function MyView($view = '', $data = [])
{
// 模板引擎数据渲染前钩子
$hook_name = 'plugins_view_fetch_begin';
MyEventTrigger($hook_name,
[
'hook_name' => $hook_name,
'is_backend' => true,
'view' => &$view,
'data' => &$data,
]);
// 调用框架视图方法
$result = \think\facade\View::fetch($view, $data);
// 模板引擎数据渲染后钩子
$hook_name = 'plugins_view_fetch_end';
MyEventTrigger($hook_name,
[
'hook_name' => $hook_name,
'is_backend' => true,
'view' => &$view,
'data' => $data,
'result' => &$result,
]);
return $result;
}
/**
* 当前请求模块组名
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-16
* @desc description
*/
function RequestModule()
{
static $request_module = null;
if($request_module === null)
{
$request_module = strtolower(app('http')->getName());
}
return $request_module;
}
/**
* 当前请求控制器名
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-16
* @desc description
*/
function RequestController()
{
static $request_controller = null;
if($request_controller === null)
{
$request_controller = strtolower(request()->controller());
}
return $request_controller;
}
/**
* 当前请求方法名
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-16
* @desc description
*/
function RequestAction()
{
static $request_action = null;
if($request_action === null)
{
$request_action = strtolower(request()->action());
}
return $request_action;
}
/**
* 获取链接http状态码
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-09
* @desc description
* @param [string] $url [链接地址]
* @param [int] $timeout [超时时间(秒)]
*/
function GetHttpCode($url, $timeout = 5)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 返回结果
$result = curl_exec($ch);
if($result !== false)
{
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return DataReturn('success', 0, $code);
} else {
$error_code = curl_errno($ch);
$error_msg = curl_error($ch);
curl_close($ch);
return DataReturn($error_msg.' ('.$error_code.')', -9999, $error_code);
}
}
/**
* 判断是否是url地址
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-07-09
* @desc description
* @param [string] $value [地址值]
*/
function IsUrl($value)
{
return empty($value) ? false : in_array(substr($value, 0, 6), ['http:/', 'https:']);
}
/**
* 快速排序
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-03-09
* @desc description
* @param [array] $data [需要排序的数据(选择一个基准元素,将待排序分成小和打两罐部分,以此类推递归的排序划分两罐部分)]
* @param [array] [数组字段]
* @param [int] [类型(0从小到大、1从大到小)]
* @return [array] [排序好的数据]
*/
function ArrayQuickSort($data, $field, $type = 0)
{
if(!empty($data) && is_array($data))
{
$len = count($data);
if($len <= 1) return $data;
$base = $data[0];
$left_array = array();
$right_array = array();
for($i=1; $i<$len; $i++)
{
if($type == 0)
{
if($base[$field] > $data[$i][$field])
{
$left_array[] = $data[$i];
} else {
$right_array[] = $data[$i];
}
} else {
if($base[$field] < $data[$i][$field])
{
$left_array[] = $data[$i];
} else {
$right_array[] = $data[$i];
}
}
}
if(!empty($left_array))
{
$left_array = ArrayQuickSort($left_array, $field, $type);
}
if(!empty($right_array))
{
$right_array = ArrayQuickSort($right_array, $field, $type);
}
return array_merge($left_array, array($base), $right_array);
}
}
/**
* 是否base64加密的字符串
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-02-28
* @desc description
* @param [string] $str [base加密的字符串]
*/
function IsBase64($str)
{
$str = urldecode($str);
return ($str == base64_encode(base64_decode($str)));
}
/**
* 根据url地址解析顶级域名
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-02-25
* @desc description
* @param [string] $url [url地址]
*/
function GetUrlHost($url)
{
// 地址解析
$arr = parse_url(strtolower($url));
$host = (count($arr) == 1) ? (isset($arr['path']) ? $arr['path'] : '') : (empty($arr['host']) ? '' : $arr['host']);
if(empty($host))
{
return $url;
}
// 是否存在斜杠
if(stripos($host, '/') !== false)
{
$slash = explode('/', $host);
$host = $slash[0];
}
// 查看是几级域名
$data = explode('.', $host);
$n = count($data);
if(count($data) == 1)
{
return $host;
}
// 判断是否是双后缀
$preg = '/[\w].+\.(com|net|org|gov|ac|bj|sh|tj|cq|he|sn|sx|nm|ln|jl|hl|js|zj|ah|fj|jx|sd|ha|hb|hn|gd|gx|hi|sc|gz|yn|gs|qh|nx|xj|tw|hk|mo|xz|edu|ge|dev|co)\.(cn|nz|mm)$/';
if(($n > 2) && preg_match($preg, $host))
{
// 双后缀取后3位
$host = $data[$n-3].'.'.$data[$n-2].'.'.$data[$n-1];
} else {
// 非双后缀取后两位
$host = $data[$n-2].'.'.$data[$n-1];
}
return $host;
}
/**
* 文件配置数据读写
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-11-13
* @desc description
* @param [string] $key [数据缓存可以]
* @param [mixed] $value [数据值(空字符串:读, null:删除, 其他:写)]
* @param [mixed] $default [默认值]
* @param [boolean] $mandatory[是否强制判断数据值(空字符串|null|0)视为空]
* @return [mixed] [缓存不存在:null, 则缓存数据]
*/
function MyFileConfig($key, $value = '', $default = null, $mandatory = false)
{
// 静态存储、不用每次都从磁盘读取
static $object_file_cache_config = [];
// 目录不存在则创建
$config_dir = ROOT.'runtime'.DS.'data'.DS.'config_data'.DS;
\base\FileUtil::CreateDir($config_dir);
// 数据文件
$file = $config_dir.md5($key).'.txt';
// 删除
if($value === null)
{
return \base\FileUtil::UnlinkFile($file);
} else {
// 读内容
if($value === '')
{
if(!array_key_exists($key, $object_file_cache_config))
{
$value = file_exists($file) ? unserialize(file_get_contents($file)) : $default;
if($mandatory === true)
{
if(empty($value))
{
$value = $default;
}
}
$object_file_cache_config[$key] = $value;
} else {
$value = $object_file_cache_config[$key];
}
return $value;
// 写内容
} else {
// 目录是否有可写权限
if(!is_writable($config_dir))
{
return false;
}
// 存在则校验写权限
if(file_exists($file) && !is_writable($file))
{
return false;
}
// 存储内容
if(file_put_contents($file, serialize($value)) !== false)
{
$object_file_cache_config[$key] = $value;
return true;
}
return false;
}
}
}
/**
* 获取参数数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-23
* @desc description
* @param [string] $key [参数key]
* @param [string] $default [默认值]
*/
function MyInput($key = null, $default = '')
{
static $params = null;
if($params === null)
{
$params = input();
if(empty($params))
{
$params = file_get_contents("php://input");
}
}
// 非数组则检查是否为json和xml数据
if(!is_array($params))
{
if(IsJson($params))
{
$params = json_decode($params, true);
} else {
if(XmlParser($params))
{
$params = XmlArray($params);
}
}
}
// 是否指定key
if(!empty($key) && is_array($params))
{
if(array_key_exists($key, $params))
{
return is_array($params[$key]) ? $params[$key] : htmlspecialchars(trim($params[$key]));
}
return $default;
}
// 未指定key则返回所有数据
return $params;
}
/**
* 当前应用平台
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-11
* @desc description
*/
function ApplicationClientType()
{
// 平台
$platform = APPLICATION_CLIENT_TYPE;
// web端手机访问
if($platform == 'pc' && IsMobile())
{
$platform = 'h5';
}
return $platform;
}
/**
* 是否微信环境
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-08-26
* @desc description
* @return [boolean] [否false, 是true]
*/
function IsWeixinEnv()
{
return (!empty($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false);
}
/**
* 是否微信环境
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-08-26
* @desc description
* @return [boolean] [否false, 是true]
*/
function IsDingdingEnv()
{
return (!empty($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'DingTalk') !== false);
}
/**
* 是否QQ环境
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-08-26
* @desc description
* @return [boolean] [否false, 是true]
*/
function IsQQEnv()
{
return (!empty($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'QQ/') !== false);
}
/**
* 是否支付宝环境
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-08-26
* @desc description
* @return [boolean] [否false, 是true]
*/
function IsAlipayEnv()
{
return (!empty($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'AlipayClient') !== false);
}
/**
* 是否新浪微博环境
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-08-26
* @desc description
* @return [boolean] [否false, 是true]
*/
function IsWeiboEnv()
{
return (!empty($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Weibo') !== false);
}
/**
* 笛卡尔积生成规格
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-15
* @desc description
* @param [array] $arr1 [要进行笛卡尔积的二维数组]
* @param [array] $arr2 [最终实现的笛卡尔积组合,可不传]
*/
function SpecCartesian($arr1, $arr2 = [])
{
$result = [];
if(!empty($arr1))
{
// 去除第一个元素
$first = array_splice($arr1, 0, 1);
// 判断是否是第一次进行拼接
if(count($arr2) > 0)
{
foreach($arr2 as $v)
{
foreach($first[0] as $vs)
{
$result[] = $v.','.$vs;
}
}
} else {
foreach($first[0] as $vs)
{
$result[] = $vs;
}
}
// 递归进行拼接
if(count($arr1) > 0)
{
$result = SpecCartesian($arr1, $result);
}
}
return $result;
}
/**
* 后台管理权限校验方法
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-12
* @desc description
* @param [string] $controller [控制器(默认读取当前)]
* @param [string] $action [方法(默认读取当前)]
* @param [array] $unwanted_power [不校验权限的方法(默认空)]
*/
function AdminIsPower($controller = null, $action = null, $unwanted_power = [])
{
// 控制器/方法
$controller = strtolower(empty($controller) ? request()->controller() : $controller);
$action = strtolower(empty($action) ? request()->action() : $action);
// 管理员
$admin = AdminService::LoginInfo();
if(!empty($admin))
{
// 不需要校验权限的方法
if(!empty($unwanted_power) && in_array($action, $unwanted_power))
{
return true;
}
// 权限
// 角色组权限列表校验
$res = AdminPowerService::PowerMenuInit();
if(!empty($res) && !empty($res['admin_power']) && is_array($res['admin_power']) && in_array($controller.'_'.$action, $res['admin_power']))
{
return true;
}
}
return false;
}
/**
* 获取数组字段名称
* @author Devil
* @blog http://gong.gg/