Auth.php
13.1 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
namespace app\admin\controller;
use function MongoDB\BSON\toJSON;
use think\Db;
use clt\Leftnav;
use app\admin\model\Admin;
use app\admin\model\AuthGroup;
use app\admin\model\authRule;
use think\facade\Request;
use think\Validate;
class Auth extends Common
{
//管理员列表
public function adminList(){
if(Request::isAjax()){
$val=input('val');
$url['val'] = $val;
$this->assign('testval',$val);
$map='';
if($val){
$map['username|email|tel']= array('like',"%".$val."%");
}
if (session('aid')!=1){
$map='admin_id='.session('aid');
}
$list=Db::table(config('database.prefix').'admin')->alias('a')
->join(config('database.prefix').'auth_group ag','a.group_id = ag.group_id','left')
->field('a.*,ag.title')
->where($map)
->select();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
}
return view();
}
public function adminAdd(){
if(Request::isAjax()){
$data = input('post.');
$check_user = Admin::get(['username'=>$data['username']]);
if ($check_user) {
return $result = ['code'=>0,'msg'=>'用户已存在,请重新输入用户名!'];
}
$data['pwd'] = input('post.pwd', '', 'md5');
$data['add_time'] = time();
$data['ip'] = request()->ip();
//验证
$msg = $this->validate($data,'app\admin\validate\Admin');
if($msg!='true'){
return $result = ['code'=>0,'msg'=>$msg];
}
//单独验证密码
$checkPwd = Validate::make([input('post.pwd')=>'require']);
if (false === $checkPwd) {
return $result = ['code'=>0,'msg'=>'密码不能为空!'];
}
//添加
if (Admin::create($data)) {
return ['code'=>1,'msg'=>'管理员添加成功!','url'=>url('adminList')];
} else {
return ['code'=>0,'msg'=>'管理员添加失败!'];
}
}else{
$auth_group = AuthGroup::all();
$this->assign('authGroup',$auth_group);
$this->assign('title',lang('add').lang('admin'));
$this->assign('info','null');
$this->assign('selected', 'null');
return view('adminForm');
}
}
//删除管理员
public function adminDel(){
$admin_id=input('post.admin_id');
if (session('aid')==1){
Admin::where('admin_id','=',$admin_id)->delete();
return $result = ['code'=>1,'msg'=>'删除成功!'];
}else{
return $result = ['code'=>0,'msg'=>'您没有删除管理员的权限!'];
}
}
//修改管理员状态
public function adminState(){
$id=input('post.id');
$is_open=input('post.is_open');
if (empty($id)){
$result['status'] = 0;
$result['info'] = '用户ID不存在!';
$result['url'] = url('adminList');
return $result;
}
db('admin')->where('admin_id='.$id)->update(['is_open'=>$is_open]);
$result['status'] = 1;
$result['info'] = '用户状态修改成功!';
$result['url'] = url('adminList');
return $result;
}
//更新管理员信息
public function adminEdit(){
if(request()->isPost()){
$data = input('post.');
$pwd=input('post.pwd');
$map[] = ['admin_id','<>',$data['admin_id']];
$where['admin_id'] = $data['admin_id'];
if($data['username']){
$map[] = ['username','=',$data['username']];
$check_user = Admin::where($map)->find();
if ($check_user) {
return $result = ['code'=>0,'msg'=>'用户已存在,请重新输入用户名!'];
}
}
if ($pwd){
$data['pwd']=input('post.pwd','','md5');
}else{
unset($data['pwd']);
}
$msg = $this->validate($data,'app\admin\validate\Admin');
if($msg!='true'){
return $result = ['code'=>0,'msg'=>$msg];
}
Admin::update($data,$where);
if( $data['admin_id'] == session('aid')){
session('username',$data['username']);
$avatar = $data['avatar']==''?'/static/admin/images/0.jpg':$data['avatar'];
session('avatar',$avatar);
}
return $result = ['code'=>1,'msg'=>'管理员修改成功!','url'=>url('adminList')];
}else{
$auth_group = AuthGroup::all();
$admin = new Admin();
$info = $admin->getInfo(input('admin_id'));
$this->assign('info', json_encode($info,true));
$this->assign('authGroup',$auth_group);
$this->assign('title',lang('edit').lang('admin'));
return view('adminForm');
}
}
/*-----------------------用户组管理----------------------*/
//用户组管理
public function adminGroup(){
if(request()->isPost()){
$list = AuthGroup::all();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
}
return view();
}
//删除管理员分组
public function groupDel(){
AuthGroup::where('group_id','=',input('id'))->delete();
return $result = ['code'=>1,'msg'=>'删除成功!'];
}
//添加分组
public function groupAdd(){
if(request()->isPost()){
$data=input('post.');
$data['addtime']=time();
AuthGroup::create($data);
$result['msg'] = '用户组添加成功!';
$result['url'] = url('adminGroup');
$result['code'] = 1;
return $result;
}else{
$this->assign('title','添加用户组');
$this->assign('info','null');
return $this->fetch('groupForm');
}
}
//修改分组
public function groupEdit(){
if(request()->isPost()) {
$data=input('post.');
$where['group_id'] = $data['group_id'];
AuthGroup::update($data,$where);
$result = ['code'=>1,'msg'=>'用户组修改成功!','url'=>url('adminGroup')];
return $result;
}else{
$id = input('id');
$info = AuthGroup::get(['group_id'=>$id]);
$this->assign('info', json_encode($info,true));
$this->assign('title','编辑用户组');
return $this->fetch('groupForm');
}
}
//分组配置规则
public function groupAccess(){
$nav = new Leftnav();
$admin_rule=db('auth_rule')->field('id,pid,title')->order('sort asc')->select();
$rules = db('auth_group')->where('group_id',input('id'))->value('rules');
$arr = $nav->auth($admin_rule,$pid=0,$rules);
$arr[] = array(
"id"=>0,
"pid"=>0,
"title"=>"全部",
"open"=>true
);
$this->assign('data',json_encode($arr,true));
return $this->fetch();
}
public function groupSetaccess(){
$rules = input('post.rules');
if(empty($rules)){
return array('msg'=>'请选择权限!','code'=>0);
}
$data = input('post.');
$where['group_id'] = $data['group_id'];
if(AuthGroup::update($data,$where)){
return array('msg'=>'权限配置成功!','url'=>url('adminGroup'),'code'=>1);
}else{
return array('msg'=>'保存错误','code'=>0);
}
}
/********************************权限管理*******************************/
public function adminRule(){
if(request()->isPost()){
$nav = new Leftnav();
$arr = cache('authRuleList');
if(!$arr){
$authRule = authRule::all(function($query){
$query->order('sort', 'asc');
});
$arr = $nav->menu($authRule);
cache('authRuleList', $arr, 3600);
}
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$arr,'rel'=>1];
}
return view();
}
public function ruleAdd(){
if(request()->isPost()){
$data = input('post.');
$data['addtime'] = time();
authRule::create($data);
cache('authRule', NULL);
cache('authRuleList', NULL);
return $result = ['code'=>1,'msg'=>'权限添加成功!','url'=>url('adminRule')];
}else{
$nav = new Leftnav();
$arr = cache('authRuleList');
if(!$arr){
$authRule = authRule::all(function($query){
$query->order('sort', 'asc');
});
$arr = $nav->menu($authRule);
cache('authRuleList', $arr, 3600);
}
$this->assign('admin_rule',$arr);//权限列表
return $this->fetch();
}
}
public function ruleOrder(){
$auth_rule=db('auth_rule');
$data = input('post.');
if($auth_rule->update($data)!==false){
cache('authRuleList', NULL);
cache('authRule', NULL);
return $result = ['code'=>1,'msg'=>'排序更新成功!','url'=>url('adminRule')];
}else{
return $result = ['code'=>0,'msg'=>'排序更新失败!'];
}
}
//设置权限菜单显示或者隐藏
public function ruleState(){
$id=input('post.id');
$menustatus=input('post.menustatus');
if(db('auth_rule')->where('id='.$id)->update(['menustatus'=>$menustatus])!==false){
cache('authRule', NULL);
cache('authRuleList', NULL);
return ['status'=>1,'msg'=>'设置成功!'];
}else{
return ['status'=>0,'msg'=>'设置失败!'];
}
}
//设置权限是否验证
public function ruleTz(){
$id=input('post.id');
$authopen=input('post.authopen');
if(db('auth_rule')->where('id='.$id)->update(['authopen'=>$authopen])!==false){
cache('authRule', NULL);
cache('authRuleList', NULL);
return ['status'=>1,'msg'=>'设置成功!'];
}else{
return ['status'=>0,'msg'=>'设置失败!'];
}
}
public function ruleDel(){
authRule::destroy(['id'=>input('param.id')]);
cache('authRule', NULL);
cache('authRuleList', NULL);
return $result = ['code'=>1,'msg'=>'删除成功!'];
}
public function ruleEdit(){
if(request()->isPost()) {
$datas = input('post.');
if(authRule::update($datas)) {
cache('authRule', NULL);
cache('authRuleList', NULL);
return json(['code' => 1, 'msg' => '保存成功!', 'url' => url('adminRule')]);
} else {
return json(['code' => 0, 'msg' =>'保存失败!']);
}
}else{
$admin_rule = authRule::get(function($query){
$query->where(['id'=>input('id')])->field('id,href,title,icon,sort,menustatus');
});
$this->assign('rule',$admin_rule);
return $this->fetch();
}
}
//小饕点餐运营人员管理
public function operateList(){
if(Request::isAjax()){
//获取运营人员列表
$list= file_get_contents('http://xt04.colorcun.com/index.php?service=App.User.GetOperateList');
$array = json_decode($list,TRUE);
foreach ($array['data'] as $key=>$val){
if($val['status']==1){
$array['data'][$key]['status'] = "运营人员";
}
$array['data'][$key]['cdate'] = date("Y-m-d H:i:s",$val['cdate']);
}
if($array['ret']==200){
$result = ['code'=>0,'msg'=>'获取成功!','data'=>$array['data'],'rel'=>1];
}
return $result;
}
return view();
}
//增加运营管理人员
public function operateAdd(){
//缓存60秒 不请求。
$arr = cache('qrcod_url');
if(!$arr){
$arr= file_get_contents("http://xt.colorcun.com/WechatApi/WechatApi/CreateOperateQrcode/");
$arr = json_decode($arr,TRUE);
cache('qrcod_url',$arr, 50);
}
$this->assign('qrcode_path',$arr['data']);
return view();
}
//增加运营管理人员
public function operateDel(){
if(Request::isAjax()){
$data = Request::except('file');
$arr= file_get_contents("http://xt04.colorcun.com/index.php?service=App.Seller.DelSellerInfo&id=".$data['id']);
$arr = json_decode($arr,TRUE);
if($arr['data'][0]==1){
$result['code'] = 1;
$result['msg'] = '删除成功!';
}else{
$result['code'] = 0;
$result['msg'] = '请求错误!';
}
return $result;
}
return view();
}
}