主页 > 编程 > php >

php web通用权限管理之用户修改 角色修改代码

2019-07-16 23:49 阅读:147 来源:智宇SEO自媒体

视图:
<form class="layui-form" action="" method="post">
  <div class="layui-form-item">
    <label class="layui-form-label">用户名</label>
    <div class="layui-input-block">
      <input type="text" name="name" value="<?php echo $this->userData['name']; ?>" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input"></div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">密码</label>
    <div class="layui-input-block">
      <input type="text" name="pwd" value="<?php echo $this->userData['pwd']; ?>" lay-verify="required" lay-reqtext="用户名是必填项,岂能为空?" placeholder="请输入" autocomplete="off" class="layui-input"></div>
  </div>
  <input type="hidden" value="<?php echo $this->userData['id']; ?> " name="uid">
  <div class="layui-form-item" pane="">
    <label class="layui-form-label">所属角色</label>
    <div class="layui-input-block">
      <?php foreach ($this->roleData as $r ) {?>
        <input <?php if (isset($this->roleids)) { if (in_array($r['id'],$this->roleids)){ echo "checked";}} ?> style="display: inline-block" type="checkbox" name="roleid[]" lay-skin="primary" title="
        <?php echo $r[ 'name'];?>" value="<?php echo $r[ 'id']?>" >
            <?php echo $r[ 'name'];?>
       <?php }?>
</div>
</div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <input class="layui-btn" lay-submit="" lay-filter="demo1" type="submit" value="提交修改" /></div>
  </div>
</form>

控制器:
public function edit()
    {
        $id=$this->gets['1'];
        //角色
        $this->roledb=db('role');
        $this->roleData= $this->roledb->where('status=?', array(1))->fetchAll();

        $this->userroledb=db('user_role');
        $this->user_role_id = $this->userroledb->where('uid=?', array($id))->fetchAll('role_id');
        //二维数组降为一维数组
        $this->roleids = array_column($this->user_role_id, 'role_id');

        $this->userData = $this->db->where('id = ?', array($id))->fetch();

        if(!empty($_POST))
        {
            $editData  = array(
                'name'=>$_POST['name'],
                'pwd'=>$_POST['pwd'],
            );
            $res=$this->db->where('id = ?', array( $id))->update($editData );

            if($res)
            {
                if(isset($_POST['roleid']))
                {
                    //简单粗暴,全部删除角色,再重新添加角色  这思路,可行,但不完美。
                    //$this->userroledb->where('uid = ?', array($id))->delete();

                    $roleid = $_POST['roleid'];
                    $rid =implode(',',$roleid);
                    $aid=explode(',',$rid);//从数据库取出数据时用。

                    //数据库里role_id 和复选框选中的对比差集
                    $result=array_diff($this->roleids,$aid);

                    if(!empty($result))//比原来少选角色
                    {
                        if ($result != $this->roleids)
                        {
                            foreach ($result as $r)
                            {
                                $this->userroledb->where('uid = ? and role_id=?', array($id,$r))->delete();
                            }

                            $res=array_diff($aid,$this->roleids);
                            foreach($res as $r)
                            {
                                $user_role_Data1  = array('uid' =>$id,
                                    'role_id' =>$r,
                                    'created_time' =>date("Y-m-d H:i:s"),
                                );
                                $user_role_id1 =$this->userroledb->add($user_role_Data1);

                            }

                        }
                        else
                        {

                            foreach ($this->roleids as $r)
                            {
                                $this->userroledb->where('uid = ? and role_id=?', array($id,$r))->delete();
                            }

                            foreach($aid as $a)
                            {
                                $user_role_Data1  = array('uid' =>$id,
                                    'role_id' =>$a,
                                    'created_time' =>date("Y-m-d H:i:s"),
                                );
                                $user_role_id1 =$this->userroledb->add($user_role_Data1);

                            }
                            //echo "没有交集了";
                            //return;
                        }


                    }else
                    {
                        if($aid!=$this->roleids) //比原来多选角色
                        {
                            $result=array_diff($aid,$this->roleids);
                            //print_r($result);
                            foreach($result as $rid)
                            {
                                $user_role_Data  = array('uid' =>$id,
                                    'role_id' =>$rid,
                                    'created_time' =>date("Y-m-d H:i:s"),
                                );
                                $user_role_id =$this->userroledb->add($user_role_Data);

                            }

                        }
                    }

                }
                else
                {

                    if(isset($this->user_role_id))
                    {
                        $this->userroledb->where('uid = ?', array($id))->delete();

                    }

                }
                s('user','index','用户修改成功');
            }
            else
            {
                e('用户修改失败');
            }
        }

        $this->display('useredit.php');
    }