quanwei
2 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
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
96
97
<?php
 
namespace app\admin\controller\edition;
use app\admin\controller\Controller;
use app\admin\model\edition\Role as RoleModel;
use app\admin\model\Access as AccessModel;
 
/**
 * 管理后台角色
 */
class Role extends Controller
{
    /**
     * 角色列表
     */
    public function index()
    {
        $model = new RoleModel();
        $list = $model->getTreeData();
        return $this->renderSuccess('', compact('list'));
    }
 
    /**
     * 新增get数据
     */
    public function addInfo()
    {
        $menu = (new AccessModel())->getList();
        $model = new RoleModel();
        // 角色列表
        $roleList = $model->getTreeData();
        return $this->renderSuccess('', compact('menu', 'roleList'));
    }
 
    /**
     * 新增
     */
    public function add()
    {
        if($this->request->isGet()){
            return $this->addInfo();
        }
        $data = json_decode($this->postData()['params'], true);
        $model = new RoleModel();
        if ($model->add($data)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?:'添加失败');
    }
 
    /**
     * 修改get数据
     */
    public function editInfo($role_id)
    {
        $menu = (new AccessModel())->getList();
        $model = RoleModel::detail($role_id);
        $select_menu = array_column($model->toArray()['access'], 'access_id');
        // 角色列表
        $roleList = $model->getTreeData();
        return $this->renderSuccess('', compact('model', 'roleList', 'menu', 'select_menu'));
    }
 
    /**
     * 修改
     */
    public function edit($role_id)
    {
        if($this->request->isGet()){
            return $this->editInfo($role_id);
        }
        $data = json_decode($this->postData()['params'], true);
        if (isset($data['access_id']) && count($data['access_id']) == 0) {
            return $this->renderError('请选择权限');
        }
 
        $model = RoleModel::detail($role_id);
 
        // 更新记录
        if ($model->edit($data)) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?:'更新失败');
    }
 
    /**
     * 删除
     */
    public function delete($role_id)
    {
        $model = new RoleModel();
        if($model->del($role_id)){
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?:'删除失败');
    }
}