diff --git a/app/api/controller/Import.php b/app/api/controller/Import.php new file mode 100644 index 0000000..a8c6fda --- /dev/null +++ b/app/api/controller/Import.php @@ -0,0 +1,180 @@ +Save('.' . $path, 256); + $Avatar->Free(); + return $path; + } + //导入员工 + public function import_admin(){ + // 获取表单上传文件 + $file[]= request()->file('file'); + if($this->uid>1){ + return to_assign(1,'该操作只能是超级管理员有权限操作'); + } + try { + // 验证文件大小,名称等是否正确 + validate(['file' => 'filesize:51200|fileExt:xls,xlsx'])->check($file); + // 日期前綴 + $dataPath = date('Ym'); + $md5 = $file[0]->hash('md5'); + $savename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file[0], function () use ($md5) { + return $md5; + }); + $fileExtendName = substr(strrchr($savename, '.'), 1); + // 有Xls和Xlsx格式两种 + if ($fileExtendName == 'xlsx') { + $objReader = IOFactory::createReader('Xlsx'); + } else { + $objReader = IOFactory::createReader('Xls'); + } + $objReader->setReadDataOnly(TRUE); + $path = get_config('filesystem.disks.public.url'); + // 读取文件,tp6默认上传的文件,在runtime的相应目录下,可根据实际情况自己更改 + $objPHPExcel = $objReader->load('.'.$path . '/' .$savename); + //$objPHPExcel = $objReader->load('./storage/202209/d11544d20b3ca1c1a5f8ce799c3b2433.xlsx'); + $sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet + $highestRow = $sheet->getHighestRow(); // 取得总行数 + $highestColumn = $sheet->getHighestColumn(); // 取得总列数 + Coordinate::columnIndexFromString($highestColumn); + $lines = $highestRow - 1; + if ($lines <= 0) { + return to_assign(1, '数据不能为空'); + exit(); + } + $sex_array=['未知','男','女']; + $type_array=['未知','正式','试用','实习']; + $mobile_array = Db::name('Admin')->where([['status','>=',0]])->column('mobile'); + $email_array = Db::name('Admin')->where([['status','>=',0]])->column('email'); + $department_array = Db::name('Department')->where(['status' => 1])->column('title', 'id'); + $position_array = Db::name('Position')->where(['status' => 1])->column('title', 'id'); + //循环读取excel表格,整合成数组。如果是不指定key的二维,就用$data[i][j]表示。 + $pinyin = new Pinyin(); + for ($j = 3; $j <= $highestRow; $j++) { + $salt = set_salt(20); + $reg_pwd = '123456'; + $name = $objPHPExcel->getActiveSheet()->getCell("A" . $j)->getValue(); + if(empty($name)){ + continue; + } + $char = mb_substr($name, 0, 1, 'utf-8'); + $sex = arraySearch($sex_array,$objPHPExcel->getActiveSheet()->getCell("D" . $j)->getValue()); + $department = arraySearch($department_array,$objPHPExcel->getActiveSheet()->getCell("E" . $j)->getValue()); + $position = arraySearch($position_array,$objPHPExcel->getActiveSheet()->getCell("f" . $j)->getValue()); + $type = arraySearch($type_array,$objPHPExcel->getActiveSheet()->getCell("G" . $j)->getValue()); + $username = $pinyin->name($name,PINYIN_UMLAUT_V); + + $mobile = $objPHPExcel->getActiveSheet()->getCell("B" . $j)->getValue(); + $email = $objPHPExcel->getActiveSheet()->getCell("C" . $j)->getValue(); + $file_check['mobile'] = $mobile; + $file_check['email'] = $email; + $validate_mobile = \think\facade\Validate::rule([ + 'mobile' => 'require|mobile', + ]); + $validate_email = \think\facade\Validate::rule([ + 'email' => 'email', + ]); + if (!$validate_mobile->check($file_check)) { + return to_assign(1, '第'.($j - 2).'行的手机号码'.$validate->getError()); + } + else{ + if(in_array($mobile,$mobile_array)){ + return to_assign(1, '第'.($j - 2).'行的手机号码已存在或者重复'); + } + else{ + array_push($mobile_array,$mobile); + } + } + + if(!empty($email)){ + if (!$validate_email->check($file_check)) { + return to_assign(1, '第'.($j - 2).'行的电子邮箱'.$validate->getError()); + } + else{ + if(in_array($email,$email_array)){ + return to_assign(1, '第'.($j - 2).'行的电子邮箱已存在或者重复'); + } + else{ + array_push($email_array,$email); + } + } + } + else{ + $email=''; + } + + if(empty($department)){ + return to_assign(1, '第'.($j - 2).'行的所在部门错误'); + } + if(empty($department)){ + return to_assign(1, '第'.($j - 2).'行的所在部门错误'); + } + if(empty($position)){ + return to_assign(1, '第'.($j - 2).'行的所属职位错误'); + } + $data[$j - 3] = [ + 'name' => $name, + 'nickname' => $name, + 'mobile' => $mobile, + 'email' => $email, + 'sex' => $sex, + 'did' => $department, + 'position_id' => $position, + 'type' => $type, + 'entry_time' => Shared::excelToTimestamp($objPHPExcel->getActiveSheet()->getCell("H" . $j)->getValue(),'Asia/Shanghai'), + 'username' => implode('', $username), + 'salt' => $salt, + 'pwd' => set_password($reg_pwd, $salt), + 'reg_pwd' => $reg_pwd, + 'thumb' => $this->to_avatars($char) + ]; + } + //dd($data);exit; + // 批量添加数据 + if ((new Admin())->saveAll($data)) { + return to_assign(0, '导入成功'); + } + else{ + return to_assign(1, '导入失败,请检查excel文件再试'); + } + } catch (\think\exception\ValidateException $e) { + return to_assign(1, $e->getMessage()); + } + } +} diff --git a/app/common.php b/app/common.php index 0b3819e..d128e4d 100644 --- a/app/common.php +++ b/app/common.php @@ -661,6 +661,25 @@ function get_desc_content($content, $count) return $res; } +//查找数组索引 +function arraySearch($array, $searchFor) { + foreach($array as $key => $value) { + if(is_array($value)){ + foreach($value as $key1 => $value1) { + if($value1 == $searchFor) { + return array("index" => $key, "key" => $key1); + } + } + } + else{ + if($value == $searchFor) { + return $key; + } + } + } + return false; +} + /** * PHP去除空格 * @param string $str 字符串 diff --git a/app/user/view/user/index.html b/app/user/view/user/index.html index 958417e..4f688e0 100644 --- a/app/user/view/user/index.html +++ b/app/user/view/user/index.html @@ -9,6 +9,11 @@ .layui-tree-line .layui-tree-set .layui-tree-set:after{top:18px;} .tree-left{width:200px; float:left; height:calc(100% - 30px); overflow: scroll; border:1px solid #eeeeee; background-color:#FAFAFA; padding:12px 12px 12px 5px;} .tree-left h3{font-size:16px; height:30px; padding-left:10px; font-weight:800} + + .gougu-upload-files{background-color: #ffffff; border:1px solid #e4e7ed;color: #c0c4cc;cursor: not-allowed; padding:0 12px; width:180px; box-sizing: border-box; display: inline-block; font-size: inherit; height: 38px; line-height: 35px; margin-right:8px; border-radius:2px;} + .gougu-upload-tips{color:#969696} + .layui-form-item{margin-bottom:8px;} + .layui-input-block{min-height:24px;} {/block} @@ -52,6 +57,7 @@ + @@ -63,30 +69,117 @@