-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
406 lines (393 loc) · 13.7 KB
/
helpers.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
<?php
/**
*-------------------------------------------------------------------------s*
* 全局配置文件
*-------------------------------------------------------------------------h*
* @copyright Copyright (c) 2015-2022 Shopwwi Inc. (http://www.shopwwi.com)
*-------------------------------------------------------------------------o*
* @license http://www.shopwwi.com s h o p w w i . c o m
*-------------------------------------------------------------------------p*
* @link http://www.shopwwi.com by 无锡豚豹科技
*-------------------------------------------------------------------------w*
* @since ShopWWI智能管理系统
*-------------------------------------------------------------------------w*
*/
use support\Redis;
function shopwwiAdminPath(){
return __DIR__. DIRECTORY_SEPARATOR . 'src';
}
if (!function_exists('shopwwiWhereParams')) {
/**
* 索引条件
* @param $model
* @param $param
* @param array $unset
* @return mixed
*/
function shopwwiWhereParams($model, $param, $unset = [])
{
unset($param['page']);unset($param['perPage']);
unset($param['limit']);
unset($param['orderDir']);
unset($param['_t']);
unset($param['orderBy']);
unset($param['resultType']);
unset($param['dataRecovery']);
unset($param['_format']);
unset($param['client_type']); //优先排除
foreach ($unset as $val) {
unset($param[$val]);
}
$with_has = '';
foreach ($param as $value => $key) {
if ($key != null) {
if (strchr($value, '_like') != null) {
$model = $model->where(strchr($value, '_like', true), 'like', '%' . $key . '%');
}else if(strchr($value, '_likeId') != null){
$model = $model->where(strchr($value, '_like', true), 'like', '%\\_' . $key . '\\_%');
}else if (strchr($value, '_betweenDate') != null && $key != null) {
if (preg_match('/^\d{4}-\d{2}-\d{2} ~ \d{4}-\d{2}-\d{2}$/', $key)) {
$date = explode('~', $key);
$timeMin = $date[0];
$timeMax = $date[1];
$model = $model->whereBetween(strchr($value, '_betweenDate', true), [$timeMin, $timeMax]);
}
} else if (strchr($value, '_betweenAmisTime') != null) {
if ($date = explode(',',$key)) {
$column = strchr($value, '_betweenAmisTime', true);
if(is_numeric($date[0])){
$date[0] = \Carbon\Carbon::parse($date[0]);
$date[1] = \Carbon\Carbon::parse($date[1]);
}
if ($date[0] > $date[1]) {
$model = $model->whereBetween($column, [$date[1], $date[0]]);
} else {
$model = $model->whereBetween($column, [$date[0], $date[1]]);
}
}
} else if (strchr($value, '_betweenTime') != null) {
if (is_array($key)) {
$column = strchr($value, '_betweenTime', true);
if(is_numeric($key[0])){
$key[0] = \Carbon\Carbon::parse($key[0])->toDateTimeString();
$key[1] = \Carbon\Carbon::parse($key[1])->toDateTimeString();
}
if ($key[0] > $key[1]) {
$model = $model->whereBetween($column, [$key[1], $key[0]]);
} else {
$model = $model->whereBetween($column, [$key[0], $key[1]]);
}
}
} else if (strchr($value, '_between') != null) {
if (is_array($key)) {
if ($key[0] > $key[1]) {
$model = $model->whereBetween(strchr($value, '_between', true), [$key[1], $key[0]]);
} else {
$model = $model->whereBetween(strchr($value, '_between', true), [$key[0], $key[1]]);
}
}
} else if (strchr($value, '_array') != null) {
if (is_array($key)) {
$model = $model->whereIn(strchr($value, '_array', true), $key);
}else if (is_string($key)) {
$model = $model->whereIn(strchr($value, '_array', true), explode(',',$key));
}
} else if (strchr($value, '_not') != null) {
$model = $model->where(strchr($value, '_not', true), '<>', $key);
} else if (strchr($value, '_has')) {
$relation = explode('_has', $value)[0];
if (!empty($param[$value])) {
$with_has = $param[$value];
$model = $model->whereHas($relation, function ($q) use (&$param, $value) {
if (!empty($param[$param[$value]])) {
$q->where($param[$value], $param[$param[$value]]);
}
});
}
} else if ($with_has != $value) {
$model = $model->where($value, $key);
}
}
}
return $model;
}
}
if (!function_exists('shopwwiParams')) {
/**
* 设定默认词
* @param $params
* @param null $request
* @param false $object
* @return array|mixed
*/
function shopwwiParams($params, $request = null, $object = false)
{
if ($request === null) $request = request()->all();
$data = $request;
$item = [];
foreach ($params as $key => $val) {
if (is_int($key)) {
$default = null;
$key = $val;
if (!array_key_exists($key,$data)) {
continue;
}
//当传过来的值为非数字型 且为空时直接过滤掉
// if (!is_numeric($data[$key]) && empty($data[$key])) {
// continue;
// }
} else {
if($val === null && !array_key_exists($key,$data)){
continue;
}
$default = $val;
}
$item[$key] = !array_key_exists($key,$data) ? $default:$data[$key];
}
if ($object) {
return shopwwiGetStdObject($item);
}
return $item;
}
}
if (!function_exists('shopwwiGetStdObject')) {
/**
* 转为对象
* @param $arrayList
* @return mixed
*/
function shopwwiGetStdObject($arrayList)
{
return json_decode(json_encode($arrayList));
}
}
if (!function_exists('shopwwiJson')) {
/**
* 状态返回
* @param int $code
* @param string $msg
* @param array $datas
* @param array $extend_data
* @return \support\Response
*/
function shopwwiJson($code = 0, $msg = "成功", $datas = [], $extend_data = [])
{
$data = array();
$data['status'] = $code;
$data['msg'] = $msg;
$data['message'] = $msg;
$data['data'] = $datas;
if (!empty($extend_data)) {
$data = array_merge($data, $extend_data);
}
return json($data);
}
}
if (!function_exists('shopwwiSuccess')) {
function shopwwiSuccess($datas = [], $msg = null||[], $extend_data = [])
{
if ($msg == null) {
$msg = trans('opSuccess',[],'messages');
} else {
if (is_array($msg) || is_object($msg)) {
$extend_data = $msg;
$msg = trans('opSuccess',[],'messages');
}
}
return shopwwiJson(0, $msg, $datas, $extend_data);
}
}
if (!function_exists('shopwwiError')) {
function shopwwiError($msg = "失败", $datas = [], $extend_data = [])
{
$extend_data['error'] = $msg;
return shopwwiJson(400, $msg, $datas, $extend_data);
}
}
if (!function_exists('shopwwiValidator')) {
function shopwwiValidator($msg = "失败", $errors = [],$datas = [])
{
return shopwwiJson(422, $msg, $datas,['errors'=>$errors]);
}
}
if (!function_exists('shopwwiMethod')) {
/**
* 判断请求类型的正确性
* @param $method
* @return void
* @throws \Exception
*/
function shopwwiMethod($method)
{
if (is_array($method)) {
if (!in_array(request()->method(), $method)) {
throw new \Exception('请求方式不正确');
}
} else {
if (request()->method() != $method) {
throw new \Exception('请求方式不正确');
}
}
}
}
if (!function_exists('shopwwiIsPhoneNumber')) {
/**
* 验证是否手机号
* @param $phone_number
* @return bool
*/
function shopwwiIsPhoneNumber($phone_number)
{
//中国联通号码:130、131、132、145(无线上网卡)、155、156、185(iPhone5上市后开放)、186、176(4G号段)、175(2015年9月10日正式启用,暂只对北京、上海和广东投放办理),166,146
//中国移动号码:134、135、136、137、138、139、147(无线上网卡)、148、150、151、152、157、158、159、178、182、183、184、187、188、198
//中国电信号码:133、153、180、181、189、177、173、149、199
$g = "/^1[34578]\d{9}$/";
$g1 = "/^19[89]\d{8}$/";
$g2 = "/^166\d{8}$/";
if (preg_match($g, $phone_number)) {
return true;
} else if (preg_match($g1, $phone_number)) {
return true;
} else if (preg_match($g2, $phone_number)) {
return true;
}
return false;
}
}
if (!function_exists('shopwwiIsTelNumber')) {
function shopwwiIsTelNumber($tel)
{
$isTel = "/^([0-9]{3,4}-)?[0-9]{7,8}$/";
if (preg_match($isTel, $tel)) {
return true;
}
return false;
}
}
if (!function_exists('shopwwiIsEmailText')) {
function shopwwiIsEmailText($email)
{
$regex = '/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/';
$regex = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
if (preg_match($regex, $email)) {
return true;
}
return false;
}
}
if (!function_exists('shopwwiIsUserOnline')) {
function shopwwiIsUserOnline($uid)
{
$has = Redis::get("user-is-online-{$uid}");
return $has ? 1 : 0;
}
}
if (!function_exists('shopwwiClientType')) {
/**
* 索引条件
* @param $model
* @param $param
* @param array $unset
* @return mixed
*/
function shopwwiClientType()
{
return request()->input('client_type', 'api');
}
}
if (!function_exists('now')) {
function now()
{
return \Illuminate\Support\Facades\Date::now();
}
}
if (!function_exists('shopwwiAmis')) {
function shopwwiAmis($type = null): \Shopwwi\Admin\Amis\Component
{
$component = Shopwwi\Admin\Amis\Component::make();
if ($type) {
$component->setType($type);
}
return $component;
}
}
if (!function_exists('shopwwiAmisFields')) {
function shopwwiAmisFields($name, $attribute)
{
return new \Shopwwi\Admin\Libraries\Amis\AmisFields($name, $attribute);
}
}
if (!function_exists('shopwwiAdminUrl')) {
function shopwwiAdminUrl($path,$auto = true,$noUrl = false)
{
$prefix = config('plugin.shopwwi.admin.app.prefix.admin','/admin');
$url = config('plugin.shopwwi.admin.app.base_url');
if(request()->host() && $auto){
$url = '//'.request()->host();
}
if(!empty($prefix)){
$url = $url.$prefix;
}
if(empty($path)){
return $url;
}
if($noUrl){
if(!empty($prefix)){
return $prefix.'/'. trim($path, '/');
}
return '/'. trim($path, '/');
}
return $url.'/'. trim($path, '/');
}
}
if (!function_exists('shopwwiUserUrl')) {
function shopwwiUserUrl($path,$auto = true,$noUrl = false)
{
$prefix = config('plugin.shopwwi.admin.app.prefix.user','/user');
$url = config('plugin.shopwwi.admin.app.base_url');
if(request()->host() && $auto){
$url = '//'.request()->host();
}
if(!empty($prefix)){
$url = $url.$prefix;
}
if(empty($path)){
return $url;
}
if($noUrl){
if(!empty($prefix)){
return $prefix.'/'. trim($path, '/');
}
return '/'. trim($path, '/');
}
return $url.'/'. trim($path, '/');
}
}
if (!function_exists('shopwwiConfig')) {
function shopwwiConfig(string $key = null, $default = null){
return (new Shopwwi\Admin\Libraries\ShopwwiConfig)->get($key,$default);
}
}
if (!function_exists('shopwwiParamsUrl')) {
function shopwwiParamsUrl($parameters = []){
$fullUrl = request()->url();
$query = request()->all();
if (count($query) > 0) {
$parameters = array_merge($query, $parameters);
}
return $fullUrl.(str_contains($fullUrl, '?') ? '&' : '?').http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);
}
}
/**
* 重定义除非 防止被除数为0时报错
* @param $num1
* @param $num2
* @param $scale
* @return string
*/
function shopwwiDiv($num1,$num2,$scale = null){
if(\bccomp($num2,0,10) == 0 || \bccomp($num1,0,10) == 0){
return 0;
}
return \bcdiv($num1,$num2,$scale);
}