Index.php
10.4 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
namespace app\admin\controller\manystore;
use app\common\model\TemplateView;
use app\school\model\Manystore;
use app\school\model\ManystoreShop;
use app\school\model\ManystoreAuthGroup;
use app\school\model\ManystoreAuthGroupAccess;
use app\common\controller\Backend;
use fast\Random;
use fast\Tree;
use think\Exception;
use think\Validate;
/**
* 管理员管理
*
* @icon fa fa-users
* @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
*/
class Index extends Backend
{
/**
* @var \app\admin\model\Admin
*/
protected $model = null;
protected $shopModel = null;
protected $childrenGroupIds = [];
protected $childrenAdminIds = [];
public function _initialize()
{
parent::_initialize();
$this->model = new Manystore();
$this->shopModel = new ManystoreShop();
$templates = TemplateView::all();
$this->assign('templates',$templates);
}
/**
* 查看
*/
public function index()
{
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->shopModel
->where($where)
->order($sort, $order)
->count();
$list =$this->shopModel
->where($where)
->field(['password', 'salt', 'token'], true)
->order($sort, $order)
->limit($offset, $limit)
->select();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
return $this->view->fetch();
}
/**
* 查看
*/
public function indexManager()
{
$school_id = $this->request->param('school_id');
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($where)
->where('school_id',$school_id)
->order($sort, $order)
->count();
$list = $this->model
->where($where)
->where('school_id',$school_id)
->field(['password', 'salt', 'token'], true)
->order($sort, $order)
->limit($offset, $limit)
->select();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
$this->assignconfig('school_id',$school_id);
return $this->view->fetch();
}
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
$this->token();
$shop = $this->request->post("shop/a");
$shop['image'] = '';
$shop['address'] = '';
$shop_info = $this->shopModel->save($shop);
if($shop_info === false){
$this->error($this->shopModel->getError());
}else{
$this->success();
}
}
return $this->view->fetch();
}
/**
*
* @return string
* @throws Exception
* @throws \think\exception\PDOException
*/
public function addManager(){
$school_id = $this->request->param('school_id');
if(empty($school_id)){
$this->error("参数错误2021-1");
}
if ($this->request->isPost()) {
$this->token();
$params = $this->request->post("row/a");
if ($params) {
if (!Validate::is($params['password'], '\S{6,16}')) {
$this->error(__("Please input correct password"));
}
db()->startTrans();
try{
$params['salt'] = Random::alnum();
$params['password'] = md5(md5($params['password']) . $params['salt']);
$params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
$params['is_main'] = 1;
$params['rule_type'] = 0;
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$result = $this->model->validate($validate)->save($params);
if ($result === false) {
$this->error($this->model->getError());
}
$manystoreAuthGroupModel = new ManystoreAuthGroup();
$group = [];
$group['school_id'] = $school_id;
$group['name'] = '超级管理员';
$group['rules'] = '*';
$group['createtime'] = time();
$group['updatetime'] = time();
$group_id = $manystoreAuthGroupModel->insertGetId($group);
if(!$group_id){
$this->error('添加失败');
}
$manystoreAuthGroupAccessModel = new ManystoreAuthGroupAccess();
$group_access = [];
$group_access['uid'] = $this->model->id;
$group_access['group_id'] = $group_id;
$manystoreAuthGroupAccessModel->insert($group_access);
db()->commit();
$this->success();
}catch (Exception $e){
db()->rollback();
$this->error($e->getMessage());
}
}
$this->error();
}
$this->assign("school_id",$school_id);
return $this->view->fetch();
}
/**
* 编辑
*/
public function edit($ids = null)
{
$shop_info = $this->shopModel->get(array('id'=>$ids));
if(!$shop_info){
$this->error(__('学校信息资料不存在'));
}
if ($this->request->isPost()) {
$this->token();
$shop = $this->request->post("shop/a");
$result = $shop_info->save($shop);
if($result === false){
$this->error(__("修改学校信息资料失败"));
}else{
$this->success();
}
$this->error();
}
$this->view->assign("shop", $shop_info);
return $this->view->fetch();
}
public function editManager($ids=null){
$school_id = $this->request->param('school_id');
$row = $this->model->get(['id' => $ids]);
if (!$row) {
$this->error(__('No Results were found'));
}
$shop_info = $this->shopModel->get(array('id'=>$school_id));
if(!$shop_info){
$this->error(__('学校信息资料不存在'));
}
if ($this->request->isPost()) {
$this->token();
$params = $this->request->post("row/a");
if ($params) {
if ($params['password']) {
if (!Validate::is($params['password'], '\S{6,16}')) {
$this->error(__("Please input correct password"));
}
$params['salt'] = Random::alnum();
$params['password'] = md5(md5($params['password']) . $params['salt']);
} else {
unset($params['password'], $params['salt']);
}
//这里需要针对username和email做唯一验证
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
$manystoreValidate = \think\Loader::validate($validate);
$manystoreValidate->rule([
'username' => 'regex:\w{3,12}|unique:manystore,username,' . $row->id,
'email' => 'require|email|unique:manystore,email,' . $row->id,
'password' => 'regex:\S{32}',
]);
$result = $row->validate($validate)->save($params);
if ($result === false) {
$this->error($row->getError());
}
$this->success();
}
$this->error();
}
$grouplist = $this->auth->getGroups($row['id']);
$groupids = [];
foreach ($grouplist as $k => $v) {
$groupids[] = $v['id'];
}
$this->view->assign("row", $row);
$this->view->assign("groupids", $groupids);
return $this->view->fetch();
}
/**
* 删除
*/
public function del($ids = "")
{
if ($ids) {
$row = $this->shopModel->get(['id' => $ids]);
if(!$row){
$this->error(__('No Results were found'));
}
$result = $this->shopModel->where(array('id'=>$row['id']))->delete();
if(!$result){
$this->error('学校信息删除失败');
}else{
$this->success();
}
}else{
$this->error(__('You have no permission'));
}
}
public function delmanager($ids = ""){
if ($ids) {
$row = $this->model->get(['id' => $ids]);
if(!$row){
$this->error(__('No Results were found'));
}
$result = $this->model->where(array('id'=>$row['id']))->delete();
if(!$result){
$this->error('账号信息删除失败');
}else{
$this->success();
}
}else{
$this->error(__('You have no permission'));
}
}
/**
* 批量更新
* @internal
*/
public function multi($ids = "")
{
// 管理员禁止批量操作
$this->error();
}
/**
* 下拉搜索
*/
public function selectpage()
{
$this->dataLimit = 'auth';
$this->dataLimitField = 'id';
return parent::selectpage();
}
}