头像批量上传

master
lion 3 days ago
parent a909687dd5
commit 48c36bd068

@ -1241,8 +1241,11 @@ class UserController extends BaseController
$all = \request()->all();
$validator = Validator::make($all, [
'course_id' => 'required|integer',
'files' => 'required|array|min:1',
'files.*.filename' => 'required|string',
], [
'course_id.required' => '请选择课程',
'files.required' => '请提供图片文件名列表',
]);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
@ -1254,20 +1257,14 @@ class UserController extends BaseController
return $this->fail([ResponseCode::ERROR_BUSINESS, '课程不存在']);
}
$files = \request()->file('files');
if (empty($files)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '请上传头像图片']);
}
if (!is_array($files)) {
$files = [$files];
}
$fileItems = $all['files'];
$students = $this->getCourseStudents((int) $courseId);
$list = [];
$stats = ['ready' => 0, 'unmatched' => 0, 'duplicate' => 0, 'invalid' => 0];
foreach ($files as $index => $file) {
$filename = $file->getClientOriginalName();
foreach ($fileItems as $index => $fileItem) {
$filename = trim((string) ($fileItem['filename'] ?? ''));
$fileSize = isset($fileItem['size']) ? (int) $fileItem['size'] : null;
$name = trim(pathinfo($filename, PATHINFO_FILENAME));
$item = [
'index' => $index,
@ -1283,15 +1280,15 @@ class UserController extends BaseController
'candidates' => [],
];
if (!$file->isValid()) {
if ($filename === '') {
$item['status'] = 'invalid';
$item['status_text'] = '文件无效';
$item['status_text'] = '文件无效';
$stats['invalid']++;
$list[] = $item;
continue;
}
$ext = strtolower($file->getClientOriginalExtension());
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (!in_array($ext, ['png', 'gif', 'jpg', 'jpeg', 'bmp', 'svg', 'webp'])) {
$item['status'] = 'invalid';
$item['status_text'] = '文件格式不支持';
@ -1300,9 +1297,9 @@ class UserController extends BaseController
continue;
}
if ($file->getSize() > 500 * 1024) {
if ($fileSize !== null && $fileSize > 1024 * 1024) {
$item['status'] = 'invalid';
$item['status_text'] = '文件超过500KB';
$item['status_text'] = '文件超过1MB';
$stats['invalid']++;
$list[] = $item;
continue;
@ -1352,6 +1349,7 @@ class UserController extends BaseController
'list' => $list,
'stats' => $stats,
'student_total' => $students->count(),
'file_total' => count($fileItems),
]);
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save