|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|