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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
namespace app\user\controller;
use app\admin\controller\Base;
use app\admin\model\DepartmentModel;
use app\admin\model\RoleModel;
use app\admin\model\StaffModel;
use app\admin\model\StaffRoleModel;
use app\admin\validate\StaffValidate;
use think\Request;
/**
* 员工控制器
* 负责员工的增删改查
* 员工密码的初始化
* Class Staff
* @package app\admin\controller
*/
class Staff extends Base
{
public function index()
{
$data = StaffModel::select();
$this->assign('data',$data);
return $this->fetch('index');
}
public function create()
{
$model = new DepartmentModel;
$data = $model->getDepartmentTree();
$this->assign('data',$data);
return $this->fetch('add');
}
public function edit($id){
$staff = StaffModel::find($id);
}
public function store(Request $request){
$validate = new StaffValidate;
if(!$validate->check($request->post())){
return json([
'status' => 0,
'message' => $validate->getError()
]);
}
$staff = new StaffModel;
$staff->name = $request->post('name');
$staff->email = $request->post('email');
$staff->d_id = $request->post('d_id');
$staff->tel = $request->post('tel') + 0;
$staff->identity = $request->post('identity');
$staff->status = $request->post('status');
if($staff->save()){
return json([
'status' => 1,
'message' => '添加员工成功'
]);
} else {
return json([
'status' => 1,
'message' => '添加员工失败'
]);
}
}
public function delete(Request $request){
$id = $request->post('id');
if(is_int($id + 0) && ($id + 0) > 0) {
$staff =StaffModel::get($id);
if(!$staff){
return json([
'status'=>0,
'message'=>'删除失败,用户不存在'
]);
}
//如果删除成功
if($staff->delete()){
//删除权限
return $this->role_del($staff);
} else {
return json([
'status' => 0,
'message' => '删除失败,请重试'
]);
}
} else {
return json([
'status' => 0,
'message' => '参数错误'
]);
}
}
//配置员工所属角色匹配角色所属权限
public function role(){
$data = StaffModel::with('roles')->select();
$this->assign('data',$data);
$this->assign('empty','<span class="layui-bg-orange">[没有权限]</span>');
return $this->fetch('role');
}
public function role_edit($id){
$data = StaffModel::with('roles')->field('id,name')->find($id)->toArray();
$roles = RoleModel::field('id,title')->select()->toArray();
//筛选匹配的键
foreach ($roles as $key => $role){
foreach ($data['roles'] as $drole){
if($drole['id'] == $role['id']){
$roles[$key]['checked'] = true;
}
}
if(!isset($roles[$key]['checked'])){
$roles[$key]['checked'] = false;
}
}
$this->assign('roles',$roles);
$this->assign('data',$data);
return $this->fetch('role_edit');
}
/**
* 添加用户权限
* @param Request $request
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @throws \think\exception\PDOException
*
*/
public function role_update(Request $request){
$id = $request->post('id');
$roles = $request->post('role');
if(!$id){
return json([
'status' => 0,
'message' => '参数有错误刷新之后添加'
]);
}
$staffRole = StaffRoleModel::where(['staff_id'=>$id])->find();
//如果没有记录,则更新
if($staffRole) {
//先清除所有权限
StaffRoleModel::where(['staff_id' => $id])->delete();
}
//生成权限数据
$staffRoleList = [];
foreach ($roles as $key => $role){
$staffRoleList[$key]['staff_id'] = $id;
$staffRoleList[$key]['role_id'] = $role;
}
//批量插入权限
if((new StaffRoleModel)->saveAll($staffRoleList)){
return json([
'status'=>1,
'message' => '添加权限成功'
]);
} else {
return json([
'status' =>0,
'message' => '添加权限失败'
]);
}
}
public function role_del($staff=''){
if(!empty($staff)){
$id = $staff->id;
} else {
$id = Request::post('id');
}
if(is_int($id + 0) && ($id + 0) > 0){
if(StaffRoleModel::where(['staff_id' => $id])->delete()){
return json([
'status' => 1,
'message' => '删除权限成功'
]);
} else {
return json([
'status' => 0,
'message' => '删除失败,用户没有设置权限'
]);
}
} else {
return json([
'status' => 0,
'message' => '参数错误'
]);
}
}
}