UploadController.php 9.5 KB

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