|
|
@@ -3,15 +3,12 @@
|
|
|
namespace ghs\controllers;
|
|
|
|
|
|
use bizHd\goods\services\CategoryService;
|
|
|
-use bizHd\goods\services\PicCategoryService;
|
|
|
-use bizHd\goods\services\PicService;
|
|
|
use common\components\business;
|
|
|
use common\components\dirUtil;
|
|
|
use common\components\noticeUtil;
|
|
|
use common\components\stringUtil;
|
|
|
use Yii;
|
|
|
use common\components\util;
|
|
|
-use yii\imagine\Image;
|
|
|
use common\components\oss;
|
|
|
|
|
|
class UploadController extends BaseController
|
|
|
@@ -22,6 +19,12 @@ class UploadController extends BaseController
|
|
|
//保存图片上传
|
|
|
public function actionSaveFile()
|
|
|
{
|
|
|
+ $uploadPath = 'uploads';
|
|
|
+ // 对上传的文件进行多文件夹管理
|
|
|
+ $rootPath = Yii::$app->request->post('rootPath');
|
|
|
+ if (!empty($rootPath) && in_array($rootPath, oss::ROOT_PATHS)) {
|
|
|
+ $uploadPath = $rootPath;
|
|
|
+ }
|
|
|
$file = $_FILES['file'];
|
|
|
if (!is_uploaded_file($file['tmp_name'])) {
|
|
|
util::fail('please upload img');
|
|
|
@@ -43,7 +46,7 @@ class UploadController extends BaseController
|
|
|
$newFile = $preFileName . ".jpg";
|
|
|
$fullPathFile = $path . $newFile;
|
|
|
if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
|
|
|
- $img = "uploads/{$mainId}/{$shopId}/{$month}/{$date}/" . $newFile;
|
|
|
+ $img = "{$uploadPath}/{$mainId}/{$shopId}/{$month}/{$date}/" . $newFile;
|
|
|
oss::uploadImage($img, $fullPathFile);
|
|
|
$data = business::formatUploadImg($img);
|
|
|
$data['shortUrl'] = $img;
|
|
|
@@ -203,4 +206,61 @@ class UploadController extends BaseController
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ //删除oss上的图片
|
|
|
+ public function actionDeleteFile()
|
|
|
+ {
|
|
|
+ $filePath = Yii::$app->request->post('filePath');
|
|
|
+ if(empty($filePath)){
|
|
|
+ util::fail('参数错误');
|
|
|
+ }
|
|
|
+ // 删除前判断文件是否存在
|
|
|
+ if(!oss::fileExist($filePath)){
|
|
|
+ Yii::error('file not exist: ' . $filePath);
|
|
|
+ util::fail('file not exist');
|
|
|
+ } else {
|
|
|
+ Yii::info('file exist: ' . $filePath);
|
|
|
+ }
|
|
|
+ // 删除OSS文件
|
|
|
+ try {
|
|
|
+ // 设置超时时间为15秒
|
|
|
+ set_time_limit(15);
|
|
|
+ $startTime = time();
|
|
|
+ $timeout = 15; // 15秒超时
|
|
|
+
|
|
|
+ oss::deleteObject($filePath);
|
|
|
+
|
|
|
+ // 检查是否超时
|
|
|
+ if (time() - $startTime > $timeout) {
|
|
|
+ throw new \Exception('删除文件操作超时');
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ util::fail('删除文件失败:' . $e->getMessage());
|
|
|
+ }
|
|
|
+ util::success('success');
|
|
|
+ }
|
|
|
+
|
|
|
+ //批量删除oss上的图片
|
|
|
+ public function actionDeleteFiles()
|
|
|
+ {
|
|
|
+ $filePaths = Yii::$app->request->post('filePaths');
|
|
|
+ if(empty($filePaths) || !is_array($filePaths)){
|
|
|
+ util::fail('参数错误');
|
|
|
+ }
|
|
|
+ foreach($filePaths as $filePath){
|
|
|
+ if(!oss::fileExist($filePath)){
|
|
|
+ Yii::error('file not exist: ' . $filePath);
|
|
|
+ util::fail('file not exist');
|
|
|
+ } else {
|
|
|
+ Yii::info('file exist: ' . $filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 删除OSS文件
|
|
|
+ try {
|
|
|
+ oss::deleteObjects($filePaths);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ util::fail('删除文件失败:' . $e->getMessage());
|
|
|
+ }
|
|
|
+ util::success('success');
|
|
|
+ }
|
|
|
+
|
|
|
}
|