helper.php
17.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
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
<?php
// 公共助手函数
use Symfony\Component\VarExporter\VarExporter;
/**
* 求两个已知经纬度之间的距离,单位为米
* @param lng1 $ ,lng2 经度
* @param lat1 $ ,lat2 纬度
* @return float 距离,单位米
* @author www.Alixixi.com
*/
function getdistance($lng1, $lat1, $lng2, $lat2)
{
// 将角度转为狐度
$radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
$radLat2 = deg2rad($lat2);
$radLng1 = deg2rad($lng1);
$radLng2 = deg2rad($lng2);
$a = $radLat1 - $radLat2;
$b = $radLng1 - $radLng2;
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
return $s;
}
function float_number($number)
{
$number = intval($number);
$length = strlen(intval($number)); //数字长度
if ($length > 3) { //万单位
//截取前俩为
$str = substr_replace(strstr($number, substr($number, -2), ' '), '.', -1, 0) . "公里";
} else {
return $number . '米';
}
return $str . '米';
}
function build_field_option($name = null, $selected = null,$type = null)
{
//获取选项列表
$fields = \addons\antis\model\Field::getField($name,$type);
$html = '<option value="">请选择'.$name.'</option>';
foreach ($fields as $v) {
if ($selected == $v) {
$html .= "<option value='{$v}' selected='selected'>{$v}</option>";
} else {
$html .= "<option value='{$v}'>{$v}</option>";
}
}
return $html;
}
//修改多维数组为一位数组
function modify_array($arr, $field1, $field2)
{
$data = [];
foreach ($arr as $v) {
$data[$v[$field1] ?? ''] = $v[$field2] ?? '';
}
return $data;
}
//显示文件夹
function showdir($path)
{
$dh = opendir($path);//打开目录
$arr = [];
while (($d = readdir($dh)) != false) {
//逐个文件读取,添加!=false条件,是为避免有文件或目录的名称为0
if ($d == '.' || $d == '..') {//判断是否为.或..,默认都会有
continue;
}
if (is_dir($path . '/' . $d)) {//如果为目录
$lowers = showdir($path . '/' . $d);//继续读取该目录下的目录或文件
if ($lowers) {
$arr = array_merge($arr, $lowers);
}
} else {
$arr[] = $path . '/' . $d;
}
}
return $arr;
}
//获取年度列表
function getYears(){
$start=2020;
$years=[];
$toYear=date('Y');
$end=date('Y')+1;
for ($start;$start<=$end;$start++){
if($start == $toYear){
$years[]=[
'id'=>$start,
'name'=>$start.'年',
'selected'=>'selected'
];
}else{
$years[]=[
'id'=>$start,
'name'=>$start.'年',
];
}
}
return $years;
}
///随机字符串,字母+数字
function genRandomString($len,$t=0)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z"
);
$chars1= array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z"
);
$chars2 = array(
"1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$chars3 = array(
"A", "B", "C", "D", "E", "F", "G", "O" ,
"H", "I", "J", "K", "L", "M", "N", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z"
);
$chars4 = array(
"A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "1","2",
"3", "4", "5", "6", "7", "8", "9"
);
if($t==1){
$charsLen = count($chars1) - 1;
shuffle($chars1); // 将数组打乱
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars1[mt_rand(0, $charsLen)];
}
}elseif($t==2){
$charsLen = count($chars2) - 1;
shuffle($chars2); // 将数组打乱
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars2[mt_rand(0, $charsLen)];
}
}elseif($t==3){
$charsLen = count($chars3) - 1;
shuffle($chars3); // 将数组打乱
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars3[mt_rand(0, $charsLen)];
}
}elseif($t==4){
$charsLen = count($chars4) - 1;
shuffle($chars4); // 将数组打乱
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars4[mt_rand(0, $charsLen)];
}
}else{
$charsLen = count($chars) - 1;
shuffle($chars); // 将数组打乱
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars[mt_rand(0, $charsLen)];
}
}
return $output;
}
//将表情进行转义 用于存储的时候
function emoji_encode($str){
if(!is_string($str)) return $str;
if(!$str || $str=='undefined') return '';
$text = json_encode($str); //暴露出unicode
$text = preg_replace_callback("/(\\\u[2def][0-9a-f]{3})/i",function($str){
return addslashes($str[0]);
},$text); //将emoji的unicode留下,其他不动,这里的正则比原答案增加了d,因为我发现我很多emoji实际上是\ud开头的,反而暂时没发现有\ue开头。
return json_decode($text);
}
//将表情进行反转义 用于读取的时候
function emoji_decode($str){
$text = json_encode($str); //暴露出unicode
$text = preg_replace_callback('/\\\\\\\\/i',function($str){
return '\\';
},$text); //将两条斜杠变成一条,其他不动
return json_decode($text);
}
//过滤emoji
function filter_Emoji($str)
{
if ($str === null) {
$str = '';
} elseif (!is_string($str)) {
return $str;
}
$str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
'/./u',
function (array $match) {
return strlen($match[0]) >= 4 ? '' : $match[0];
},
$str);
return $str;
}
//获取编号
function get_num($type)
{
return 'R' . date('Ymd') . rand(10000, 99999);
}
if (!function_exists('setTimes')) {
/**
* @param $times
* @param string $type date|datetime|time
* @return array|string
*/
function setTimes($times, $type = 'date')
{
switch ($times) {
case 'today':
$today = date('Y-m-d');
$todayend = date('Y-m-d');
$result = [$today, $todayend];
break;
case 'yesterday':
$yesterday = date('Y-m-d', strtotime('-1 day'));
$result = [$yesterday, $yesterday];
break;
case 'tomorrow':
$tomorrow = date('Y-m-d', strtotime('+1 day'));
$result = [$tomorrow, $tomorrow];
break;
case 'thisweek':
$thisweek_start = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
$thisweek_end = date("Y-m-d", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y")));
$result = [$thisweek_start, $thisweek_end];
break;
case 'lastweek':
$lastweek_start = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y")));
$lastweek_end = date("Y-m-d", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y")));
$result = [$lastweek_start, $lastweek_end];
break;
case 'nextweek':
$lastweek_start = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 + 7, date("Y")));
$lastweek_end = date("Y-m-d", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 + 7, date("Y")));
$result = [$lastweek_start, $lastweek_end];
break;
case 'thismonth':
$thismonth_start = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y")));
$thismonth_end = date("Y-m-d", mktime(23, 59, 59, date("m"), date("t"), date("Y")));
$result = [$thismonth_start, $thismonth_end];
break;
case 'lastmonth':
$lastmonth_start = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
$lastmonth_end = date("Y-m-d", mktime(23, 59, 59, date("m"), 0, date("Y")));
$result = [$lastmonth_start, $lastmonth_end];
break;
case 'nextmonth':
$nextmonth_start = date("Y-m-d", mktime(0, 0, 0, date("m") + 1, 1, date("Y")));
$nextmonth_end = date("Y-m-d", mktime(23, 59, 59, date("m") + 1, date("t"), date("Y")));
$result = [$nextmonth_start, $nextmonth_end];
break;
case 'thisquarter':
//本季度未最后一月天数
$getMonthDays = date("t", mktime(0, 0, 0, date('n') + (date('n') - 1) % 3, 1, date("Y")));
//本季度/
$thisquarter_start = date('Y-m-d', mktime(0, 0, 0, date('n') - (date('n') - 1) % 3, 1, date('Y')));
$thisquarter_end = date('Y-m-d', mktime(23, 59, 59, date('n') + (date('n') - 1) % 3, $getMonthDays, date('Y')));
$result = [$thisquarter_start, $thisquarter_end];
break;
case 'lastquarter':
//上季度未最后一月天数
$getMonthDays = date("t", mktime(0, 0, 0, date('n') + (date('n') - 1) % 3 - 3, 1, date("Y")));
$thisquarter_start = date('Y-m-d', mktime(0, 0, 0, date('n') - (date('n') - 1) % 3 - 3, 1, date('Y')));
$thisquarter_end = date('Y-m-d', mktime(23, 59, 59, date('n') + (date('n') - 1) % 3 - 3, $getMonthDays, date('Y')));
$result = [$thisquarter_start, $thisquarter_end];
break;
case 'nextquarter':
//下季度未最后一月天数
$getMonthDays = date("t", mktime(0, 0, 0, date('n') + (date('n') - 1) % 3 + 3, 1, date("Y")));
$thisquarter_start = date('Y-m-d', mktime(0, 0, 0, date('n') - (date('n') - 1) % 3 + 3, 1, date('Y')));
$thisquarter_end = date('Y-m-d', mktime(23, 59, 59, date('n') + (date('n') - 1) % 3 + 3, $getMonthDays, date('Y')));
$result = [$thisquarter_start, $thisquarter_end];
break;
case 'thisyear':
//今年
$thisyear_start = date('Y') . '-1-1';
$thisquarter_end = date('Y') . '-12-31';
$result = [$thisyear_start, $thisquarter_end];
break;
case 'lastyear':
//去年
$lastyear_start = date('Y') . '-1-1';
$lastyear_end = date('Y') . '-12-31';
$result = [$lastyear_start, $lastyear_end];
break;
case 'nextyear':
//下一年
$nextyear_start = date('Y') . '-1-1';
$nextyear_end = date('Y') . '-12-31';
$result = [$nextyear_start, $nextyear_end];
break;
default:
$times = explode(',', $times);
$result = [$times[0] ?? date('Y-m-d'), $times[1] ?? date('Y-m-d')];
break;
}
if ($type == 'date') {
return $result;
} elseif ($type == 'datetime') {
$result[0] = date('Y-m-d H:i:s', strtotime($result[0]));
$result[1] = date('Y-m-d H:i:s', strtotime($result[1]) + 86400 - 1);
return $result;
} elseif ($type == 'time') {
$result[0] =strtotime($result[0]);
$result[1] =strtotime($result[1]) + 86400 - 1;
return $result;
} else {
return $result;
}
}
}
if (!function_exists('export_excel')){
//导出excel
function export_excel($title, $expTableData, $excel = 'export')
{
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $excel . '.xlsx');
header('Cache-Control: max-age=0');
$obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
// 以下内容是excel文件的信息描述信息
$obj->getProperties()->setTitle('导出文件'); //设置标题
$obj->setActiveSheetIndex(0);
$obj->getActiveSheet()->setTitle('导出文件');
/* 循环读取每个单元格的数据 */
$a = 'A';
$currentSheet = $obj->getActiveSheet();
foreach ($title as $key => $value) {
//读取工作表1
// 设置第一行加粗
$obj->getActiveSheet()->getStyle($a . '1')->getFont()->setBold(true);
//这里是设置单元格的内容
$currentSheet->getCell($a . '1')->setValue($value);
$a++;
}
//行数循环
$b = 2;
foreach ($expTableData as $k => $row) {
$a = 'A';
foreach ($row as $key => $value) {
//这里是设置单元格的内容
$currentSheet->getCell($a . $b)->setValue($value);
$a++;
}
$b++;
}
$PHPWriter = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($obj);
$fileName = $excel . date('YmdHis') . '.xlsx';
$file_path = ROOT_PATH . 'public' . '/export/' . date('Y-m-d') . '/' . $fileName;
// 创建文件夹
$path_info = pathinfo($file_path);
if ($path_info['dirname'] && !is_dir($path_info['dirname'])) {
$result = create_dir($path_info['dirname']);
if (false === $result) {
return false;
}
}
$PHPWriter->save($file_path);
$datas['filePath'] = '/' . 'export/' . date('Y-m-d') . '/' . $fileName;
$datas['fileName'] = $fileName;
$datas['size'] = filesize('./' . 'export/' . date('Y-m-d') . '/' . $fileName);
return $datas;
}
}
if (!function_exists('create_dir')){
/**
* 创建文件夹,可以多级创建
* @param string $filename 需要创建的文件夹路径
* @return boolean
*/
function create_dir($filename) {
if (!is_dir($filename)) {
create_dir(dirname($filename));
return mkdir($filename, 0777);
}
return true;
}
}
if (!function_exists('convertAmountToCn')){
/**
* 将数值金额转换为中文大写金额
* @param $amount float 金额(支持到分)
* @param $type int 补整类型,0:到角补整;1:到元补整
* @return mixed 中文大写金额
*/
function convertAmountToCn($amount, $type = 1)
{
// 判断输出的金额是否为数字或数字字符串
if (!is_numeric($amount)) {
return "要转换的金额只能为数字!";
}
// 金额为0,则直接输出"零元整"
if ($amount == 0) {
return "人民币零元整";
}
// 金额不能为负数
if ($amount < 0) {
return "要转换的金额不能为负数!";
}
// 金额不能超过万亿,即12位
if (strlen($amount) > 12) {
return "要转换的金额不能为万亿及更高金额!";
}
// 预定义中文转换的数组
$digital = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
// 预定义单位转换的数组
$position = array('仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾', '元');
// 将金额的数值字符串拆分成数组
$amountArr = explode('.', $amount);
// 将整数位的数值字符串拆分成数组
$integerArr = str_split($amountArr[0], 1);
// 将整数部分替换成大写汉字
$result = '';
$integerArrLength = count($integerArr); // 整数位数组的长度
$positionLength = count($position); // 单位数组的长度
for ($i = 0; $i < $integerArrLength; $i++) {
// 如果数值不为0,则正常转换
if ($integerArr[$i] != 0) {
$result = $result . $digital[$integerArr[$i]] . $position[$positionLength - $integerArrLength + $i];
} else {
// 如果数值为0, 且单位是亿,万,元这三个的时候,则直接显示单位
if (($positionLength - $integerArrLength + $i + 1) % 4 == 0) {
$result = $result . $position[$positionLength - $integerArrLength + $i];
}
}
}
// 如果小数位也要转换
if ($type == 0) {
// 将小数位的数值字符串拆分成数组
$decimalArr = str_split($amountArr[1], 1);
// 将角替换成大写汉字. 如果为0,则不替换
if ($decimalArr[0] != 0) {
$result = $result . $digital[$decimalArr[0]] . '角';
}
// 将分替换成大写汉字. 如果为0,则不替换
if ($decimalArr[1] != 0) {
$result = $result . $digital[$decimalArr[1]] . '分';
}
} else {
$result = $result . '整';
}
return $result;
}
}