🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
添加 ~~~ //添加菜单1-2 public function save() { $status=0; $message="添加失败"; $data = $this->request->param(); $map = ['title'=> $data['title']]; $map1 = ['name'=> $data['name']]; $node1 = AuthRule::get($map1); if ($data['name']!='#'&&($node1)) { $message="添加失败,该菜单url已存在"; }else{ $node = AuthRule::get($map); if($node){ $message="添加失败,该菜单名称已存在"; }else{ if(model('AuthRule')->allowField(true)->save($data)){ $status=1; $message="添加成功"; } } } return ['status'=>$status, 'message'=>$message]; } ~~~ 更新 ~~~ //编辑菜单1-2 public function update() { $status=0; $message="更新失败"; $data = $this->request->param(); $id=$data['id']; $map = ['title'=> $data['title']]; $map1 = ['name'=> $data['name']]; $node1 = AuthRule::get($map1); $node = AuthRule::get($map); if ($data['name']!='#'&&($node1&&($id!=$node1['id']))){ $message="更新失败,该菜单url已存在"; }else{ if($node&&($id!=$node['id'])){ $message="更新失败,用户名已存在"; }else{ //执行更新操作 if (model('AuthRule')->allowField(true)->save($data, $id) !== false) { $status=1; $message="更新成功"; } } } return ['status'=>$status, 'message'=> $message]; } ~~~ 前端 ~~~ <script> layui.use(['form','layer'], function(){ $ = layui.jquery; var form = layui.form() ,layer = layui.layer; //自定义验证规则 form.verify({ title: function(value){ if(value.length < 2){ return '名称至少得2个字符啊'; } } }); //监听提交 form.on('submit(menu_add)', function(data){ //发异步,把数据提交给php console.log(data); $.ajax({ url:"{:url('menu/save')}", data:$('.layui-form').serialize(), dataType: "json", type:'post', async: false, success:function(res) { if(res.status == 1) { window.parent.location.reload(); layer.msg(res.message); // 获得frame索引 var index = parent.layer.getFrameIndex(window.name); //关闭当前frame parent.layer.close(index); } else { layer.msg(res.message); } } }) return false; }); }); </script> ~~~