|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: liu21st <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
//------------------------ |
|
14
|
|
|
// ThinkPHP 助手函数 |
|
15
|
|
|
//------------------------- |
|
16
|
|
|
|
|
17
|
|
|
use think\App; |
|
18
|
|
|
use think\Container; |
|
19
|
|
|
use think\db\Raw; |
|
20
|
|
|
use think\exception\HttpException; |
|
|
|
|
|
|
21
|
|
|
use think\exception\HttpResponseException; |
|
|
|
|
|
|
22
|
|
|
use think\facade\Cache; |
|
23
|
|
|
use think\facade\Config; |
|
24
|
|
|
use think\facade\Cookie; |
|
25
|
|
|
use think\facade\Db; |
|
26
|
|
|
use think\facade\Env; |
|
27
|
|
|
use think\facade\Event; |
|
|
|
|
|
|
28
|
|
|
use think\facade\Lang; |
|
29
|
|
|
use think\facade\Log; |
|
30
|
|
|
use think\facade\Request; |
|
31
|
|
|
use think\facade\Route; |
|
32
|
|
|
use think\facade\Session; |
|
33
|
|
|
use think\Model; |
|
34
|
|
|
use think\model\Collection as ModelCollection; |
|
35
|
|
|
use think\Response; |
|
36
|
|
|
use think\route\RuleItem; |
|
37
|
|
|
use think\Validate; |
|
38
|
|
|
|
|
39
|
|
|
if (!function_exists('abort')) { |
|
40
|
|
|
/** |
|
41
|
|
|
* 抛出HTTP异常 |
|
42
|
|
|
* @param integer|Response $code 状态码 或者 Response对象实例 |
|
43
|
|
|
* @param string $message 错误信息 |
|
44
|
|
|
* @param array $header 参数 |
|
45
|
|
|
*/ |
|
|
|
|
|
|
46
|
|
|
function abort($code, string $message = null, array $header = []) |
|
47
|
|
|
{ |
|
48
|
|
|
if ($code instanceof Response) { |
|
49
|
|
|
throw new HttpResponseException($code); |
|
50
|
|
|
} else { |
|
51
|
|
|
throw new HttpException($code, $message, null, $header); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!function_exists('app')) { |
|
57
|
|
|
/** |
|
58
|
|
|
* 快速获取容器中的实例 支持依赖注入 |
|
59
|
|
|
* @param string $name 类名或标识 默认获取当前应用实例 |
|
|
|
|
|
|
60
|
|
|
* @param array $args 参数 |
|
|
|
|
|
|
61
|
|
|
* @param bool $newInstance 是否每次创建新的实例 |
|
|
|
|
|
|
62
|
|
|
* @return object|App |
|
63
|
|
|
*/ |
|
64
|
|
|
function app(string $name = App::class, array $args = [], bool $newInstance = false) |
|
65
|
|
|
{ |
|
66
|
|
|
return Container::pull($name, $args, $newInstance); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (!function_exists('bind')) { |
|
71
|
|
|
/** |
|
72
|
|
|
* 绑定一个类到容器 |
|
73
|
|
|
* @param string|array $abstract 类标识、接口(支持批量绑定) |
|
74
|
|
|
* @param mixed $concrete 要绑定的类、闭包或者实例 |
|
75
|
|
|
* @return Container |
|
76
|
|
|
*/ |
|
77
|
|
|
function bind($abstract, $concrete = null) |
|
78
|
|
|
{ |
|
79
|
|
|
return Container::getInstance()->bind($abstract, $concrete); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!function_exists('cache')) { |
|
84
|
|
|
/** |
|
85
|
|
|
* 缓存管理 |
|
86
|
|
|
* @param mixed $name 缓存名称,如果为数组表示进行缓存设置 |
|
87
|
|
|
* @param mixed $value 缓存值 |
|
88
|
|
|
* @param mixed $options 缓存参数 |
|
89
|
|
|
* @param string $tag 缓存标签 |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
|
|
function cache($name, $value = '', $options = null, $tag = null) |
|
93
|
|
|
{ |
|
94
|
|
|
if (is_array($name)) { |
|
95
|
|
|
// 缓存初始化 |
|
96
|
|
|
return Cache::connect($name); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ('' === $value) { |
|
100
|
|
|
// 获取缓存 |
|
101
|
|
|
return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name); |
|
102
|
|
|
} elseif (is_null($value)) { |
|
103
|
|
|
// 删除缓存 |
|
104
|
|
|
return Cache::rm($name); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// 缓存数据 |
|
108
|
|
|
if (is_array($options)) { |
|
109
|
|
|
$expire = $options['expire'] ?? null; //修复查询缓存无法设置过期时间 |
|
110
|
|
|
} else { |
|
111
|
|
|
$expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间 |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (is_null($tag)) { |
|
115
|
|
|
return Cache::set($name, $value, $expire); |
|
116
|
|
|
} else { |
|
117
|
|
|
return Cache::tag($tag)->set($name, $value, $expire); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if (!function_exists('call')) { |
|
123
|
|
|
/** |
|
124
|
|
|
* 调用反射执行callable 支持依赖注入 |
|
125
|
|
|
* @param mixed $callable 支持闭包等callable写法 |
|
126
|
|
|
* @param array $args 参数 |
|
127
|
|
|
* @return mixed |
|
128
|
|
|
*/ |
|
129
|
|
|
function call(callable $callable, array $args = []) |
|
130
|
|
|
{ |
|
131
|
|
|
return Container::getInstance()->invoke($callable, $args); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (!function_exists('class_basename')) { |
|
136
|
|
|
/** |
|
137
|
|
|
* 获取类名(不包含命名空间) |
|
138
|
|
|
* |
|
139
|
|
|
* @param mixed $class 类名 |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
function class_basename($class): string |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
$class = is_object($class) ? get_class($class) : $class; |
|
145
|
|
|
return basename(str_replace('\\', '/', $class)); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if (!function_exists('class_uses_recursive')) { |
|
150
|
|
|
/** |
|
151
|
|
|
*获取一个类里所有用到的trait,包括父类的 |
|
|
|
|
|
|
152
|
|
|
* |
|
153
|
|
|
* @param mixed $class 类名 |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
function class_uses_recursive($class): array |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
if (is_object($class)) { |
|
159
|
|
|
$class = get_class($class); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$results = []; |
|
163
|
|
|
$classes = array_merge([$class => $class], class_parents($class)); |
|
164
|
|
|
foreach ($classes as $class) { |
|
|
|
|
|
|
165
|
|
|
$results += trait_uses_recursive($class); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return array_unique($results); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if (!function_exists('config')) { |
|
173
|
|
|
/** |
|
174
|
|
|
* 获取和设置配置参数 |
|
175
|
|
|
* @param string|array $name 参数名 |
|
176
|
|
|
* @param mixed $value 参数值 |
|
177
|
|
|
* @return mixed |
|
178
|
|
|
*/ |
|
179
|
|
|
function config($name = '', $value = null) |
|
180
|
|
|
{ |
|
181
|
|
|
if (is_array($name)) { |
|
182
|
|
|
return Config::set($name, $value); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name, $value); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if (!function_exists('cookie')) { |
|
190
|
|
|
/** |
|
191
|
|
|
* Cookie管理 |
|
192
|
|
|
* @param string $name cookie名称 |
|
193
|
|
|
* @param mixed $value cookie值 |
|
194
|
|
|
* @param mixed $option 参数 |
|
195
|
|
|
* @return mixed |
|
196
|
|
|
*/ |
|
197
|
|
|
function cookie(string $name, $value = '', $option = null) |
|
198
|
|
|
{ |
|
199
|
|
|
if (is_null($value)) { |
|
200
|
|
|
// 删除 |
|
201
|
|
|
Cookie::delete($name); |
|
202
|
|
|
} elseif ('' === $value) { |
|
203
|
|
|
// 获取 |
|
204
|
|
|
return 0 === strpos($name, '?') ? Request::has(substr($name, 1), 'cookie') : Request::cookie($name); |
|
205
|
|
|
} else { |
|
206
|
|
|
// 设置 |
|
207
|
|
|
return Cookie::set($name, $value, $option); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
if (!function_exists('download')) { |
|
213
|
|
|
/** |
|
214
|
|
|
* 获取\think\response\Download对象实例 |
|
215
|
|
|
* @param string $filename 要下载的文件 |
|
216
|
|
|
* @param string $name 显示文件名 |
|
217
|
|
|
* @param bool $content 是否为内容 |
|
218
|
|
|
* @param int $expire 有效期(秒) |
|
219
|
|
|
* @return \think\response\File |
|
220
|
|
|
*/ |
|
221
|
|
|
function download(string $filename, string $name = '', bool $content = false, int $expire = 180) |
|
222
|
|
|
{ |
|
223
|
|
|
return Response::create($filename, 'file')->name($name)->isContent($content)->expire($expire); |
|
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
if (!function_exists('dump')) { |
|
228
|
|
|
/** |
|
229
|
|
|
* 浏览器友好的变量输出 |
|
230
|
|
|
* @param mixed $var 变量 |
|
231
|
|
|
* @param bool $echo 是否输出 默认为true 如果为false 则返回输出字符串 |
|
232
|
|
|
* @param string $label 标签 默认为空 |
|
233
|
|
|
* @return void|string |
|
234
|
|
|
*/ |
|
235
|
|
|
function dump($var, bool $echo = true, string $label = null) |
|
236
|
|
|
{ |
|
237
|
|
|
$label = (null === $label) ? '' : rtrim($label) . ':'; |
|
238
|
|
|
if ($var instanceof Model || $var instanceof ModelCollection) { |
|
239
|
|
|
$var = $var->toArray(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
ob_start(); |
|
243
|
|
|
var_dump($var); |
|
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
$output = ob_get_clean(); |
|
246
|
|
|
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output); |
|
247
|
|
|
|
|
248
|
|
|
if (PHP_SAPI == 'cli') { |
|
249
|
|
|
$output = PHP_EOL . $label . $output . PHP_EOL; |
|
250
|
|
|
} else { |
|
251
|
|
|
if (!extension_loaded('xdebug')) { |
|
252
|
|
|
$output = htmlspecialchars($output, ENT_SUBSTITUTE); |
|
253
|
|
|
} |
|
254
|
|
|
$output = '<pre>' . $label . $output . '</pre>'; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
if ($echo) { |
|
258
|
|
|
echo $output; |
|
259
|
|
|
return; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
return $output; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
if (!function_exists('env')) { |
|
267
|
|
|
/** |
|
268
|
|
|
* 获取环境变量值 |
|
269
|
|
|
* @access public |
|
270
|
|
|
* @param string $name 环境变量名(支持二级 .号分割) |
|
271
|
|
|
* @param string $default 默认值 |
|
272
|
|
|
* @return mixed |
|
273
|
|
|
*/ |
|
274
|
|
|
function env(string $name = null, $default = null) |
|
275
|
|
|
{ |
|
276
|
|
|
return Env::get($name, $default); |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
if (!function_exists('error')) { |
|
281
|
|
|
/** |
|
282
|
|
|
* 操作错误跳转的快捷方法 |
|
283
|
|
|
* @param mixed $msg 提示信息 |
|
|
|
|
|
|
284
|
|
|
* @param string $url 跳转的URL地址 |
|
|
|
|
|
|
285
|
|
|
* @param mixed $data 返回的数据 |
|
|
|
|
|
|
286
|
|
|
* @param integer $wait 跳转等待时间 |
|
|
|
|
|
|
287
|
|
|
* @param array $header 发送的Header信息 |
|
288
|
|
|
* @return void |
|
289
|
|
|
*/ |
|
290
|
|
|
function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): void |
|
291
|
|
|
{ |
|
292
|
|
|
if (is_null($url)) { |
|
293
|
|
|
$url = request()->isAjax() ? '' : 'javascript:history.back(-1);'; |
|
294
|
|
|
} elseif ($url) { |
|
295
|
|
|
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
$result = [ |
|
299
|
|
|
'code' => 0, |
|
300
|
|
|
'msg' => $msg, |
|
301
|
|
|
'data' => $data, |
|
302
|
|
|
'url' => $url, |
|
303
|
|
|
'wait' => $wait, |
|
304
|
|
|
]; |
|
305
|
|
|
|
|
306
|
|
|
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html'; |
|
|
|
|
|
|
307
|
|
|
if ('html' == strtolower($type)) { |
|
308
|
|
|
$type = 'jump'; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
$response = Response::create($result, $type)->header($header)->options(['jump_template' => app('config')->get('app.dispatch_error_tmpl')]); |
|
312
|
|
|
|
|
313
|
|
|
throw new HttpResponseException($response); |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
if (!function_exists('event')) { |
|
318
|
|
|
/** |
|
319
|
|
|
* 触发事件 |
|
320
|
|
|
* @param mixed $event 事件名(或者类名) |
|
321
|
|
|
* @param mixed $args 参数 |
|
322
|
|
|
* @return mixed |
|
323
|
|
|
*/ |
|
324
|
|
|
function event($event, $args = null) |
|
325
|
|
|
{ |
|
326
|
|
|
return Event::trigger($event, $args); |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
if (!function_exists('exception')) { |
|
331
|
|
|
/** |
|
332
|
|
|
* 抛出异常处理 |
|
333
|
|
|
* |
|
334
|
|
|
* @param string $msg 异常消息 |
|
335
|
|
|
* @param int $code 异常代码 默认为0 |
|
336
|
|
|
* @param string $exception 异常类 |
|
337
|
|
|
* |
|
338
|
|
|
* @throws Exception |
|
339
|
|
|
*/ |
|
|
|
|
|
|
340
|
|
|
function exception(string $msg, int $code = 0, string $exception = '') |
|
341
|
|
|
{ |
|
342
|
|
|
$e = $exception ?: '\think\Exception'; |
|
343
|
|
|
throw new $e($msg, $code); |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
if (!function_exists('halt')) { |
|
348
|
|
|
/** |
|
349
|
|
|
* 调试变量并且中断输出 |
|
350
|
|
|
* @param mixed $var 调试变量或者信息 |
|
351
|
|
|
*/ |
|
|
|
|
|
|
352
|
|
|
function halt($var) |
|
353
|
|
|
{ |
|
354
|
|
|
dump($var); |
|
355
|
|
|
|
|
356
|
|
|
throw new HttpResponseException(new Response); |
|
357
|
|
|
} |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
if (!function_exists('input')) { |
|
361
|
|
|
/** |
|
362
|
|
|
* 获取输入数据 支持默认值和过滤 |
|
363
|
|
|
* @param string $key 获取的变量名 |
|
364
|
|
|
* @param mixed $default 默认值 |
|
365
|
|
|
* @param string $filter 过滤方法 |
|
366
|
|
|
* @return mixed |
|
367
|
|
|
*/ |
|
368
|
|
|
function input(string $key = '', $default = null, $filter = '') |
|
369
|
|
|
{ |
|
370
|
|
|
if (0 === strpos($key, '?')) { |
|
371
|
|
|
$key = substr($key, 1); |
|
372
|
|
|
$has = true; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
if ($pos = strpos($key, '.')) { |
|
376
|
|
|
// 指定参数来源 |
|
377
|
|
|
$method = substr($key, 0, $pos); |
|
378
|
|
|
if (in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'route', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) { |
|
379
|
|
|
$key = substr($key, $pos + 1); |
|
380
|
|
|
} else { |
|
381
|
|
|
$method = 'param'; |
|
382
|
|
|
} |
|
383
|
|
|
} else { |
|
384
|
|
|
// 默认为自动判断 |
|
385
|
|
|
$method = 'param'; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
return isset($has) ? |
|
389
|
|
|
request()->has($key, $method) : |
|
390
|
|
|
request()->$method($key, $default, $filter); |
|
391
|
|
|
} |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
if (!function_exists('json')) { |
|
395
|
|
|
/** |
|
396
|
|
|
* 获取\think\response\Json对象实例 |
|
397
|
|
|
* @param mixed $data 返回的数据 |
|
398
|
|
|
* @param int $code 状态码 |
|
399
|
|
|
* @param array $header 头部 |
|
400
|
|
|
* @param array $options 参数 |
|
401
|
|
|
* @return \think\response\Json |
|
402
|
|
|
*/ |
|
403
|
|
|
function json($data = [], $code = 200, $header = [], $options = []) |
|
404
|
|
|
{ |
|
405
|
|
|
return Response::create($data, 'json', $code)->header($header)->options($options); |
|
406
|
|
|
} |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
if (!function_exists('jsonp')) { |
|
410
|
|
|
/** |
|
411
|
|
|
* 获取\think\response\Jsonp对象实例 |
|
412
|
|
|
* @param mixed $data 返回的数据 |
|
413
|
|
|
* @param int $code 状态码 |
|
414
|
|
|
* @param array $header 头部 |
|
415
|
|
|
* @param array $options 参数 |
|
416
|
|
|
* @return \think\response\Jsonp |
|
417
|
|
|
*/ |
|
418
|
|
|
function jsonp($data = [], $code = 200, $header = [], $options = []) |
|
419
|
|
|
{ |
|
420
|
|
|
return Response::create($data, 'jsonp', $code)->header($header)->options($options); |
|
421
|
|
|
} |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
if (!function_exists('lang')) { |
|
425
|
|
|
/** |
|
426
|
|
|
* 获取语言变量值 |
|
427
|
|
|
* @param string $name 语言变量名 |
|
428
|
|
|
* @param array $vars 动态变量值 |
|
429
|
|
|
* @param string $lang 语言 |
|
430
|
|
|
* @return mixed |
|
431
|
|
|
*/ |
|
432
|
|
|
function lang(string $name, array $vars = [], string $lang = '') |
|
433
|
|
|
{ |
|
434
|
|
|
return Lang::get($name, $vars, $lang); |
|
435
|
|
|
} |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
if (!function_exists('parse_name')) { |
|
439
|
|
|
/** |
|
440
|
|
|
* 字符串命名风格转换 |
|
441
|
|
|
* type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格 |
|
442
|
|
|
* @param string $name 字符串 |
|
443
|
|
|
* @param int $type 转换类型 |
|
444
|
|
|
* @param bool $ucfirst 首字母是否大写(驼峰规则) |
|
445
|
|
|
* @return string |
|
446
|
|
|
*/ |
|
447
|
|
|
function parse_name(string $name, int $type = 0, bool $ucfirst = true): string |
|
|
|
|
|
|
448
|
|
|
{ |
|
449
|
|
|
if ($type) { |
|
450
|
|
|
$name = preg_replace_callback('/_([a-zA-Z])/', function ($match) { |
|
|
|
|
|
|
451
|
|
|
return strtoupper($match[1]); |
|
452
|
|
|
}, $name); |
|
|
|
|
|
|
453
|
|
|
|
|
454
|
|
|
return $ucfirst ? ucfirst($name) : lcfirst($name); |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_")); |
|
458
|
|
|
} |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
if (!function_exists('raw')) { |
|
462
|
|
|
/** |
|
463
|
|
|
* 生成一个数据库的Raw对象 |
|
464
|
|
|
* @param string $sql SQL指令 |
|
465
|
|
|
* @return \think\db\Raw |
|
466
|
|
|
*/ |
|
467
|
|
|
function raw(string $sql): Raw |
|
468
|
|
|
{ |
|
469
|
|
|
return Db::raw($sql); |
|
470
|
|
|
} |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
if (!function_exists('redirect')) { |
|
474
|
|
|
/** |
|
475
|
|
|
* 获取\think\response\Redirect对象实例 |
|
476
|
|
|
* @param mixed $url 重定向地址 支持Url::build方法的地址 |
|
477
|
|
|
* @param array|integer $params 额外参数 |
|
478
|
|
|
* @param int $code 状态码 |
|
479
|
|
|
* @return \think\response\Redirect |
|
480
|
|
|
*/ |
|
481
|
|
|
function redirect($url = [], $params = [], $code = 302) |
|
482
|
|
|
{ |
|
483
|
|
|
if (is_integer($params)) { |
|
484
|
|
|
$code = $params; |
|
485
|
|
|
$params = []; |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
return Response::create($url, 'redirect', $code)->params($params); |
|
|
|
|
|
|
489
|
|
|
} |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
if (!function_exists('request')) { |
|
493
|
|
|
/** |
|
494
|
|
|
* 获取当前Request对象实例 |
|
495
|
|
|
* @return Request |
|
496
|
|
|
*/ |
|
497
|
|
|
function request() |
|
498
|
|
|
{ |
|
499
|
|
|
return app('request'); |
|
500
|
|
|
} |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
if (!function_exists('response')) { |
|
504
|
|
|
/** |
|
505
|
|
|
* 创建普通 Response 对象实例 |
|
506
|
|
|
* @param mixed $data 输出数据 |
|
507
|
|
|
* @param int|string $code 状态码 |
|
508
|
|
|
* @param array $header 头信息 |
|
509
|
|
|
* @param string $type |
|
|
|
|
|
|
510
|
|
|
* @return Response |
|
511
|
|
|
*/ |
|
512
|
|
|
function response($data = '', $code = 200, $header = [], $type = 'html') |
|
513
|
|
|
{ |
|
514
|
|
|
return Response::create($data, $type, $code)->header($header); |
|
|
|
|
|
|
515
|
|
|
} |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
if (!function_exists('result')) { |
|
519
|
|
|
/** |
|
520
|
|
|
* 返回封装后的API数据到客户端 |
|
521
|
|
|
* @param mixed $data 要返回的数据 |
|
|
|
|
|
|
522
|
|
|
* @param integer $code 返回的code |
|
|
|
|
|
|
523
|
|
|
* @param mixed $msg 提示信息 |
|
|
|
|
|
|
524
|
|
|
* @param string $type 返回数据格式 |
|
|
|
|
|
|
525
|
|
|
* @param array $header 发送的Header信息 |
|
526
|
|
|
* @return void |
|
527
|
|
|
*/ |
|
528
|
|
|
function result($data, int $code = 0, $msg = '', string $type = '', array $header = []): void |
|
529
|
|
|
{ |
|
530
|
|
|
$result = [ |
|
531
|
|
|
'code' => $code, |
|
532
|
|
|
'msg' => $msg, |
|
533
|
|
|
'time' => time(), |
|
534
|
|
|
'data' => $data, |
|
535
|
|
|
]; |
|
536
|
|
|
|
|
537
|
|
|
$type = $type ?: 'json'; |
|
538
|
|
|
$response = Response::create($result, $type)->header($header); |
|
539
|
|
|
|
|
540
|
|
|
throw new HttpResponseException($response); |
|
541
|
|
|
} |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
if (!function_exists('route')) { |
|
545
|
|
|
/** |
|
546
|
|
|
* 路由注册 |
|
547
|
|
|
* @param string $rule 路由规则 |
|
548
|
|
|
* @param mixed $route 路由地址 |
|
549
|
|
|
* @param string $method 请求类型 |
|
550
|
|
|
* @return RuleItem |
|
551
|
|
|
*/ |
|
552
|
|
|
function route(string $rule, $route, $method = '*'): RuleItem |
|
553
|
|
|
{ |
|
554
|
|
|
return Route::rule($rule, $route, $method); |
|
|
|
|
|
|
555
|
|
|
} |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
if (!function_exists('session')) { |
|
559
|
|
|
/** |
|
560
|
|
|
* Session管理 |
|
561
|
|
|
* @param string $name session名称 |
|
562
|
|
|
* @param mixed $value session值 |
|
563
|
|
|
* @return mixed |
|
564
|
|
|
*/ |
|
565
|
|
|
function session(string $name = null, $value = '') |
|
566
|
|
|
{ |
|
567
|
|
|
if (is_null($name)) { |
|
568
|
|
|
// 清除 |
|
569
|
|
|
Session::clear(); |
|
570
|
|
|
} elseif (is_null($value)) { |
|
571
|
|
|
// 删除 |
|
572
|
|
|
Session::delete($name); |
|
573
|
|
|
} elseif ('' === $value) { |
|
574
|
|
|
// 判断或获取 |
|
575
|
|
|
return 0 === strpos($name, '?') ? Session::has(substr($name, 1)) : Session::get($name); |
|
576
|
|
|
} else { |
|
577
|
|
|
// 设置 |
|
578
|
|
|
Session::set($name, $value); |
|
579
|
|
|
} |
|
580
|
|
|
} |
|
581
|
|
|
} |
|
582
|
|
|
|
|
583
|
|
|
if (!function_exists('success')) { |
|
584
|
|
|
/** |
|
585
|
|
|
* 操作成功跳转的快捷方法 |
|
586
|
|
|
* @param mixed $msg 提示信息 |
|
|
|
|
|
|
587
|
|
|
* @param string $url 跳转的URL地址 |
|
|
|
|
|
|
588
|
|
|
* @param mixed $data 返回的数据 |
|
|
|
|
|
|
589
|
|
|
* @param integer $wait 跳转等待时间 |
|
|
|
|
|
|
590
|
|
|
* @param array $header 发送的Header信息 |
|
591
|
|
|
* @return void |
|
592
|
|
|
*/ |
|
593
|
|
|
function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): void |
|
594
|
|
|
{ |
|
595
|
|
|
if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) { |
|
596
|
|
|
$url = $_SERVER["HTTP_REFERER"]; |
|
597
|
|
|
} elseif ($url) { |
|
598
|
|
|
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url); |
|
599
|
|
|
} |
|
600
|
|
|
|
|
601
|
|
|
$result = [ |
|
602
|
|
|
'code' => 1, |
|
603
|
|
|
'msg' => $msg, |
|
604
|
|
|
'data' => $data, |
|
605
|
|
|
'url' => $url, |
|
606
|
|
|
'wait' => $wait, |
|
607
|
|
|
]; |
|
608
|
|
|
|
|
609
|
|
|
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html'; |
|
610
|
|
|
// 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式 |
|
611
|
|
|
if ('html' == strtolower($type)) { |
|
612
|
|
|
$type = 'jump'; |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
$response = Response::create($result, $type)->header($header)->options(['jump_template' => app('config')->get('app.dispatch_success_tmpl')]); |
|
616
|
|
|
|
|
617
|
|
|
throw new HttpResponseException($response); |
|
618
|
|
|
} |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
if (!function_exists('token')) { |
|
622
|
|
|
/** |
|
623
|
|
|
* 生成表单令牌 |
|
624
|
|
|
* @param string $name 令牌名称 |
|
625
|
|
|
* @param mixed $type 令牌生成方法 |
|
626
|
|
|
* @return string |
|
627
|
|
|
*/ |
|
628
|
|
|
function token(string $name = '__token__', string $type = 'md5'): string |
|
629
|
|
|
{ |
|
630
|
|
|
$token = Request::token($name, $type); |
|
631
|
|
|
|
|
632
|
|
|
return '<input type="hidden" name="' . $name . '" value="' . $token . '" />'; |
|
633
|
|
|
} |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
if (!function_exists('trace')) { |
|
637
|
|
|
/** |
|
638
|
|
|
* 记录日志信息 |
|
639
|
|
|
* @param mixed $log log信息 支持字符串和数组 |
|
640
|
|
|
* @param string $level 日志级别 |
|
641
|
|
|
* @return array|void |
|
642
|
|
|
*/ |
|
643
|
|
|
function trace($log = '[think]', string $level = 'log') |
|
644
|
|
|
{ |
|
645
|
|
|
if ('[think]' === $log) { |
|
646
|
|
|
return Log::getLog(); |
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
Log::record($log, $level); |
|
650
|
|
|
} |
|
651
|
|
|
} |
|
652
|
|
|
|
|
653
|
|
|
if (!function_exists('trait_uses_recursive')) { |
|
654
|
|
|
/** |
|
655
|
|
|
* 获取一个trait里所有引用到的trait |
|
656
|
|
|
* |
|
657
|
|
|
* @param string $trait Trait |
|
658
|
|
|
* @return array |
|
659
|
|
|
*/ |
|
660
|
|
|
function trait_uses_recursive(string $trait): array |
|
|
|
|
|
|
661
|
|
|
{ |
|
662
|
|
|
$traits = class_uses($trait); |
|
663
|
|
|
foreach ($traits as $trait) { |
|
664
|
|
|
$traits += trait_uses_recursive($trait); |
|
665
|
|
|
} |
|
666
|
|
|
|
|
667
|
|
|
return $traits; |
|
668
|
|
|
} |
|
669
|
|
|
} |
|
670
|
|
|
|
|
671
|
|
|
if (!function_exists('url')) { |
|
672
|
|
|
/** |
|
673
|
|
|
* Url生成 |
|
674
|
|
|
* @param string $url 路由地址 |
|
|
|
|
|
|
675
|
|
|
* @param array $vars 变量 |
|
|
|
|
|
|
676
|
|
|
* @param bool|string $suffix 生成的URL后缀 |
|
|
|
|
|
|
677
|
|
|
* @param bool|string $domain 域名 |
|
|
|
|
|
|
678
|
|
|
* @return string |
|
679
|
|
|
*/ |
|
680
|
|
|
function url(string $url = '', array $vars = [], $suffix = true, $domain = false): string |
|
681
|
|
|
{ |
|
682
|
|
|
return Route::buildUrl($url, $vars, $suffix, $domain); |
|
683
|
|
|
} |
|
684
|
|
|
} |
|
685
|
|
|
|
|
686
|
|
|
if (!function_exists('validate')) { |
|
687
|
|
|
/** |
|
688
|
|
|
* 验证数据 |
|
689
|
|
|
* @param array $data 数据 |
|
690
|
|
|
* @param string|array $validate 验证器名或者验证规则数组 |
|
691
|
|
|
* @param array $message 提示信息 |
|
692
|
|
|
* @param bool $batch 是否批量验证 |
|
693
|
|
|
* @return bool |
|
694
|
|
|
* @throws ValidateException |
|
695
|
|
|
*/ |
|
696
|
|
|
function validate(array $data, $validate, array $message = [], bool $batch = false): bool |
|
697
|
|
|
{ |
|
698
|
|
|
if (is_array($validate)) { |
|
699
|
|
|
$v = new Validate(); |
|
700
|
|
|
$v->rule($validate); |
|
701
|
|
|
} else { |
|
702
|
|
|
if (strpos($validate, '.')) { |
|
703
|
|
|
// 支持场景 |
|
704
|
|
|
list($validate, $scene) = explode('.', $validate); |
|
705
|
|
|
} |
|
706
|
|
|
|
|
707
|
|
|
$class = app()->parseClass('validate', $validate); |
|
708
|
|
|
$v = new $class(); |
|
709
|
|
|
|
|
710
|
|
|
if (!empty($scene)) { |
|
711
|
|
|
$v->scene($scene); |
|
712
|
|
|
} |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
return $v->message($message)->batch($batch)->failException(true)->check($data); |
|
716
|
|
|
} |
|
717
|
|
|
} |
|
718
|
|
|
|
|
719
|
|
|
if (!function_exists('view')) { |
|
720
|
|
|
/** |
|
721
|
|
|
* 渲染模板输出 |
|
722
|
|
|
* @param string $template 模板文件 |
|
|
|
|
|
|
723
|
|
|
* @param array $vars 模板变量 |
|
|
|
|
|
|
724
|
|
|
* @param int $code 状态码 |
|
|
|
|
|
|
725
|
|
|
* @param callable $filter 内容过滤 |
|
|
|
|
|
|
726
|
|
|
* @return \think\response\View |
|
727
|
|
|
*/ |
|
728
|
|
|
function view(string $template = '', $vars = [], $code = 200, $filter = null) |
|
729
|
|
|
{ |
|
730
|
|
|
return Response::create($template, 'view', $code)->assign($vars)->filter($filter); |
|
|
|
|
|
|
731
|
|
|
} |
|
732
|
|
|
} |
|
733
|
|
|
|
|
734
|
|
|
if (!function_exists('xml')) { |
|
735
|
|
|
/** |
|
736
|
|
|
* 获取\think\response\Xml对象实例 |
|
737
|
|
|
* @param mixed $data 返回的数据 |
|
738
|
|
|
* @param int $code 状态码 |
|
739
|
|
|
* @param array $header 头部 |
|
740
|
|
|
* @param array $options 参数 |
|
741
|
|
|
* @return \think\response\Xml |
|
742
|
|
|
*/ |
|
743
|
|
|
function xml($data = [], $code = 200, $header = [], $options = []) |
|
744
|
|
|
{ |
|
745
|
|
|
return Response::create($data, 'xml', $code)->header($header)->options($options); |
|
746
|
|
|
} |
|
747
|
|
|
} |
|
748
|
|
|
|
|
749
|
|
|
if (!function_exists('yaconf')) { |
|
750
|
|
|
/** |
|
751
|
|
|
* 获取yaconf配置 |
|
752
|
|
|
* |
|
753
|
|
|
* @param string $name 配置参数名 |
|
754
|
|
|
* @param mixed $default 默认值 |
|
755
|
|
|
* @return mixed |
|
756
|
|
|
*/ |
|
757
|
|
|
function yaconf(string $name, $default = null) |
|
758
|
|
|
{ |
|
759
|
|
|
return Config::yaconf($name, $default); |
|
760
|
|
|
} |
|
761
|
|
|
} |
|
762
|
|
|
|