wang hou sheng 7 months ago
parent 375d476255
commit d684bd41b3
  1. 53
      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();
@ -77,9 +98,9 @@ class Upload extends Controller
// 构造请求头部信息 // 构造请求头部信息
$headers = array( $headers = array(
"Content-Type: multipart/form-data", "Content-Type: multipart/form-data",
"storeId: ".$this->storeId, "storeId: " . $this->storeId,
"Access-Token: ".$this->request->header("Access-Token"), "Access-Token: " . $this->request->header("Access-Token"),
"platform: ".$this->request->header("platform") "platform: " . $this->request->header("platform")
); );
//请求数据 //请求数据
$post_data = [ $post_data = [
@ -102,21 +123,23 @@ class Upload extends Controller
curl_close($ch); curl_close($ch);
$result = json_decode($result, true); $result = json_decode($result, true);
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';
if(!file_exists($savePath)) { if (!file_exists($savePath)) {
mkdir($savePath, 0777,true); mkdir($savePath, 0777, true);
} }
file_put_contents($savePath.$fileName, $imageFile); file_put_contents($savePath . $fileName, $imageFile);
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