角色表结构如下:

 

 权限表结构如下:

 角色控制器代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
     * 角色列表 【模糊查询、分页】
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        //获取角色名称、当前页数和每页显示条数
        $data = $request->input();
        $roleName = emptyParam($data,'role_name');
        $pageName = emptyParam($data,'pagename',1);
        $pageSize = emptyParam($data,'pagesezi',10);
        $where = [];
        if (!empty($roleName)){
            $where[] = ["role_name", "like", "%" . $roleName . "%"];
        }
        $result = ManageRoleModel::query()->where($where)->paginate($pageSize,['role_name','created_at'],'',$pageName)->toArray();
 
        foreach ($result['data'] as &$v){
             $v['created_at'] = date("Y-m-d",strtotime($v['created_at']));
            }
 
        return $this->success($result['data'],'查询成功');
    }
 
    /**
     * 添加角色
     * @param RoleRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(RoleRequest $request){
        $manage = [
            "role_name" => $request->role_name,
            "created_at" => date("Y-m-d H:i:s")
        ];
        $role = ManageRoleModel::query()->create($manage);
        if ($role){
            return $this->success($role, "角色新增成功");
        }else{
            return  $this->failed("角色新增失败");
        }
    }
 
    /**
     * 角色修改
     * @param RoleRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(RoleRequest $request){
        if (empty($request['id']) || !is_numeric($request['id'])){
            return  $this->failed("非法传参");
        }
        $roleInfo = [
          'id' => $request->id,
          'role_name' => $request->role_name,
          'created_at' => date("Y-m-d H:i:s"),
        ];
        $edit = ManageRoleModel::query()->where(['id' => $request->id])->update($roleInfo);
        if ($edit){
            return $this->success($edit, "角色修改成功");
        }else{
            return  $this->failed("角色修改失败");
        }
    }
 
    /**
     * 删除角色
     * @param CheckIdRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(CheckIdRequest $request){
        $id = $request->id;
        if (ManageRoleModel::query()->where(['id' => $id])->delete()){
            return $this->success([], "删除成功");
        }else{
            return  $this->failed("删除失败");
        }
    }
 
    //给用户赋权
    public function addPermission(ManageRolePermission $request)
    {
        $permissionInfo = [
          'id' => $request['id'],
          'permission_id' => $request['permission_id'],
          'updated_at' => date("Y-m-d H:i:s")
        ];
//        dd($permissionInfo);
        $addPermission = ManageRoleModel::query()->where('id',$request['id'])->update($permissionInfo);
        if ($addPermission){
            return $this->success($addPermission,'添加权限成功');
        }else{
            return $this->failed('添加权限失败');
        }
    }

 

————————————————————————————————————————————————————————————————————————————————————

 

 权限控制器代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//递归查询权限列表
  public function index(){
      $data = ManagePermissionModel::query()->get()->toArray();
      $array = $this->recursion($data, $pid = 0);
      return $this->success($array);
  }
 
 
  /***
   * 获取取全树
   */
  public function navList(){
      $operationModel = new ManagePermissionModel();
      $query = $operationModel->where('type','<>','a')->orderBy('sort', 'asc')->get()->toArray();
      $tree = PHPTree::makeTreeForHtml($query,array(
          'parent_key' => 'pid',
          'primary_name' => 'name',
          'primary_code' => 'code',
      ));
      return $this->success($tree);
  }
 
  public function menus(){
      $operationModel = new ManagePermissionModel();
      $menus = $operationModel->where('perm_type','<',3)->orderBy('sort', 'asc')->get()->toArray();
      $r = PHPTree::makeTreeForHtml($menus,array(
          'parent_key' => 'pid',
          'primary_name' => 'name',
          'primary_code' => 'code',
      ));
      return $this->success($r);
  }
 
  /**
   * 创建栏目
   * @param OperationRequest $request
   */
  public function create(OperationRequest $request){
      $operation = [
          'permission_name' => $request->permission_name,
          'address' => $request->address,
          'type' => $request->type,
          'pid' => $request->pid,
          'parent_menu_id' => $request->parent_menu_id,
          'perm_type' => $request->perm_type,
          'sort' => $request->sort,
      ];
      $addPermission = ManagePermissionModel::query()->create($operation);
      if ($addPermission){
          return $this->success($addPermission, "创建成功");
      } else {
          return $this->failed("创建失败");
      }
  }
 
  public function del(CheckIdRequest $request){
      $id = $request->id;
      $del = ManagePermissionModel::query()->where(['id' => $id])->delete();
      if ($del){
          return $this->success($del, "删除成功");
      }else{
          return  $this->failed("删除失败");
      }
  }
 
  public function edit(OperationRequest $request){
      if (empty($request['id']) || !is_numeric($request['id'])){
          return  $this->failed("非法传参");
      }
      $operation = [
          'permission_name' => $request->permission_name,
          'address' => $request->address,
          'type' => $request->type,
          'pid' => $request->pid,
          'parent_menu_id' => $request->parent_menu_id,
          'id' => $request->id,
          'perm_type' => $request->perm_type,
          'sort' => $request->sort,
      ];
      $edit = ManagePermissionModel::query()->where(['id' => $operation['id']])->update($operation);
      if ($edit){
          return $this->success($edit, "修改成功");
      } else {
          return $this->failed("修改失败");
      }
  }

