Commit a162353e by 朱继来

temp

parent 7aec11d6
Showing with 80 additions and 0 deletions
......@@ -6,8 +6,88 @@ use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Session;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/**
* 防止表单重复提交的key前缀
* @var string
*/
private $formResubmitPrefix = 'f_';
/**
* 将key加个前缀
* @param unknown $key
* @return string
*/
private function formResubmitKeyProcess($key)
{
if(empty($key)){
//默认使用当前路由的uri为key
return $this->formResubmitPrefix.Route::current()->uri;
}else{
return $this->formResubmitPrefix.$key;
}
}
/**
* 在初始化表单前调用(如上面分步实现中的showRegistrationForm()方法中)
* @param unknown $key
*/
protected function formInit($key = null)
{
$key = $this->formResubmitKeyProcess($key);
Session::put($key, time());
}
/**
* 在处理表单提交的方法中调用(如上面分步实现中的register()方法)
* @param string $message
* @param unknown $key
* @throws HttpException
*/
protected function formSubmited(string $message = '请忽重复提交!', $key = null)
{
$key = $this->formResubmitKeyProcess($key);
if (Session::has($key)) {
Session::forget($key);
} else {
throw new HttpException(403, $message);
}
}
/**
* 在处理表单提交的方法中调用(如上面分步实现中的register()方 法),该方法方便自定义重复提交时的提示页面,可以在子类中if判断一下,如果发生重复提交,响应自定义的界面
* @param string $message
* @param unknown $key
*/
protected function formSubmitIsRepetition(string $message = '请勿重复提交!', $key = null)
{
$key = $this->formResubmitKeyProcess($key);
if (Session::has($key)) {
Session::forget($key);
return false;
} else {
return response()->view('errors.403', ['message'=>$message], 403);
}
}
/**
* 该方法用于ajax请求,返回的数据是数组
* @param string $message
* @param unknown $key
*/
protected function formSubmitedForAjax(string $message = '请勿重复提交!', $key = null)
{
$key = $this->formResubmitKeyProcess($key);
if (Session::has($key)) {
Session::forget($key);
return false;
} else {
return ['result'=>'fail','message'=>$message];
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment