UploaderDb.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace App\Services\Interface\Database\Uploader;
  3. use App\Services\Interface\Database\CommonDb;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Http\UploadedFile;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Request;
  8. use Illuminate\Support\Str;
  9. class UploaderDb extends CommonDb
  10. {
  11. private array $uploadConfig = [];
  12. public function __construct(array $interfaceInfo, array $assembleInfo, array $assembleColumns, array $requestData, array $columnIdToCodes)
  13. {
  14. parent::__construct($interfaceInfo, $assembleInfo, $assembleColumns, $requestData, $columnIdToCodes);
  15. }
  16. public function exec()
  17. {
  18. if (!isset($this->interfaceInfo['upload_config']) || empty($this->interfaceInfo['upload_config'])) {
  19. abort(508, '接口错误,上传参数配置有误! ');
  20. }
  21. // 获取接口信息
  22. $this->uploadConfig = json_decode($this->interfaceInfo['upload_config'], true);
  23. $requestSettings = json_decode($this->interfaceInfo['request_settings'], true);
  24. if (empty($requestSettings)) {
  25. $fileKey = 'file';
  26. } else {
  27. $fileKey = $this->uploadConfig['file'] ?? "file";
  28. }
  29. $type = $this->uploadConfig['type'];
  30. if ($type == 2) {
  31. $fileObj = $this->requestData[$fileKey];
  32. if (empty($fileObj)) {
  33. abort(508, '上传有误!文件不存在!');
  34. }
  35. unset($this->requestData[$fileKey]);
  36. // 图片base64
  37. return $this->upBase64($fileObj);
  38. } elseif ($type == 3) {
  39. // 二进制流
  40. dd('暂不支持');
  41. } else {
  42. $fileObj = Request::file($fileKey);
  43. if (empty($fileObj)) {
  44. abort(508, '上传有误!文件不存在!');
  45. }
  46. unset($this->requestData[$fileKey]);
  47. return $this->uploadFilePath($fileObj);
  48. }
  49. }
  50. /**
  51. * 上传
  52. *
  53. * @param UploadedFile $file
  54. * @return JsonResponse
  55. */
  56. private function uploadFilePath(UploadedFile $file): JsonResponse
  57. {
  58. $fileName = $file->getClientOriginalName();
  59. $size = $file->getSize();
  60. $path = $this->uploadConfig['savePath'];
  61. $isRandomFilename = $this->uploadConfig['isRandomFilename'];
  62. if (empty($path)) {
  63. $uploadDir = '/upload/' . mt_rand(0, 10000) . '/' . mt_rand(0, 10000);
  64. } else {
  65. $uploadDir = $this->getSavePath($path);
  66. }
  67. $disk_dir = public_path('static/' . $uploadDir);
  68. if (!file_exists($disk_dir) || !is_dir($disk_dir)) {
  69. @mkdir($disk_dir, 0777, true);
  70. }
  71. $extension = $file->getClientOriginalExtension();
  72. if ($isRandomFilename) {
  73. $fileName = md5(microtime()) . '.' . $extension;
  74. }
  75. $file->move($disk_dir, $fileName);
  76. $path = '/static' . $uploadDir . '/' . $fileName;
  77. $data = [
  78. 'filename' => $fileName,
  79. 'path' => $path,
  80. 'size' => $size,
  81. 'extension' => $extension
  82. ];
  83. $isSuccess = $this->saveData($data);
  84. if ($isSuccess) {
  85. return responseMessage(1001, '', $data);
  86. } else {
  87. return responseMessage(2001, '上传失败,请重试!');
  88. }
  89. }
  90. /**
  91. * 上传图片base64
  92. *
  93. * @param $base64Data
  94. * @return JsonResponse
  95. */
  96. public function upBase64($base64Data): JsonResponse
  97. {
  98. //截取字符串
  99. $base64Data = preg_replace('/data:image\/png;base64,/is', '', $base64Data);
  100. $img = base64_decode($base64Data);
  101. if (empty($img)) {
  102. return responseMessage(2002, '上传失败');
  103. }
  104. $path = $this->uploadConfig['savePath'];
  105. if (empty($path)) {
  106. $uploadDir = '/upload/' . mt_rand(0, 10000) . '/' . mt_rand(0, 10000) . '/';
  107. } else {
  108. $uploadDir = $this->getSavePath($path);
  109. }
  110. $disk_dir = public_path('static/' . $uploadDir);
  111. if (!file_exists($disk_dir) || !is_dir($disk_dir)) {
  112. @mkdir($disk_dir, 0777, true);
  113. }
  114. $fileName = md5(microtime()) . '.png';
  115. $filePath = $disk_dir . '/' . $fileName;
  116. if (!file_put_contents($filePath, $img)) { //失败
  117. return responseMessage(2003, '上传失败,请重试!');
  118. } else { //成功
  119. $data = [
  120. 'mid' => Str::random(12),
  121. 'filename' => $fileName,
  122. 'path' => $path,
  123. 'size' => strlen($img),
  124. 'extension' => 'png'
  125. ];
  126. $isSuccess = $this->saveData($data);
  127. if ($isSuccess) {
  128. $data['id'] = $isSuccess;
  129. return responseMessage(1001, '', $data);
  130. } else {
  131. return responseMessage(2001, '上传失败,请重试!');
  132. }
  133. }
  134. }
  135. private function saveData($data)
  136. {
  137. $find = DB::connection($this->getDbConnection())->table($this->table);
  138. if (!isset($this->requestData['mid'])) {
  139. $this->requestData['mid'] = Str::random(12);
  140. }
  141. if (!isset($this->requestData['created_at'])) {
  142. $this->requestData['created_at'] = time();
  143. }
  144. if (!isset($this->requestData['updated_at'])) {
  145. $this->requestData['updated_at'] = time();
  146. }
  147. $data = array_merge($this->requestData, $data);
  148. return $find->insertGetId($data);
  149. }
  150. /**
  151. * 获取保存路径,替换变量或随机数
  152. *
  153. * @param $path
  154. * @return string
  155. */
  156. private function getSavePath($path): string
  157. {
  158. // 变量替换
  159. preg_match_all('/\$\{(.*?)\}/i', $path, $arr);
  160. if (isset($arr[1]) && $arr[1]) {
  161. foreach ($arr[1] as $keyName) {
  162. $path = preg_replace('/\$\{' . $keyName . '\}/i', $this->requestData[$keyName], $path);
  163. }
  164. }
  165. // 随机数替换
  166. preg_match_all('/\$\[(.*?)\]/i', $path, $arr2);
  167. if (isset($arr2[1]) && $arr2[1]) {
  168. foreach ($arr2[1] as $keyName) {
  169. $exArr = explode('-', $keyName);
  170. if ($exArr[0]) {
  171. $start = intval($exArr[0]);
  172. } else {
  173. $start = 0;
  174. }
  175. if ($exArr[1]) {
  176. $end = intval($exArr[1]);
  177. } else {
  178. $end = 10000;
  179. }
  180. $path = preg_replace('/\$\[' . $keyName . '\]/i', mt_rand($start, $end), $path);
  181. }
  182. }
  183. return '/' . trim($path, '/');
  184. }
  185. }