| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- namespace ghs\controllers;
- use bizHd\goods\services\CategoryService;
- use common\components\business;
- use common\components\dirUtil;
- use common\components\noticeUtil;
- use common\components\stringUtil;
- use Yii;
- use common\components\util;
- use common\components\oss;
- class UploadController extends BaseController
- {
- public $guestAccess = ['save-img', 'save-file'];
- //保存图片上传
- 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; // 变动:$this->sjId 改为 $this->mainId
- if (intval($mainId) == 0) {
- Yii::error('------- lose token -------');
- noticeUtil::push('save-file 无token进行上传文件');
- }
- $shopId = $this->shopId;
- $month = date("Ym");
- $date = date("d");
- $path = dirUtil::getImgUploadDir() . "/{$mainId}/{$shopId}/{$month}/{$date}/";
- if (!file_exists($path)) {
- //检查是否有该文件夹,如果没有就创建,并给予最高权限
- mkdir($path, 0700, true);
- }
- $preFileName = stringUtil::uniqueFileName();
- $newFile = $preFileName . ".jpg";
- $fullPathFile = $path . $newFile;
- if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
- $img = "{$uploadPath}/{$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');
- }
- }
- //上传图片接口
- public function actionSaveImg()
- {
- header('Content-type:text/html;charset=utf-8');
- $base64 = file_get_contents('php://input');
- $base64_image_content = str_replace("content=", "", urldecode($base64));
- $categoryIds = Yii::$app->request->post('categoryId', []);
- if (!empty($categoryIds)) {
- $categoryList = CategoryService::getByIds($categoryIds);
- foreach ($categoryList as $cat) {
- if ($cat['sjId'] != $this->sjId) {
- util::fail('请选择自己的分类');
- }
- }
- }
- //匹配出图片的格式
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
- $type = $result[2];
- $sjId = $this->sjId;
- $month = date("Ym");
- $date = date("d");
- $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
- if (!file_exists($path)) {
- //检查是否有该文件夹,如果没有就创建,并给予最高权限
- mkdir($path, 0700, true);
- }
- $preFileName = stringUtil::uniqueFileName();
- $newFile = $preFileName . ".{$type}";
- $fullPathFile = $path . $newFile;
- if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
- util::fail('上传失败!!');
- }
- $img = "uploads/{$sjId}/{$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);
- }
- util::fail('上传失败!');
- }
- //保存图片 ssh 2019.12.20
- public function actionSave()
- {
- header('Content-type:text/html;charset=utf-8');
- $base64_image_content = '';
- //匹配出图片的格式
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
- $type = $result[2];
- $sjId = 12358;
- $month = date("Ym");
- $date = date("d");
- $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
- if (!file_exists($path)) {
- //检查是否有该文件夹,如果没有就创建,并给予最高权限
- mkdir($path, 0700, true);
- }
- $preFileName = stringUtil::uniqueFileName();
- $newFile = $preFileName . ".{$type}";
- $fullPath = $path . $newFile;
- if (file_put_contents($fullPath, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
- util::fail('上传失败');
- }
- $img = "/uploads/{$sjId}/{$month}/{$date}/" . $newFile;
- $data = business::formatUploadImg($img);
- $data['shortUrl'] = $img;
- util::success($data);
- }
- }
- //上传图片--小菊
- public function actionNewSave()
- {
- header('Content-type:text/html;charset=utf-8');
- $base64 = file_get_contents('php://input');
- $base64_image_content = str_replace("content=", "", urldecode($base64));
- //匹配出图片的格式
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
- $type = $result[2];
- $sjId = 0;
- $month = date("Ym");
- $date = date("d");
- $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
- if (!file_exists($path)) {
- //检查是否有该文件夹,如果没有就创建,并给予最高权限
- mkdir($path, 0700, true);
- }
- $preFileName = stringUtil::uniqueFileName();
- $newFile = $preFileName . ".{$type}";
- $fullPathFile = $path . $newFile;
- if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
- util::fail('上传失败!!');
- }
- $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
- //上传oss 2021.3.18 lqh
- oss::uploadImage($img, $fullPathFile);
- $data = business::formatUploadImg($img);
- $data['shortUrl'] = $img;
- util::success($data);
- }
- util::fail('上传失败!');
- }
- //批量导入客户 ssh 2024426
- public function actionCustom()
- {
- $file = $_FILES['file'];
- if (!is_uploaded_file($file['tmp_name'])) {
- util::fail('please upload img');
- }
- $mainId = $this->sjId;
- $shopId = $this->shopId;
- $month = date("Ym");
- $date = date("d");
- $path = dirUtil::getImgUploadDir() . "/{$mainId}/{$shopId}/{$month}/{$date}/";
- if (!file_exists($path)) {
- //检查是否有该文件夹,如果没有就创建,并给予最高权限
- mkdir($path, 0700, true);
- }
- $preFileName = stringUtil::uniqueFileName();
- //$newFile = $preFileName . ".txt";
- $newFile = $preFileName . ".txt";
- $fullPathFile = $path . $newFile;
- if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
- // //队列方式去处理
- // $customerCachedKey = 'file_batch_create_customer_list';
- $value = $shopId . ',' . $fullPathFile;
- // Yii::$app->redis->executeCommand('LPUSH', [$customerCachedKey, $value]);
- noticeUtil::push("待批量导入客户的文件:" . $value, '15280215347');
- util::complete();
- } else {
- util::fail('fail');
- }
- }
- //删除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');
- }
- }
|