|
|
|
|
@ -8,6 +8,7 @@ use App\Models\Department;
|
|
|
|
|
use App\Models\OperateLog;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
use Rap2hpoutre\FastExcel\FastExcel;
|
|
|
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
|
|
|
|
|
|
@ -243,65 +244,221 @@ class AdminController extends CommonController
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @OA\Post (
|
|
|
|
|
* path="/api/admin/import",
|
|
|
|
|
* tags={"后台管理"},
|
|
|
|
|
* summary="导入数据",
|
|
|
|
|
* description="",
|
|
|
|
|
* @OA\Parameter(name="file", in="query", @OA\Schema(type="object"), required=true, description="文件"),
|
|
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
|
|
|
|
* description="导入用户"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
|
|
|
|
public function importPreview(Request $request)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$rows = $this->parseImportRows($request);
|
|
|
|
|
$previewRows = [];
|
|
|
|
|
$existsCount = 0;
|
|
|
|
|
$missingDepartmentRows = [];
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
if ($row['exists']) {
|
|
|
|
|
$existsCount++;
|
|
|
|
|
}
|
|
|
|
|
if (!$row['department_exists']) {
|
|
|
|
|
$missingDepartmentRows[] = $row['line_no'];
|
|
|
|
|
}
|
|
|
|
|
$previewRows[] = [
|
|
|
|
|
'line_no' => $row['line_no'],
|
|
|
|
|
'name' => $row['name'],
|
|
|
|
|
'username' => $row['username'],
|
|
|
|
|
'mobile' => $row['mobile'],
|
|
|
|
|
'department' => $row['department'],
|
|
|
|
|
'position' => $row['position'],
|
|
|
|
|
'birthday' => $row['birthday'],
|
|
|
|
|
'email' => $row['email'],
|
|
|
|
|
'password_filled' => $row['password_filled'],
|
|
|
|
|
'department_exists' => $row['department_exists'],
|
|
|
|
|
'exists' => $row['exists'],
|
|
|
|
|
'message' => $row['message'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
return $this->success([
|
|
|
|
|
'rows' => $previewRows,
|
|
|
|
|
'exists_count' => $existsCount,
|
|
|
|
|
'missing_department_rows' => $missingDepartmentRows,
|
|
|
|
|
'can_import' => count($missingDepartmentRows) === 0,
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function importSubmit(Request $request)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$rows = $this->parseImportRows($request);
|
|
|
|
|
$forceUpdate = (bool)$request->get('force_update', false);
|
|
|
|
|
$missingDepartmentNames = [];
|
|
|
|
|
$existsUsernames = [];
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
if (!$row['department_exists']) {
|
|
|
|
|
$missingDepartmentNames[] = $row['department'];
|
|
|
|
|
}
|
|
|
|
|
if ($row['exists']) {
|
|
|
|
|
$existsUsernames[] = $row['username'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!empty($missingDepartmentNames)) {
|
|
|
|
|
$missingDepartmentNames = array_values(array_unique($missingDepartmentNames));
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '部门不存在:' . implode('、', $missingDepartmentNames) . ',请先创建部门后再导入']);
|
|
|
|
|
}
|
|
|
|
|
if (!$forceUpdate && !empty($existsUsernames)) {
|
|
|
|
|
$existsUsernames = array_values(array_unique($existsUsernames));
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '用户' . implode('、', $existsUsernames) . '已存在,是否直接更新?']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
$created = 0;
|
|
|
|
|
$updated = 0;
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
$data = [
|
|
|
|
|
'name' => $row['name'],
|
|
|
|
|
'username' => $row['username'],
|
|
|
|
|
'department_id' => $row['department_id'],
|
|
|
|
|
'mobile' => $row['mobile'],
|
|
|
|
|
'position' => $row['position'] ?: null,
|
|
|
|
|
'birthday' => $row['birthday'],
|
|
|
|
|
'email' => $row['email'] ?: null,
|
|
|
|
|
];
|
|
|
|
|
$model = Admin::where('username', $row['username'])->first();
|
|
|
|
|
if ($model) {
|
|
|
|
|
if (!empty($row['password_plain'])) {
|
|
|
|
|
$data['password'] = Hash::make($row['password_plain']);
|
|
|
|
|
}
|
|
|
|
|
$model->update($data);
|
|
|
|
|
$updated++;
|
|
|
|
|
} else {
|
|
|
|
|
if (!empty($row['password_plain'])) {
|
|
|
|
|
$data['password'] = Hash::make($row['password_plain']);
|
|
|
|
|
} else {
|
|
|
|
|
$data['password'] = Hash::make('Admin' . date('Y'));
|
|
|
|
|
}
|
|
|
|
|
Admin::create($data);
|
|
|
|
|
$created++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DB::commit();
|
|
|
|
|
return $this->success([
|
|
|
|
|
'created' => $created,
|
|
|
|
|
'updated' => $updated,
|
|
|
|
|
'total' => count($rows),
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
if (DB::transactionLevel() > 0) {
|
|
|
|
|
DB::rollBack();
|
|
|
|
|
}
|
|
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 保留旧接口兼容,默认按“允许更新已存在用户”执行
|
|
|
|
|
public function import(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$request->merge(['force_update' => 1]);
|
|
|
|
|
return $this->importSubmit($request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function parseImportRows(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$file = $request->file('file');
|
|
|
|
|
//判断文件是否有效
|
|
|
|
|
if (!($request->hasFile('file') && $file->isValid())) {
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件不存在或无效']);
|
|
|
|
|
throw new \Exception('文件不存在或无效', ResponseCode::ERROR_BUSINESS);
|
|
|
|
|
}
|
|
|
|
|
//获取文件大小
|
|
|
|
|
$img_size = floor($file->getSize() / 1024);
|
|
|
|
|
if ($img_size >= 5 * 1024) {
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件必须小于5M']);
|
|
|
|
|
$sizeKb = floor($file->getSize() / 1024);
|
|
|
|
|
if ($sizeKb >= 5 * 1024) {
|
|
|
|
|
throw new \Exception('文件必须小于5M', ResponseCode::ERROR_BUSINESS);
|
|
|
|
|
}
|
|
|
|
|
//过滤文件后缀
|
|
|
|
|
$ext = $file->getClientOriginalExtension();
|
|
|
|
|
$ext = strtolower($file->getClientOriginalExtension());
|
|
|
|
|
if (!in_array($ext, ['xls', 'xlsx', 'csv'])) {
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '仅支持xls/xlsx/csv格式']);
|
|
|
|
|
throw new \Exception('仅支持xls/xlsx/csv格式', ResponseCode::ERROR_BUSINESS);
|
|
|
|
|
}
|
|
|
|
|
$dataArray = (new FastExcel)->import($file->getRealPath())->toArray();
|
|
|
|
|
if (empty($dataArray)) {
|
|
|
|
|
throw new \Exception('导入文件为空', ResponseCode::ERROR_BUSINESS);
|
|
|
|
|
}
|
|
|
|
|
$tempFile = $file->getRealPath();
|
|
|
|
|
$dataArray = (new FastExcel)->import($tempFile)->toArray();
|
|
|
|
|
// 获取所有key
|
|
|
|
|
$keyList = array_keys($dataArray[0]);
|
|
|
|
|
if (!in_array('GID(登录用户名)', $keyList)) {
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, 'GID(登录用户名)字段不存在']);
|
|
|
|
|
$requiredHeaders = ['姓名', '用户名', '手机号', '所属部门'];
|
|
|
|
|
foreach ($requiredHeaders as $header) {
|
|
|
|
|
if (!in_array($header, $keyList)) {
|
|
|
|
|
throw new \Exception($header . '字段不存在', ResponseCode::ERROR_BUSINESS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rows = [];
|
|
|
|
|
foreach ($dataArray as $index => $value) {
|
|
|
|
|
$name = trim((string)($value['姓名'] ?? ''));
|
|
|
|
|
$username = trim((string)($value['用户名'] ?? ''));
|
|
|
|
|
$mobile = trim((string)($value['手机号'] ?? ''));
|
|
|
|
|
$department = trim((string)($value['所属部门'] ?? ''));
|
|
|
|
|
$position = trim((string)($value['职位'] ?? ''));
|
|
|
|
|
$birthday = $this->normalizeImportBirthday($value['生日'] ?? null);
|
|
|
|
|
$email = trim((string)($value['邮箱'] ?? ''));
|
|
|
|
|
$passwordPlain = trim((string)($value['密码'] ?? ''));
|
|
|
|
|
$passwordFilled = $passwordPlain !== '';
|
|
|
|
|
|
|
|
|
|
if ($username === '' && $department === '' && $name === '' && $mobile === '') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ($username === '' || $department === '' || $name === '' || $mobile === '') {
|
|
|
|
|
throw new \Exception('第' . ($index + 2) . '行数据不完整(姓名/用户名/手机号/所属部门为必填)', ResponseCode::ERROR_BUSINESS);
|
|
|
|
|
}
|
|
|
|
|
$departmentId = Department::where('name', $department)->value('id');
|
|
|
|
|
$exists = Admin::where('username', $username)->exists();
|
|
|
|
|
$message = '';
|
|
|
|
|
if (!$departmentId) {
|
|
|
|
|
$message = '请先创建部门';
|
|
|
|
|
} elseif ($exists) {
|
|
|
|
|
$message = '用户' . $username . '已存在,是否直接更新';
|
|
|
|
|
}
|
|
|
|
|
$rows[] = [
|
|
|
|
|
'line_no' => $index + 2,
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'department' => $department,
|
|
|
|
|
'department_id' => $departmentId,
|
|
|
|
|
'department_exists' => !empty($departmentId),
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'mobile' => $mobile,
|
|
|
|
|
'position' => $position,
|
|
|
|
|
'birthday' => $birthday,
|
|
|
|
|
'email' => $email,
|
|
|
|
|
'password_plain' => $passwordPlain,
|
|
|
|
|
'password_filled' => $passwordFilled,
|
|
|
|
|
'exists' => $exists,
|
|
|
|
|
'message' => $message,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
|
throw new \Exception('导入文件无有效数据', ResponseCode::ERROR_BUSINESS);
|
|
|
|
|
}
|
|
|
|
|
if (!in_array('部门', $keyList)) {
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '部门字段不存在']);
|
|
|
|
|
return $rows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生日:支持 yyyy-MM-dd 文本或 Excel 日期序列号。
|
|
|
|
|
*/
|
|
|
|
|
private function normalizeImportBirthday($value): ?string
|
|
|
|
|
{
|
|
|
|
|
if ($value === null || $value === '') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (!in_array('姓名', $keyList)) {
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '姓名字段不存在']);
|
|
|
|
|
if (is_numeric($value)) {
|
|
|
|
|
$excel = (float)$value;
|
|
|
|
|
if ($excel > 20000 && $excel < 60000) {
|
|
|
|
|
$unix = (int)(($excel - 25569) * 86400);
|
|
|
|
|
|
|
|
|
|
return gmdate('Y-m-d', $unix);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!in_array('手机号码', $keyList)) {
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '手机号码字段不存在']);
|
|
|
|
|
$s = trim((string)$value);
|
|
|
|
|
if ($s === '') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$list = [];
|
|
|
|
|
foreach ($dataArray as $key => $value) {
|
|
|
|
|
$departmentId = Department::where('name', $value['部门'])->value('id');
|
|
|
|
|
$whereArray = ['name' => $value['姓名']];
|
|
|
|
|
$updateDataArray = ['name' => $value['姓名'], 'username' => $value['GID(登录用户名)'],
|
|
|
|
|
'department_id' => $departmentId,
|
|
|
|
|
'mobile' => $value['手机号码'],
|
|
|
|
|
'password'=> \Illuminate\Support\Facades\Hash::make("Admin" . date("Y"))
|
|
|
|
|
];
|
|
|
|
|
Admin::updateOrCreate($whereArray, $updateDataArray);
|
|
|
|
|
if (preg_match('/^\d{4}-\d{2}-\d{2}/', $s)) {
|
|
|
|
|
return substr($s, 0, 10);
|
|
|
|
|
}
|
|
|
|
|
return $this->success($list);
|
|
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|