forked from dominator88/laravel-smart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunc.php
executable file
·294 lines (256 loc) · 7.53 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
<?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' ) ) {
/**
* 水平radios
*
* @param $name
* @param $data
* @param int $checked_value
*
* @return mixed|string
*/
function form_radios( $name , $data , $checked_value = 0 ) {
$html = '';
foreach ( $data as $key => $val ) {
$html .= '<label class="radio-inline"><input name="' . $name . '" type="radio" value="' . $key . '" >' . $val . '</label>';
}
if ( $checked_value >= 0 ) {
$html = str_replace( 'value="' . $checked_value . '"' , "value='$checked_value' checked" , $html );
}
return $html;
}
}
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;
}
}