|
@@ -13,6 +13,13 @@ use think\Db;
|
|
use think\Model;
|
|
use think\Model;
|
|
use think\Validate;
|
|
use think\Validate;
|
|
|
|
|
|
|
|
+use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
|
|
+use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
|
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
+use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
|
|
|
+use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
|
|
|
+use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
|
|
|
+
|
|
|
|
|
|
/**
|
|
/**
|
|
* 部门成员
|
|
* 部门成员
|
|
@@ -32,7 +39,7 @@ class Admin extends Backend
|
|
* @var array
|
|
* @var array
|
|
*/
|
|
*/
|
|
protected $allDepartment = [];//
|
|
protected $allDepartment = [];//
|
|
- protected $noNeedRight=['selectpage','getDepartmentIds','departadmintotal'];
|
|
|
|
|
|
+ protected $noNeedRight=['selectpage','getDepartmentIds','departadmintotal','export','import'];
|
|
|
|
|
|
|
|
|
|
public function _initialize()
|
|
public function _initialize()
|
|
@@ -685,5 +692,259 @@ class Admin extends Backend
|
|
return json($result);
|
|
return json($result);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ //导入
|
|
|
|
+ public function import(){
|
|
|
|
+ error_reporting(E_ALL);
|
|
|
|
+ ini_set("memory_limit","8000M");
|
|
|
|
+ $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'));
|
|
|
|
+ }
|
|
|
|
+ if ($ext === 'csv') {
|
|
|
|
+ $file = fopen($filePath, 'r');
|
|
|
|
+ $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
|
|
|
|
+ $fp = fopen($filePath, "w");
|
|
|
|
+ $n = 0;
|
|
|
|
+ while ($line = fgets($file)) {
|
|
|
|
+ $line = rtrim($line, "\n\r\0");
|
|
|
|
+ $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
|
|
|
|
+ if ($encoding != 'utf-8') {
|
|
|
|
+ $line = mb_convert_encoding($line, 'utf-8', $encoding);
|
|
|
|
+ }
|
|
|
|
+ if ($n == 0 || preg_match('/^".*"$/', $line)) {
|
|
|
|
+ fwrite($fp, $line . "\n");
|
|
|
|
+ } else {
|
|
|
|
+ fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
|
|
|
|
+ }
|
|
|
|
+ $n++;
|
|
|
|
+ }
|
|
|
|
+ fclose($file) || fclose($fp);
|
|
|
|
+
|
|
|
|
+ $reader = new Csv();
|
|
|
|
+ } elseif ($ext === 'xls') {
|
|
|
|
+ $reader = new Xls();
|
|
|
|
+ } else {
|
|
|
|
+ $reader = new Xlsx();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
|
|
|
|
+ $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
|
|
|
|
+ $this->model = new \app\admin\model\department\AuthAdmin();
|
|
|
|
+ $table = $this->model->getQuery()->getTable();
|
|
|
|
+
|
|
|
|
+ $fieldArr['学号'] = 'username';
|
|
|
|
+ $fieldArr['姓名'] = 'nickname';
|
|
|
|
+ $fieldArr['区队'] = 'depart_id';
|
|
|
|
+ //加载文件
|
|
|
|
+ $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);
|
|
|
|
+ $fields = [];
|
|
|
|
+ for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
|
|
|
|
+ for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
|
+ $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
|
+ $fields[] = $val;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
|
|
|
|
+ $values = [];
|
|
|
|
+ for ($currentColumn = 1; $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]] = trim($v);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if ($row) {
|
|
|
|
+ $insert[] = $row;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception $exception) {
|
|
|
|
+ $this->error($exception->getMessage());
|
|
|
|
+ }
|
|
|
|
+ if (!$insert) {
|
|
|
|
+ $this->error(__('No rows were updated'));
|
|
|
|
+ }
|
|
|
|
+ foreach ($insert as $key => $v)
|
|
|
|
+ {
|
|
|
|
+ $admin_info = Db::name('admin')->where('username',$v['username'])->find();
|
|
|
|
+ if(empty($admin_info)){
|
|
|
|
+ $salt = Random::alnum();
|
|
|
|
+ $depart_id = Db::name('department')->where('name',$v['depart_id'])->value('id');
|
|
|
|
+ $data = [
|
|
|
|
+ 'username'=>$v['username'],
|
|
|
|
+ 'nickname'=>$v['username'],
|
|
|
|
+ 'salt'=>$salt,
|
|
|
|
+ 'password'=>$this->auth->getEncryptPassword('123456', $salt),
|
|
|
|
+ 'avatar'=>'/assets/img/avatar.png',
|
|
|
|
+ 'loginfailure'=>0,
|
|
|
|
+ 'email'=>$v['username'].'@163.com',
|
|
|
|
+ 'status'=>'normal',
|
|
|
|
+ 'createtime'=>time(),
|
|
|
|
+ 'updatetime'=>time(),
|
|
|
|
+ 'depart_id'=>$depart_id??0,
|
|
|
|
+ ];
|
|
|
|
+ $adminId = Db::name('admin')->insertGetId($data);
|
|
|
|
+
|
|
|
|
+ $add = [
|
|
|
|
+ 'department_id'=>$depart_id??0,
|
|
|
|
+ 'admin_id'=>$adminId,
|
|
|
|
+ 'create_time'=>time(),
|
|
|
|
+ 'update_time'=>time(),
|
|
|
|
+ ];
|
|
|
|
+ $departmentId = Db::name('department_admin')->insertGetId($add);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ $group_info = Db::name('auth_group_access')->where('uid',$adminId)->find();
|
|
|
|
+ if(empty($group_info)){
|
|
|
|
+ $arr = [
|
|
|
|
+ 'uid'=>$adminId,
|
|
|
|
+ 'group_id'=>8
|
|
|
|
+ ];
|
|
|
|
+ Db::name('auth_group_access')->insertGetId($arr);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $this->success('导入成功');
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //导出
|
|
|
|
+ public function export(){
|
|
|
|
+
|
|
|
|
+ $this->model = new \app\admin\model\department\AuthAdmin();
|
|
|
|
+ list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
|
|
+ $list = $this->model->where($where)->where(['depart_id'=>['>',0]])->order($sort, $order)->select();
|
|
|
|
+
|
|
|
|
+ $xlsName = '学员信息';
|
|
|
|
+ $this->exportExcel($xlsName,'Excel2007', $list);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 输出到浏览器(需要设置header头)
|
|
|
|
+ * @param string $fileName 文件名
|
|
|
|
+ * @param string $fileType 文件类型
|
|
|
|
+ */
|
|
|
|
+ function exportExcel($fileName, $fileType,$data)
|
|
|
|
+ {
|
|
|
|
+ //文件名称校验
|
|
|
|
+ if (!$fileName) {
|
|
|
|
+ trigger_error('文件名不能为空', E_USER_ERROR);
|
|
|
|
+ }
|
|
|
|
+ //Excel文件类型校验
|
|
|
|
+ $type = ['Excel2007', 'Xlsx', 'Excel5', 'xls'];
|
|
|
|
+ if (!in_array($fileType, $type)) {
|
|
|
|
+ trigger_error('未知文件类型', E_USER_ERROR);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $ext = '';
|
|
|
|
+
|
|
|
|
+ if ($fileType == 'Excel2007' || $fileType == 'Xlsx') {
|
|
|
|
+ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
|
+ header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
|
|
|
|
+ header('Cache-Control: max-age=0');
|
|
|
|
+ $ext = 'Xlsx';
|
|
|
|
+ } else { //Excel5
|
|
|
|
+ header('Content-Type: application/vnd.ms-excel');
|
|
|
|
+ header('Content-Disposition: attachment;filename="' . $fileName . '.xls"');
|
|
|
|
+ header('Cache-Control: max-age=0');
|
|
|
|
+ $ext = 'Xls';
|
|
|
|
+ }
|
|
|
|
+ $spreadsheet = new Spreadsheet();
|
|
|
|
+ $worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
+ $worksheet->getColumnDimension('A')->setWidth(15);
|
|
|
|
+ $worksheet->getColumnDimension('B')->setWidth(15);
|
|
|
|
+ $worksheet->getColumnDimension('C')->setWidth(15);
|
|
|
|
+ $worksheet->getColumnDimension('D')->setWidth(15);
|
|
|
|
+
|
|
|
|
+ $worksheet->getRowDimension('1')->setRowHeight(25);
|
|
|
|
+
|
|
|
|
+ //设置工作表标题名称
|
|
|
|
+ $worksheet->setTitle('学员信息');
|
|
|
|
+
|
|
|
|
+ $worksheet->setCellValue('A1',"序号");
|
|
|
|
+ $worksheet->setCellValue('B1',"学号");
|
|
|
|
+ $worksheet->setCellValue('C1',"姓名");
|
|
|
|
+ $worksheet->setCellValue('D1',"区队");
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ foreach ($data as $ky => $value)
|
|
|
|
+ {
|
|
|
|
+ $qudui = Db::name('department')->where('id',$value['depart_id'])->value('name');
|
|
|
|
+ $lie = $ky+2;
|
|
|
|
+ $worksheet->setCellValue('A'.$lie,++$ky);
|
|
|
|
+ $worksheet->setCellValue('B'.$lie,$value['username']);
|
|
|
|
+ $worksheet->setCellValue('C'.$lie,$value['nickname']);
|
|
|
|
+ $worksheet->setCellValue('D'.$lie,$qudui);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ $titlestyleArray = [
|
|
|
|
+ 'font' => [
|
|
|
|
+ 'name' => '黑体',
|
|
|
|
+ 'size' => 14
|
|
|
|
+ ],
|
|
|
|
+ ];
|
|
|
|
+ $headerStyleArray = [
|
|
|
|
+ 'font' => [
|
|
|
|
+ 'name' => '方正小标宋简体',
|
|
|
|
+ 'size' => 18
|
|
|
|
+ ],
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ $commonStyleArray = [
|
|
|
|
+ 'alignment' => [
|
|
|
|
+ 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
|
|
|
|
+ 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
|
|
|
|
+ 'wrapText' => true,
|
|
|
|
+ ]
|
|
|
|
+ ];
|
|
|
|
+ $borderStyleArray = [
|
|
|
|
+ 'borders' => [
|
|
|
|
+ 'allBorders' => [
|
|
|
|
+ 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
|
|
|
|
+ ],
|
|
|
|
+ ],
|
|
|
|
+ 'font' => [
|
|
|
|
+ 'name' => '黑体',
|
|
|
|
+ ],
|
|
|
|
+ ];
|
|
|
|
+ $worksheet->getStyle('A1:D1')->applyFromArray($titlestyleArray);
|
|
|
|
+ $worksheet->getStyle('A1:D1')->applyFromArray($commonStyleArray);
|
|
|
|
+ // $worksheet->getStyle('A2:R2')->applyFromArray($commonStyleArray);
|
|
|
|
+ // $worksheet->getStyle('A3:R3')->applyFromArray($commonStyleArray);
|
|
|
|
+ // $worksheet->getStyle('A'.$footer_total.':R'.$footer_total)->applyFromArray($commonStyleArray);
|
|
|
|
+ // $worksheet->getStyle('A1:C1')->applyFromArray($borderStyleArray);
|
|
|
|
+ // $worksheet->mergeCells('A1:R1');
|
|
|
|
+ $writer = IOFactory::createWriter($spreadsheet,$ext);
|
|
|
|
+ $writer->save('php://output');
|
|
|
|
+ die();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
}
|
|
}
|