quanwei
2025-12-09 ca425b889f3c1b5847ffc26a0229307f7f8ef43e
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
<?php
 
use think\facade\Request;
use think\facade\Log;
 
 
// 应用公共文件
/**
 * 打印调试函数
 * @param $content
 * @param $is_die
 */
function pre($content, $is_die = true)
{
 
    header('Content-type: text/html; charset=utf-8');
    echo '<pre>' . print_r($content, true);
    $is_die && die();
}
 
/**
 * 隐藏敏感字符
 * @param $value
 * @return string
 */
function substr_cut($value)
{
    $strlen = mb_strlen($value, 'utf-8');
    if ($strlen <= 1) return $value;
    $firstStr = mb_substr($value, 0, 1, 'utf-8');
    $lastStr = mb_substr($value, -1, 1, 'utf-8');
    return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
}
 
/**
 * 获取当前系统版本号
 * @return mixed|null
 * @throws Exception
 */
function get_version()
{
    try {
        $file = root_path() . '/version.json';
        $version = json_decode(file_get_contents($file), true);
        return $version['version'];
    } catch (\Exception $e) {
        return '';
    }
 
}
/**
 * 驼峰命名转下划线命名
 * @param $str
 * @return string
 */
function toUnderScore($str)
{
    $dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
        return '_' . strtolower($matchs[0]);
    }, $str);
    return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
}
/**
 * 生成密码hash值
 * @param $password
 * @return string
 */
function salt_hash($password)
{
    return md5(md5($password) . 'jjjshop_salt_2020');
}
 
/**
 * curl请求指定url (post)
 * @param $url
 * @param array $data
 * @return mixed
 */
function curlPost($url, $data = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
 
/**
 * 多维数组合并
 * @param $array1
 * @param $array2
 * @return array
 */
function array_merge_multiple($array1, $array2)
{
    $merge = $array1 + $array2;
    $data = [];
    foreach ($merge as $key => $val) {
        if (
            isset($array1[$key])
            && is_array($array1[$key])
            && isset($array2[$key])
            && is_array($array2[$key])
        ) {
            $data[$key] = array_merge_multiple($array1[$key], $array2[$key]);
        } else {
            $data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key];
        }
    }
    return $data;
}
 
/**
 * 二维数组排序
 * @param $arr
 * @param $keys
 * @param bool $desc
 * @return mixed
 */
function array_sort($arr, $keys, $desc = false)
{
    $key_value = $new_array = array();
    foreach ($arr as $k => $v) {
        $key_value[$k] = $v[$keys];
    }
    if ($desc) {
        arsort($key_value);
    } else {
        asort($key_value);
    }
    reset($key_value);
    foreach ($key_value as $k => $v) {
        $new_array[$k] = $arr[$k];
    }
    return $new_array;
}
 
/**
 * 数据导出到excel(csv文件)
 * @param $fileName
 * @param array $tileArray
 * @param array $dataArray
 */
function export_excel($fileName, $tileArray = [], $dataArray = [])
{
    ini_set('memory_limit', '512M');
    ini_set('max_execution_time', 0);
    ob_end_clean();
    ob_start();
    header("Content-Type: text/csv");
    header("Content-Disposition:filename=" . $fileName);
    $fp = fopen('php://output', 'w');
    fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));// 转码 防止乱码(比如微信昵称)
    fputcsv($fp, $tileArray);
    $index = 0;
    foreach ($dataArray as $item) {
        if ($index == 1000) {
            $index = 0;
            ob_flush();
            flush();
        }
        $index++;
        fputcsv($fp, $item);
    }
    ob_flush();
    flush();
    ob_end_clean();
}
 
/**
 * 写入日志
 * @param $value
 * @param string $type
 */
function log_write($value, $channel = '')
{
    $msg = is_string($value) ? $value : var_export($value, true);
    if($channel != ''){
        Log::channel('task')->write($msg);
    }else{
        Log::channel($channel)->write($msg);
    }
}
 
/**
 * 获取当前域名及根路径
 * @return string
 */
function base_url()
{
    static $baseUrl = '';
    if (empty($baseUrl)) {
        $request = Request::instance();
        //$subDir = str_replace('\\', '/', dirname($request->server('PHP_SELF')));
        $baseUrl = $request->scheme() . '://' . $request->host() . '/';
    }
    return $baseUrl;
}
 
