<?php namespace App\Http\Middleware; use App\Exceptions\InvalidRequestException; use Closure; use Illuminate\Support\Facades\Redis; class PreventDupSubmit { /** * 简单的防止前端重复提交数据 导致的数据异常 * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $requestParams = $request->all(); $requestParams["adminId"] = getAdminUserId(); $requestParams["uri"] = $request->getUri(); ksort($requestParams); $key = md5(serialize($requestParams)); $result = Redis::Connection("wms")->set('wms_' . $key, 1,"EX",5,"NX"); if(!$result){ throw new InvalidRequestException("请勿重复请求,请5秒后再发起请求"); } $response = $next($request); Redis::Connection("wms")->del('wms_' . $key); return $response; } }
使用:
Route::match(['get', 'post'], '/putawayAction', 'StockShelfController@putawayAction')->middleware('prevent_dup_submit');//上架 Route::match(['get', 'post'], '/cancelPutawayAction', 'StockShelfController@cancelPutawayAction')->middleware('prevent_dup_submit');//取消上架 Route::match(['get', 'post'], '/oneKeyPutawayAction', 'StockShelfController@oneKeyPutawayAction')->middleware('prevent_dup_submit');//一键上架 Route::match(['get', 'post'], '/submitPutawayAction', 'StockShelfController@submitPutawayAction')->middleware('prevent_dup_submit');//提交上架
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/17522085.html