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);
@@ -1,5 +1,6 @@
package com.central.order.controller;
import com.central.alipay.service.AlipayQrcodeService;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.common.utils.StringUtils;
@@ -17,10 +18,11 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
@@ -59,6 +61,15 @@ public class QrCodeController {
@Autowired
private IPackagePlanService packagePlanService;
@Autowired
private AlipayQrcodeService alipayQrcodeService;
@Value("${zlt.security.alipay.pagePath:pages/link/link}")
private String alipayPagePath;
@Value("${zlt.security.alipay.queryKey:uuid}")
private String alipayQueryKey;
/**
* 列表
*/
@@ -73,6 +84,9 @@ public class QrCodeController {
if (pageResult.getResp_code() == 0){
for (QrCode qrCode: pageResult.getData()){
qrCode.setViewUrl(fileCenterService.getViewUrlByPath(qrCode.getFilePath()));
if (qrCode.getAlipayFilePath() != null && !qrCode.getAlipayFilePath().isEmpty()) {
qrCode.setAlipayViewUrl(fileCenterService.getS3ViewUrlByPath(qrCode.getAlipayFilePath()));
}
}
}
return pageResult;
@@ -252,4 +266,42 @@ public class QrCodeController {
return result;
}
@Operation(summary = "为已存在的二维码生成支付宝小程序码(同行追加,不另起一行)")
@PostMapping("/{id}/alipay")
@Transactional(rollbackFor = Exception.class)
public Result<QrCode> generateAlipayQrcode(@PathVariable("id") Long id) throws Exception {
QrCode qrCode = qrCodeService.getById(id);
Assert.notNull(qrCode, "二维码不存在");
Assert.hasText(qrCode.getCodeId(), "codeId 缺失");
Assert.hasText(qrCode.getTenantId(), "tenantId 缺失");
String qrCodeUrl = alipayQrcodeService.createMiniQrcode(
qrCode.getTenantId(),
qrCode.getCodeId(),
alipayPagePath,
alipayQueryKey,
qrCode.getName());
if (qrCodeUrl == null || qrCodeUrl.isEmpty()) {
return Result.failed("支付宝二维码生成失败:qr_code_url 为空");
}
Result<Map> upload = fileCenterService.uploadQrCodeToS3ByUrl(qrCode.getCodeId(), qrCodeUrl, "png");
if (upload == null || upload.getResp_code() != 0) {
return Result.failed("支付宝二维码上传 S3 失败");
}
Object fileId = upload.getData().get("fileId");
Object filePath = upload.getData().get("filePath");
if (fileId == null || filePath == null) {
return Result.failed("支付宝二维码上传响应缺少 fileId/filePath");
}
qrCode.setAlipayFileId(fileId.toString());
qrCode.setAlipayFilePath(filePath.toString());
qrCodeService.updateById(qrCode);
// 列表展示用:同步把 view url 拉一次
qrCode.setViewUrl(fileCenterService.getViewUrlByPath(qrCode.getFilePath()));
qrCode.setAlipayViewUrl(fileCenterService.getS3ViewUrlByPath(qrCode.getAlipayFilePath()));
return Result.succeed(qrCode, "支付宝二维码生成成功");
}
}
@@ -36,6 +36,20 @@ public interface FileCenterService {
@GetMapping("/qrcode/{codeId}")
Result<Map> uploadQrCodeFile(@PathVariable("codeId") String codeId) throws Exception;
/**
* 拉取外部图片 URL 字节并强制入 S3(用于支付宝小程序二维码)
*/
@GetMapping("/qrcode/s3/by-url")
Result<Map> uploadQrCodeToS3ByUrl(@RequestParam("codeId") String codeId,
@RequestParam("qrUrl") String qrUrl,
@RequestParam(value = "suffix", required = false, defaultValue = "png") String suffix) throws Exception;
/**
* 强制返回 S3 预签名 URL(绕开智能路由)
*/
@GetMapping("/files/s3/path")
String getS3ViewUrlByPath(@RequestParam("path") String path);
@PostMapping("/path")
FileInfo creatFileInfo(@RequestBody String path);
@@ -41,6 +41,17 @@ public class FileCenterServiceFallbackFactory implements FallbackFactory<FileCen
return null;
}
@Override
public Result<Map> uploadQrCodeToS3ByUrl(String codeId, String qrUrl, String suffix) throws Exception {
log.warn("file-center 调用失败,支付宝二维码上传降级返回 null, codeId={}", codeId, cause);
return null;
}
@Override
public String getS3ViewUrlByPath(String path) {
return "";
}
@Override
public FileInfo creatFileInfo(String path) {
return null;
@@ -132,4 +132,22 @@ public class QrCode extends SuperEntity {
@Schema(description = "携程产品ID")
private Long ctripProductId;
/**
* 支付宝小程序二维码 fileId(S3)
*/
@Schema(description = "支付宝小程序二维码 fileId(S3)")
private String alipayFileId;
/**
* 支付宝小程序二维码对象路径(S3 bucket/objectKey)
*/
@Schema(description = "支付宝小程序二维码对象路径(S3 bucket/objectKey)")
private String alipayFilePath;
/**
* 支付宝小程序二维码访问 URL(运行时填充,不入库)
*/
@Schema(description = "支付宝小程序二维码访问 URL(运行时填充,不入库)")
private transient String alipayViewUrl;
}
@@ -0,0 +1,6 @@
-- 二维码表添加支付宝小程序二维码相关字段
-- 用途:在同一行 qrcodes 上挂支付宝小程序二维码图(与微信码共用 code_id 与所有业务字段)
-- 一行业务、两张图:用户扫微信码进微信小程序,扫支付宝码进支付宝小程序,后端用同一 code_id 命中同一行
ALTER TABLE qrcodes
ADD COLUMN alipay_file_id VARCHAR(64) NULL COMMENT '支付宝小程序二维码 fileId(S3)',
ADD COLUMN alipay_file_path VARCHAR(512) NULL COMMENT '支付宝小程序二维码对象 path(S3 bucket/objectKey)';