From e29555326fbb3e8ea4960381ddebf5d74b575947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=93=9C=E5=93=A5?= Date: Sat, 31 Jul 2021 22:54:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9admin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/AdminInfo.php | 64 ++++++------ app/admin/controller/AuthGroup.php | 3 - app/admin/controller/AuthGroupAccess.php | 7 +- app/admin/controller/AuthRule.php | 6 +- app/admin/controller/Index.php | 68 +++++-------- app/admin/model/Admin.php | 119 +++++++++++++++++------ app/admin/model/AuthGroupAccess.php | 34 ++++++- app/admin/model/AuthRule.php | 21 ++-- app/admin/validate/SetPassword.php | 2 +- app/admin/view/auth_rule/index.html | 4 +- app/admin/view/index/index.html | 4 +- app/tools/controller/OnLine.php | 10 +- 12 files changed, 201 insertions(+), 141 deletions(-) diff --git a/app/admin/controller/AdminInfo.php b/app/admin/controller/AdminInfo.php index f23a9bbb..b5b0f033 100644 --- a/app/admin/controller/AdminInfo.php +++ b/app/admin/controller/AdminInfo.php @@ -16,25 +16,8 @@ class AdminInfo extends AdminBase { // 获取管理员信息 $ad = new AD; - $list = AD::where('id', $id) - ->field('id, xingming, sex, shengri, username, teacher_id, school_id, phone, denglucishu, lastip, ip, lasttime, quanpin, shoupin, zhiwu_id, zhicheng_id, biye, zhuanye, xueli_id, worktime, create_time, update_time') - ->with([ - 'adSchool' => function($query){ - $query->field('id, title'); - }, - 'adZhiwu' => function($query){ - $query->field('id, title'); - }, - 'adZhicheng' => function($query){ - $query->field('id, title'); - }, - 'adXueli' => function($query){ - $query->field('id, title'); - }, - ]) - ->find(); + $list = AD::searchOne($id); $list['webtitle'] = '帐号信息'; - $list->groupnames = $ad->getGroupnames($id); // 模板赋值 $this->view->assign('list', $list); @@ -79,7 +62,7 @@ class AdminInfo extends AdminBase return json(['msg' => $msg, 'val' => 0]);; } - // 获取用户名 + // 获取服务器上的密码 $serpassword = AD::where('id', $id)->value('password'); // 实例化加密类 @@ -89,7 +72,7 @@ class AdminInfo extends AdminBase if(!$check) { - $data = ['msg' => '旧密码错误', 'val' => 0]; + $data = ['msg' => '原密码错误', 'val' => 0]; return json($data); } @@ -149,7 +132,9 @@ class AdminInfo extends AdminBase ,'hjshijian' ,'update_time' ]); - $data = reSetObject($data, $src); + $src['cnt'] = true; + $cnt = $rongyu->srcTeacherRongyu($src['teacher_id'])->count(); + $data = reset_data($data, $cnt); return json($data); } @@ -170,7 +155,9 @@ class AdminInfo extends AdminBase // 查询数据 $keti = new \app\keti\model\KetiInfo; $data = $keti->srcTeacherKeti($src['teacher_id']); - $data = reSetObject($data, $src); + $src['cnt'] = true; + $cnt = $keti->srcTeacherKeti($src['teacher_id'])->count(); + $data = reset_data($data, $cnt); return json($data); } @@ -192,7 +179,9 @@ class AdminInfo extends AdminBase // 查询数据 $bzr = new \app\teach\model\BanZhuRen; $data = $bzr->srcTeacher($src); - $data = reSetObject($data, $src); + $src['cnt'] = true; + $cnt = $bzr->srcTeacher($src)->count(); + $data = reSetObject($data, $cnt); return json($data); } @@ -201,16 +190,25 @@ class AdminInfo extends AdminBase // 修改信息 public function edit() { - $id = session('user_id'); - // 获取用户信息 - $list['data'] = AD::where('id', $id) - ->field('id, xingming, quanpin, shoupin, username, shengri, sex, phone, zhiwu_id, zhicheng_id, xueli_id, biye, zhuanye, worktime') - ->with([ - 'adSchool'=>function($query){ - $query->field('id, jiancheng'); - } - ]) - ->find(); + $id = session('user_id'); + // 获取用户信息 + $ad = new AD; + $list['data'] = $ad->searchOne($id) + ->visible([ + 'id' + ,'xingming' + ,'quanpin' + ,'shoupin' + ,'username' + ,'shengri' + ,'sex' + ,'phone' + ,'zhicheng_id' + ,'xueli_id' + ,'biye' + ,'zhuanye' + ,'worktime' + ]); // 设置页面标题 $list['set'] = array( diff --git a/app/admin/controller/AuthGroup.php b/app/admin/controller/AuthGroup.php index 526385ec..53542b97 100755 --- a/app/admin/controller/AuthGroup.php +++ b/app/admin/controller/AuthGroup.php @@ -67,7 +67,6 @@ class AuthGroup extends AdminBase $ruleSelect = array(); $auth = $rule->digui($ruleList, $ruleSelect); # 递归获取所有权限 $list['auth'] = $auth; - // halt($auth); // 模板赋值 $this->view->assign('list', $list); @@ -125,9 +124,7 @@ class AuthGroup extends AdminBase $aglist = AG::where('id', $id) ->field('id, title, miaoshu, rules') ->find(); - $ruleSelect = explode(",", $aglist->rules); - $a = array_search(1, $ruleSelect); $auth = $rule->digui($ruleList, $ruleSelect, 0); $list['auth'] = $auth; diff --git a/app/admin/controller/AuthGroupAccess.php b/app/admin/controller/AuthGroupAccess.php index b1319fb0..24489fc1 100755 --- a/app/admin/controller/AuthGroupAccess.php +++ b/app/admin/controller/AuthGroupAccess.php @@ -10,7 +10,8 @@ use app\admin\model\AuthGroupAccess as AGA; class AuthGroupAccess extends AdminBase { - // 用户列表 + + // 用户列表 public function index($group_id) { // 设置要给模板赋值的信息 @@ -44,7 +45,9 @@ class AuthGroupAccess extends AdminBase // 根据数据查询信息 $aga = new AGA; $data = $aga->search($src); - $data = reSetObject($data, $src); + $src['cnt'] = true; + $cnt = $aga->search($src)->count(); + $data = reset_data($data, $cnt); return json($data); } diff --git a/app/admin/controller/AuthRule.php b/app/admin/controller/AuthRule.php index 7d092114..b64929d6 100755 --- a/app/admin/controller/AuthRule.php +++ b/app/admin/controller/AuthRule.php @@ -43,7 +43,9 @@ class AuthRule extends AdminBase // 根据条件查询数据 $ar = new AR; $data = $ar->search($src); - $data = reSetObject($data, $src); + $src['cnt'] = true; + $cnt = $ar->search($src)->count(); + $data = reset_data($data, $cnt); return json($data); } @@ -148,7 +150,7 @@ class AuthRule extends AdminBase } $data = AR::where('id', $id)->update($list); - $data>=0 ? $data = ['msg' => '更新成功', 'val' => 1] + $data >= 0 ? $data = ['msg' => '更新成功', 'val' => 1] : $data = ['msg' => '数据处理错误', 'val' => 0]; // 返回信息 diff --git a/app/admin/controller/Index.php b/app/admin/controller/Index.php index 8363efe6..0a041eaf 100755 --- a/app/admin/controller/Index.php +++ b/app/admin/controller/Index.php @@ -228,7 +228,21 @@ class Index extends AdminBase $id = request()->delete('id'); $id = explode(',', $id); - $data = AD::destroy($id); + + // 启动事务 + \think\facade\Db::startTrans(); + try { + AD::destroy($id); + $aga = new \app\admin\model\AuthGroupAccess; + $id = $aga->where('uid', 'in', $id)->column('id'); + $aga::destroy($id); + \think\facade\Db::commit(); + $data = true; + } catch (\Exception $e) { + // 回滚事务 + \think\facade\Db::rollback(); + $data = false; + } // 根据更新结果设置返回提示信息 $data ? $data = ['msg' => '删除成功', 'val' => 1] @@ -338,9 +352,12 @@ class Index extends AdminBase ->only([ 'searchval' => '' ,'school_id' => '' + ,'teacher_id' => '' + ,'page' => 1 + ,'limit' => 10 ,'field' => 'id' ,'order' => 'desc' - ,'teacher_id' => '' + ,'cnt' => false ], 'POST'); $ad = new AD(); @@ -358,7 +375,9 @@ class Index extends AdminBase ,'id' => $value->id ]; } - $data = reSetArray($data, $src); + $src['cnt'] = true; + $cnt = $ad->strSrcTeachers($src)->count(); + $data = reset_data($data, $cnt); return json($data); } @@ -373,29 +392,10 @@ class Index extends AdminBase 'searchval' => '' ,'id' => '' ], 'POST'); - $src = [ - 'searchval' => '' - ,'id' => '' - ]; - $src = array_cover($srcfrom, $src); $ad = new AD(); - $list = $ad->where('username', $src['searchval']) - ->find(); - $data = ['msg' => '用户名已经存在!', 'val' => 0]; + $data = $ad->onlyUsername($srcfrom); - if($list) - { - if($src['id'] > 0) - { - - if($src['id'] == $list->id){ - $data = ['msg' => '', 'val' => 1]; - } - } - }else{ - $data = ['msg' => '', 'val' => 1]; - } return json($data); } @@ -409,30 +409,10 @@ class Index extends AdminBase 'searchval' => '' ,'id' ], 'POST'); - $src = [ - 'searchval' => '' - ,'id' => '' - ]; - $src = array_cover($srcfrom, $src); $ad = new AD(); - $list = $ad->where('phone', $src['searchval']) - ->find(); + $data = $ad->onlePhone($srcfrom); - // 根据更新结果设置返回提示信息 - $data = ['msg' => '电话号码已经存在!', 'val' => 0]; - if($list) - { - if($src['id'] > 0) - { - - if($src['id'] == $list->id){ - $data = ['msg' => '', 'val' => 1]; - } - } - }else{ - $data = ['msg' => '', 'val' => 1]; - } return json($data); } } diff --git a/app/admin/model/Admin.php b/app/admin/model/Admin.php index 13456b1f..08a77a0b 100755 --- a/app/admin/model/Admin.php +++ b/app/admin/model/Admin.php @@ -91,10 +91,11 @@ class Admin extends BaseModel public function srcAuth($user_id) { // 查询权限 - $data = self::where('id', $user_id) - ->find(); + $group = self::where('id', $user_id) + ->glGroup() + ->where('status', 1) + ->column(['rules']); // 整理权限 - $group = $data->glGroup->where('status', 1)->column(['rules']); $rules = array_unique(explode(',', implode(',', $rules))); return $rules; @@ -105,25 +106,26 @@ class Admin extends BaseModel public function searchOne($id) { $adminInfo = $this - ->where('id', $id) - ->with([ - 'adSchool' => function($query){ - $query->field('id, title'); - }, - 'adZhiwu' => function($query){ - $query->field('id, title'); - }, - 'adZhicheng' => function($query){ - $query->field('id, title'); - }, - 'adXueli' => function($query){ - $query->field('id, title'); - }, - 'glGroup' - ]) - ->append(['groupnames']) - ->hidden(['password', 'delete_time', 'beizhu']) - ->find(); + ->where('id', $id) + ->with([ + 'adSchool' => function($query){ + $query->field('id, title'); + }, + 'adZhiwu' => function($query){ + $query->field('id, title'); + }, + 'adZhicheng' => function($query){ + $query->field('id, title'); + }, + 'adXueli' => function($query){ + $query->field('id, title'); + }, + 'glGroup' + ]) + ->append(['groupnames']) + ->hidden(['password', 'delete_time', 'beizhu']) + ->find(); + return $adminInfo; } @@ -134,6 +136,11 @@ class Admin extends BaseModel $src = [ 'searchval' => '' ,'school_id' => '' + ,'page' => 1 + ,'limit' => 10 + ,'field' => 'id' + ,'order' => 'desc' + ,'cnt' => false ]; $src = array_cover($srcfrom, $src); $src['searchval'] = trim($src['searchval']); @@ -159,6 +166,11 @@ class Admin extends BaseModel } ] ) + ->when($src['cnt'] == false, function ($query) use($src) { + $query + ->page($src['page'], $src['limit']) + ->order([$src['field'] => $src['order']]); + }) ->append(['age']) ->select(); return $list; @@ -218,12 +230,12 @@ class Admin extends BaseModel $i ++; } - // 启动事务 \think\facade\Db::startTrans(); try { - @$data = $this->saveAll($teacherInfo) or halt("Database Connect Error"); + $data = $this->saveAll($teacherInfo); $success = $data->count(); + \think\facade\Db::commit(); } catch (\Exception $e) { // 回滚事务 \think\facade\Db::rollback(); @@ -231,7 +243,6 @@ class Admin extends BaseModel $err[] = $e->getMessage(); } - // 整理返回信息 $data['success'] = '共有'. $cnt .'条记录,成功导入' . $success . '条记录。'; if($cnt == $success) { @@ -257,6 +268,57 @@ class Admin extends BaseModel } + // 查询用户名是否唯一 + public function onlyUsername($srcfrom) + { + // 初始值 + $src = [ + 'searchval' => '' + ,'id' => '' + ]; + $src = array_cover($srcfrom, $src); + + $list = $this::withTrashed() + ->where('username', $src['searchval']) + ->find(); + $data = ['msg' => '用户名已经存在!', 'val' => 0]; + + if($list) + { + if($src['id'] == $list->id){ + $data = ['msg' => '', 'val' => 1]; + } + }else{ + $data = ['msg' => '', 'val' => 1]; + } + } + + // 查询用户名是否唯一 + public function onlyPhone($srcfrom) + { + // 初始值 + $src = [ + 'searchval' => '' + ,'id' => '' + ]; + $src = array_cover($srcfrom, $src); + + $list = $this::withTrashed() + ->where('phone', $src['searchval']) + ->find(); + $data = ['msg' => '手机号已经存在!', 'val' => 0]; + + if($list) + { + if($src['id'] == $list->id){ + $data = ['msg' => '', 'val' => 1]; + } + }else{ + $data = ['msg' => '', 'val' => 1]; + } + } + + // 关联用户组 public function glGroup() { @@ -267,7 +329,7 @@ class Admin extends BaseModel // 单位关联模型 public function adSchool() { - return $this->hasOne(\app\system\model\School::class, 'id', 'school_id'); + return $this->belongsTo(\app\system\model\School::class, 'school_id', 'id'); } @@ -330,6 +392,7 @@ class Admin extends BaseModel }else{ $str = '未知'; } + return $str; } @@ -433,7 +496,7 @@ class Admin extends BaseModel // 职务权限 public function zwAuth() { - $adInfo = \app\facade\OnLine::myInfo(); + $adInfo = $this->searchOne(session('user_id')); $banji_id = array(); // 获取职务权限 $zhiwu_array = array(10703, 10705); @@ -443,7 +506,7 @@ class Admin extends BaseModel $bjList = $bj->where('school_id', $adInfo->school_id) ->where('status', 1) ->column('id'); - $banji_id = array_merge($banji_id, $bjList); + $banji_id = array_merge($banji_id, $bjList); # 这个地方为什么要合并? } return $banji_id; diff --git a/app/admin/model/AuthGroupAccess.php b/app/admin/model/AuthGroupAccess.php index b715e781..35f8754d 100755 --- a/app/admin/model/AuthGroupAccess.php +++ b/app/admin/model/AuthGroupAccess.php @@ -8,6 +8,20 @@ use think\model\Pivot; class AuthGroupAccess extends Pivot { + // 设置字段信息 + protected $schema = [ + 'id' => 'int' + ,'uid' => 'int' + ,'group_id' => 'int' + ,'create_time' => 'int' + ,'update_time' => 'int' + ,'delete_time' => 'int' + ]; + + // 开启软删除 + use \think\model\concern\SoftDelete; + protected $deleteTime = 'delete_time'; + // 根据角色ID查询用户 public function search($srcfrom) { @@ -15,10 +29,16 @@ class AuthGroupAccess extends Pivot 'group_id' => 0 ,'searchval' => '' ,'school_id' => array() + ,'page' => 1 + ,'limit' => 10 + ,'field' => 'id' + ,'order' => 'desc' + ,'cnt' => false ]; + // 用新值替换初始值 $src = array_cover($srcfrom, $src); - $src['school_id'] = strToArray($src['school_id']); + $src['school_id'] = str_to_array($src['school_id']); $data = $this ->where('group_id', $src['group_id']) @@ -43,16 +63,22 @@ class AuthGroupAccess extends Pivot }) ->where('uid','<>', session('userid')) + ->where('uid', '>', 2) ->with([ 'jsUser' => function($query) use($src) { - $query->field('id,xingming,school_id,username') + $query->field('id, xingming, school_id, username') ->with([ 'adSchool' => function($q){ - $q->field('id,title,jiancheng'); + $q->field('id, title, jiancheng'); } ]); } ]) + ->when($src['cnt'] == false, function ($query) use($src) { + $query + ->page($src['page'], $src['limit']) + ->order([$src['field'] => $src['order']]); + }) ->select(); return $data; @@ -62,6 +88,6 @@ class AuthGroupAccess extends Pivot // 考试设置关联表 public function jsUser() { - return $this->belongsTo('Admin', 'uid', 'id'); + return $this->belongsTo(Admin::class, 'uid', 'id'); } } \ No newline at end of file diff --git a/app/admin/model/AuthRule.php b/app/admin/model/AuthRule.php index 3d6c04d7..738d4ee2 100755 --- a/app/admin/model/AuthRule.php +++ b/app/admin/model/AuthRule.php @@ -49,17 +49,16 @@ class AuthRule extends BaseModel ->when(strlen($src['status']) > 0, function($query) use($src){ $query->where('status', $src['status']); }) - ->when($src['cnt'] == false, function ($query) { + ->with([ + 'authPid' => function($query){ + $query->field('id, title'); + } + ]) + ->field('id, name, title, condition, paixu, ismenu, url, pid, type, status') + ->when($src['cnt'] == false, function ($query) use($src) { $query - ->with([ - 'authPid' => function($query){ - $query->field('id, title'); - } - ]) - ->field('id, name, title, condition, paixu, ismenu, url, pid, type, status'); - }, function ($query) { - $query - ->field('id'); + ->page($src['page'], $src['limit']) + ->order([$src['field'] => $src['order']]); }) ->select(); @@ -136,7 +135,7 @@ class AuthRule extends BaseModel // 父级菜单数据模型关联 public function authPid() { - return $this->belongsTo('AuthRule', 'pid', 'id'); + return $this->belongsTo(AuthRule::class, 'pid', 'id'); } diff --git a/app/admin/validate/SetPassword.php b/app/admin/validate/SetPassword.php index edc1d2a7..2afa056d 100755 --- a/app/admin/validate/SetPassword.php +++ b/app/admin/validate/SetPassword.php @@ -15,7 +15,7 @@ class SetPassword extends Validate protected $rule = [ 'oldpassword|原密码' => 'require', 'newpassword|新密码' => ['require', 'regex' => '/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){6,20}$/'], - 'newpassword2|两次密码' => 'require|confirm:newpassword', + 'newpassword2|第二次密码' => 'require|confirm:newpassword', ]; /** diff --git a/app/admin/view/auth_rule/index.html b/app/admin/view/auth_rule/index.html index c543ffcc..20a9316a 100755 --- a/app/admin/view/auth_rule/index.html +++ b/app/admin/view/auth_rule/index.html @@ -62,8 +62,8 @@ layui.use(['table', 'mytable', 'form'], function() { sort: true, templet: function(d) { var str = ''; - if (d.auth_pid) { - str = d.auth_pid.title; + if (d.authPid) { + str = d.authPid.title; } return str; } diff --git a/app/admin/view/index/index.html b/app/admin/view/index/index.html index a705fe3e..e718a399 100755 --- a/app/admin/view/index/index.html +++ b/app/admin/view/index/index.html @@ -62,9 +62,9 @@ layui.use(['table', 'mytable', 'form'], function() { [ //表头 { type: 'checkbox', fixed: 'left' }, { field: 'xingming', title: '姓名', sort: true, minWidth: 80, fixed: 'left' }, - { field: 'sex', title: '性别', minWidth: 100, sort: true }, + { field: 'sex', title: '性别', minWidth: 80, sort: true }, { field: "shengri", title: '生日', minWidth: 100, sort: true }, - { field: 'username', title: '帐号', sort: true }, + { field: 'username', title: '帐号', minWidth: 100, sort: true }, { field: 'school_id', title: '所在单位', diff --git a/app/tools/controller/OnLine.php b/app/tools/controller/OnLine.php index 4a83976c..4af1632a 100755 --- a/app/tools/controller/OnLine.php +++ b/app/tools/controller/OnLine.php @@ -28,16 +28,8 @@ class OnLine // 获取用户信息 public function myInfo() { - $id = session('user_id'); $ad = new \app\admin\model\Admin; - $adInfo = $ad->where('id', $id) - ->field('zhiwu_id, school_id') - ->with([ - 'adSchool' => function($query){ - $query->field('id, jiancheng, jibie_id'); - }, - ]) - ->find(); + $adInfo = $ad->searchOne(session('user_id')); return $adInfo; }