laravel表单验证,验证规则

laravel表单验证规则,验证用户信息是否正确

  • LNMP环境
  • php5.6
  • mysql5.4
代码中有需要注意的位置,都是用“重点”关键字进行标记,可以检索“重点”关键字进行查看。
路由
Route::get('/admin/regulate','admin\RegulateController@regulate');
    //管理员页面
    Route::get('/admin/regulateadd','admin\RegulateController@regulateAdd');
    //添加管理员
    Route::post('/admin/regulatecreate','admin\RegulateController@regulateCreate');
    //编辑管理员
    Route::match(['get','post'],'/admin/regulateedit/{id}','admin\RegulateController@regulateEdit');
    //停用管理员
控制器
  • 此控制器包含,新增,修改,停用管理员的操作,以及验证规则。
  • 验证规则用到了,正则,用户唯一判断,不能为空等。
<?php namespace App\Http\Controllers\admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

use Illuminate\Http\Request;

class RegulateController extends Controller {

	/**
	 * @Author <winner443@163.com>
	 *
	 * @return Response
	 */
	public function regulate()
	{
		/*
		 * @Author <winner>
		 * function 管理员控制器
		 * */
		//读取管理员数据
        $data = \DB::select("select a.*,(select s.group_name from sys_group as s where s.id = a.auth_id) as group_name from admins as a");
		return view('admin.regulate')->with('data',$data);
	}
	//添加管理员页面
	public function regulateAdd(){
	    $group_name = \DB::table('sys_group')->get();
	    return view('admin.adminAdd')->with(['group_name'=>$group_name]);
    }
    //添加管理员操作
    public function regulateCreate(Request $request){
	    //接收全部数据
	    $values = $request->all();
	    //设置验证规则
        $rule = [
          'login_name'=>'required|unique:admins,login_name|regex:/^[a-zA-Z][a-zA-Z0-9_]{3,10}$/',
          //'login_name'=>array('regex:/^[a-zA-Z][a-zA-Z0-9_]{4,10}$/'),//TODO <winner remarks>正则如果要是这样写,那么required就不能验证了。
          'pwd'=>'required|min:6|max:18|same:repwd',
          'tel'=>'required|regex:/^1[34578]\d{9}$/',
          'email'=>'required|email',
          'auth_id'=>'required'
        ];
        //设置提示信息
        $message = [
          'login_name.required'=>'用户名不能为空',
          'login_name.unique'=>'用户名已存在,请重新输入',
          'login_name.regex'=>'用户名格式必须为字母、数字、下划线,3-10位组成。',
          'pwd.max'=>'请输入6-18位密码',
          'pwd.min'=>'请输入6-18位密码',
          'pwd.required'=>'密码不能为空',
          'pwd.same'=>'两次密码不一致',
          'tel.required'=>'手机号不能为空',
          'tel.regex'=>'手机号格式不正确',
          'email.required'=>'邮箱不能为空',
          'email'=>'邮箱格式不正确',
          'auth_id.required'=>'角色不能为空'
        ];
        $validator = \Validator::make($values,$rule,$message);
        if ($validator->passes()){
            //插入数据
            //过滤掉多余的字段
            unset($_POST['repwd']);
            //$_POST['pwd'] = base64_encode(md5($_POST['pwd']));
            $_POST['pwd'] = md5(SALT.$_POST['pwd']);
            $_POST['token'] = str_random(50);
            $_POST['create_time'] = time();
            $_POST['update_time'] = null;
            $result = \DB::table('admins')->insert($_POST);
            if ($result){
                $data = [
                  'status'=>'200',
                  'href'=>'http://mfwx.bjmfzyz.com/admin/regulate',
                  'info'=> '添加成功'
                ];
                return json_encode($data);
            }else{
                $data = [
                  'status'=>'202',
                  'info'=>'添加失败,稍后重试'
                ];
                return json_encode($data);
            }
        }else{
            return response()->json([
                'status'=>'201',
                'info'=>$validator->errors()->first()
            ]);
        }
    }
    //编辑管理员
    public function regulateEdit($id,Request $request){
        if ($request->isMethod('GET')){
            //角色列表
            $group_name = \DB::table('sys_group')->get();
            //查询管理员,嵌套查询看此管理员属于哪一个角色的。
            $data = \DB::select("select a.*,(select s.group_name from sys_group as s where s.id = a.auth_id) as group_name from admins as a where a.id = $id");
            return view('admin.adminEdit')->with(['data'=>$data,'group_name'=>$group_name]);
        }else{
            //接收全部数据
            $values = $request->all();
            //设置验证规则
            $rule = [
                'login_name'=>'required|regex:/^[a-zA-Z][a-zA-Z0-9_]{3,10}$/|unique:admins,login_name,'.$id,//忽略指定此id的验证
                'tel'=>'required|regex:/^1[34578]\d{9}$/',
                'email'=>'required|email',
            ];
            $message = [
                'login_name.required'=>'用户名不能为空',
                'login_name.unique'=>'用户名已存在,请重新输入',
                'login_name.regex'=>'用户名格式必须为字母、数字、下划线,3-10位组成。',
                'tel.required'=>'手机号不能为空',
                'tel.regex'=>'手机号格式不正确',
                'email.required'=>'邮箱不能为空',
                'email'=>'邮箱格式不正确'
            ];
            $validator = \Validator::make($values,$rule,$message);
            if ($validator->passes()){
                $_POST['update_time'] = time();
                $result = \DB::table('admins')->where('id','=',$id)->update($_POST);
                if ($result){
                    $data = [
                        'status'=>'200',
                        'info'=>'修改成功'
                    ];
                    return json_encode($data);
                }else{
                    $data = [
                        'status'=>'202',
                        'info'=>'修改失败'
                    ];
                    return json_encode($data);
                }
            }else{
                return response()->json([
                   'status'=>'201',
                   'info'=>$validator->errors()->first()
                ]);
            }
        }
    }
    //停用管理员
    public function regulateStop($id,Request $request){
	    if ($request->input('start')=='1'){
	        //停用
            $data = [];
            $data['start'] = 1;
            $result = \DB::table('admins')->where('id','=',$id)->update($data);
            if ($result){
                $data = [
                    'status'=>'200',
                    'info'=>'停用成功'
                ];
                return json_encode($data);
            }else{
                $data = [
                    'status'=>'201',
                    'info'=>'停用失败'
                ];
                return json_encode($data);
            }
        }else{
	        //启用
            $data['start'] = 0;
            $result = \DB::table('admins')->where('id','=',$id)->update($data);
            if ($result){
                $data = [
                    'status'=>'200',
                    'info'=>'启用成功'
                ];
                return json_encode($data);
            }else{
                $data = [
                    'status'=>'201',
                    'info'=>'启用失败'
                ];
                return json_encode($data);
            }
        }
    }
    //修改管理员密码
    public function regulateResetPwd($id,Request $request){
        if ($request->isMethod('GET')){
            $data = \DB::select("select a.*,(select s.group_name from sys_group as s where s.id = a.auth_id) as group_name from admins as a where a.id = $id");
            return view('admin.adminResetPwd')->with(['data'=>$data]);
        }else{
            $values = $request->all();
            $rule = [
              'pwd'=>'required|min:5|max:18|same:repwd',
            ];
            $message = [
                'pwd.required'=>'密码不能为空',
                'pwd.min'=>'请输入6-18位密码',
                'pwd.max'=>'请输入6-18位密码',
                'pwd.same'=>'两次密码不一致'
            ];
            $validator = \Validator::make($values,$rule,$message);
            if ($validator->passes()){
                $data = array();
                $data['pwd'] = md5(SALT.$request->input('pwd'));
                $result = DB::table('admins')->where('id','=',$id)->update($data);
                if ($result){
                    $data = [
                        'status'=>'10000',
                        'info'=>'密码修改成功'
                    ];
                    return $data;
                }else{
                    $data = [
                        'status'=>'10004',
                        'info'=>'密码修改失败'
                    ];
                    return $data;
                }
            }else{
                return response()->json([
                   'status'=>'10001',
                   'info'=>$validator->errors()->first()
                ]);
            }
        }
    }
 }   
