-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathFunc.php
executable file
·434 lines (373 loc) · 8.83 KB
/
Func.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
<?php
if (!function_exists('full_uri')) {
function full_uri($uri, $param = []) {
return url(strtolower($uri), $param);
}
}
if (!function_exists('extend')) {
/**
* 扩展数组
*
* @param $config
* @param $default
*
* @return mixed
*/
function extend($default, $config) {
foreach ($default as $key => $val) {
if (!isset($config[$key]) || $config[$key] === '') {
$config[$key] = $val;
} else if (is_array($config[$key])) {
$config[$key] = extend($val, $config[$key]);
}
}
return $config;
}
}
if (!function_exists('form_radios')) {
/**
* 水平radio 改良
*
* @param $name
* @param $data
* @param int $checked_value
*
* @return mixed|string
*/
function form_radios($name, $data, $checked_value = 0) {
$html = [];
$i = 0;
foreach ($data as $key => $val) {
if($val == (string)$checked_value || ($checked_value==0 && $i++ == 0)){
$html[] = '<label class="radio-inline"><input name="' . $name . '" type="radio" value="' . $key . '" checked>' . $val . '</label>';
}else{
$html[] = '<label class="radio-inline"><input name="' . $name . '" type="radio" value="' . $key . '" >' . $val . '</label>';
}
}
return join('',$html);
}
}
if (!function_exists('form_radio_rows')) {
/**
* 水平radio 改良
*
* @param $name
* @param $data
* @param int $checked_value
*
* @return mixed|string
*/
function form_radio_rows($name, $data, $key = 'id', $val = 'name', $checked_value = 0) {
$html = [];
foreach ($data as $item) {
if($val == (string)$checked_value ){
$html[] = '<label class="radio-inline"><input name="' . $name . '" type="radio" value="' . $item[$key] . '" checked>' . $item[$val] . '</label>';
}else{
$html[] = '<label class="radio-inline"><input name="' . $name . '" type="radio" value="' . $item[$key] . '" >' . $item[$val] . '</label>';
}
}
return join('',$html);
}
}
if (!function_exists('form_radio')) {
function form_radio($name, $data, $checked_value = 0, $title) {
$data = [
'type' => 'radio',
'title' => $title,
'name' => $name,
'data' => $data,
'value' => $checked_value,
];
return form_field($data);
}
}
if (!function_exists('form_text2')) {
function form_text2($name, $value = 0, $title, $help = '', $placeholder = '') {
$data = [
'type' => 'text',
'title' => $title,
'name' => $name,
'data' => $data,
'value' => $value,
'help' => $help,
'placeholder' => $placeholder,
];
return form_field($data);
}
}
//use Facades\Smart\Service\WidgetService;
function form_field($param) {
$widgetService = Facades\Smart\Service\WidgetService::make($param);
return $widgetService;
}
if (!function_exists("ajax_arr")) {
/**
* 生成需要返回 ajax 数组
*
* @param $msg //消息
* @param int $code //0 正常 , > 0 错误
* @param array $data //需要传递的参数
*
* @return array
*/
function ajax_arr($msg, $code = 500, $data = []) {
$arr = [
'msg' => $msg,
'code' => $code,
];
if ($data !== '') {
$arr['data'] = $data;
}
return $arr;
}
}
if (!function_exists('form_options')) {
/**
* 生成下拉选项
*
* @param $data
* @param int $selected_value
*
* @return mixed|string
*/
function form_options($data, $selected_value = -1) {
$html = '';
foreach ($data as $key => $val) {
$html .= "<option value='$key'>$val</option>";
}
if ($selected_value >= 0) {
$html = str_replace("value='$selected_value'", "value='$selected_value' selected", $html);
}
return $html;
}
}
if (!function_exists('form_checkbox_rows')) {
/**
* checkbox
*
* @param $name
* @param $data
* @param string $key
* @param string $val
* @param int $checked_value
*
* @return mixed|string
*/
function form_checkbox_rows($name, $data, $key = 'id', $val = 'name', $checked_value = 0) {
$html = '';
foreach ($data as $item) {
$html .= '<label class="checkbox-inline"><input name="' . $name . '[]" type="checkbox" value="' . $item[$key] . '" >' .
$item[$val] . '</label>';
}
if ($checked_value >= 0) {
$html = str_replace('value="' . $checked_value . '"', "value='$checked_value' checked", $html);
}
return $html;
}
}
if (!function_exists('str2pwd')) {
/**
* 字符串加密
*
* @param $str
*
* @return bool|string
*/
function str2pwd($str) {
return password_hash($str, PASSWORD_BCRYPT, ["cost" => 10]);
}
}
if (!function_exists('json')) {
function json(Array $array) {
return response()->json($array);
}
}
if (!function_exists('api_result')) {
function api_result($msg, $code_or_data = 500, $data = []) {
$result = [
'msg' => $msg,
];
if (is_array($code_or_data)) {
$result['code'] = 0;
$data = array_merge($code_or_data, $data);
} else {
$result['code'] = $code_or_data;
}
if (!empty($data)) {
$result['data'] = $data;
}
return $result;
}
}
if (!function_exists('rand_string')) {
/**
* 生成随机字符串
*
* @param $length
*
* @return string
*/
function rand_string($length = 6) {
$str = NULL;
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol) - 1;
for ($i = 0; $i < $length; $i++) {
$str .= $strPol[rand(0, $max)]; // rand($min,$max)生成介于min和max两个数之间的一个随机整数
}
return $str;
}
}
if (!function_exists('form_options_rows')) {
/**
* 生成下拉选项 from rows
*
* @param $data
* @param string $id
* @param string $text
* @param string $node_field
* @param int $selected_value
* @param array $dat
*
* @return mixed|string
*/
function form_options_rows($data, $id = 'id', $text = "name", $node_field = "children", $selected_value = 0, $dat = []) {
$html = '';
foreach ($data as $row) {
$value = $row->$id;
$prefix = '';
if (isset($row->level)) {
$prefix = $row->level - 1 > 0 ? str_repeat(' ', $row->level - 1) . '└─ ' : ''; // ┗
}
$title = $prefix . $row->$text;
$d = '';
foreach ($dat as $p) {
$d .= sprintf(' data-%s="%s"', $p, $row[$p]);
}
$html .= sprintf('<option value="%s" %s>%s</option>', $value, $d, $title);
if (isset($row->$node_field)) {
$html .= form_options_rows($row[$node_field], $id, $text, 0, $row->level + 1);
}
}
if (!empty($selected_value)) {
$html = str_replace('value="' . $selected_value . '"', 'value="' . $selected_value . '" selected', $html);
}
return $html;
}
}
if (!function_exists('form_options_rows_group')) {
/**
* optgroup 显示 options
*
* @param $data
* @param $valueField
* @param $textField
* @param $groupField
*
* @return string
*/
function form_options_rows_group($data, $valueField = 'id', $textField = 'text', $groupField = 'type_text') {
$newData = [];
foreach ($data as $item) {
if (array_key_exists($groupField, $item)) {
$newData[$item[$groupField]][] = $item;
}
}
$html = '';
foreach ($newData as $key => $row) {
$html .= '<optgroup label="' . $key . '">';
foreach ($row as $r) {
$html .= '<option value="' . $r[$valueField] . '">' . $r[$textField] . '</option> ';
}
$html .= '</optgroup> ';
}
return $html;
}
}
if (!function_exists('file_size')) {
function file_size($size) {
$unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$index = 0;
do {
$size = $size / 1024;
$index++;
} while ($size >= 1024);
return round($size, 1) . $unit[$index];
}
}
if (!function_exists('full_img_uri')) {
/**
* 返回图片绝对路径
*
* @param $imgUri
*
* @return string
*/
function full_img_uri($imgUri) {
if (config('backend.image.uploadType') == 'local') {
return route($imgUri, '', '', TRUE);
}
return config('backend.image.imgUri') . $imgUri;
}
}
if (!function_exists('css')) {
/**
* 返回图片绝对路径
*
* @param $imgUri
*
* @return string
*/
function css() {
if(class_exists(Mp\Service\Common\Asset::class)){
return Mp\Service\Common\Asset::css();
}
}
}
if (!function_exists('js')) {
/**
* 返回图片绝对路径
*
* @param $imgUri
*
* @return string
*/
function js() {
if(class_exists(Mp\Service\Common\Asset::class)){
return Mp\Service\Common\Asset::js();
}
}
}
if (!function_exists('script')) {
/**
* 返回图片绝对路径
*
* @param $imgUri
*
* @return string
*/
function script() {
if(class_exists(Mp\Service\Common\Asset::class)){
return Mp\Service\Common\Asset::script();
}
}
}
if(!function_exists('human_filesize')){
/**
* 返回可读性更好的文件尺寸
*/
function human_filesize($bytes, $decimals = 2)
{
$size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) .@$size[$factor];
}
}
if(!function_exists('is_image')){
/**
* 判断文件的MIME类型是否为图片
*/
function is_image($mimeType)
{
return starts_with($mimeType, 'image/');
}
}