200 lines
6.5 KiB
PHP
200 lines
6.5 KiB
PHP
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\home\controller;
|
|
|
|
use app\service\AwsUploadService;
|
|
use app\service\BaiduOcrService;
|
|
use app\service\CompressImgService;
|
|
use app\service\SwooleService;
|
|
use think\facade\Log;
|
|
use think\file\UploadedFile;
|
|
|
|
class Upload extends Base
|
|
{
|
|
public function upload_passport()
|
|
{
|
|
// 获取上传的文件
|
|
$file = request()->file('file');
|
|
$path = request()->param('path', '');
|
|
|
|
if (!$file) {
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => lang('controller.upload_missing'),
|
|
]);
|
|
}
|
|
$imgdata = file_get_contents($file->getPathname());
|
|
/*同时上传护照文件*/
|
|
$fileName = $path . '/' . date('YmdHis') . uniqid() . '.' . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
|
|
try {
|
|
$client = SwooleService::getInstance();
|
|
|
|
$client->sendTask([
|
|
'type' => 'upload_ali',
|
|
'data' => [
|
|
'Bucket' => BUCKET,
|
|
'Key' => $fileName, //重命名的文件名
|
|
'file_content' => base64_encode($imgdata),
|
|
]
|
|
]);
|
|
$client->close();
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => lang('controller.upload_success'),
|
|
'path' => OSS_URL . $fileName,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => lang('controller.upload_failed'),
|
|
'path' => ''
|
|
]);
|
|
}
|
|
}
|
|
public function visa()
|
|
{
|
|
// 获取上传的文件
|
|
$file = request()->file('file');
|
|
$path = request()->param('path', '');
|
|
|
|
if (!$file) {
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => lang('controller.upload_missing'),
|
|
]);
|
|
}
|
|
$imgdata = file_get_contents($file->getPathname());
|
|
/*同时上传签证文件*/
|
|
$fileName = $path . '/' . date('YmdHis') . uniqid() . '.' . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
|
|
try {
|
|
$client = SwooleService::getInstance();
|
|
$client->sendTask([
|
|
'type' => 'upload_ali',
|
|
'data' => [
|
|
'Bucket' => BUCKET,
|
|
'Key' => $fileName, //重命名的文件名
|
|
'file_content' => base64_encode($imgdata),
|
|
]
|
|
]);
|
|
$client->close();
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => lang('controller.upload_success'),
|
|
'path' => OSS_URL . $fileName,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => lang('controller.upload_failed'),
|
|
'path' => ''
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function identification()
|
|
{
|
|
// 获取上传的文件
|
|
$file = request()->file('file');
|
|
$path = request()->param('path', '');
|
|
|
|
if (!$file) {
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => lang('controller.upload_missing'),
|
|
]);
|
|
}
|
|
$imgdata = file_get_contents($file->getPathname());
|
|
/*同时上传身份证文件*/
|
|
$fileName = $path . '/' . date('YmdHis') . uniqid() . '.' . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
|
|
try {
|
|
$client = SwooleService::getInstance();
|
|
$client->sendTask([
|
|
'type' => 'upload_ali',
|
|
'data' => [
|
|
'Bucket' => BUCKET,
|
|
'Key' => $fileName, //重命名的文件名
|
|
'file_content' => base64_encode($imgdata),
|
|
]
|
|
]);
|
|
$client->close();
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => lang('controller.upload_success'),
|
|
'path' => OSS_URL . $fileName,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => lang('controller.upload_failed'),
|
|
'path' => ''
|
|
]);
|
|
}
|
|
}
|
|
public function awsUpload($file, $fileName)
|
|
{
|
|
$fileSize = $file->getSize();
|
|
$originalName = $file->getOriginalName();
|
|
$extension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
|
|
$sizeLimit = 3 * 1024 * 1024;
|
|
if ($fileSize > $sizeLimit && in_array($extension, ['jpg', 'jpeg', 'png', 'gif'])) {
|
|
$compressedFilePath = (new CompressImgService())->compressImage($file->getRealPath(), $fileSize);
|
|
$file = new UploadedFile(
|
|
$compressedFilePath,
|
|
pathinfo($originalName, PATHINFO_FILENAME) . '_compressed.' . $extension,
|
|
mime_content_type($compressedFilePath) ?: $file->getMime(),
|
|
0,
|
|
true
|
|
);
|
|
}
|
|
$service = new AwsUploadService();
|
|
// 额外传递的字段
|
|
$extraFields = [
|
|
'Bucket' => BUCKET,
|
|
'Key' => $fileName //重命名的文件名
|
|
];
|
|
$result = $service->uploadAndForward($file, 'file', $extraFields);
|
|
Log::info(json_encode($result));
|
|
return $result;
|
|
}
|
|
|
|
public function file_upload()
|
|
{
|
|
// 获取上传的文件
|
|
$file = request()->file('file');
|
|
$path = request()->param('path', '');
|
|
|
|
if (!$file) {
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => lang('controller.upload_missing'),
|
|
]);
|
|
}
|
|
$imgdata = file_get_contents($file->getPathname());
|
|
$fileName = $path . '/' . date('YmdHis') . uniqid() . '.' . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
|
|
try {
|
|
$client = SwooleService::getInstance();
|
|
$client->sendTask([
|
|
'type' => 'upload_ali',
|
|
'data' => [
|
|
'Bucket' => BUCKET,
|
|
'Key' => $fileName, //重命名的文件名
|
|
'file_content' => base64_encode($imgdata),
|
|
]
|
|
]);
|
|
$client->close();
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => lang('controller.upload_success'),
|
|
'path' => OSS_URL . $fileName,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => lang('controller.upload_failed'),
|
|
'path' => '',
|
|
]);
|
|
}
|
|
}
|
|
}
|