You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
user_center/app/Http/Middleware/ParamVerify.php

134 lines
4.8 KiB

<?php
namespace App\Http\Middleware;
use App\Libs\MyLib;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class ParamVerify
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$rules = $request->get('rules');
$attributes = $request->get('attributes');
array_walk($attributes, function(&$value){
if(is_array($value)) $value = $value['title'];
});
if(count($rules) > 0) {
$validator = Validator::make($request->input(), $rules['rules'], $rules['messages'] ?? [], $attributes);
if (isset($rules['custom']) && $rules['custom']) {
$param = $request->input();
if($request->route('id')){
$param['id'] = $request->route('id');
}
$validator->after(function ($validator) use ($rules, &$param, &$attributes) {
$data = true;
if(is_array($rules['custom'])){
foreach ($rules['custom'] as $callback){
$data = $this->validate(explode(',', $callback), $param, $attributes);
if($data !== true){
break;
}
}
} else {
$data = $this->validate(explode(',', $rules['custom']), $param, $attributes);
}
if (is_array($data)) {
$validator->errors()->add($data['field'], $data['message']);
}
});
}
if ($validator->fails()) {
return response()->json(['code' => 422, 'msg' => $validator->errors()->first(), 'data' => $validator->errors()->all()], 400);
}
}
return $next($request);
}
private function validate($rule, $param, $attributes)
{
switch ($rule[0]) {
case 'exists':
$model = new $rule[1]();
if ($model->where($rule[2], $param[$rule[2]])->count() > 0) {
return true;
} else {
return ['field' => 'id', 'message' => '当前信息不存在'];
}
break;
case 'unique':
$model = new $rule[1]();
$msg = $attributes[$rule[2]];
$model = $model->where($rule[2], $param[$rule[2]]);
if((count($rule)) >= 4){
foreach ($rule as $key => $item){
if($item == 'id'){
$model = $model->where('id', '<>', $param['id']);
} else if($key > 2){
$msg .= '--'. $attributes[$item];
$model = $model->where($item, $param[$item]);
}
}
}
if ($model->count() > 0) {
return ['field' => 'id', 'message' => $msg .' 字段已存在'];
} else {
return true;
}
break;
case 'ip_check':
$model = new $rule[1]();
$ip_addrs = explode(',', $model->where('name', $rule[2])->value('content'));
if (in_array(MyLib::GetClientIP(), $ip_addrs)) {
return true;
} else {
return ['field' => 'ip', 'message' => '禁止外网访问'];
}
break;
case 'regx':
$pattern = config('patterns.' . $rule[1]);
if(isset($pattern[$param[$rule[3]]])){
$pattern = $pattern[$param[$rule[3]]];
}
if (preg_match($pattern['pattern'], $param[$rule[2]]) > 0) {
return true;
} else {
return ['field' => '$rule[2]', 'message' => $attributes[$rule[2]] . ' 字段格式错误'];
}
break;
}
}
private function calc($operator, $value1, $value2)
{
switch ($operator){
case 'eq':
return $value1 == $value2;
case 'neq':
return $value1 != $value2;
case 'gt':
return $value1 > $value2;
case 'egt':
return $value1 >= $value2;
case 'lt':
return $value1 < $value2;
case 'elt':
return $value1 <= $value2;
case 'heq':
return $value1 === $value2;
case 'nheq':
return $value1 !== $value2;
}
}
}