wang hou sheng 7 months ago
parent 272ee7850a
commit d20fd4d5a8
  1. 21
      app/api/controller/Upload.php
  2. 19
      app/api/service/Upload.php

@ -45,27 +45,6 @@ class Upload extends Controller
return $this->renderSuccess(['fileInfo' => $service->getFileInfo()], '文件上传成功');
}
public function uploadBase64Image(): Json
{
$base64_file = $this->request->post('file'); // 获取Base64编码的字符串
$file_info = explode(';', $base64_file);
$file_type_info = explode('/', $file_info[0]);
$file_extension = $file_type_info[1]; // 获取文件扩展名
$file_data = explode(',', $base64_file)[1]; // 获取Base64编码后的数据
$file_data = base64_decode($file_data); // 解码Base64数据
$file_name = uniqid((string)rand(), true) . '.' . $file_extension; // 创建唯一文件名
$file_path = base_url() . 'temp/' . $file_name;
if (file_put_contents($file_path, $file_data)) {
$service = new UploadService();
if (!$service->uploadBase64(FileTypeEnum::IMAGE, false, $file_path)) {
return $this->renderError('文件上传失败:' . $service->getError());
}
// 图片上传成功
return $this->renderSuccess(['fileInfo' => $service->getFileInfo()], '文件上传成');
}
return $this->renderError('文件上传失败');
}
public function wxUpload(): Json
{
$checkLogin = $this->request->param('checkLogin');

@ -68,24 +68,7 @@ class Upload extends BaseService
// 添加文件库记录
return $this->record($fileInfo, $fileType, $userId);
}
public function uploadBase64(int $fileType, bool $checkLogin = true, $filePath): bool
{
$userId = $checkLogin && UserService::isLogin(true) ? UserService::getCurrentLoginUserId() : 0;
$storage = $this->getDriver(self::UPLOAD_SCENE_ENUM[$fileType]);
$storage->setUploadFileByReal($filePath);
// 执行文件上传
if (!$storage->upload()) {
$this->error = $storage->getError();
return false;
}
// 文件信息
$fileInfo = $storage->getSaveFileInfo();
@unlink($filePath);
// 添加文件库记录
return $this->record($fileInfo, $fileType, $userId);
}
/**
* 实例化存储驱动

Loading…
Cancel
Save