/**
 * 左侧填充0
 * @param $value
 * @param int $padLength
 * @return string
 */
function pad_left($value, $padLength = 2)
{
    return \str_pad($value, $padLength, "0", STR_PAD_LEFT);
}
 
/**
 * 过滤emoji表情
 * @param $text
 * @return null|string|string[]
 */
function filter_emoji($text)
{
    // 此处的preg_replace用于过滤emoji表情
    // 如需支持emoji表情, 需将mysql的编码改为utf8mb4
    return preg_replace('/[\xf0-\xf7].{3}/', '', $text);
}
 
 
/**
 * 获取全局唯一标识符
 * @param bool $trim
 * @return string
 */
function getGuidV4($trim = true)
{
    // Windows
    if (function_exists('com_create_guid') === true) {
        $charid = com_create_guid();
        return $trim == true ? trim($charid, '{}') : $charid;
    }
    // OSX/Linux
    if (function_exists('openssl_random_pseudo_bytes') === true) {
        $data = openssl_random_pseudo_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }
    // Fallback (PHP 4.2+)
    mt_srand((double)microtime() * 10000);
    $charid = strtolower(md5(uniqid(rand(), true)));
    $hyphen = chr(45);                  // "-"
    $lbrace = $trim ? "" : chr(123);    // "{"
    $rbrace = $trim ? "" : chr(125);    // "}"
    $guidv4 = $lbrace .
        substr($charid, 0, 8) . $hyphen .
        substr($charid, 8, 4) . $hyphen .
        substr($charid, 12, 4) . $hyphen .
        substr($charid, 16, 4) . $hyphen .
        substr($charid, 20, 12) .
        $rbrace;
    return $guidv4;
}
 
function format_time($value)
{
    return date('Y-m-d', $value);
}
 
 
/**
 * curl请求指定url (get)
 * @param $url
 * @param array $data
 * @return mixed
 */
function curl($url, $data = [])
{
    // 处理get数据
    if (!empty($data)) {
        $url = $url . '?' . http_build_query($data);
    }
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}
 
/**
 * json 转换true,false
 */
function jsonRecursive(&$array)
{
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            jsonRecursive($array[$key]);
        } else {
            if($value === 'true'){
                $array[$key] = true;
            } else if($value === 'false'){
                $array[$key] = false;
            }
        }
    }
}
 
/**
 * 判断浏览器名称和版本
 */
function get_client_browser()
{
    if(empty($_SERVER['HTTP_USER_AGENT'])){
        return 'robot!';
    }
    if( (false == strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident')!==FALSE) ){
        return 'Internet Explorer 11.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 10.0')){
        return 'Internet Explorer 10.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0')){
        return 'Internet Explorer 9.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0')){
        return 'Internet Explorer 8.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0')){
        return 'Internet Explorer 7.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0')){
        return 'Internet Explorer 6.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Edge')){
        return 'Edge';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
        return 'Firefox';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
        return 'Chrome';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
        return 'Safari';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
        return 'Opera';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){
        return '360SE';
    }
    //微信浏览器
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessage')){
        return 'MicroMessage';
    }
    return '';
}
 
/**
 * AES 加密
 */
function aes_encrypted($requestConfigs, $key = 'sign')
{
    $text = json_encode($requestConfigs);
    // $key = 'sign'; //⾃定义⼀个key
    $iv = md5($key,true); //通过MD5将iv参数规定为16位
 
    if (function_exists('openssl_encrypt')) {
        $encrypted = openssl_encrypt($text, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
    } else {
        $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($text), MCRYPT_MODE_CBC, $iv);
    }
 
    return base64_encode($encrypted);
}
 
/**
 * AES 解密
 */
function aes_decrypted($encrypted, $key = 'sign')
{
    $encrypted = base64_decode($encrypted);
    // $key = 'sign'; //与加密时⾃定义的key要一致
    $iv = md5($key,true); //通过MD5将iv参数规定为16位
 
    if (function_exists('openssl_decrypt')) {
        $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
    } else {
        $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv);
    }
 
    return json_decode($decrypted,true);
}
 
/**
 * @param BaseQuery $model
 * @param string $section
 * @param string $prefix
 * @param string $field
 * @return mixed
 */
