UploadController.php 7.6 KB

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