UploadController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace ghs\controllers;
  3. use bizHd\goods\services\CategoryService;
  4. use common\components\business;
  5. use common\components\dirUtil;
  6. use common\components\noticeUtil;
  7. use common\components\stringUtil;
  8. use Yii;
  9. use common\components\util;
  10. use common\components\oss;
  11. class UploadController extends BaseController
  12. {
  13. public $guestAccess = ['save-img', 'save-file'];
  14. //保存图片上传
  15. public function actionSaveFile()
  16. {
  17. $uploadPath = 'uploads';
  18. // 对上传的文件进行多文件夹管理
  19. $rootPath = Yii::$app->request->post('rootPath');
  20. if (!empty($rootPath) && in_array($rootPath, oss::ROOT_PATHS)) {
  21. $uploadPath = $rootPath;
  22. }
  23. $file = $_FILES['file'];
  24. if (!is_uploaded_file($file['tmp_name'])) {
  25. util::fail('please upload img');
  26. }
  27. $mainId = $this->mainId; // 变动:$this->sjId 改为 $this->mainId
  28. if (intval($mainId) == 0) {
  29. Yii::error('------- lose token -------');
  30. noticeUtil::push('save-file 无token进行上传文件');
  31. return;
  32. }
  33. $shopId = $this->shopId;
  34. $month = date("Ym");
  35. $date = date("d");
  36. $path = dirUtil::getImgUploadDir() . "/{$mainId}/{$shopId}/{$month}/{$date}/";
  37. // 检查目录是否存在,如果不存在则尝试创建。
  38. // 使用 @ 抑制 mkdir 在目录已存在时(并发导致)的 warning。
  39. // 如果 mkdir 失败,则再次检查目录是否存在,以区分是并发创建成功还是真的创建失败。
  40. if (!is_dir($path) && !@mkdir($path, 0700, true) && !is_dir($path)) {
  41. throw new \yii\web\ServerErrorHttpException("目录 '{$path}' 创建失败。");
  42. }
  43. $preFileName = stringUtil::uniqueFileName();
  44. $newFile = $preFileName . ".jpg";
  45. $fullPathFile = $path . $newFile;
  46. if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
  47. $img = "{$uploadPath}/{$mainId}/{$shopId}/{$month}/{$date}/" . $newFile;
  48. oss::uploadImage($img, $fullPathFile);
  49. $data = business::formatUploadImg($img);
  50. $data['shortUrl'] = $img;
  51. $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
  52. util::success($data, 'ok');
  53. } else {
  54. util::fail('fail');
  55. }
  56. }
  57. //上传图片接口
  58. public function actionSaveImg()
  59. {
  60. header('Content-type:text/html;charset=utf-8');
  61. $base64 = file_get_contents('php://input');
  62. $base64_image_content = str_replace("content=", "", urldecode($base64));
  63. $categoryIds = Yii::$app->request->post('categoryId', []);
  64. if (!empty($categoryIds)) {
  65. $categoryList = CategoryService::getByIds($categoryIds);
  66. foreach ($categoryList as $cat) {
  67. if ($cat['sjId'] != $this->sjId) {
  68. util::fail('请选择自己的分类');
  69. }
  70. }
  71. }
  72. //匹配出图片的格式
  73. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  74. $type = $result[2];
  75. $sjId = $this->sjId;
  76. $month = date("Ym");
  77. $date = date("d");
  78. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  79. if (!file_exists($path)) {
  80. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  81. mkdir($path, 0700, true);
  82. }
  83. $preFileName = stringUtil::uniqueFileName();
  84. $newFile = $preFileName . ".{$type}";
  85. $fullPathFile = $path . $newFile;
  86. if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  87. util::fail('上传失败!!');
  88. }
  89. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  90. oss::uploadImage($img, $fullPathFile);
  91. $data = business::formatUploadImg($img);
  92. $data['shortUrl'] = $img;
  93. $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
  94. util::success($data);
  95. }
  96. util::fail('上传失败!');
  97. }
  98. //保存图片 ssh 2019.12.20
  99. public function actionSave()
  100. {
  101. header('Content-type:text/html;charset=utf-8');
  102. $base64_image_content = '';
  103. //匹配出图片的格式
  104. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  105. $type = $result[2];
  106. $sjId = 12358;
  107. $month = date("Ym");
  108. $date = date("d");
  109. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  110. if (!file_exists($path)) {
  111. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  112. mkdir($path, 0700, true);
  113. }
  114. $preFileName = stringUtil::uniqueFileName();
  115. $newFile = $preFileName . ".{$type}";
  116. $fullPath = $path . $newFile;
  117. if (file_put_contents($fullPath, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  118. util::fail('上传失败');
  119. }
  120. $img = "/uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  121. $data = business::formatUploadImg($img);
  122. $data['shortUrl'] = $img;
  123. util::success($data);
  124. }
  125. }
  126. //上传图片--小菊
  127. public function actionNewSave()
  128. {
  129. header('Content-type:text/html;charset=utf-8');
  130. $base64 = file_get_contents('php://input');
  131. $base64_image_content = str_replace("content=", "", urldecode($base64));
  132. //匹配出图片的格式
  133. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  134. $type = $result[2];
  135. $sjId = 0;
  136. $month = date("Ym");
  137. $date = date("d");
  138. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  139. if (!file_exists($path)) {
  140. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  141. mkdir($path, 0700, true);
  142. }
  143. $preFileName = stringUtil::uniqueFileName();
  144. $newFile = $preFileName . ".{$type}";
  145. $fullPathFile = $path . $newFile;
  146. if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  147. util::fail('上传失败!!');
  148. }
  149. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  150. //上传oss 2021.3.18 lqh
  151. oss::uploadImage($img, $fullPathFile);
  152. $data = business::formatUploadImg($img);
  153. $data['shortUrl'] = $img;
  154. util::success($data);
  155. }
  156. util::fail('上传失败!');
  157. }
  158. //批量导入客户 ssh 2024426
  159. public function actionCustom()
  160. {
  161. $file = $_FILES['file'];
  162. if (!is_uploaded_file($file['tmp_name'])) {
  163. util::fail('please upload img');
  164. }
  165. $mainId = $this->sjId;
  166. $shopId = $this->shopId;
  167. $month = date("Ym");
  168. $date = date("d");
  169. $path = dirUtil::getImgUploadDir() . "/{$mainId}/{$shopId}/{$month}/{$date}/";
  170. if (!file_exists($path)) {
  171. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  172. mkdir($path, 0700, true);
  173. }
  174. $preFileName = stringUtil::uniqueFileName();
  175. //$newFile = $preFileName . ".txt";
  176. $newFile = $preFileName . ".txt";
  177. $fullPathFile = $path . $newFile;
  178. if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
  179. // //队列方式去处理
  180. // $customerCachedKey = 'file_batch_create_customer_list';
  181. $value = $shopId . ',' . $fullPathFile;
  182. // Yii::$app->redis->executeCommand('LPUSH', [$customerCachedKey, $value]);
  183. noticeUtil::push("待批量导入客户的文件:" . $value, '15280215347');
  184. util::complete();
  185. } else {
  186. util::fail('fail');
  187. }
  188. }
  189. //删除oss上的图片
  190. public function actionDeleteFile()
  191. {
  192. $filePath = Yii::$app->request->post('filePath');
  193. if(empty($filePath)){
  194. util::fail('参数错误');
  195. }
  196. // 删除前判断文件是否存在
  197. if(!oss::fileExist($filePath)){
  198. Yii::error('file not exist: ' . $filePath);
  199. util::fail('file not exist');
  200. } else {
  201. Yii::info('file exist: ' . $filePath);
  202. }
  203. // 删除OSS文件
  204. try {
  205. // 设置超时时间为15秒
  206. set_time_limit(15);
  207. $startTime = time();
  208. $timeout = 15; // 15秒超时
  209. oss::deleteObject($filePath);
  210. // 检查是否超时
  211. if (time() - $startTime > $timeout) {
  212. throw new \Exception('删除文件操作超时');
  213. }
  214. } catch (\Exception $e) {
  215. util::fail('删除文件失败:' . $e->getMessage());
  216. }
  217. util::success('success');
  218. }
  219. //批量删除oss上的图片
  220. public function actionDeleteFiles()
  221. {
  222. $filePaths = Yii::$app->request->post('filePaths');
  223. if(empty($filePaths) || !is_array($filePaths)){
  224. util::fail('参数错误');
  225. }
  226. foreach($filePaths as $filePath){
  227. if(!oss::fileExist($filePath)){
  228. Yii::error('file not exist: ' . $filePath);
  229. util::fail('file not exist');
  230. } else {
  231. Yii::info('file exist: ' . $filePath);
  232. }
  233. }
  234. // 删除OSS文件
  235. try {
  236. oss::deleteObjects($filePaths);
  237. } catch (\Exception $e) {
  238. util::fail('删除文件失败:' . $e->getMessage());
  239. }
  240. util::success('success');
  241. }
  242. }