|
|
@@ -2,21 +2,58 @@
|
|
|
|
|
|
namespace mall\controllers;
|
|
|
|
|
|
+use common\components\business;
|
|
|
+use common\components\dirUtil;
|
|
|
+use common\components\noticeUtil;
|
|
|
+use common\components\oss;
|
|
|
+use common\components\stringUtil;
|
|
|
use Yii;
|
|
|
use common\components\util;
|
|
|
|
|
|
class UploadController extends BaseController
|
|
|
{
|
|
|
-
|
|
|
- public $guestAccess = [];
|
|
|
-
|
|
|
- //上传图片接口
|
|
|
- public function actionSaveImg()
|
|
|
- {
|
|
|
- $img = '/uploads/201905/25/jpg/19052591518_12358.jpg';
|
|
|
- $data = business::formatUploadImg($img);
|
|
|
- $data['shortUrl'] = $img;
|
|
|
- util::success($data);
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ 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');
|
|
|
+ }
|
|
|
+ $mainId = $this->mainId;
|
|
|
+ if (intval($mainId) == 0) {
|
|
|
+ Yii::error('------- lose token -------');
|
|
|
+ noticeUtil::push('save-file 无token进行上传文件');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $shopId = $this->shopId;
|
|
|
+ $month = date("Ym");
|
|
|
+ $date = date("d");
|
|
|
+ $path = dirUtil::getImgUploadDir() . "/wxAvatar/{$mainId}/{$shopId}/{$month}/{$date}/";
|
|
|
+ // 检查目录是否存在,如果不存在则尝试创建。
|
|
|
+ // 使用 @ 抑制 mkdir 在目录已存在时(并发导致)的 warning。
|
|
|
+ // 如果 mkdir 失败,则再次检查目录是否存在,以区分是并发创建成功还是真的创建失败。
|
|
|
+ if (!is_dir($path) && !@mkdir($path, 0700, true) && !is_dir($path)) {
|
|
|
+ throw new \yii\web\ServerErrorHttpException("目录 '{$path}' 创建失败。");
|
|
|
+ }
|
|
|
+ $preFileName = stringUtil::uniqueFileName();
|
|
|
+ $newFile = $preFileName . ".jpg";
|
|
|
+ $fullPathFile = $path . $newFile;
|
|
|
+ if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
|
|
|
+ $img = "{$uploadPath}/wxAvatar/{$mainId}/{$shopId}/{$month}/{$date}/" . $newFile;
|
|
|
+ oss::uploadImage($img, $fullPathFile);
|
|
|
+ $data = business::formatUploadImg($img);
|
|
|
+ $data['shortUrl'] = $img;
|
|
|
+ $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
|
|
|
+ util::success($data, 'ok');
|
|
|
+ } else {
|
|
|
+ util::fail('fail');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|