function getModelTime($model, string $section, $prefix = 'create_time', $field = '-',$time = '')
{
    if (!isset($section)) return $model;
    switch ($section) {
        case 'today':
            $model->whereBetween($prefix, [strtotime('today'), strtotime('tomorrow -1second')]);
            break;
        case 'week':
            $model->whereBetween($prefix, [strtotime('this week 00:00:00'), strtotime('next week 00:00:00 -1second')]);
            break;
        case 'month':
            $model->whereBetween($prefix, [strtotime('first Day of this month 00:00:00'), strtotime('first Day of next month 00:00:00 -1second')]);
            break;
        case 'year':
            $model->whereBetween($prefix, [strtotime('this year 1/1'), strtotime('next year 1/1 -1second')]);
            break;
        case 'yesterday':
            $model->whereBetween($prefix, [strtotime('yesterday'), strtotime('today -1second')]);
            break;
        case 'quarter':
            list($startTime, $endTime) = getMonth();
            $model = $model->where($prefix, '>', $startTime);
            $model = $model->where($prefix, '<', $endTime);
            break;
        case 'lately7':
            $model = $model->where($prefix, 'between', [strtotime("-7 day"), strtotime(date('Y-m-d H:i:s'))]);
            break;
        case 'lately30':
            $model = $model->where($prefix, 'between', [strtotime("-30 day"), strtotime(date('Y-m-d H:i:s'))]);
            break;
        default:
            if (strstr($section, $field) !== false) {
                list($startTime, $endTime) = explode($field, $section);
                if (strlen($startTime) == 4) {
                    $model->whereBetweenTime($prefix, strtotime($section), strtotime($section . ' +1day -1second'));
                } else {
                    if ($startTime == $endTime) {
                        $model = $model->whereBetweenTime($prefix, strtotime(date('Y-m-d 0:0:0', strtotime($startTime))), strtotime(date('Y-m-d 23:59:59', strtotime($endTime))));
                    } else if(strpos($startTime, ':')) {
                        $model = $model->whereBetweenTime($prefix, $startTime, $endTime);
                    } else {
                        $model = $model->whereBetweenTime($prefix, strtotime($startTime), strtotime($endTime . ' +1day -1second'));
                    }
                }
            }
            break;
    }
    return $model;
}
 
function getDatesBetweenTwoDays($startDate, $endDate, $md = 'm-d')
{
    $dates = [];
    if (strtotime($startDate) > strtotime($endDate)) {
        //如果开始日期大于结束日期,直接return 防止下面的循环出现死循环
        return $dates;
    } elseif ($startDate == $endDate) {
        //开始日期与结束日期是同一天时
        array_push($dates, date($md, strtotime($startDate)));
        return $dates;
    } else {
        array_push($dates, date($md, strtotime($startDate)));
        $currentDate = $startDate;
        do {
            $nextDate = date('Y-m-d', strtotime($currentDate . ' +1 days'));
            array_push($dates, date($md, strtotime($currentDate . ' +1 days')));
            $currentDate = $nextDate;
        } while ($endDate != $currentDate);
        return $dates;
    }
}
 
function getStartModelTime(string $section)
{
    switch ($section) {
        case 'today':
        case 'yesterday':
            return date('Y-m-d', strtotime($section));
        case 'week':
            return date('Y-m-d', strtotime('this week'));
        case 'month':
            return date('Y-m-d', strtotime('first Day of this month'));
        case 'year':
            return date('Y-m-d', strtotime('this year 1/1'));
        case 'quarter':
            list($startTime, $endTime) = getMonth();
            return $startTime;
        case 'lately7':
            return date('Y-m-d', strtotime("-7 day"));
        case 'lately30':
            return date('Y-m-d', strtotime("-30 day"));
        default:
            if (strstr($section, '-') !== false) {
                list($startTime, $endTime) = explode('-', $section);
                return date('Y-m-d H:i:s', strtotime($startTime));
            }
            return date('Y-m-d H:i:s');
    }
}
/**
 * 获取本季度 time
 * @param int|string $time
 * @param $ceil
 * @return array
 */
function getMonth($time = '', $ceil = 0)
{
    if ($ceil != 0)
        $season = ceil(date('n') / 3) - $ceil;
    else
        $season = ceil(date('n') / 3);
    $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
    $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
    return array($firstday, $lastday);
}