权限模型层代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
     * 根据传过来的数组,构建以parent_menu_id为父节点的菜单树..
     * @param $list 构建树所需要的节点,此值是根据权限节点算出来的
     * @param $parent_menu_id   构建树的根节点
     * @return array
     */
    public function createTree($list,$parent_menu_id){
        $data = [];
        foreach($list as $k => $v){
            if($v['parent_menu_id'] == $parent_menu_id){
                $row = $v;
                //取当前节点的url
                // $row['url'] = $this->getUrl($v['id']);
                $row['children'] = $this->createTree($list,$v['id']);
                $data[] = $row;
            }
        }
        return $data;
    }
 
    /**
     * 根据当前节点,取出当前节点的url,用于后台菜单节点的url生成
     * @param $operation_id
     * @param $list
     */
    private function getUrl($operation_id){
        static $list = [];
        if(!$list){
            $all = ManagePermissionModel::query()->get();
            if(!$all->isEmpty()){
                $all = $all->toArray();
            }else{
                $all = [];
            }
            foreach($all as $v){
                $list[$v['id']] = $v;
            }
        }
 
        if(!isset($list[$operation_id])){
            return "";
        }
        if($list[$operation_id]['type'] == 'm'){
            return url($list[$operation_id]['code'] . '/index/index');          //一个模型,搞什么url?
        }
        if($list[$operation_id]['type'] == 'c'){
            if(isset($list[$list[$operation_id]['pid']])){
                return url($list[$list[$operation_id]['pid']]['code'] . '/'.$list[$operation_id]['code'].'/index');
            }else{
                return "";
            }
        }
        if($list[$operation_id]['type'] == 'a'){
            //取控制器
            if(isset($list[$list[$operation_id]['pid']]) && isset($list[$list[$list[$operation_id]['pid']]['pid']])){
                return url($list[$list[$list[$operation_id]['pid']]['pid']]['code'] . '/'.$list[$list[$operation_id]['pid']]['code'].'/'.$list[$operation_id]['code']);
            }else{
                return "";
            }
        }
        return "";
    }

在Laravel框架配置里封装:

配置目录:

 

封装代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
     * 根据父级id查找子级数据
     * @param $data 要查询的数据
     * @param int $pid 父级id
     */
    public function recursion($data, $pid = 0)
    {
        // 定义存储子级数据数组
        $child = [];
        foreach ($data as $key => $value) {
            if ($value['pid'] == $pid) {
                // 使用过后可以销毁
                unset($data[$key]);
                // 递归调用,查找当前数据的子级
                $value['child'] = $this->recursion($data, $value['id']);
                // 把子级数据添加进数组
                $child[] = $value;
            }
        }
        return $child;
    }