Manager.php
5.6 KB
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
<?php
namespace app\api\controller;
use app\admin\controller\Base;
use app\admin\model\Manager as ManagerModel;
use app\admin\model\Menu as MenuModel;
use app\admin\model\Role as RoleModel;
use think\Cache;
use think\Validate;
/**
* @name:管理员管理
*/
class Manager extends Base
{
/**
* @name:获取管理员列表
* @method:get
* @param /p:页数 row:条数
*/
public function lists(){
$managerModel = new ManagerModel();
$lists = $managerModel->lists($this->param,$this->p,$this->row,'sort desc');
$this->allCount = $managerModel->allCount;
$this->result($lists);
}
/**
* @name:添加管理员
* @method:post
* @param:
*/
public function add(){
/*参数验证*/
$rules = [
['username','require|max:25','名称必须|名称最多不能超过25个字符'],
['password','password|between:6,25','密码必须|年龄必须在6~26之间'],
['re_password','require||confirm:password','密码必须|两次密码不一致'],
];
$validate = new Validate($rules);
$res = $validate->check($this->param);
if(!$res){
$this->response($validate->getError(),400,[]);
}
$managerModel = new ManagerModel();
$res = $managerModel->add($this->param);
if($res === false){
$this->response('添加失败',202,[]);
}
$this->response('添加成功',200);
}
/**
* @name : 编辑管理员
* @return void
* @author :liyuhang
* @method
*/
public function edit(){
$rules = [
['id','require','必须'],
];
if(isset($this->param['password']) && !empty($this->param['password'])){
array_push($rules,['password','password|between:6,25','密码必须|年龄必须在6~26之间']);
array_push($rules,['re_password','require||confirm:password','密码必须|两次密码不一致']);
}
$validate = new Validate($rules);
$res = $validate->check($this->param);
if(!$res){
$this->response($validate->getError(),400,[]);
}
$managerModel = new ManagerModel();
if(isset($this->param['password'])){
$this->param['password'] = think_center_md5($this->param['password']);
}
$res = $managerModel->edit($this->param,['id'=>$this->param['id']]);
if($res === false){
$this->response('编辑失败',202,[]);
}
$this->response('编辑成功',200);
}
/**
* @name :修改当前管理员状态
* @return void
* @author :liyuhang
* @method
*/
public function status()
{
/*参数验证*/
$rules = [
['id','require','必须'],
['status','require','必须'],
];
$validate = new Validate($rules);
$res = $validate->check($this->param);
if(!$res){
$this->response($validate->getError(),202,[]);
}
$managerModel = new ManagerModel();
$info = $managerModel->read(['id'=>$this->param['id']]);
if(empty($info)){
$this->response('当前管理员不存在','');
}
$res = $managerModel->edit(['status'=>$this->param['status']],['id'=>$this->param['id']]);
if ($res === false) {
$this->response('修改状态失败',202,[]);
} else {
$this->response($this->param['status'] == 1 ? '启用成功': '禁用成功',200,[]);
}
}
/**
* @name:排序
* @return void
* @author :liyuhang
* @method
*/
public function sort()
{
/*参数验证*/
$rules = [
['id','require','必须'],
['sort','require','必须'],
];
$validate = new Validate($rules);
$res = $validate->check($this->param);
if(!$res){
$this->response($validate->getError(),202,[]);
}
$res = ManagerModel::where('id', $this->param['id'])->update(['sort' => $this->param['sort']]);
if ($res === false) {
$this->response('排序失败',202,[]);
} else {
$this->response('成功',200,[]);;
}
}
/**
* @name
* @return void
* @author :liyuhang
* @method
*/
public function delete(){
$rules = [
['id','require','必须'],
];
$validate = new Validate($rules);
$res = $validate->check($this->param);
if(!$res){
$this->response($validate->getError(),202,[]);
}
$managerModel = new ManagerModel();
$res = $managerModel->edit(['deleted'=>0],['id'=>$this->param['id']]);
if ($res === false) {
$this->response('排序失败',202,[]);
} else {
$this->response('成功',200,[]);;
}
}
/**
* @name :获取当前用户菜单列表
* @return void
* @author :liyuhang
* @method : get
*/
public function get_menu(){
//获取当前用户角色
$roleModel = new RoleModel();
$role_info = $roleModel->read(['id'=>$this->user['role_id']]);
//根据当前角色获取当前菜单栏
$menuModel = new MenuModel();
$menu_lists = $menuModel->where(['pid'=>0,'status'=>1,'deleted'=>1])->where('id','in',$role_info['menu'])->select()->toArray();
foreach ($menu_lists as $k => $v){
$menu_lists[$k]['son'] = $menuModel->where(['pid'=>$v['id'],'status'=>1,'deleted'=>1])->where('id','in',$role_info['menu'])->select()->toArray();
}
$this->response('当前用户菜单列表',200,$menu_lists);
}
}