视图

添加视图

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta id="farm_csrf" name="csrf-token" content="{{ csrf_token() }}"<重点:new insert>这个是处理laravel的CSRF验证
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="admin/lib/html5shiv.js"></script>
<script type="text/javascript" src="admin/lib/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui/css/H-ui.min.css")}} />
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui.admin/css/H-ui.admin.css")}}/>
<link rel="stylesheet" type="text/css" href={{asset("admin/lib/Hui-iconfont/1.0.8/iconfont.css")}}/>
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui.admin/skin/default/skin.css")}} id="skin" />
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui.admin/css/style.css")}}/>
<!--[if IE 6]>
<script type="text/javascript" src="admin/lib/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>添加管理员 - 管理员管理</title>
<meta name="keywords" content="">
<meta name="description" content="">
</head>
<body>
<article class="page-container">
	<form class="form form-horizontal" id="formAdd">
	<div class="row cl" style="width:90%;">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>管理员:</label>
		<div class="formControls col-xs-8 col-sm-9">
			<input type="text" class="input-text" value="" placeholder="" id="loginName" name="login_name">
		</div>
	</div>
	<div class="row cl" style="width:90%;">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>初始密码:</label>
		<div class="formControls col-xs-8 col-sm-9">
			<input type="password" class="input-text" autocomplete="off" value="" placeholder="密码" id="pwd" name="pwd">
		</div>
	</div>
	<div class="row cl" style="width:90%;">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>确认密码:</label>
		<div class="formControls col-xs-8 col-sm-9">
			<input type="password" class="input-text" id="rePwd" autocomplete="off"  placeholder="确认新密码" name="repwd">
		</div>
	</div>
	<div class="row cl" style="width:90%;">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>状态:</label>
		<div class="formControls col-xs-8 col-sm-9 skin-minimal">
			<div class="radio-box">
				<input name="start" type="radio" id="start" value="0" checked>
				<label for="sex-1">启用</label>
			</div>
			<div class="radio-box">
				<input type="radio" id="start" name="start" value="1">
				<label for="sex-2">禁用</label>
			</div>
		</div>
	</div>
	<div class="row cl" style="width:90%;">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>手机:</label>
		<div class="formControls col-xs-8 col-sm-9">
			<input type="text" class="input-text" value="" placeholder="" id="tel" name="tel">
		</div>
	</div>
	<div class="row cl" style="width:90%;">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>邮箱:</label>
		<div class="formControls col-xs-8 col-sm-9">
			<input type="text" class="input-text" placeholder="@" name="email" id="email">
		</div>
	</div>
	<div class="row cl" style="width:90%;">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色:</label>
		<div class="formControls col-xs-8 col-sm-9">
			<span class="select-box" style="width:150px;">
				<select class="select" name="auth_id" id="authID" size="1">
					<option value="">请选择</option>
					@foreach($group_name as $v)
						<option value="{{$v->id}}">{{$v->group_name}}</option>
					@endforeach
				</select>
			</span>
		</div>
	</div>
	{{--<div class="row cl" style="width:90%;">
		<label class="form-label col-xs-4 col-sm-3">备注:</label>
		<div class="formControls col-xs-8 col-sm-9">
			<textarea name="" cols="" rows="" class="textarea"  placeholder="" dragonfly="true" onKeyUp="$.Huitextarealength(this,100)"></textarea>
			<p class="textarea-numberbar"><em class="textarea-length">0</em>/100</p>
		</div>
	</div>--}}
	<div class="row cl" style="width:90%;">
		<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
			{{--<input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">--}}
			<button id="adminsSubmit" class="btn btn-primary radius" type="button"><i class="Hui-iconfont">&#xe632;</i> 保存并提交</button>
			<input class="btn btn-default radius"  type="reset" value="&nbsp;&nbsp;重&nbsp;置&nbsp;&nbsp;">
		</div>
	</div>
	</form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src={{asset("admin/static/h-ui.admin/js/H-ui.admin.js")}}></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src={{asset("admin/lib/jquery.validation/1.14.0/jquery.validate.js")}}></script>
<script type="text/javascript" src={{asset("admin/js/md5.js")}}></script>
<script type="text/javascript">
 <重点:new insert>这个是处理laravel的CSRF验证
$(function(){
	$.ajaxSetup({
		headers: {
			'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
		}
	});
	//TODO <winner insert 201906101906> 添加管理员ajax
	$("#adminsSubmit").click(function(){
		/* //TODO <winner sign>想着用js的加密了。但是这样密码的位数在后端就不好判断了。所以不加密了。直接传。
		var loginName = $("#loginName").val();
		var pwd = hex_md5($("#pwd").val());
		var rePwd = hex_md5($("#rePwd").val());
		//选中radio单选框的被选中的val值
		var start = $('input[name="start"]:checked').val();
		var tel = $("#tel").val();
		var email = $("#email").val();
		var authID = $("#authID").val();*/
		$.ajax({
			type:'POST',
			url:'/admin/regulatecreate',
			dataType:'json',
			data:$("#formAdd").serialize(),
			//data:{'login_name':loginName,'pwd':pwd,'repwd':rePwd,'start':start,'tel':tel,'email':email,'auth_id':authID},
			success:function(data){
				if (data.status=='201'){
					layer.msg(data.info,{icon:5});
				}else if(data.status=='202'){
					layer.msg(data.info,{icon:5,time:1500});
					//window.parent.location.reload();
				}else{
					layer.msg(data.info,{icon:1,time:1500});
					//window.parent.location.reload();
				}
			},
			error:function(){
				layer.msg('网络错误,稍后重试',{icon:5,time:1500});
				//window.parent.location.reload();
			}
		});
	});
	$('.skin-minimal input').iCheck({
		checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
		increaseArea: '20%'
	});
	
	$("#form-admin-add").validate({
		rules:{
			adminName:{
				required:true,
				minlength:4,
				maxlength:16
			},
			password:{
				required:true,
			},
			password2:{
				required:true,
				equalTo: "#password"
			},
			sex:{
				required:true,
			},
			phone:{
				required:true,
				isPhone:true,
			},
			email:{
				required:true,
				email:true,
			},
			adminRole:{
				required:true,
			},
		},
		onkeyup:false,
		focusCleanup:true,
		success:"valid",
		submitHandler:function(form){
			$(form).ajaxSubmit({
				type: 'post',
				url: "xxxxxxx" ,
				success: function(data){
					layer.msg('添加成功!',{icon:1,time:1000});
				},
                error: function(XmlHttpRequest, textStatus, errorThrown){
					layer.msg('error!',{icon:1,time:1000});
				}
			});
			var index = parent.layer.getFrameIndex(window.name);
			parent.$('.btn-refresh').click();
			parent.layer.close(index);
		}
	});
});
</script> 
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>

修改视图

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta id="farm_csrf" name="csrf-token" content="{{ csrf_token() }}"><重点:new insert>这个是处理laravel的CSRF验证
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="admin/lib/html5shiv.js"></script>
<script type="text/javascript" src="admin/lib/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui/css/H-ui.min.css")}} />
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui.admin/css/H-ui.admin.css")}}/>
<link rel="stylesheet" type="text/css" href={{asset("admin/lib/Hui-iconfont/1.0.8/iconfont.css")}}/>
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui.admin/skin/default/skin.css")}} id="skin" />
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui.admin/css/style.css")}}/>
<!--[if IE 6]>
<script type="text/javascript" src="admin/lib/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>修改管理员 - 管理员管理</title>
<meta name="keywords" content="">
<meta name="description" content="">
</head>
<body>
<article class="page-container">
	@foreach($data as $data)
		<form class="form form-horizontal" id="formEdit">
		<input id="editID" type="hidden" value="{{$data->id}}">
		<div class="row cl" style="width:90%;">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>管理员:</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="{{$data->login_name}}" placeholder="" id="adminName" name="login_name">
			</div>
		</div>
		<div class="row cl" style="width:90%;">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>状态:</label>
			<div class="formControls col-xs-8 col-sm-9 skin-minimal">
				<div class="radio-box">
					<input name="start" type="radio" id="sex-1" value="0" @if($data->start==0)checked @endif>
					<label for="sex-1">启用</label>
				</div>
				<div class="radio-box">
					<input type="radio" id="sex-2" name="start" value="1" @if($data->start==1)checked @endif>
					<label for="sex-2">禁用</label>
				</div>
			</div>
		</div>
		<div class="row cl" style="width:90%;">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>手机:</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="{{$data->tel}}" placeholder="" id="phone" name="tel">
			</div>
		</div>
		<div class="row cl" style="width:90%;">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>邮箱:</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" placeholder="@" name="email" id="email" value="{{$data->email}}">
			</div>
		</div>
		<div class="row cl" style="width:90%;">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色:</label>
			<div class="formControls col-xs-8 col-sm-9"> <span class="select-box" style="width:150px;">
			<select class="select" name="auth_id" size="1">
				<option value="{{$data->auth_id}}">{{$data->group_name}}</option>
				@foreach($group_name as $v)
					<option @if($v->id==$data->auth_id)style="display: none;"@endif value="{{$v->id}}">{{$v->group_name}}</option>
				@endforeach
			</select>
			</span>
			</div>
		</div>
		<div class="row cl" style="width:90%;">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				{{--<input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">--}}
				<button id="adminsEdit" class="btn btn-primary radius" type="button"><i class="Hui-iconfont">&#xe632;</i> 保存并提交</button>
				{{--<input class="btn btn-default radius"  type="reset" value="&nbsp;&nbsp;重&nbsp;置&nbsp;&nbsp;">--}}
			</div>
		</div>
	</form>
	@endforeach
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src={{asset('admin/layui/layui.all.js')}}></script>
<script type="text/javascript" src={{asset("admin/lib/jquery/1.9.1/jquery.min.js")}}></script>
<script type="text/javascript" src={{asset("admin/lib/layer/2.4/layer.js")}}></script>
<script type="text/javascript" src={{asset("admin/static/h-ui/js/H-ui.min.js")}}></script>
<script type="text/javascript" src={{asset("admin/static/h-ui.admin/js/H-ui.admin.js")}}></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src={{asset("admin/lib/jquery.validation/1.14.0/jquery.validate.js")}}></script>
<script type="text/javascript" src={{asset("admin/lib/jquery.validation/1.14.0/validate-methods.js")}}></script>
<script type="text/javascript" src={{asset("admin/lib/jquery.validation/1.14.0/messages_zh.js")}}></script>
<script type="text/javascript">
<重点:new insert>这个是处理laravel的CSRF验证
$(function(){
	$.ajaxSetup({
		headers: {
			'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
		}
	});
	//TODO <winner insert 201906101906> 添加管理员ajax
	$("#adminsEdit").click(function(){
		var editID = $("#editID").val();
		$.ajax({
			type:'POST',
			url:'/admin/regulateedit/'+editID,
			dataType:'json',
			data:$("#formEdit").serialize(),
			success:function(data){
				if (data.status=='201'){
					layer.msg(data.info,{icon:5});
				}else if(data.status=='202'){
					layer.msg(data.info,{icon:5,time:1500});
					window.parent.location.reload();
				}else{
					layer.msg(data.info,{icon:1,time:1500});
					window.parent.location.reload();
				}
			},
			error:function(){
				layer.msg('网络错误,稍后重试',{icon:5,time:1500});
				window.parent.location.reload();
			}
		});
	});
	$('.skin-minimal input').iCheck({
		checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
		increaseArea: '20%'
	});
	
	$("#form-admin-add").validate({
		rules:{
			adminName:{
				required:true,
				minlength:4,
				maxlength:16
			},
			password:{
				required:true,
			},
			password2:{
				required:true,
				equalTo: "#password"
			},
			sex:{
				required:true,
			},
			phone:{
				required:true,
				isPhone:true,
			},
			email:{
				required:true,
				email:true,
			},
			adminRole:{
				required:true,
			},
		},
		onkeyup:false,
		focusCleanup:true,
		success:"valid",
		submitHandler:function(form){
			$(form).ajaxSubmit({
				type: 'post',
				url: "xxxxxxx" ,
				success: function(data){
					layer.msg('添加成功!',{icon:1,time:1000});
				},
                error: function(XmlHttpRequest, textStatus, errorThrown){
					layer.msg('error!',{icon:1,time:1000});
				}
			});
			var index = parent.layer.getFrameIndex(window.name);
			parent.$('.btn-refresh').click();
			parent.layer.close(index);
		}
	});
});
</script> 
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>

管理员列表

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta id="farm_csrf" name="csrf-token" content="{{ csrf_token() }}"><重点:new insert>这个是处理laravel的CSRF验证
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="Bookmark" href="/favicon.ico" >
<link rel="Shortcut Icon" href="/favicon.ico" />
<!--[if lt IE 9]>
<script type="text/javascript" src="admin/lib/html5shiv.js"></script>
<script type="text/javascript" src="admin/lib/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui/css/H-ui.min.css")}} />
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui.admin/css/H-ui.admin.css")}} />
<link rel="stylesheet" type="text/css" href={{asset("admin/lib/Hui-iconfont/1.0.8/iconfont.css")}} />
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui.admin/skin/default/skin.css")}} id="skin" />
<link rel="stylesheet" type="text/css" href={{asset("admin/static/h-ui.admin/css/style.css")}}/>
<!--[if IE 6]>
<script type="text/javascript" src="admin/lib/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>管理员列表</title>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 管理员管理 <span class="c-gray en">&gt;</span> 管理员列表 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
	{{--<div class="text-c"> 日期范围:--}}
		{{--<input type="text" onfocus="WdatePicker({ maxDate:'#F{$dp.$D(\'datemax\')||\'%y-%M-%d\'}' })" id="datemin" class="input-text Wdate" style="width:120px;">--}}
		{{-----}}
		{{--<input type="text" onfocus="WdatePicker({ minDate:'#F{$dp.$D(\'datemin\')}',maxDate:'%y-%M-%d' })" id="datemax" class="input-text Wdate" style="width:120px;">--}}
		{{--<input type="text" class="input-text" style="width:250px" placeholder="输入管理员名称" id="" name="">--}}
		{{--<button type="submit" class="btn btn-success" id="" name=""><i class="Hui-iconfont">&#xe665;</i> 搜用户</button>--}}
	{{--</div>--}}
	<div class="cl pd-5 bg-1 bk-gray mt-20">
		<span class="l">
			{{--<a href="javascript:;" onclick="datadel()" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>--}}
			<a href="javascript:;" onclick="admin_add('添加管理员','/admin/regulateadd','800','')" class="btn btn-primary radius">
				<i class="Hui-iconfont">&#xe600;</i> 添加管理员</a>
		</span> <span class="r">共有数据:<strong>54</strong> 条</span>
	</div>
	<table class="table table-border table-bordered table-bg">
		<thead>
			<tr>
				<th scope="col" colspan="9">管理员列表</th>
			</tr>
			<tr class="text-c">
				<th width="25"><input type="checkbox" name="" value=""></th>
				<th width="40">ID</th>
				<th width="150">登录名</th>
				<th width="90">手机</th>
				<th width="150">邮箱</th>
				<th>角色</th>
				<th width="130">加入时间</th>
				<th width="100">是否已启用</th>
				<th width="100">操作</th>
			</tr>
		</thead>
		@foreach($data as $v)
		<tbody>
			<tr class="text-c">
				<td><input type="checkbox" value="1" name=""></td>
				<td>{{$v->id}}</td>
				<td>{{$v->login_name}}</td>
				<td>{{$v->tel}}</td>
				<td>{{$v->email}}</td>
				<td>
					@if($v->group_name)
						{{$v->group_name}}
					@else
						角色已删除
					@endif
				</td>
				<td>{{date('Y-m-d H:i:s',$v->create_time)}}</td>
				<td class="td-status">
					@if($v->start==0)
					<span class="label label-success radius">已启用</span>
					@else
						<span class="label label-default radius">已禁用</span>
					@endif
				</td>
				<td class="td-manage">
					@if($v->start==0)
						<a style="text-decoration:none" onClick="admin_stop(this,{{$v->id}})" href="javascript:;" title="停用">
							<i class="Hui-iconfont">&#xe631;</i>
						</a>
					@else
						<a onClick="admin_start(this,{{$v->id}})" href="javascript:;" title="启用" style="text-decoration:none">
							<i class="Hui-iconfont">&#xe615;</i>
						</a>
					@endif
					<a title="编辑" href="javascript:;" onclick="admin_edit('管理员编辑','/admin/regulateedit/{{$v->id}}','1','800','')" class="ml-5" style="text-decoration:none">
						<i class="Hui-iconfont">&#xe6df;</i>
					</a>
						<a title="修改密码" href="javascript:;" onclick="admin_regulateresetpwd('修改密码','/admin/regulateresetpwd/{{$v->id}}','1','800','')" class="ml-5" style="text-decoration:none">
							<i class="Hui-iconfont">&#xe63f;</i>
						</a>

				</td>
			</tr>
		</tbody>
		@endforeach
	</table>
</div>
<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src={{asset("admin/lib/jquery/1.9.1/jquery.min.js")}}></script>
<script type="text/javascript" src={{asset("admin/lib/layer/2.4/layer.js")}}></script>
<script type="text/javascript" src={{asset("admin/static/h-ui/js/H-ui.min.js")}}></script>
<script type="text/javascript" src={{asset("admin/static/h-ui.admin/js/H-ui.admin.js")}}></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src={{asset("admin/lib/My97DatePicker/4.8/WdatePicker.js")}}></script>
<script type="text/javascript" src={{asset("admin/lib/datatables/1.10.0/jquery.dataTables.min.js")}}></script>
<script type="text/javascript" src={{asset("admin/lib/laypage/1.2/laypage.js")}}></script>
<script type="text/javascript">
/*
	参数解释:
	title	标题
	url		请求的url
	id		需要操作的数据id
	w		弹出层宽度(缺省调默认值)
	h		弹出层高度(缺省调默认值)
*/
/*管理员-增加*/
function admin_add(title,url,w,h){
	layer_show(title,url,w,h);
}
/*管理员-删除*/
function admin_del(obj,id){
	layer.confirm('确认要删除吗?',function(index){
		$.ajax({
			type: 'POST',
			url: 'admin/regulatestop',
			dataType: 'json',
			success: function(data){
				$(obj).parents("tr").remove();
				layer.msg('已删除!',{icon:1,time:1000});
			},
			error:function(data) {
				console.log(data.msg);
			},
		});		
	});
}

/*管理员-编辑*/
function admin_edit(title,url,id,w,h){
	layer_show(title,url,w,h);
}
function admin_regulateresetpwd(title,url,id,w,h){
	layer_show(title,url,w,h);
}
/*管理员-停用*/
<重点:new insert>这个是处理laravel的CSRF验证
$.ajaxSetup({
	headers: {
		'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
	}
});
function admin_stop(obj,id){
	//alert('停用'+id);
	layer.confirm('确认要停用吗?',function(index){
		//此处请求后台程序,下方是成功后的前台处理……
		$.ajax({
			type:'POST',
			url:'/admin/regulatestop/'+id,
			//dataType:'json',
			data:{"start":'1'},
			success:function(data){
				//TODO <winner sign>js的逻辑有点问题,在上面用if判断执行启用停用操作
				{{--$(obj).parents("tr").find(".td-status").html('<span class="label label-default radius">@if($v->start==1)已禁用@endif</span>');--}}
				//$(obj).remove();
				{{--$(obj).parents("tr").find(".td-manage").prepend('<a onClick="admin_start(this,{{$v->id}})" href="javascript:;" title="启用" style="text-decoration:none"><i class="Hui-iconfont">&#xe615;</i></a>');--}}
				layer.msg('已停用!',{icon: 5,time:1000});
				//刷新本页面,功能跟页面上右上角的刷新一样。
				window.location.replace(location.href);
			},error:function(){
				layer.msg('网络错误,稍后再试!',{icon: 5,time:1000});
			}
		});
	});
}

/*管理员-启用*/
function admin_start(obj,id){
	//alert('启用'+id);
	layer.confirm('确认要启用吗?',function(index){
		//此处请求后台程序,下方是成功后的前台处理……
		$.ajax({
			type:'POST',
			url:'/admin/regulatestop/'+id,
			//dataType:'json',
			data:{"start":'0'},
			success:function(data){
				//TODO <winner sign>js的逻辑有点问题,在上面用if判断执行启用停用操作
				{{--$(obj).parents("tr").find(".td-manage").prepend('<a onClick="admin_stop(this,{{$v->id}})" href="javascript:;" title="停用" style="text-decoration:none"><i class="Hui-iconfont">&#xe631;</i></a>');--}}
				{{--$(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">@if($v->start==0)已启用@endif</span>');--}}
				// $(obj).remove();
				layer.msg('已启用!', {icon: 6,time:1000});
				//刷新本页面,功能跟页面上右上角的刷新一样。
				window.location.replace(location.href);
			},
			error:function(){
				layer.msg('网络错误,稍后再试!',{icon: 5,time:1000});
			}
		});
		

	});
}
</script>
</body>
</html>
我只是一个小白,增加,修改分开写的,哪位小伙伴可以结合在一起,欢迎修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值