123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\controller;
- use app\admin\BaseController;
- use app\admin\model\AdminGroup;
- use app\admin\validate\GroupCheck;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\facade\View;
- use app\admin\model\Department as DepartmentModel;
- class Role extends BaseController
- {
- protected $login_admin;
-
- public function initialize(){
- parent::initialize();
- $this->login_admin = get_login_admin();
- }
- public function index()
- {
- if (request()->isAjax()) {
- $param = get_params();
- $where = array();
-
- if (!empty($param['keywords'])) {
- $where[] = ['id|title|desc', 'like', '%' . $param['keywords'] . '%'];
- }
- $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
- $unit_name = $this->login_admin['unit_name'];
- $permission = $this->login_admin['permission'];
- $group_id = Db::name('AdminGroupAccess')->where('uid', $this->login_admin['id'])->value('group_id');
- $where_unit = array();
- $whereor = array();
- $whereor_ = array();
- if($permission != 1){
- $where_unit[] = [
- ['unit_name', '=', $unit_name],
- ];
- }else{
- if(get_login_admin('user_type') == -1){
- $whereor[] = [
-
- ['pid', '=', $group_id],
-
- ];
- }
- }
- $group = AdminGroup::with(['department','admin'])
- ->where($where)
- ->where($whereor)
- ->where($where_unit)
- ->order('create_time asc')
- ->paginate($rows, false, ['query' => $param])->toArray();
-
- // $group_mine = Db::name('AdminGroup')->where('id', $group_id)->select()->toArray();
- // dump($group);
- if(get_login_admin('user_type') == -1){
- $group_ = AdminGroup::with(['department','admin'])
- ->where('id', $group_id)
- ->find()
- ->toArray();
- $group_n[] = $group_;
-
- $group['data'] = array_merge($group_n, $group['data']);
- }
- // $group = array_merge($group_mine, $group);
- // halt($group);
-
- return table_assign(0, '', $group);
- } else {
- return view();
- }
- }
- //添加&编辑
- public function add()
- {
- $param = get_params();
- // halt($param);
- if (request()->isAjax()) {
-
- $ruleData = isset($param['rule']) ? $param['rule'] : 0;
- $param['rules'] = implode(',', $ruleData);
- $param['operator'] = get_login_admin('nickname');
- if (!empty($param['id']) && $param['id'] > 0) { //编辑
- // halt($param);
- if(get_login_admin('permission') == 0 || get_login_admin('user_type') == -1){
- $login_group_id = Db::name('AdminGroupAccess')->where('uid', get_login_admin('id'))->value('group_id');
- if($login_group_id == $param['id']){
- return to_assign(0, '不可修改自身权限');
- }
- }
- try {
- validate(GroupCheck::class)->scene('edit')->check($param);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- return to_assign(1, $e->getError());
- }
- //为了系统安全id为1的系统所有者管理组不允许修改
- if ($param['id'] == 1) {
- return to_assign(1, '为了系统安全,该管理组不允许修改');
- }
- Db::name('AdminGroup')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
- $field_id = $param['field_id'];
- Db::name('FieldRules')->where('id', $field_id)->update(['group_id' => $param['id']]);
- add_log('edit', $param['id'], $param);
- } else { //添加
- try {
- validate(GroupCheck::class)->scene('add')->check($param);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- return to_assign(1, $e->getError());
- }
-
- $group_id = Db::name('AdminGroupAccess')->where('uid', get_login_admin('id'))->value('group_id');
- if($this->login_admin['permission'] == 1){ //系统方添加
- if($param['unit_name'] != 0){ //添加单位权限组
- if($this->login_admin['user_type'] == -1){
- $param['pid'] = $group_id;
- }else{
- $param['pid'] = 0;
- }
- }else{ //添加系统权限组
- $param['pid'] = $group_id;
- }
-
- }else{
- $param['unit_name'] = $this->login_admin['unit_name'];
- $param['pid'] = $group_id;
- }
- // halt($param);
- $gid = Db::name('AdminGroup')->strict(false)->field(true)->insertGetId($param);
- $field_id = $param['field_id'];
- if($field_id != 0){
- Db::name('FieldRules')->where('id', $field_id)->update(['group_id' => $gid]);
- }else{
- $field_rules = '{"see_auth":"1","project_status":"1","project_num":"1","project_name":"1","project_time":"1","entrust_unit":"1","entrust_head":"1","entrust_phone":"1","review_unit":"1","review_head":"1","review_head_phone":"1","sent_review_unit":"1","sent_review_head":"1","sent_review_phone":"1","construction_unit":"1","construction_head":"1","construction_phone":"1","project_scale":"1","project_region":"1","engineering_category":"1","engineering_type":"1","fiscal_nature":"1","sent_review_cost":"1","preparation_amount":"1","sent_review_amount":"1","sent_review_approver_amount":"1","construction_approver_amount":"1","authorize_amount":"1","review_add_reduce_amount":"1","review_add_reduce_rate":"1","report_time":"1","charge_standard":"1","invoicing__amount":"1","invoicing__num":"1","operate_name":"1","operate_team_names":"1","project_dead_time":"1","create_time":"1"}';
- $field_rules_edit = '{"project_status_edit":"1","project_num_edit":"1","project_name_edit":"1","project_time_edit":"1","entrust_unit_edit":"1","entrust_head_edit":"1","entrust_phone_edit":"1","review_unit_edit":"1","review_head_edit":"1","review_head_phone_edit":"1","sent_review_unit_edit":"1","sent_review_head_edit":"1","sent_review_phone_edit":"1","construction_unit_edit":"1","construction_head_edit":"1","construction_phone_edit":"1","project_scale_edit":"1","project_region_edit":"1","engineering_category_edit":"1","engineering_type_edit":"1","fiscal_nature_edit":"1","sent_review_cost_edit":"1","preparation_amount_edit":"1","sent_review_amount_edit":"1","sent_review_approver_amount_edit":"1","construction_approver_amount_edit":"1","authorize_amount_edit":"1","review_add_reduce_amount_edit":"1","review_add_reduce_rate_edit":"1","report_time_edit":"1","charge_standard_edit":"1","invoicing__amount_edit":"1","invoicing__num_edit":"1","operate_name_edit":"1","operate_team_names_edit":"1","project_dead_time_edit":"1","create_time_edit":"1"}';
- $data = [
- 'group_id' => $gid,
- 'field_rules' => $field_rules,
- 'field_edit_rules' => $field_rules_edit,
- 'operator' => get_login_admin('nickname'),
- ];
- $field_id = Db::name('FieldRules')->strict(false)->field(true)->insertGetId($data);
- }
-
- add_log('add', $gid, $param);
- }
- //清除菜单\权限缓存
- clear_cache('adminMenu');
- return to_assign();
- } else {
-
- $id = isset($param['id']) ? $param['id'] : 0; //id为编辑的权限组的表id
- $rule = get_admin_rule();
-
- $group_id = Db::name('admin_group_access')->where('uid', get_login_admin('id'))->value('group_id');
-
- $rules = get_admin_group_info($group_id); //登录账号的权限组
-
-
- $role_rule = create_tree_list(0, $rule, $rules);
- $role_rule = self::remove_checked_false($role_rule);
-
- if ($id > 0) { //编辑
-
- if($group_id == $id){
- // return to_assign(0, '不可修改自身权限');
- if(get_login_admin('user_type') != -2){
- echo '<div style="text-align:center;color:red;margin-top:20%;">不可修改自身权限!</div>';
- exit;
- }
- }
- $pid = Db::name('AdminGroup')->where('id', $group_id)->value('pid');
- if(get_login_admin('user_type' != -2)){
- if($pid == Db::name('AdminGroup')->where('id', $id)->value('pid')){
- echo '<div style="text-align:center;color:red;margin-top:20%;">不可修改同级权限组!</div>';
- exit;
- }
- }
-
- while($pid != 0){
- if($id == $pid){
- // return to_assign(0, '不可修改上级权限');
- echo '<div style="text-align:center;color:red;margin-top:20%;">不可修改上级权限!</div>';
- exit;
- // break;
- }
- $pid = Db::name('AdminGroup')->where('id', $pid)->value('pid');
- }
- // halt("@@@@");
- $rules = get_admin_group_info($id); //编辑的权限组全部权限
- $edit_role_rule = create_tree_list(0, $rule, $rules);
- $edit_role_rule = self::remove_checked_false($edit_role_rule);
- $login_role_rule = $role_rule;
- // dump($edit_role_rule);
- $role_rule = self::diff_role_rules($login_role_rule, $edit_role_rule);
-
- $role = Db::name('AdminGroup')->where(['id' => $id])->find();
-
- View::assign('role', $role);
- } else { //新增
- // $group_id = Db::name('admin_group_access')->where('uid', get_login_admin('id'))->value('group_id');
- // $rules = get_admin_group_info($group_id); //登录账号的权限组
- // $role_rule = create_tree_list(0, $rule, $rules);
- // $role_rule = self::remove_checked_false($role_rule);
- }
- if($this->login_admin['permission'] == 1){
- $department = Db::name('department')->where('pid', 0)->field('id,title')->select()->toArray();
- $system = [
- 'id' => 0,
- 'title' => '系统方',
- ];
- $department[count($department)] = $system;
- View::assign('department', $department);
- }
- $field_id = Db::name('field_rules')->where('group_id', $id)->value('id');
- // dump($field_id);
- view::assign('field_id', $field_id);
- // dump($role_rule);
- if(get_login_admin('permission') == 1 || get_login_admin('user_type') == -1){
- for($i = 0; $i < count($role_rule); $i++){
- // dump($role_rule[$i]['name']);
- if($role_rule[$i]['name'] == '公司项目管理'){
- $role_rule[$i]['title'] = '项目管理--公司';
- }else if($role_rule[$i]['name'] == '财政局项目管理'){
- $role_rule[$i]['title'] = '项目管理--财政局';
- }else if($role_rule[$i]['name'] == '业主项目管理'){
- $role_rule[$i]['title'] = '项目管理--业主';
- }
- // dump($role_rule[$i]['title']);
- if($role_rule[$i]['name'] == '项目看板'){
- for($j = 0; $j < count($role_rule[$i]['children']); $j++){
- if($role_rule[$i]['children'][$j]['name'] == '财政局项目看板'){
- $role_rule[$i]['children'][$j]['title'] = '项目看板--财政局';
- }else if($role_rule[$i]['children'][$j]['name'] == '公司项目看板'){
- $role_rule[$i]['children'][$j]['title'] = '项目看板--公司';
- }
- }
- }
- if($role_rule[$i]['name'] == '请款详情'){
- for($j = 0; $j < count($role_rule[$i]['children']); $j++){
- if($role_rule[$i]['children'][$j]['name'] == '财政局请款详情'){
- $role_rule[$i]['children'][$j]['title'] = '请款详情--财政局';
- }else if($role_rule[$i]['children'][$j]['name'] == '公司请款详情'){
- $role_rule[$i]['children'][$j]['title'] = '请款详情--公司';
- }
- }
- }
- }
- }
-
- // dump($role_rule);
- View::assign('permission', $this->login_admin['permission']);
- View::assign('role_rule', $role_rule);
- View::assign('id', $id);
- return view();
-
- }
- }
- //删除
- public function delete()
- {
- $id = get_params("id");
- if ($id == 1) {
- return to_assign(1, "该组是系统所有者,无法删除");
- }
- if (Db::name('AdminGroup')->delete($id) !== false) {
- add_log('delete', $id, []);
- return to_assign(0, "删除角色成功");
- } else {
- return to_assign(1, "删除失败");
- }
- }
- // 消除checked为false的权限
- public function remove_checked_false($role_rule){
- foreach($role_rule as $key => $value){
- if($role_rule[$key]['checked'] == false){
- unset($role_rule[$key]);
- continue;
- }
- // 只能用 $role_rule[$key]['children'] 不能用 $value['children]
- if(isset($role_rule[$key]['children'])){
- $role_rule[$key]['children'] = self::remove_checked_false($role_rule[$key]['children']);
- }
- }
- return $role_rule;
- }
- // 修改checked的值 true => false
- public function change_checked($arr){
- foreach($arr as $key => $value){
- $arr[$key]['checked'] = false;
- if(isset($arr[$key]['children'])){
- $arr[$key]['children'] = self::change_checked($arr[$key]['children']);
- }
- }
- return $arr;
- }
- // 对比两个权限组
- public function diff_role_rules($login_rules, $edit_rules){
- // dump($login_rules);
- // dump($edit_rules);
- $data_diff = array_diff_key($login_rules, $edit_rules);
- $data_diff = self::change_checked($data_diff);
- $login_rules = array_diff_key($login_rules, $data_diff);
- // dump($login_rules);
- try{
- foreach($edit_rules as $key => $value){
- if(isset($edit_rules[$key]['children'])){
- $edit_rules[$key]['children'] = self::diff_role_rules($login_rules[$key]['children'], $edit_rules[$key]['children']);
- }else if(isset($login_rules[$key]['children'])){
- $edit_rules[$key]['children'] = self::change_checked($login_rules[$key]['children']);
- }
- }
- }catch(\Exception $e){
- echo '<div style="text-align:center;color:red;margin-top:20%;">权限选择有误,没有相关权限!</div>';
- exit;
- }
-
- $edit_rules = $edit_rules + $data_diff;
- ksort($edit_rules);
- return $edit_rules;
- }
-
- }
|