1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: frowhy |
5
|
|
|
* Date: 2017/11/26 |
6
|
|
|
* Time: 下午2:45. |
7
|
|
|
*/ |
8
|
|
|
use Carbon\Carbon; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
|
13
|
|
|
/* |
14
|
|
|
* 生成验证码 |
15
|
|
|
*/ |
16
|
|
|
if (!function_exists('verification_code')) { |
17
|
|
|
function verification_code(int $length = 4, string $type = 'int'): string |
18
|
|
|
{ |
19
|
|
|
if ('int' === $type) { |
20
|
|
|
return sprintf("%0{$length}d", rand(0, pow(10, $length) - 1)); |
21
|
|
|
} else { |
22
|
|
|
return Str::random($length); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/* |
28
|
|
|
* 相对 URL |
29
|
|
|
*/ |
30
|
|
|
if (!function_exists('relative_url')) { |
31
|
|
|
function relative_url(?string $url = null): ?string |
32
|
|
|
{ |
33
|
|
|
return $url === null |
34
|
|
|
? $url |
35
|
|
|
: (false === Str::startsWith($url, 'http://') ? (false === Str::startsWith($url, 'https://') |
36
|
|
|
? $url : Str::replaceFirst('https://', '//', $url)) : Str::replaceFirst('http://', '//', $url)); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/* |
41
|
|
|
* 储存 URL |
42
|
|
|
*/ |
43
|
|
|
if (!function_exists('storage_url')) { |
44
|
|
|
function storage_url(?string $url = null): ?string |
45
|
|
|
{ |
46
|
|
|
return $url === null ? $url : (Str::startsWith($url, 'http') ? $url : Storage::url($url)); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* |
51
|
|
|
* 两位小数 |
52
|
|
|
*/ |
53
|
|
|
if (!function_exists('price')) { |
54
|
|
|
function price(float $price): string |
55
|
|
|
{ |
56
|
|
|
return number_format($price, 2); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/* |
61
|
|
|
* 16 进制转 RGB |
62
|
|
|
*/ |
63
|
|
|
if (!function_exists('hex2rgb')) { |
64
|
|
|
function hex2rgb(string $hexColor): array |
65
|
|
|
{ |
66
|
|
|
$color = str_replace('#', '', $hexColor); |
67
|
|
|
if (strlen($color) > 3) { |
68
|
|
|
$rgb = [ |
69
|
|
|
'r' => hexdec(substr($color, 0, 2)), |
70
|
|
|
'g' => hexdec(substr($color, 2, 2)), |
71
|
|
|
'b' => hexdec(substr($color, 4, 2)), |
72
|
|
|
]; |
73
|
|
|
} else { |
74
|
|
|
$color = $hexColor; |
75
|
|
|
$r = substr($color, 0, 1).substr($color, 0, 1); |
76
|
|
|
$g = substr($color, 1, 1).substr($color, 1, 1); |
77
|
|
|
$b = substr($color, 2, 1).substr($color, 2, 1); |
78
|
|
|
$rgb = [ |
79
|
|
|
'r' => hexdec($r), |
80
|
|
|
'g' => hexdec($g), |
81
|
|
|
'b' => hexdec($b), |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $rgb; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/* |
90
|
|
|
* 灰度等级 |
91
|
|
|
*/ |
92
|
|
|
if (!function_exists('gray_level')) { |
93
|
|
|
function gray_level(array $rgb): float |
94
|
|
|
{ |
95
|
|
|
return $rgb['r'] * 0.299 + $rgb['g'] * 0.587 + $rgb['b'] * 0.114; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* |
100
|
|
|
* 去年时间范围 |
101
|
|
|
*/ |
102
|
|
|
if (!function_exists('last_year')) { |
103
|
|
|
function last_year(): array |
104
|
|
|
{ |
105
|
|
|
$carbon = new Carbon(); |
106
|
|
|
$start_at = $carbon->today()->subYear()->startOfYear(); |
107
|
|
|
$end_at = $carbon->today()->subYear()->endOfYear(); |
108
|
|
|
|
109
|
|
|
return compact('start_at', 'end_at'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/* |
114
|
|
|
* 今年时间范围 |
115
|
|
|
*/ |
116
|
|
|
if (!function_exists('this_year')) { |
117
|
|
|
function this_year(): array |
118
|
|
|
{ |
119
|
|
|
$carbon = new Carbon(); |
120
|
|
|
$start_at = $carbon->today()->startOfYear(); |
121
|
|
|
$end_at = $carbon->today()->endOfYear(); |
122
|
|
|
|
123
|
|
|
return compact('start_at', 'end_at'); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/* |
128
|
|
|
* 明年时间范围 |
129
|
|
|
*/ |
130
|
|
|
if (!function_exists('next_year')) { |
131
|
|
|
function next_year(): array |
132
|
|
|
{ |
133
|
|
|
$carbon = new Carbon(); |
134
|
|
|
$start_at = $carbon->today()->addYear()->startOfYear(); |
135
|
|
|
$end_at = $carbon->today()->addYear()->endOfYear(); |
136
|
|
|
|
137
|
|
|
return compact('start_at', 'end_at'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/* |
142
|
|
|
* 上个月时间范围 |
143
|
|
|
*/ |
144
|
|
|
if (!function_exists('last_month')) { |
145
|
|
|
function last_month(): array |
146
|
|
|
{ |
147
|
|
|
$carbon = new Carbon(); |
148
|
|
|
$start_at = $carbon->today()->subMonth()->startOfMonth(); |
149
|
|
|
$end_at = $carbon->today()->subMonth()->endOfMonth(); |
150
|
|
|
|
151
|
|
|
return compact('start_at', 'end_at'); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/* |
156
|
|
|
* 本月时间范围 |
157
|
|
|
*/ |
158
|
|
|
if (!function_exists('this_month')) { |
159
|
|
|
function this_month(): array |
160
|
|
|
{ |
161
|
|
|
$carbon = new Carbon(); |
162
|
|
|
$start_at = $carbon->today()->startOfMonth(); |
163
|
|
|
$end_at = $carbon->today()->endOfMonth(); |
164
|
|
|
|
165
|
|
|
return compact('start_at', 'end_at'); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/* |
170
|
|
|
* 下个月时间范围 |
171
|
|
|
*/ |
172
|
|
|
if (!function_exists('next_month')) { |
173
|
|
|
function next_month(): array |
174
|
|
|
{ |
175
|
|
|
$carbon = new Carbon(); |
176
|
|
|
$start_at = $carbon->today()->addMonth()->startOfMonth(); |
177
|
|
|
$end_at = $carbon->today()->addMonth()->endOfMonth(); |
178
|
|
|
|
179
|
|
|
return compact('start_at', 'end_at'); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/* |
184
|
|
|
* 上周时间范围 |
185
|
|
|
*/ |
186
|
|
|
if (!function_exists('last_week')) { |
187
|
|
|
function last_week(): array |
188
|
|
|
{ |
189
|
|
|
$carbon = new Carbon(); |
190
|
|
|
$start_at = $carbon->today()->subWeek()->startOfWeek(); |
191
|
|
|
$end_at = $carbon->today()->subWeek()->endOfWeek(); |
192
|
|
|
|
193
|
|
|
return compact('start_at', 'end_at'); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/* |
198
|
|
|
* 本周时间范围 |
199
|
|
|
*/ |
200
|
|
|
if (!function_exists('this_week')) { |
201
|
|
|
function this_week(): array |
202
|
|
|
{ |
203
|
|
|
$carbon = new Carbon(); |
204
|
|
|
$start_at = $carbon->today()->startOfWeek(); |
205
|
|
|
$end_at = $carbon->today()->endOfWeek(); |
206
|
|
|
|
207
|
|
|
return compact('start_at', 'end_at'); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/* |
212
|
|
|
* 下周时间范围 |
213
|
|
|
*/ |
214
|
|
|
if (!function_exists('next_week')) { |
215
|
|
|
function next_week(): array |
216
|
|
|
{ |
217
|
|
|
$carbon = new Carbon(); |
218
|
|
|
$start_at = $carbon->today()->addWeek()->startOfWeek(); |
219
|
|
|
$end_at = $carbon->today()->addWeek()->endOfWeek(); |
220
|
|
|
|
221
|
|
|
return compact('start_at', 'end_at'); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/* |
226
|
|
|
* 昨天时间范围 |
227
|
|
|
*/ |
228
|
|
|
if (!function_exists('yesterday')) { |
229
|
|
|
function yesterday(): array |
230
|
|
|
{ |
231
|
|
|
$carbon = new Carbon(); |
232
|
|
|
$start_at = $carbon->yesterday()->startOfDay(); |
233
|
|
|
$end_at = $carbon->yesterday()->startOfDay(); |
234
|
|
|
|
235
|
|
|
return compact('start_at', 'end_at'); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/* |
240
|
|
|
* 今天时间范围 |
241
|
|
|
*/ |
242
|
|
|
if (!function_exists('today')) { |
243
|
|
|
function today(): array |
244
|
|
|
{ |
245
|
|
|
$carbon = new Carbon(); |
246
|
|
|
$start_at = $carbon->today()->startOfDay(); |
247
|
|
|
$end_at = $carbon->today()->startOfDay(); |
248
|
|
|
|
249
|
|
|
return compact('start_at', 'end_at'); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/* |
254
|
|
|
* 明天时间范围 |
255
|
|
|
*/ |
256
|
|
|
if (!function_exists('tomorrow')) { |
257
|
|
|
function tomorrow(): array |
258
|
|
|
{ |
259
|
|
|
$carbon = new Carbon(); |
260
|
|
|
$start_at = $carbon->tomorrow()->startOfDay(); |
261
|
|
|
$end_at = $carbon->tomorrow()->startOfDay(); |
262
|
|
|
|
263
|
|
|
return compact('start_at', 'end_at'); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/* |
268
|
|
|
* 微信浏览器 |
269
|
|
|
*/ |
270
|
|
|
if (!function_exists('in_wechat')) { |
271
|
|
|
function in_wechat(): bool |
272
|
|
|
{ |
273
|
|
|
return Str::contains(request(), 'MicroMessenger'); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/* |
278
|
|
|
* 微信 |
279
|
|
|
*/ |
280
|
|
|
if (!function_exists('is_wechat')) { |
281
|
|
|
function is_wechat(): bool |
282
|
|
|
{ |
283
|
|
|
return in_wechat() && !is_mini_program(); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/* |
288
|
|
|
* 小程序 |
289
|
|
|
*/ |
290
|
|
|
if (!function_exists('is_mini_program')) { |
291
|
|
|
function is_mini_program(): bool |
292
|
|
|
{ |
293
|
|
|
return in_wechat() && request()->get('is_mini_program', false); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/* |
298
|
|
|
* 读取 DATA 数据 |
299
|
|
|
*/ |
300
|
|
|
if (!function_exists('get_data')) { |
301
|
|
|
function get_data($data, $index = null, $key = null) |
302
|
|
|
{ |
303
|
|
|
if (method_exists($data, 'toArray')) { |
304
|
|
|
$data = $data->toArray(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$field = condition(Arr::has($data, 'data'), 'data.', ''); |
308
|
|
|
|
309
|
|
|
if (Arr::has($data, "{$field}0") && !Arr::has($data, "{$field}1")) { |
310
|
|
|
if (!is_null($index) && is_int($index)) { |
311
|
|
|
$key = "{$index}.{$key}"; |
312
|
|
|
} else { |
313
|
|
|
$key = condition(is_null($index), 0, "0.{$index}"); |
314
|
|
|
} |
315
|
|
|
} else { |
316
|
|
|
$key = condition(is_null($index), '', $index); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
$key = rtrim("{$field}{$key}", '.'); |
320
|
|
|
|
321
|
|
|
return condition($key, Arr::get($data, $key), $data); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/* |
326
|
|
|
* 三元运算 |
327
|
|
|
*/ |
328
|
|
|
if (!function_exists('condition')) { |
329
|
|
|
function condition($condition, $true, $false) |
330
|
|
|
{ |
331
|
|
|
if ($condition) { |
332
|
|
|
return $true; |
333
|
|
|
} else { |
334
|
|
|
return $false; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/* |
340
|
|
|
* 清空缓存 |
341
|
|
|
*/ |
342
|
|
|
if (!function_exists('clear_cache')) { |
343
|
|
|
function clear_cache(): void |
344
|
|
|
{ |
345
|
|
|
Cache::tags('website')->flush(); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/* |
350
|
|
|
* 判定缓存 |
351
|
|
|
*/ |
352
|
|
|
if (!function_exists('has_cache')) { |
353
|
|
|
function has_cache(string $uri): bool |
354
|
|
|
{ |
355
|
|
|
return Cache::tags('website')->has($uri); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/* |
360
|
|
|
* 读取缓存 |
361
|
|
|
*/ |
362
|
|
|
if (!function_exists('get_cache')) { |
363
|
|
|
function get_cache(string $uri) |
364
|
|
|
{ |
365
|
|
|
return Cache::tags('website')->get($uri); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/* |
370
|
|
|
* 写缓存 |
371
|
|
|
*/ |
372
|
|
|
if (!function_exists('set_cache')) { |
373
|
|
|
function set_cache(string $uri, string $response): void |
374
|
|
|
{ |
375
|
|
|
Cache::tags('website')->put($uri, $response, config('cache.timeout')); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/* |
380
|
|
|
* 随机值 |
381
|
|
|
*/ |
382
|
|
|
if (!function_exists('random')) { |
383
|
|
|
function random(int $length = 4, string $type = 'digital'): string |
384
|
|
|
{ |
385
|
|
|
if ('digital' === $type) { |
386
|
|
|
return random_digital($length); |
387
|
|
|
} elseif ('alphabet' === $type) { |
388
|
|
|
return random_alphabet($length); |
389
|
|
|
} else { |
390
|
|
|
return Str::random($length); |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/* |
396
|
|
|
* 随机数字 |
397
|
|
|
*/ |
398
|
|
|
if (!function_exists('random_digital')) { |
399
|
|
|
function random_digital(int $length = 4): string |
400
|
|
|
{ |
401
|
|
|
return sprintf("%0{$length}d", rand(0, pow(10, $length) - 1)); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/* |
406
|
|
|
* 随机字母 |
407
|
|
|
*/ |
408
|
|
|
if (!function_exists('random_alphabet')) { |
409
|
|
|
function random_alphabet(int $length = 4): string |
410
|
|
|
{ |
411
|
|
|
$str = ''; |
412
|
|
|
$map = [ |
413
|
|
|
['65', '90'], |
414
|
|
|
['97', '122'], |
415
|
|
|
]; |
416
|
|
|
for ($i = 0; $i < $length; $i++) { |
417
|
|
|
$param = Arr::random($map); |
418
|
|
|
$str .= chr(call_user_func_array('rand', $param)); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
return $str; |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/* |
426
|
|
|
* 随机大写字母 |
427
|
|
|
*/ |
428
|
|
|
if (!function_exists('random_alphabet_upper')) { |
429
|
|
|
function random_alphabet_upper(int $length = 4): string |
430
|
|
|
{ |
431
|
|
|
return strtoupper(random_alphabet($length)); |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/* |
436
|
|
|
* 随机小写字母 |
437
|
|
|
*/ |
438
|
|
|
if (!function_exists('random_alphabet_lower')) { |
439
|
|
|
function random_alphabet_lower(int $length = 4): string |
440
|
|
|
{ |
441
|
|
|
return strtolower(random_alphabet($length)); |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/* |
446
|
|
|
* 随机日期 |
447
|
|
|
*/ |
448
|
|
|
if (!function_exists('random_date')) { |
449
|
|
|
function random_date(): string |
450
|
|
|
{ |
451
|
|
|
return (string) mt_rand(2000, (int) date('Y')).sprintf('%02d', mt_rand(1, 12)).sprintf('%02d', mt_rand(1, 28)); |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/* |
456
|
|
|
* 轮询调度 |
457
|
|
|
*/ |
458
|
|
|
if (!function_exists('round_robin')) { |
459
|
|
|
function round_robin(array &$items, array &$result): void |
460
|
|
|
{ |
461
|
|
|
$total = 0; |
462
|
|
|
$best = null; |
463
|
|
|
|
464
|
|
|
foreach ($items as $key => $item) { |
465
|
|
|
$current = &$items[$key]; |
466
|
|
|
$weight = $current['weight']; |
467
|
|
|
|
468
|
|
|
$current['current_weight'] += $weight; |
469
|
|
|
$total += $weight; |
470
|
|
|
|
471
|
|
|
if (($best == null) || ($items[$best]['current_weight'] < |
472
|
|
|
$current['current_weight'])) { |
473
|
|
|
$best = $key; |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
$items[$best]['current_weight'] -= $total; |
478
|
|
|
$items[$best]['count']++; |
479
|
|
|
|
480
|
|
|
$result[] = $best; |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/* |
485
|
|
|
* 13 位时间戳 |
486
|
|
|
* |
487
|
|
|
* @return float |
488
|
|
|
*/ |
489
|
|
|
if (!function_exists('get_millisecond')) { |
490
|
|
|
function get_millisecond(): float |
491
|
|
|
{ |
492
|
|
|
list($t1, $t2) = explode(' ', microtime()); |
493
|
|
|
|
494
|
|
|
return (float) sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000); |
495
|
|
|
} |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/* |
499
|
|
|
* Get the boolean value of a variable |
500
|
|
|
*/ |
501
|
|
|
if (!function_exists('is_true')) { |
502
|
|
|
function is_true($val): bool |
503
|
|
|
{ |
504
|
|
|
return $val instanceof \Modules\Core\Contracts\Support\Boolable |
505
|
|
|
? $val->toBool() |
506
|
|
|
: (is_string($val) |
507
|
|
|
? filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) : boolval($val)); |
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
if (!function_exists('get_routes')) { |
512
|
|
|
function get_routes($module = null): Collection |
513
|
|
|
{ |
514
|
|
|
/** @var \Illuminate\Support\Collection $routes */ |
515
|
|
|
$routes = collect(Route::getRoutes()->getRoutesByName())->groupBy(function ($item, $key) { |
516
|
|
|
$keys = explode('.', $key); |
517
|
|
|
|
518
|
|
|
return $keys[0]; |
519
|
|
|
}, true)->map(function (Collection $item) { |
520
|
|
|
return $item->mapWithKeys(function ($item, $key) { |
521
|
|
|
$keys = explode('.', $key); |
522
|
|
|
$route = collect($item->action) |
523
|
|
|
->put('method', $item->methods[0]) |
524
|
|
|
->put('uri', $item->uri) |
525
|
|
|
->forget('uses') |
526
|
|
|
->sort(); |
527
|
|
|
|
528
|
|
|
return [implode('.', Arr::except($keys, 0)) => $route]; |
529
|
|
|
})->sortKeys(); |
530
|
|
|
})->sortKeys(); |
531
|
|
|
|
532
|
|
|
if (null !== $module) { |
533
|
|
|
return $routes->get($module) ?? collect(); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
return $routes; |
537
|
|
|
} |
538
|
|
|
} |
539
|
|
|
|