2db47680
hexiaodong
chore: 初始提交 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
|
<?php
if (!function_exists('dd')) {
function dd($obj)
{
halt($obj);
}
}
if (!function_exists('fail')) {
/**
* 主动抛错
*/
function fail($message, $data = [], $code = 0)
{
$result = [
'code' => $code,
'data' => $data,
'msg' => is_array($message) ? json_encode($message) : $message,
];
// 如果未设置类型则自动判断
$type = 'json';
$response = \think\Response::create($result, $type, 200);
throw new \think\exception\HttpResponseException($response);
}
}
if (!function_exists('succ')) {
/**
* 成功返回
*/
function succ($data = [], $message = '')
{
$result = [
'code' => 1,
'data' => $data,
'msg' => $message,
];
// 如果未设置类型则自动判断
$type = 'json';
$response = \think\Response::create($result, $type, 200);
throw new \think\exception\HttpResponseException($response);
}
}
if (!function_exists('getConfig')) {
/**
* 获取配置
* @param string $field 配置组名
* @param string $key 字段
* @param string $default 字段默认值
* @param boolean $refresh 是否刷新缓存
* @return mixed
*/
function getConfig(string $field, $key = '', $default = '', $refresh = true)
{
$config = \think\Cache::get($field);
if (!$config || $refresh) {
$config = \think\Db::name('exam_config_info')->order('id')->limit(1)->value($field);
if (!$config) {
return null;
}
$config = json_decode($config, true);
//存入缓存
\think\Cache::set($field, $config);
}
if ($key) {
return $config[$key] ?? $default;
}
return $config;
}
}
if (!function_exists('getCurl')) {
/**
* get请求
* @param $url
* @return bool|string
*/
function getCurl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}
if (!function_exists('postCurl')) {
/**
* post请求
* @param $url
* @param string $data
* @param string $type
* @return bool|string
*/
function postCurl($url, $data = '', $type = 'json')
{
if ($type == 'json') {
$data = json_encode($data); //对数组进行json编码
$header = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
} else {
$header = array("Content-type: application/x-www-form-urlencoded;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$res = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Error+' . curl_error($curl);
}
curl_close($curl);
return $res;
}
}
if (!function_exists('only_keys')) {
/**
* 只取数组部分key数据
* @param array $array
* @param array $keys
* @return array
*/
function only_keys(array $array, array $keys)
{
$result = [];
foreach ($array as $k => $value) {
if (in_array($k, $keys)) {
$result[$k] = $value;
}
}
return $result;
}
}
if (!function_exists('hidden_keys')) {
/**
* 隐藏数组部分key数据
* @param array $array
* @param array $keys
* @return array
*/
function hidden_keys(array $array, array $keys)
{
$result = [];
foreach ($array as $k => $value) {
if (in_array($k, $keys)) {
unset($value[$k]);
$result[$k] = $value;
}
}
return $result;
}
}
if (!function_exists('hidden_list_keys')) {
/**
* 隐藏数组部分key数据
* @param array $list
* @param array $keys
* @return array
*/
function hidden_list_keys(array $list, array $keys)
{
$list = collection($list)->toArray();
$result = [];
foreach ($list as $i => $item) {
foreach ($item as $k => $value) {
if (in_array($k, $keys)) {
unset($item[$k]);
}
}
$result[$i] = $item;
}
return $result;
}
}
if (!function_exists('is_empty_in_array')) {
/**
* 数组内是否包含且存在字段值
* @param $array
* @param $field
* @return bool
*/
function is_empty_in_array($array, $field)
{
if (!isset($array[$field]) || !$array[$field]) {
return true;
}
return false;
}
}
if (!function_exists('cache_data')) {
/**
* 获取/设置缓存数据
* @param string $cache_key 缓存key名
* @param Closure $fun 用户函数,获取并返回数据
* @param int $expire_time 缓存过期时间
* @return mixed
*/
function cache_data(string $cache_key, Closure $fun, int $expire_time = 0, bool $refresh = false)
{
// 固定前缀
$cache_key = "exam:{$cache_key}";
// 存在缓存,返回缓存
if (!$refresh && $cache = cache($cache_key)) {
return $cache;
}
// 执行数据获取
$data = $fun();
$data = is_array($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
// 设置缓存
cache($cache_key, $data, $expire_time);
return $data;
}
}
if (!function_exists('getUser')) {
/**
* 获取Api用户信息
* @return mixed
*/
function getUser()
{
if (\app\common\library\Auth::instance()->isLogin()) {
return \app\common\library\Auth::instance();
}
return null;
}
}
if (!function_exists('getUserId')) {
/**
* 获取Api用户ID
* @return mixed
*/
function getUserId()
{
if ($user = getUser()) {
return $user->id;
}
return 0;
}
}
if (!function_exists('generate_no')) {
/**
* 根据时间生成编号
* @return string
*/
function generate_no($pre = '')
{
$date = date('YmdHis', time());
$u_timestamp = microtime(true);
$timestamp = floor($u_timestamp);
$milliseconds = round(($u_timestamp - $timestamp) * 100); // 改这里的数值控制毫秒位数
return $pre . $date . date(preg_replace('`(?<!\\\\)u`', $milliseconds, 'u'), $timestamp);
}
}
if (!function_exists('str_trim')) {
/**
* 字符串去除空格
* @return string
*/
function str_trim($str)
{
return str_replace(' ', '', $str);
}
}
if (!function_exists('formatHour')) {
/**
* 秒数转换为时分秒
* @param $duration
* @return string
*/
function formatHour($duration)
{
$hour = floor($duration / 3600);
$min = floor(($duration - $hour * 3600) / 60);
$sec = floor($duration - $hour * 3600 - $min * 60);
return sprintf("%02d:%02d:%02d", $hour, $min, $sec);
}
}
|