UploadController.php 9.8 KB

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