fix:支付宝二维码

This commit is contained in:
2026-06-17 18:37:12 +08:00
parent 0ac3bece42
commit 73d7c819cf
15 changed files with 959 additions and 2 deletions
@@ -201,6 +201,46 @@ public class FileController {
return Result.succeed(map);
}
/**
* 拉取外部图片 URL 字节并强制入 S3(不进 OSS / 不进智能路由)
* 用于支付宝小程序二维码:alipay.open.app.qrcode.create 返回临时 qr_code_url,需立刻拉回入 S3
*/
@GetMapping("/qrcode/s3/by-url")
public Result<Map> uploadQrCodeToS3ByUrl(@RequestParam("codeId") String codeId,
@RequestParam("qrUrl") String qrUrl,
@RequestParam(value = "suffix", required = false, defaultValue = "png") String suffix) throws Exception {
if (codeId == null || codeId.isEmpty() || qrUrl == null || qrUrl.isEmpty()) {
return Result.failed("codeId 和 qrUrl 不能为空");
}
ResponseEntity<byte[]> resp = restTemplate.exchange(qrUrl, HttpMethod.GET, HttpEntity.EMPTY, byte[].class);
if (!resp.getStatusCode().is2xxSuccessful() || resp.getBody() == null || resp.getBody().length == 0) {
return Result.failed("拉取二维码图片失败: " + resp.getStatusCode());
}
String fileName = codeId + "." + suffix;
FileInfo fileInfo = fileService.uploadForS3(fileName, new ByteArrayInputStream(resp.getBody()));
Map<String, Object> map = new LinkedHashMap<>();
map.put("codeId", codeId);
map.put("fileId", fileInfo.getId());
map.put("filePath", fileInfo.getPath());
return Result.succeed(map);
}
/**
* 直接 S3 预签名(绕过 TimeWindowStorageService 智能路由)
*/
@GetMapping("/files/s3/path")
public String getS3ViewUrlByPath(@RequestParam("path") String path) {
if (path == null || path.isEmpty()) {
return "";
}
try {
return fileService.getS3ViewUrlByPath(path);
} catch (Exception e) {
log.warn("获取S3预签名URL失败: path={}, err={}", path, e.getMessage());
return "";
}
}
@PostMapping("/path")
public FileInfo creatFileInfo(@RequestBody String path) {
return fileService.createFile(path);
@@ -20,6 +20,11 @@ public interface IFileService extends IService<FileInfo> {
FileInfo uploadForS3(MultipartFile file ) throws Exception;
/**
* 强制以 S3 方式上传字节流(用于支付宝小程序二维码等需绕开 OSS 的场景)
*/
FileInfo uploadForS3(String name, InputStream inputStream) throws Exception;
FileInfo upload(String name, InputStream inputStream) throws Exception;
PageResult<FileInfo> findList(Map<String, Object> params);
@@ -43,4 +48,9 @@ public interface IFileService extends IService<FileInfo> {
String getViewUrlByPathAndExpires(String name,int expires);
String getS3UrlByPath(String path);
/**
* 强制返回 S3 预签名 URL(绕开智能路由,用于支付宝小程序二维码展示)
*/
String getS3ViewUrlByPath(String path);
}
@@ -79,6 +79,26 @@ public abstract class AbstractIFileService extends ServiceImpl<FileMapper, FileI
return fileInfo;
}
@Override
public FileInfo uploadForS3(String name, InputStream inputStream) {
if (s3Template == null) {
throw new RuntimeException("S3服务未配置,无法使用S3上传");
}
FileInfo fileInfo = FileUtil.getFileInfo(name, inputStream);
if (!fileInfo.getName().contains(FILE_SPLIT)) {
throw new IllegalArgumentException("缺少后缀名");
}
ObjectInfo objectInfo = s3Template.upload(name, inputStream);
if (objectInfo == null) {
throw new RuntimeException("S3上传失败: " + name);
}
fileInfo.setPath(objectInfo.getObjectPath());
fileInfo.setUrl(buildCdnUrl(objectInfo.getObjectPath()));
fileInfo.setSource("s3");
baseMapper.insert(fileInfo);
return fileInfo;
}
/**
* 构建CDN URL
* @param objectPath 对象路径 (格式: bucket/objectKey)
@@ -161,6 +181,32 @@ public abstract class AbstractIFileService extends ServiceImpl<FileMapper, FileI
public abstract String getDownloadUrl(String path);
/**
* 强制返回 S3 预签名 URL(绕开智能路由 / 智能存储路由),用于支付宝小程序二维码等需要稳定 S3 入口的场景。
* 默认返回 60 分钟预签名;S3 未配置时抛运行时异常,由调用方决定是否降级。
*/
@Override
public String getS3ViewUrlByPath(String path) {
if (s3Template == null) {
throw new RuntimeException("S3服务未配置,无法获取S3预签名URL");
}
if (path == null || path.isEmpty()) {
return "";
}
try {
int splitIndex = path.lastIndexOf('/');
if (splitIndex < 0) {
return "";
}
String bucketName = path.substring(0, splitIndex);
String objectName = path.substring(splitIndex + 1);
return s3Template.getViewUrl(bucketName, objectName, 60);
} catch (Exception e) {
log.warn("获取S3预签名URL失败: path={}, err={}", path, e.getMessage());
return "";
}
}
@Override
public FileInfo getFileInfo(String id) {
return baseMapper.selectById(id);