model = model("AuthRule"); $this->relationModel = model("SpecialMenuRelation"); $this->channelMenuModel = model("ChannelMenuList"); } /** * 查看 */ public function index() { $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $maps = [ 'is_special' => ['eq', 1] ]; $total = $this->model ->field("auth_rule.*, special_menu_relation.channel_id") ->join('special_menu_relation smr', 'smr.auth_rule_id=auth_rule.id', 'left') ->where($where) ->where($maps) ->order($sort, $order) ->count(); $list = $this->model ->field("auth_rule.*, special_menu_relation.channel_id") ->join('special_menu_relation smr', 'smr.auth_rule_id=auth_rule.id', 'left') ->where($where) ->where($maps) ->order($sort, $order) ->limit($offset, $limit) ->select(); if ($list) { foreach ($list as $row) { //查询关联的角色 $groupRows = model("AuthGroup")->field("id")->where('FIND_IN_SET(:id,rules)',['id' => $row['id']])->select(); $row['group_id'] = $groupRows ? implode(',', array_column($groupRows, 'id')) : ''; } } $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } public function operate() { if ($this->request->isAjax()) { $authRuleId = $this->request->param('auth_rule_id'); $channelIds = $this->request->param('channel_ids'); $operate = $this->request->param('method'); $row = $this->relationModel->where('auth_rule_id', 'eq', $authRuleId)->find(); if ($row) { //更新 $menuRow = $this->channelMenuModel->where('id', 'eq', $row['channel_id'])->find(); $hasChannelIds = explode(',', $menuRow['channel_id']); if ($hasChannelIds) { if ($operate == 'add') { if ($channelIds == "*") { $allIdsArr = model('AuthGroup') ->join('auth_group_access','auth_group.id=auth_group_access.group_id') ->field('uid')->where('FIND_IN_SET(:id,rules)',['id' => $authRuleId])->select(); $updateChannelIds = $allIdsArr ? implode(',', array_column($allIdsArr, 'uid')) : "*"; } else { $channelIds = explode(',', $channelIds); $updateChannelIds = implode(',', array_unique(array_merge($hasChannelIds, $channelIds))); } } else { if ($channelIds == "*") { $updateChannelIds = ''; } else { $channelIds = explode(',', $channelIds); $updateChannelIdsArr = array_diff($hasChannelIds, $channelIds); $updateChannelIds = ''; if ($updateChannelIdsArr) { $updateChannelIds = implode(',', $updateChannelIdsArr); } } } } model("ChannelMenuList")->update(['channel_id' => $updateChannelIds, 'updatetime' => time()], ['id' => $menuRow['id']]); } else { //新增 $insertId = $this->channelMenuModel->allowField(true)->insertGetId([ 'channel_id' => $channelIds, 'updatetime' => time(), 'createtime' => time() ]); $data = [ 'auth_rule_id' => $authRuleId, 'channel_id' => $insertId, 'updatetime' => time(), 'createtime' => time(), ]; $this->relationModel->allowField(true)->insertGetId($data); } } exit(json_encode(['code' => 200])); } /** * 导入 */ public function import() { if ($this->request->isPost()) { $params = $this->request->param('row/a'); if (!$params['image']) { $this->error(__('Parameter %s can not be empty', 'file')); } $id = $params['ids']; $rule_id = $params['rule_id']; if (!$id || !$rule_id) { $this->error(__('缺少参数')); } $filePath = ROOT_PATH . DS . 'public' . DS . $params['image']; if (!is_file($filePath)) { $this->error(__('No results were found')); } $PHPReader = new \PHPExcel_Reader_Excel2007(); if (!$PHPReader->canRead($filePath)) { $PHPReader = new \PHPExcel_Reader_Excel5(); if (!$PHPReader->canRead($filePath)) { $PHPReader = new \PHPExcel_Reader_CSV(); if (!$PHPReader->canRead($filePath)) { $this->error(__('Unknown data format')); } } } $PHPExcel = $PHPReader->load($filePath); //加载文件 $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表 //$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号 $allRow = $currentSheet->getHighestRow(); //取得一共有多少行 $maxColumnNumber = 3; for ($currentRow = 1; $currentRow <= 1; $currentRow++) { for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); $fields[] = $val; } } $fieldArr = ['id' => 'id', '昵称' => 'nickname', '用户名(账号)' => 'username']; $insert = []; for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) { $values = []; for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); $values[] = is_null($val) ? '' : $val; } $row = []; $temp = array_combine($fields, $values); foreach ($temp as $k => $v) { if (isset($fieldArr[$k]) && $k !== '') { $row[$fieldArr[$k]] = $v; } } if ($row) { $insert[] = $row; } } $list = []; if ($insert) { //进行过滤 $groupRows = model("AuthGroup")->field("id")->where('FIND_IN_SET(:id,rules)',['id' => $rule_id])->select(); $groupIds = array_column($groupRows, 'id'); foreach ($insert as $item) { if ($item['id']) { $admin = model("Admin") ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner") ->where("admin.id", 'eq', $item['id']) ->where('auth_group_access.group_id', 'in', implode(',', $groupIds)) ->find(); } else if ($item['username']) { $admin = model("Admin") ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner") ->where("admin.username", 'eq', $item['username']) ->where('auth_group_access.group_id', 'in', implode(',', $groupIds)) ->find(); } else if ($item['nickname']) { $admin = model("Admin") ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner") ->where("admin.nickname", 'eq', $item['nickname']) ->where('auth_group_access.group_id', 'in', implode(',', $groupIds)) ->find(); } if ($admin) { $list[] = $item; } } } if (!$list) { $this->error(__('No rows were updated')); } try { $channelIds = array_filter(array_unique(array_column($list, 'id'))); $menuRow = $this->channelMenuModel->where('id', 'eq', $id)->find(); $hasChannelIds = explode(',', $menuRow['channel_id']); $hastotal = 0; if ($hasChannelIds) { $hastotal = count($hasChannelIds); $total = count(array_unique(array_merge($hasChannelIds, $channelIds))); if ($hastotal == $total) { $this->error(__('No rows were updated')); } $updateChannelIds = implode(',', array_unique(array_merge($hasChannelIds, $channelIds))); model("ChannelMenuList")->update(['channel_id' => $updateChannelIds, 'updatetime' => time()], ['id' => $menuRow['id']]); } else { $total = count($channelIds); $insertId = $this->channelMenuModel->allowField(true)->insertGetId([ 'channel_id' => $channelIds, 'updatetime' => time(), 'createtime' => time() ]); $data = [ 'auth_rule_id' => $rule_id, 'channel_id' => $insertId, 'updatetime' => time(), 'createtime' => time(), ]; $this->relationModel->allowField(true)->insertGetId($data); } } catch (\think\exception\PDOException $exception) { $this->error($exception->getMessage()); } $successTotal = $total - $hastotal; $this->success("本次上传共关联{$successTotal}个账号"); } $this->assign('ids', $this->request->param('ids')); $this->assign('rule_id', $this->request->param('rule_id')); return $this->view->fetch(); } }