wang hou sheng 8 months ago
parent 375d476255
commit d684bd41b3
  1. 33
      app/api/controller/Upload.php
  2. 20
      app/api/service/Upload.php

@ -12,10 +12,10 @@ declare (strict_types=1);
namespace app\api\controller; namespace app\api\controller;
use app\api\service\User as UserService;
use think\response\Json;
use app\api\service\Upload as UploadService; use app\api\service\Upload as UploadService;
use app\api\service\User as UserService;
use app\common\enum\file\FileType as FileTypeEnum; use app\common\enum\file\FileType as FileTypeEnum;
use think\response\Json;
/** /**
* 文件上传管理 * 文件上传管理
@ -45,8 +45,29 @@ class Upload extends Controller
return $this->renderSuccess(['fileInfo' => $service->getFileInfo()], '文件上传成功'); 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 { public function wxUpload(): Json
{
$checkLogin = $this->request->param('checkLogin'); $checkLogin = $this->request->param('checkLogin');
// 执行文件上传 // 执行文件上传
$service = new UploadService(); $service = new UploadService();
@ -105,7 +126,8 @@ class Upload extends Controller
return $this->renderSuccess($result['data'], $result['message']); return $this->renderSuccess($result['data'], $result['message']);
} }
public function getFileName($imageUrl,$user_id) { public function getFileName($imageUrl, $user_id)
{
$imageFile = $this->setFileData($imageUrl); $imageFile = $this->setFileData($imageUrl);
$savePath = "./uploads/wxImage/" . date("Y-m-d") . '/'; $savePath = "./uploads/wxImage/" . date("Y-m-d") . '/';
$fileName = $user_id . '.jpg'; $fileName = $user_id . '.jpg';
@ -116,7 +138,8 @@ class Upload extends Controller
return $savePath . $fileName; return $savePath . $fileName;
} }
public function setFileData($image_url) { public function setFileData($image_url)
{
return file_get_contents($image_url); return file_get_contents($image_url);
} }
} }

@ -15,8 +15,8 @@ namespace app\api\service;
use app\api\model\Setting as SettingModel; use app\api\model\Setting as SettingModel;
use app\api\model\UploadFile as UploadFileModel; use app\api\model\UploadFile as UploadFileModel;
use app\api\service\User as UserService; use app\api\service\User as UserService;
use app\common\enum\Setting as SettingEnum;
use app\common\enum\file\FileType as FileTypeEnum; use app\common\enum\file\FileType as FileTypeEnum;
use app\common\enum\Setting as SettingEnum;
use app\common\library\storage\Driver as StorageDriver; use app\common\library\storage\Driver as StorageDriver;
use app\common\service\BaseService; use app\common\service\BaseService;
use cores\exception\BaseException; use cores\exception\BaseException;
@ -69,6 +69,24 @@ class Upload extends BaseService
return $this->record($fileInfo, $fileType, $userId); 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);
}
/** /**
* 实例化存储驱动 * 实例化存储驱动
* @param string $scene 上传场景 image和video * @param string $scene 上传场景 image和video

Loading…
Cancel
Save