model = new \app\admin\model\Users; } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); 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) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); $list = collection($list)->toArray(); $result = array("total" => $total, "rows" => $list); return json($result); } // 设置下载模板文件的地址 $path = config('site.url').'\template\template.xls'; return $this->view->fetch('index', ['path'=>$path]); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException(true)->validate($validate); } $result = $this->model->allowField(true)->save($params); Db::commit(); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were inserted')); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 编辑 */ public function edit($ids = null) { $row = $this->model->get($ids); // 查出子单位数据 $type = Db::name('type')->where('t_id',$row['user_type'])->find(); // 查出父单位数据 $f_type = Db::name('type')->where('t_id',$type['f_id'])->find(); $row['type_fname'] = $f_type['name']; // 父单位名称 $row['type_zname'] = $type['name']; // 子单位名称 $row['f_id'] = $type['f_id']; // 父单位id if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException(true)->validate($validate); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were updated')); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign("row", $row); return $this->view->fetch(); } /** * 简历修改或添加 */ public function resume($ids = NUll) { if ($this->request->isPost()) { $data = input('post.'); if ($data['r_id'] == '') { // 添加简历 $add = Db::name('resume')->insert($data); if ($add) { return $this->success('添加成功'); } else { return $this->success('添加失败'); } } else { $edit = Db::name('resume')->where('r_id',$data['r_id'])->update($data); if ($edit) { return $this->success('修改成功'); } else { return $this->success('修改失败'); } } } $id = input('ids'); $data = Db::name('resume')->where('user_id',$id)->find(); if ($data == NULL) { $data = array( 'user_id' => $id, 'r_resume' => '', 'r_rewards' => '', 'r_results' => '', 'r_id' => '', ); } return $this->fetch('resume',['data' => $data]); } /** * 表格导入 */ public function import() { $file = $this->request->request('file'); if (!$file) { $this->error(__('Parameter %s can not be empty', 'file')); } $filePath = ROOT_PATH . DS . 'public' . DS . $file; if (!is_file($filePath)) { $this->error(__('No results were found')); } //实例化reader $ext = pathinfo($filePath, PATHINFO_EXTENSION); if (!in_array($ext, ['csv', 'xls', 'xlsx'])) { $this->error(__('Unknown data format')); } $reader = new Xls(); //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment'; $table = $this->model->getQuery()->getTable(); $database = \think\Config::get('database.database'); $fieldArr = []; $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]); foreach ($list as $k => $v) { if ($importHeadType == 'comment') { $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME']; } else { $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME']; } } //加载文件 $insert = []; try { if (!$PHPExcel = $reader->load($filePath)) { $this->error(__('Unknown data format')); } $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表 $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号 $allRow = $currentSheet->getHighestRow(); //取得一共有多少行 $maxColumnNumber = Coordinate::columnIndexFromString($allColumn); //处理图片 $imageFilePath=ROOT_PATH.'public/uploads/images/' ;//图片保存目录 if (!file_exists ( $imageFilePath )) { mkdir("$imageFilePath", 0777, true); } //处理图片 foreach($currentSheet->getDrawingCollection() as $img) { list($startColumn,$startRow)= Coordinate::coordinateFromString($img->getCoordinates());//获取图片所在行和列 $imageFileName = Random::uuid(); switch($img->getMimeType()) { case 'image/jpg': case 'image/jpeg': $imageFileName.='.jpg'; imagejpeg($img->getImageResource(),$imageFilePath.$imageFileName); break; case 'image/gif': $imageFileName.='.gif'; imagegif($img->getImageResource(),$imageFilePath.$imageFileName); break; case 'image/png': $imageFileName.='.png'; imagepng($img->getImageResource(),$imageFilePath.$imageFileName); break; } $currentSheet->setCellValue($startColumn.$startRow,'/uploads/images/'.$imageFileName); } //前两行是备注,从A3第三行开始导入 for ($currentRow = 3; $currentRow <= $allRow; $currentRow++) { $values = []; for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); $values[] = is_null($val) ? '' : $val; } $resutl[] = array(); if (empty($values)) { return $this->error('文件为空',""); $resutl[$currentRow][] = '第'.$currentRow.'行数据为空'; } if ($values[0] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行头像不能为空'; } else { $image_dir = ROOT_PATH . 'public' . $values[0]; if (file_exists($image_dir)) { $images[] = $image_dir; } } if ($values[1] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行姓名不能为空'; } if ($values[2] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行性别不能为空'; } if ($values[3] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行出生年月不能为空'; } if ($values[4] != '') { if (gettype($values[4]) != 'integer') { $resutl[$currentRow][] = '第'.$currentRow.'行年龄格式不正确'; } } else { $resutl[$currentRow][] = '第'.$currentRow.'行年龄格式不正确'; } if ($values[5] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行民族不能为空'; } if ($values[6] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行籍贯不能为空'; } if ($values[7] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行出生地不能为空'; } if ($values[8] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行政治面貌不能为空'; } if ($values[9] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行入党时间不能为空'; } if ($values[10] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行参加工作时间不能为空'; } if ($values[11] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行健康状况不能为空'; } if ($values[12] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行专业技术职务不能为空'; } if ($values[13] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行专业特长不能为空'; } if ($values[14] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行工作单位不能为空'; } if ($values[15] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行现任职务不能为空'; } if ($values[16] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行行政级别不能为空'; } if ($values[17] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行全日制教育学历不能为空'; } if ($values[18] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行全日制教育学位不能为空'; } if ($values[19] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行全日制教育毕业学校及专业不能为空'; } if ($values[20] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行在职制教育学历不能为空'; } if ($values[21] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行在职制教育学位不能为空'; } if ($values[22] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行在职制教育毕业学校及专业不能为空'; } if ($values[66] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行单位不能为空'; } else { if ($values[66] != '无') { $arr = explode('-',$values[66]); $count = count($arr); if ($count != 2) { $resutl[$currentRow][] = '第'.$currentRow.'行单位树格式不正确(无或者乌海市纪委建委-信访室格式)'; } } } } if (empty($resutl)) { return $this->error('文件为空'); } foreach ($resutl as $kk=>$vv) { if (empty($vv)) { unset($resutl[$kk]); } } if (!$resutl){ $resutl = array(); } if (count($resutl) != 0) { foreach($images as $v) { if (isset($v) && file_exists($v)) { unlink($v); } } return $this->error('检测到错误,正在排列',"",$resutl); } //前两行是备注,从A3第三行开始导入 for ($currentRow = 3; $currentRow <= $allRow; $currentRow++) { $values = []; for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); $values[] = is_null($val) ? '' : $val; } // halt($values); if (gettype($values[3]) != 'string' ) { $values[3] = strtotime(gmdate('Y/m/d',($values[3]-25569)*24*60*60)); } // echo gettype($values[9]);die; if (gettype($values[9]) != 'string') { $values[9] = strtotime(gmdate('Y/m/d',($values[9]-25569)*24*60*60)); } if (gettype($values[10]) != 'string') { $values[10] = strtotime(gmdate('Y/m/d',($values[10]-25569)*24*60*60)); } // 截取数组中用户基本信息 $userInfo = array_slice($values,0,23); // 处理单位树 $userInfo[23] = $values['66']; if ($userInfo[23] == '无') { $userInfo[23] = 6; } else if($userInfo[23] != '') { $type_name = explode("-",$userInfo[23]); // 先根据单位名称查出ftypeid $f_typeid = Db::name('type')->where('name',$type_name[0])->find(); if (isset($f_typeid)) { // 查出子typeid $z_type_id = Db::name('type') ->where('name',$type_name[1]) ->where('f_id',$f_typeid['t_id']) ->find(); if ($z_type_id) { $userInfo[23] = $z_type_id['t_id']; } else { $userInfo[23] = 6; } } else { $userInfo[23] = 6; } } else { $userInfo[23] = 6; } // halt($userInfo); // 截取数组中简历信息 $resume_test = array_slice($values,23,3); // 截取数组中社会关系数组 $sociogram =array_slice($values,26,40); // 将一个一维数组平均分成n个二维数组 $sociograms = []; for ($i = 0; $i <= 7; $i++) { $sociograms[] = array_slice($sociogram, $i * 5, 5); } if (isset($values[1])) { // 使用原生SQL添加用户信息 $userInfo=implode("','",$userInfo); $sql = "INSERT INTO hu_users VALUES ( '' , '" .$userInfo. " ') "; $insert = Db::execute($sql); if ($insert) { // 获取用户id $userId = Db::name('user')->getLastInsID(); // 插入简历信息 $resume['user_id'] = $userId; $resume['r_resume'] = $resume_test[0]; $resume['r_rewards'] = $resume_test[1]; $resume['r_results'] = $resume_test[2]; $add_resume = Db::name('resume')->insert($resume); if ($add_resume) { // 插入社会关系 foreach($sociograms as $s) { if ($s[0] != '') { $sociogram_info['user_id'] = $userId; $sociogram_info['s_appellation'] = $s[0]; $sociogram_info['s_name'] = $s[1]; $sociogram_info['s_age'] = $s[2]; $sociogram_info['s_politics'] = $s[3]; $sociogram_info['s_unit'] = $s[4]; Db::name('sociogram')->insert($sociogram_info); } } } } if (!empty($row)){ $insert[]=$row; } } } } catch (Exception $exception) { $this->error($exception->getMessage()); } if (!$insert) { $this->error(__('无数据导入')); } $num = $allRow-2; $this->success('导入成功,共'.$num.'条数据','','','3'); } /** * 表格导入 */ public function testing() { $file = $this->request->request('file'); if (!$file) { $this->error(__('未获取到文件', 'file')); } $filePath = ROOT_PATH . DS . 'public' . DS . $file; if (!is_file($filePath)) { $this->error(__('未获取到文件')); } //实例化reader $ext = pathinfo($filePath, PATHINFO_EXTENSION); if (!in_array($ext, ['xls'])) { $this->error(__('文件必须为xls')); } $reader = new Xls(); //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment'; $table = $this->model->getQuery()->getTable(); $database = \think\Config::get('database.database'); $fieldArr = []; $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]); foreach ($list as $k => $v) { if ($importHeadType == 'comment') { $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME']; } else { $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME']; } } //加载文件 $insert = []; try { if (!$PHPExcel = $reader->load($filePath)) { $this->error(__('Unknown data format')); } $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表 $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号 $allRow = $currentSheet->getHighestRow(); //取得一共有多少行 $maxColumnNumber = Coordinate::columnIndexFromString($allColumn); //处理图片 $imageFilePath=ROOT_PATH.'public/uploads/images/' ;//图片保存目录 if (!file_exists ( $imageFilePath )) { mkdir("$imageFilePath", 0777, true); } //处理图片 foreach($currentSheet->getDrawingCollection() as $img) { list($startColumn,$startRow)= Coordinate::coordinateFromString($img->getCoordinates());//获取图片所在行和列 $imageFileName = Random::uuid(); switch($img->getMimeType()) { case 'image/jpg': case 'image/jpeg': $imageFileName.='.jpg'; imagejpeg($img->getImageResource(),$imageFilePath.$imageFileName); break; case 'image/gif': $imageFileName.='.gif'; imagegif($img->getImageResource(),$imageFilePath.$imageFileName); break; case 'image/png': $imageFileName.='.png'; imagepng($img->getImageResource(),$imageFilePath.$imageFileName); break; } $currentSheet->setCellValue($startColumn.$startRow,'/uploads/images/'.$imageFileName); } //前两行是备注,从A3第三行开始导入 for ($currentRow = 3; $currentRow <= $allRow; $currentRow++) { $values = []; for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); $values[] = is_null($val) ? '' : $val; } $resutl[] = array(); if (empty($values)) { return $this->error('文件为空',""); $resutl[$currentRow][] = '第'.$currentRow.'行数据为空'; } if ($values[0] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行头像不能为空'; } if(!empty($values[0])) { $image_dir = ROOT_PATH . 'public' . $values[0]; if (file_exists($image_dir)){ unlink($image_dir); } } if ($values[1] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行姓名不能为空'; } if ($values[2] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行性别不能为空'; } if ($values[3] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行出生年月不能为空'; } else { if ($values[3] != '无') { if (gettype($values[3]) != 'integer') { $resutl[$currentRow][] = '第'.$currentRow.'行出生年月格式不正确(无或者1970/1/1格式)'; } } } if ($values[4] != '') { if (gettype($values[4]) != 'integer') { $resutl[$currentRow][] = '第'.$currentRow.'行年龄格式不正确'; } } else { $resutl[$currentRow][] = '第'.$currentRow.'行年龄格式不正确'; } if ($values[5] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行民族不能为空'; } if ($values[6] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行籍贯不能为空'; } if ($values[7] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行出生地不能为空'; } if ($values[8] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行政治面貌不能为空'; } if ($values[9] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行入党时间不能为空'; } else { if ($values[9] != '无') { if (gettype($values[9]) != 'integer') { $resutl[$currentRow][] = '第'.$currentRow.'行入党时间格式不正确(无或者1970/1/1格式)'; } } } if ($values[10] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行参加工作时间不能为空'; } else { if ($values[10] != '无') { if (gettype($values[10]) != 'integer') { $resutl[$currentRow][] = '第'.$currentRow.'行参加工作时间格式不正确(无或者1970/1/1格式)'; } } } if ($values[11] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行健康状况不能为空'; } if ($values[12] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行专业技术职务不能为空'; } if ($values[13] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行专业特长不能为空'; } if ($values[14] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行工作单位不能为空'; } if ($values[15] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行现任职务不能为空'; } if ($values[16] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行行政级别不能为空'; } if ($values[17] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行全日制教育学历不能为空'; } if ($values[18] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行全日制教育学位不能为空'; } if ($values[19] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行全日制教育毕业学校及专业不能为空'; } if ($values[20] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行在职制教育学历不能为空'; } if ($values[21] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行在职制教育学位不能为空'; } if ($values[22] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行在职制教育毕业学校及专业不能为空'; } if ($values[66] == '') { $resutl[$currentRow][] = '第'.$currentRow.'行单位不能为空'; } else { if ($values[66] != '无') { $arr = explode('-',$values[66]); $count = count($arr); if ($count != 2) { $resutl[$currentRow][] = '第'.$currentRow.'行单位树格式不正确(无或者乌海市纪委建委-信访室格式)'; } } } } } catch (Exception $exception) { $this->error($exception->getMessage()); } if (empty($resutl)) { return $this->error('文件为空'); } foreach ($resutl as $kk=>$vv) { if (empty($vv)) { unset($resutl[$kk]); } } if (!$resutl){ $resutl = array(); } $file_dir = ROOT_PATH . 'public' . $file; unlink($file_dir); if (count($resutl) == 0) { return $this->success('无错误字段'); } else { return $this->error('检测到错误,正在排列',"users/a",$resutl); } $this->success(); } public function jdump() { $data = input('get.'); $data = $data['data']; $data = json_decode($data,true); // halt($data); return $this->fetch('testing',['data'=>$data]); } }