UploadController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace ghs\controllers;
  3. use bizHd\goods\services\CategoryService;
  4. use bizHd\goods\services\PicCategoryService;
  5. use bizHd\goods\services\PicService;
  6. use common\components\business;
  7. use common\components\dirUtil;
  8. use common\components\noticeUtil;
  9. use common\components\stringUtil;
  10. use Yii;
  11. use common\components\util;
  12. use yii\imagine\Image;
  13. use common\components\oss;
  14. class UploadController extends BaseController
  15. {
  16. public $guestAccess = ['save-img', 'save-file'];
  17. //保存图片上传
  18. public function actionSaveFile()
  19. {
  20. $file = $_FILES['file'];
  21. if (!is_uploaded_file($file['tmp_name'])) {
  22. util::fail('please upload img');
  23. }
  24. $mainId = $this->sjId;
  25. $shopId = $this->shopId;
  26. $month = date("Ym");
  27. $date = date("d");
  28. $path = dirUtil::getImgUploadDir() . "/{$mainId}/{$shopId}/{$month}/{$date}/";
  29. if (!file_exists($path)) {
  30. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  31. mkdir($path, 0700, true);
  32. }
  33. $preFileName = stringUtil::uniqueFileName();
  34. $newFile = $preFileName . ".jpg";
  35. $fullPathFile = $path . $newFile;
  36. if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
  37. $img = "uploads/{$mainId}/{$shopId}/{$month}/{$date}/" . $newFile;
  38. oss::uploadImage($img, $fullPathFile);
  39. $data = business::formatUploadImg($img);
  40. $data['shortUrl'] = $img;
  41. $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
  42. util::success($data, 'ok');
  43. } else {
  44. util::fail('fail');
  45. }
  46. }
  47. //上传图片接口
  48. public function actionSaveImg()
  49. {
  50. header('Content-type:text/html;charset=utf-8');
  51. $base64 = file_get_contents('php://input');
  52. $base64_image_content = str_replace("content=", "", urldecode($base64));
  53. $categoryIds = Yii::$app->request->post('categoryId', []);
  54. if (!empty($categoryIds)) {
  55. $categoryList = CategoryService::getByIds($categoryIds);
  56. foreach ($categoryList as $cat) {
  57. if ($cat['sjId'] != $this->sjId) {
  58. util::fail('请选择自己的分类');
  59. }
  60. }
  61. }
  62. //匹配出图片的格式
  63. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  64. $type = $result[2];
  65. $sjId = $this->sjId;
  66. $month = date("Ym");
  67. $date = date("d");
  68. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  69. if (!file_exists($path)) {
  70. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  71. mkdir($path, 0700, true);
  72. }
  73. $preFileName = stringUtil::uniqueFileName();
  74. $newFile = $preFileName . ".{$type}";
  75. $fullPathFile = $path . $newFile;
  76. if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  77. util::fail('上传失败!!');
  78. }
  79. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  80. oss::uploadImage($img, $fullPathFile);
  81. $data = business::formatUploadImg($img);
  82. $data['shortUrl'] = $img;
  83. $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
  84. util::success($data);
  85. }
  86. util::fail('上传失败!');
  87. }
  88. //保存图片 ssh 2019.12.20
  89. public function actionSave()
  90. {
  91. header('Content-type:text/html;charset=utf-8');
  92. $base64_image_content = '';
  93. //匹配出图片的格式
  94. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  95. $type = $result[2];
  96. $sjId = 12358;
  97. $month = date("Ym");
  98. $date = date("d");
  99. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  100. if (!file_exists($path)) {
  101. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  102. mkdir($path, 0700, true);
  103. }
  104. $preFileName = stringUtil::uniqueFileName();
  105. $newFile = $preFileName . ".{$type}";
  106. $fullPath = $path . $newFile;
  107. if (file_put_contents($fullPath, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  108. util::fail('上传失败');
  109. }
  110. $img = "/uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  111. $data = business::formatUploadImg($img);
  112. $data['shortUrl'] = $img;
  113. util::success($data);
  114. }
  115. }
  116. //上传图片--小菊
  117. public function actionNewSave()
  118. {
  119. header('Content-type:text/html;charset=utf-8');
  120. $base64 = file_get_contents('php://input');
  121. $base64_image_content = str_replace("content=", "", urldecode($base64));
  122. //匹配出图片的格式
  123. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  124. $type = $result[2];
  125. $sjId = 0;
  126. $month = date("Ym");
  127. $date = date("d");
  128. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  129. if (!file_exists($path)) {
  130. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  131. mkdir($path, 0700, true);
  132. }
  133. $preFileName = stringUtil::uniqueFileName();
  134. $newFile = $preFileName . ".{$type}";
  135. $fullPathFile = $path . $newFile;
  136. if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  137. util::fail('上传失败!!');
  138. }
  139. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  140. //上传oss 2021.3.18 lqh
  141. oss::uploadImage($img, $fullPathFile);
  142. $data = business::formatUploadImg($img);
  143. $data['shortUrl'] = $img;
  144. util::success($data);
  145. }
  146. util::fail('上传失败!');
  147. }
  148. //批量导入客户 ssh 2024426
  149. public function actionCustom()
  150. {
  151. $file = $_FILES['file'];
  152. if (!is_uploaded_file($file['tmp_name'])) {
  153. util::fail('please upload img');
  154. }
  155. $mainId = $this->sjId;
  156. $shopId = $this->shopId;
  157. $month = date("Ym");
  158. $date = date("d");
  159. $path = dirUtil::getImgUploadDir() . "/{$mainId}/{$shopId}/{$month}/{$date}/";
  160. if (!file_exists($path)) {
  161. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  162. mkdir($path, 0700, true);
  163. }
  164. $preFileName = stringUtil::uniqueFileName();
  165. //$newFile = $preFileName . ".txt";
  166. $newFile = $preFileName . ".txt";
  167. $fullPathFile = $path . $newFile;
  168. if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
  169. // //队列方式去处理
  170. // $customerCachedKey = 'file_batch_create_customer_list';
  171. $value = $shopId . ',' . $fullPathFile;
  172. // Yii::$app->redis->executeCommand('LPUSH', [$customerCachedKey, $value]);
  173. noticeUtil::push("待批量导入客户的文件:" . $value, '15280215347');
  174. util::complete();
  175. } else {
  176. util::fail('fail');
  177. }
  178. }
  179. }