interfaceInfo['upload_config']) || empty($this->interfaceInfo['upload_config'])) { abort(508, '接口错误,上传参数配置有误! '); } // 获取接口信息 $this->uploadConfig = json_decode($this->interfaceInfo['upload_config'], true); $requestSettings = json_decode($this->interfaceInfo['request_settings'], true); if (empty($requestSettings)) { $fileKey = 'file'; } else { $fileKey = $this->uploadConfig['file'] ?? "file"; } $type = $this->uploadConfig['type']; if ($type == 2) { $fileObj = $this->requestData[$fileKey]; if (empty($fileObj)) { abort(508, '上传有误!文件不存在!'); } unset($this->requestData[$fileKey]); // 图片base64 return $this->upBase64($fileObj); } elseif ($type == 3) { // 二进制流 dd('暂不支持'); } else { $fileObj = Request::file($fileKey); if (empty($fileObj)) { abort(508, '上传有误!文件不存在!'); } unset($this->requestData[$fileKey]); return $this->uploadFilePath($fileObj); } } /** * 上传 * * @param UploadedFile $file * @return JsonResponse */ private function uploadFilePath(UploadedFile $file): JsonResponse { $fileName = $file->getClientOriginalName(); $size = $file->getSize(); $path = $this->uploadConfig['savePath']; $isRandomFilename = $this->uploadConfig['isRandomFilename']; if (empty($path)) { $uploadDir = '/upload/' . mt_rand(0, 10000) . '/' . mt_rand(0, 10000); } else { $uploadDir = $this->getSavePath($path); } $disk_dir = public_path('static/' . $uploadDir); if (!file_exists($disk_dir) || !is_dir($disk_dir)) { @mkdir($disk_dir, 0777, true); } $extension = $file->getClientOriginalExtension(); if ($isRandomFilename) { $fileName = md5(microtime()) . '.' . $extension; } $file->move($disk_dir, $fileName); $path = '/static' . $uploadDir . '/' . $fileName; $data = [ 'filename' => $fileName, 'path' => $path, 'size' => $size, 'extension' => $extension ]; $isSuccess = $this->saveData($data); if ($isSuccess) { return responseMessage(1001, '', $data); } else { return responseMessage(2001, '上传失败,请重试!'); } } /** * 上传图片base64 * * @param $base64Data * @return JsonResponse */ public function upBase64($base64Data): JsonResponse { //截取字符串 $base64Data = preg_replace('/data:image\/png;base64,/is', '', $base64Data); $img = base64_decode($base64Data); if (empty($img)) { return responseMessage(2002, '上传失败'); } $path = $this->uploadConfig['savePath']; if (empty($path)) { $uploadDir = '/upload/' . mt_rand(0, 10000) . '/' . mt_rand(0, 10000) . '/'; } else { $uploadDir = $this->getSavePath($path); } $disk_dir = public_path('static/' . $uploadDir); if (!file_exists($disk_dir) || !is_dir($disk_dir)) { @mkdir($disk_dir, 0777, true); } $fileName = md5(microtime()) . '.png'; $filePath = $disk_dir . '/' . $fileName; if (!file_put_contents($filePath, $img)) { //失败 return responseMessage(2003, '上传失败,请重试!'); } else { //成功 $data = [ 'mid' => Str::random(12), 'filename' => $fileName, 'path' => $path, 'size' => strlen($img), 'extension' => 'png' ]; $isSuccess = $this->saveData($data); if ($isSuccess) { $data['id'] = $isSuccess; return responseMessage(1001, '', $data); } else { return responseMessage(2001, '上传失败,请重试!'); } } } private function saveData($data) { $find = DB::connection($this->getDbConnection())->table($this->table); if (!isset($this->requestData['mid'])) { $this->requestData['mid'] = Str::random(12); } if (!isset($this->requestData['created_at'])) { $this->requestData['created_at'] = time(); } if (!isset($this->requestData['updated_at'])) { $this->requestData['updated_at'] = time(); } $data = array_merge($this->requestData, $data); return $find->insertGetId($data); } /** * 获取保存路径,替换变量或随机数 * * @param $path * @return string */ private function getSavePath($path): string { // 变量替换 preg_match_all('/\$\{(.*?)\}/i', $path, $arr); if (isset($arr[1]) && $arr[1]) { foreach ($arr[1] as $keyName) { $path = preg_replace('/\$\{' . $keyName . '\}/i', $this->requestData[$keyName], $path); } } // 随机数替换 preg_match_all('/\$\[(.*?)\]/i', $path, $arr2); if (isset($arr2[1]) && $arr2[1]) { foreach ($arr2[1] as $keyName) { $exArr = explode('-', $keyName); if ($exArr[0]) { $start = intval($exArr[0]); } else { $start = 0; } if ($exArr[1]) { $end = intval($exArr[1]); } else { $end = 10000; } $path = preg_replace('/\$\[' . $keyName . '\]/i', mt_rand($start, $end), $path); } } return '/' . trim($path, '/'); } }