fix:支付宝二维码
This commit is contained in:
@@ -0,0 +1,548 @@
|
||||
# 小程序二维码生成与展示业务逻辑
|
||||
|
||||
> 入口页面:`zlt-web/react-web-next/src/pages/orders/Tourism/components/QrcodeListModal.tsx`(打卡点二维码列表)
|
||||
>
|
||||
> 本文先把现有「微信小程序二维码」的生成与展示链路梳理清楚,再据此给出「支付宝小程序二维码」(`alipay.open.app.qrcode.create`)的对接设计方案。
|
||||
|
||||
---
|
||||
|
||||
## 一、微信小程序二维码业务逻辑
|
||||
|
||||
### 1.1 端到端流程概览
|
||||
|
||||
```
|
||||
React 表单
|
||||
│ POST /api-order/qrcode/withPlan
|
||||
▼
|
||||
order-center QrCodeController.saveOrUpdateQrcodeWithPlan
|
||||
│ 生成 codeId = UUID.randomUUID()
|
||||
│ Feign → file-center FileCenterService.uploadQrCodeFile(codeId)
|
||||
▼
|
||||
file-center FileController.uploadQrCodeFile
|
||||
│ 1) GET 微信 access_token
|
||||
│ 2) POST createwxaqrcode body: { "path": pagePath + codeId }
|
||||
│ 3) 拿到 byte[](JPEG)
|
||||
│ 4) IFileService.upload(codeId+".jpeg", inputStream) → OSS
|
||||
│ 5) 返回 { codeId, fileId, filePath }
|
||||
▼
|
||||
order-center 持久化:
|
||||
- qrcodes 表写入:codeId / fileId / filePath / tenantId
|
||||
- package_plan 表写入/更新:vlogCount / videoCount / photoCount / tourismFileIds
|
||||
```
|
||||
|
||||
### 1.2 关键代码定位
|
||||
|
||||
| 角色 | 文件 |
|
||||
|------|------|
|
||||
| 前端列表 + 预览 + 路由 | `zlt-web/react-web-next/src/pages/orders/Tourism/components/QrcodeListModal.tsx` |
|
||||
| 前端表单(新增/编辑) | `zlt-web/react-web-next/src/pages/orders/Tourism/components/QrcodeTourismForm.tsx` |
|
||||
| 前端 API 封装 | `zlt-web/react-web-next/src/services/orders.ts:79-101` |
|
||||
| 业务控制器 | `zlt-business/order-center/src/main/java/com/central/order/controller/QrCodeController.java` |
|
||||
| Feign 客户端 | `zlt-business/order-center/src/main/java/com/central/order/feign/FileCenterService.java:36` |
|
||||
| 文件中心控制器 | `zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java`(生成 L192、查询 URL L159) |
|
||||
| 实体 | `order-center/src/main/java/com/central/order/model/QrCode.java`(`@TableName("qrcodes")`) |
|
||||
| Mapper | `order-center/src/main/resources/mapper/QrCodeMapper.xml` |
|
||||
| OSS 抽象 | `zlt-business/file-center/src/main/java/com/central/file/service/impl/OssService.java` |
|
||||
| 智能 URL 路由 | `zlt-business/file-center/src/main/java/com/central/file/service/TimeWindowStorageService.java` |
|
||||
| 微信凭证配置 | Nacos:`zlt.security.tencent.{accessTokenUrl,getwxacodeUrl,pagePath}` |
|
||||
|
||||
### 1.3 生成业务逻辑(写入路径)
|
||||
|
||||
#### 1.3.1 前端入口(QrcodeTourismForm)
|
||||
|
||||
- `QrcodeListModal` 点击"新增" → 打开 `QrcodeTourismForm`。
|
||||
- 提交时调 `ordersApi.saveOrUpdateQrcodePlan(payload)`,`payload` 形如:
|
||||
|
||||
```ts
|
||||
{
|
||||
qrCode: { tenantId, tourismId, name, deviceSn, waylineId, price, status,
|
||||
templateId, enableVideoEdit, isTest, qrcodeType, showVideoEditOnly,
|
||||
enableCtripOrder, ctripProductId, qrDescribe, materialCount },
|
||||
vlogCount, videoCount, photoCount,
|
||||
tourismFileIds: number[]
|
||||
}
|
||||
```
|
||||
|
||||
后端入口:`POST /api-order/qrcode/withPlan` → `QrCodeController.saveOrUpdateQrcodeWithPlan`。
|
||||
|
||||
#### 1.3.2 后端编排(order-center)
|
||||
|
||||
文件:`order-center/.../controller/QrCodeController.java:194-254`。
|
||||
|
||||
1. 校验 `qrCode` 非空。
|
||||
2. **新增分支**(`qrCode.id == null`):
|
||||
- `codeId = UUID.randomUUID().toString()`。
|
||||
- Feign 调 `fileCenterService.uploadQrCodeFile(codeId)`。
|
||||
- 回填:`qrCode.codeId / filePath / fileId / tenantId(取自 tourism.tenantId)`。
|
||||
- `qrCodeService.saveOrUpdate(qrCode)` 写 `qrcodes` 表。
|
||||
3. **修改分支**:直接 `saveOrUpdate`,不重新生成图片。
|
||||
4. 同步 `PackagePlan`(按 `codeId` 找到则更新,否则新建),把 `vlogCount / videoCount / photoCount / tourismFileIds` 写进去(`tourismFileIds` 用逗号字符串保存)。
|
||||
|
||||
整体加 `@Transactional(rollbackFor = Exception.class)`。
|
||||
|
||||
#### 1.3.3 文件中心生成 + 入 OSS(file-center)
|
||||
|
||||
文件:`file-center/.../controller/FileController.java:192-255`。
|
||||
|
||||
```java
|
||||
@GetMapping("/qrcode/{codeId}")
|
||||
public Result<Map> uploadQrCodeFile(@PathVariable String codeId) {
|
||||
String accessToken = getAccessTokenUrl(); // 调 access_token 接口
|
||||
InputStream qr = getQRCode(accessToken, codeId); // 调 createwxaqrcode 拿字节
|
||||
FileInfo fileInfo = fileService.upload(codeId + ".jpeg", qr); // 写 OSS
|
||||
return Result.succeed(Map.of(
|
||||
"codeId", codeId,
|
||||
"fileId", fileInfo.getId(),
|
||||
"filePath", fileInfo.getPath()
|
||||
));
|
||||
}
|
||||
```
|
||||
|
||||
对应外部调用:
|
||||
|
||||
| 步骤 | 方法 | URL | 关键参数 |
|
||||
|------|------|-----|----------|
|
||||
| 取 access_token | `GET` | `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=...&secret=...` | 直接用 Nacos 里拼好的整串 URL |
|
||||
| 生成小程序码 | `POST` | `https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=<token>` | body: `{ "path": "/pages/link/link?uuid=<codeId>" }` |
|
||||
| 入 OSS | `IFileService.upload` | 抽象层 → `OssTemplate.upload(name, is, len, "image/jpeg")` | OSS 对象 key = `<codeId>.jpeg` |
|
||||
|
||||
要点:
|
||||
- `codeId`(UUID)一码三用:① 透传给小程序的 `?uuid=` 参数;② OSS 对象 key 前缀;③ 业务表 `qrcodes.code_id` 主键。
|
||||
- 用的是旧版 `createwxaqrcode`(仅传 `path`),**不是** `getwxacodeunlimit`:
|
||||
- 不支持 `scene`,参数都拼在 URL 里。
|
||||
- 小程序页面必须已发布。
|
||||
- 微信凭证目前是**全局静态**配置(`zlt.security.tencent.*`),未走多租户 `wechat_config` 表。
|
||||
- 上传时 `contentType` 写死 `image/jpeg`(`OssService.uploadFile`)。
|
||||
|
||||
### 1.4 展示业务逻辑(读取路径)
|
||||
|
||||
#### 1.4.1 后端列表(order-center)
|
||||
|
||||
文件:`QrCodeController.java:65-79`。
|
||||
|
||||
```java
|
||||
@GetMapping
|
||||
public PageResult list(@RequestParam Map<String,Object> params) {
|
||||
PageResult<QrCode> page = qrCodeService.findList(params); // qrcodes 表分页
|
||||
if (page.getResp_code() == 0) {
|
||||
for (QrCode q : page.getData()) {
|
||||
q.setViewUrl(fileCenterService.getViewUrlByPath(q.getFilePath())); // 实时签名
|
||||
}
|
||||
}
|
||||
return page;
|
||||
}
|
||||
```
|
||||
|
||||
#### 1.4.2 智能 URL 路由(file-center)
|
||||
|
||||
`FileController.getViewUrlByPath` → `TimeWindowStorageService.getSmartViewUrlByPath`:
|
||||
|
||||
- 热数据(`createTime <= zlt.storage.time-router.days`)→ Aliyun OSS 预签名 URL(可叠加 `cdn.oss.domain` = `cdn.goflytec.com`)。
|
||||
- 冷数据 → MinIO(S3)预签名(可叠加 `cdn.s3.domain` = `minicdn.goflytec.com`)。
|
||||
|
||||
#### 1.4.3 前端展示
|
||||
|
||||
- `QrcodeListModal.tsx:131-146`:列 `viewUrl` 用 `<img>` 直接渲染,缩略 60×60,点击放大走 Antd `Modal`(L327-331)。
|
||||
- `viewUrl` **不入库**(实体 `QrCode.viewUrl` 是 `transient`),每次列表都重新签名,避免预签名 URL 过期问题。
|
||||
- 表格其他扩展列:`code_id` / `wayline_id` 加复制按钮、`price` 单位为分(前端自动除以 100 显示元)、`templateId` 走 `ordersApi.getVideoTemplateByTemplateId` 懒加载映射成中文模板名。
|
||||
|
||||
### 1.5 数据库结构
|
||||
|
||||
#### 1.5.1 `qrcodes` 表(`@TableName("qrcodes")`)
|
||||
|
||||
| 字段 | 含义 |
|
||||
|------|------|
|
||||
| `id` | 主键 |
|
||||
| `code_id` | UUID。扫码透传 `uuid`、OSS 对象 key 前缀 |
|
||||
| `tenant_id` | 租户 ID(来自所属景区) |
|
||||
| `tourism_id` | 景区 ID |
|
||||
| `name` | 二维码名称 |
|
||||
| `wayline_id` / `device_sn` | 关联航线 / 设备 |
|
||||
| `price` | 价格(分) |
|
||||
| `template_id` | 视频模板 ID |
|
||||
| `file_id` / `file_path` | 文件中心返回的 fileId 与 OSS 路径 |
|
||||
| `status` | 0 启用 / 1 禁用 |
|
||||
| `enable_video_edit` | 是否一键成片 |
|
||||
| `material_count` | 素材数量 |
|
||||
| `is_test` | 是否测试码 |
|
||||
| `qrcode_type` | 1 游客下单 / 2 定时巡查 / 3 自定义 |
|
||||
| `show_video_edit_only` | 是否只显示一键成片 |
|
||||
| `enable_ctrip_order` / `ctrip_product_id` | 携程联动 |
|
||||
| `payee_account` / `payee_type` / `profit_rate` / `payee_status` / `payee_fail_reason` / `enable_profit` | 分润 |
|
||||
| `qr_describe` | 描述 |
|
||||
| `is_del` / `create_time` / `update_time` | 软删 + 审计 |
|
||||
|
||||
> `view_url` 在实体里是 `transient`,**不持久化**,每次查询动态生成。
|
||||
|
||||
#### 1.5.2 `package_plan` 表(关联用 `code_id`)
|
||||
|
||||
- `vlog_count` / `video_count` / `photo_count`
|
||||
- `tourism_file_ids`:逗号拼接的素材 ID 列表
|
||||
|
||||
### 1.6 隐含约定(移植到其他平台时要小心)
|
||||
|
||||
1. `codeId` 是业务主键,**写库前一刻才生成**,并立即用于"上传文件名 / 三方接口入参 / 表字段"三处,三处必须保持一致。
|
||||
2. 文件 `viewUrl` 永远是临时签名,前端不要做缓存(包括复制走的"图片地址")。
|
||||
3. 上传与生成是同一次 RPC(Feign 调用),失败时通过 `@Transactional` 整体回滚,不会留下"DB 有记录但 OSS 没文件"的脏数据。
|
||||
4. WeChat 这条链路目前 **不是多租户**(凭证写死在 Nacos)。如果要按租户挂不同小程序,需要走 `wechat_config` + `WxMaService` 模式,本文档不涉及。
|
||||
|
||||
---
|
||||
|
||||
## 二、支付宝小程序二维码 — 对接设计
|
||||
|
||||
> 目标接口:`alipay.open.app.qrcode.create` —— [官方文档](https://opendocs.alipay.com/mini/a25c5d8f_alipay.open.app.qrcode.create?pathHash=2334bbff&scene=common)
|
||||
|
||||
### 2.0 两条核心设计原则(与微信链路的关键差异)
|
||||
|
||||
**原则 A — 一行业务、两张图。**
|
||||
微信码与支付宝码指向**同一个打卡点**,业务参数(航线、价格、设备、模板、分润、素材列表……)完全一致;用户扫哪张码进哪个小程序,但落到后端都是同一个 `codeId`,查同一行 `qrcodes`。
|
||||
|
||||
> 因此 **不引入 `provider` 列、不在 `qrcodes` 中写两条记录**。改为:在同一行上挂第二张图的 `file_id` / `file_path`,列表里同行展示两张缩略图。
|
||||
|
||||
**原则 B — 支付宝二维码图片走 S3,不进智能路由。**
|
||||
支付宝的二维码字节直接存 MinIO(S3),读取时也直接调 S3 预签名 URL(或 CDN 域名 `minicdn.goflytec.com`),**不走 `TimeWindowStorageService` 的冷热路由**。微信码继续走当前 OSS + 智能路由不变。
|
||||
|
||||
> 这样能让支付宝资产生命周期与 S3 完全对齐,便于独立审计和迁移。
|
||||
|
||||
### 2.1 现有可复用资产盘点
|
||||
|
||||
| 资产 | 位置 | 用途 |
|
||||
|------|------|------|
|
||||
| 支付宝 SDK | `zlt-alipay-spring-boot-starter/pom.xml`(`alipay-sdk-java 4.40.816.ALL`) | 提供 `AlipayOpenAppQrcodeCreateRequest` |
|
||||
| 多租户配置表 | `alipay_config`(实体 `AlipayConfig`) | `type=MINI` 即小程序配置(按你的决策选 MINI) |
|
||||
| 客户端工厂 | `AlipayClientFactory.createAlipayClient(AlipayConfig)` | 已支持公钥 / 证书模式 + 缓存 |
|
||||
| Feign 客户端 | `AlipayConfigService.getConfig("MINI", tenantId)` | 取当前租户的小程序配置 |
|
||||
| 服务封装风格 | `AlipayPayService` / `AlipayLoginService` / `AlipayRefundService` | 仿照同风格新增 `AlipayQrcodeService` |
|
||||
| S3 上传能力 | `IFileService.uploadForS3(MultipartFile)`(已有)<br>`S3Template.upload(name, InputStream)`(已有) | 字节直入 S3,绕过 OSS |
|
||||
| S3 预签名 | `S3Service.getViewUrlByPath(path)` 60 分钟 | 列表 `alipayViewUrl` 实时签名来源 |
|
||||
|
||||
### 2.2 接口规范摘要(`alipay.open.app.qrcode.create`)
|
||||
|
||||
请求 bizContent:
|
||||
|
||||
| 字段 | 必填 | 说明 |
|
||||
|------|------|------|
|
||||
| `url_param` | 是 | 小程序页面路径,如 `pages/link/link`(不带前导 `/`) |
|
||||
| `query_param` | 否 | URL 参数串,如 `uuid=<codeId>`(多个用 `&` 拼) |
|
||||
| `describe` | 是 | 业务描述,≤16 字符 |
|
||||
|
||||
响应字段:
|
||||
|
||||
| 字段 | 说明 |
|
||||
|------|------|
|
||||
| `qr_code_url` | 支付宝 CDN 上的二维码图片 URL(PNG),临时链接 → **必须立即拉回字节落 S3** |
|
||||
|
||||
调用注意:
|
||||
- 应用需在支付宝开放平台开通对应能力。
|
||||
- `url_param` 必须是已发布版本中存在的 `app.json` 页面。
|
||||
- ISV 模式下要带 `app_auth_token`(沿用 `AlipayPayService` 的处理方式)。
|
||||
|
||||
### 2.3 数据模型(同行双图)
|
||||
|
||||
#### 2.3.1 数据库迁移
|
||||
|
||||
新建 `order-center/src/main/resources/sql/add_qrcode_alipay_fields.sql`:
|
||||
|
||||
```sql
|
||||
-- 在同一行 qrcodes 上挂支付宝二维码图(与微信码共用 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)';
|
||||
```
|
||||
|
||||
> **不加索引**:支付宝码不参与查询过滤,只是同行附属资源。
|
||||
>
|
||||
> **不加 `provider` 列**:原因见 §2.0 原则 A。
|
||||
|
||||
#### 2.3.2 实体改造
|
||||
|
||||
`QrCode.java` 新增字段:
|
||||
|
||||
```java
|
||||
@Schema(description = "支付宝小程序二维码 fileId(S3)")
|
||||
private String alipayFileId;
|
||||
|
||||
@Schema(description = "支付宝小程序二维码对象路径(S3 bucket/objectKey)")
|
||||
private String alipayFilePath;
|
||||
|
||||
@Schema(description = "支付宝小程序二维码访问 URL(运行时填充,不入库)")
|
||||
private transient String alipayViewUrl;
|
||||
```
|
||||
|
||||
`QrCodeMapper.xml` `findList` 不需要改(无新过滤条件)。
|
||||
|
||||
### 2.4 调用流程
|
||||
|
||||
#### 2.4.1 生成(写入路径)
|
||||
|
||||
支付宝码独立于 `withPlan` 主流程,不和创建打卡点耦合。**入口设计为:对一条已存在的 `qrcodes` 行调用一次"生成支付宝码"**:
|
||||
|
||||
```
|
||||
React 列表行 → 点击「生成支付宝二维码」
|
||||
│ POST /api-order/qrcode/{id}/alipay
|
||||
▼
|
||||
order-center QrCodeController.generateAlipayQrcode(id)
|
||||
│ 1) qrCode = qrCodeService.getById(id)(必须存在 codeId、tenantId)
|
||||
│ 2) qrCodeUrl = AlipayQrcodeService.createMiniQrcode(
|
||||
│ tenantId = qrCode.tenantId,
|
||||
│ codeId = qrCode.codeId,
|
||||
│ urlParam = ${zlt.security.alipay.pagePath},
|
||||
│ queryParam = uuid=<codeId>,
|
||||
│ describe = trim16(qrCode.name))
|
||||
│ 3) Feign → file-center FileCenterService.uploadQrCodeToS3ByUrl(
|
||||
│ codeId, qrCodeUrl, "png")
|
||||
▼
|
||||
file-center FileController.uploadQrCodeToS3ByUrl
|
||||
│ RestTemplate 拉取 qrCodeUrl 字节
|
||||
│ IFileService.uploadForS3(<codeId>.png, inputStream) ← 直接走 S3,不走 OSS
|
||||
│ 返回 { codeId, fileId, filePath }
|
||||
▼
|
||||
order-center 回填
|
||||
- qrcodes.alipay_file_id = fileId
|
||||
- qrcodes.alipay_file_path = filePath
|
||||
- update by id
|
||||
```
|
||||
|
||||
幂等:再次调用即重新生成(旧 `alipay_file_path` 被覆盖;不主动删 S3 旧对象,由 S3 生命周期策略自行清理)。
|
||||
|
||||
#### 2.4.2 展示(读取路径)
|
||||
|
||||
`QrCodeController.list` 在补 `viewUrl` 时**额外**补 `alipayViewUrl`:
|
||||
|
||||
```java
|
||||
for (QrCode q : page.getData()) {
|
||||
q.setViewUrl(fileCenterService.getViewUrlByPath(q.getFilePath())); // 微信,走智能路由
|
||||
if (q.getAlipayFilePath() != null) {
|
||||
q.setAlipayViewUrl(
|
||||
fileCenterService.getS3ViewUrlByPath(q.getAlipayFilePath())); // 支付宝,强制走 S3
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`getS3ViewUrlByPath` 是 file-center 新增端点,内部直调 `S3Service.getViewUrlByPath(path)`,**不进 `TimeWindowStorageService`**。
|
||||
|
||||
### 2.5 拆分到代码层
|
||||
|
||||
#### 2.5.1 Alipay 二维码服务(zlt-alipay-spring-boot-starter)
|
||||
|
||||
新增 `AlipayQrcodeService`:
|
||||
|
||||
```java
|
||||
public class AlipayQrcodeService {
|
||||
|
||||
@Autowired private AlipayClientFactory clientFactory;
|
||||
@Autowired private AlipayConfigService alipayConfigService;
|
||||
|
||||
public String createMiniQrcode(String tenantId, String codeId,
|
||||
String urlParam, String queryKey, String describe) {
|
||||
AlipayConfig cfg = alipayConfigService.getConfig("MINI", tenantId);
|
||||
if (cfg == null) throw new RuntimeException("支付宝小程序配置不存在: " + tenantId);
|
||||
AlipayClient client = clientFactory.createAlipayClient(cfg);
|
||||
|
||||
AlipayOpenAppQrcodeCreateRequest req = new AlipayOpenAppQrcodeCreateRequest();
|
||||
JSONObject biz = new JSONObject();
|
||||
biz.put("url_param", urlParam); // 如 pages/link/link
|
||||
biz.put("query_param", queryKey + "=" + codeId);// 如 uuid=<codeId>
|
||||
biz.put("describe", trim16(describe));
|
||||
req.setBizContent(biz.toJSONString());
|
||||
|
||||
if (Boolean.TRUE.equals(cfg.getIsServiceMode()) && cfg.getAuthToken() != null) {
|
||||
req.putOtherTextParam("app_auth_token", cfg.getAuthToken());
|
||||
}
|
||||
|
||||
AlipayOpenAppQrcodeCreateResponse resp = client.execute(req);
|
||||
if (!resp.isSuccess()) {
|
||||
throw new RuntimeException("支付宝小程序码生成失败: " + resp.getSubMsg());
|
||||
}
|
||||
return resp.getQrCodeUrl();
|
||||
}
|
||||
|
||||
private String trim16(String s) {
|
||||
if (s == null) return "二维码";
|
||||
return s.length() > 16 ? s.substring(0, 16) : s;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
并在 `AlipayAutoConfiguration` 注册为 `@Bean`。
|
||||
|
||||
#### 2.5.2 文件中心新增端点(file-center)
|
||||
|
||||
需要两个端点 + 一个新的 `IFileService` 重载:
|
||||
|
||||
**`IFileService.uploadForS3(String name, InputStream is)`**(新方法,强制 S3):
|
||||
|
||||
```java
|
||||
@Override
|
||||
public FileInfo uploadForS3(String name, InputStream inputStream) throws IOException {
|
||||
if (s3Template == null) throw new RuntimeException("S3未配置");
|
||||
ObjectInfo info = s3Template.upload(name, inputStream);
|
||||
FileInfo fileInfo = FileUtil.getFileInfo(name, inputStream);
|
||||
fileInfo.setPath(info.getObjectPath());
|
||||
fileInfo.setUrl(buildCdnUrl(info.getObjectPath()));
|
||||
fileInfo.setSource("s3");
|
||||
baseMapper.insert(fileInfo);
|
||||
return fileInfo;
|
||||
}
|
||||
```
|
||||
|
||||
**`FileController` 新增两个端点**:
|
||||
|
||||
```java
|
||||
/** 支付宝小程序码:拉外部 URL 字节,直接入 S3(不进 OSS / 不进智能路由) */
|
||||
@GetMapping("/qrcode/s3/by-url")
|
||||
public Result<Map> uploadQrCodeToS3ByUrl(@RequestParam String codeId,
|
||||
@RequestParam String qrUrl,
|
||||
@RequestParam(defaultValue = "png") String suffix) throws Exception {
|
||||
ResponseEntity<byte[]> resp = restTemplate.exchange(qrUrl, HttpMethod.GET, HttpEntity.EMPTY, byte[].class);
|
||||
if (!resp.getStatusCode().is2xxSuccessful() || resp.getBody() == null) {
|
||||
return Result.failed("拉取二维码失败: " + resp.getStatusCode());
|
||||
}
|
||||
FileInfo fi = fileService.uploadForS3(codeId + "." + suffix,
|
||||
new ByteArrayInputStream(resp.getBody()));
|
||||
return Result.succeed(Map.of("codeId", codeId, "fileId", fi.getId(), "filePath", fi.getPath()));
|
||||
}
|
||||
|
||||
/** 直接 S3 预签名(绕过 TimeWindowStorageService) */
|
||||
@GetMapping("/files/s3/path")
|
||||
public String getS3ViewUrlByPath(@RequestParam String path) {
|
||||
return s3Service.getViewUrlByPath(path);
|
||||
}
|
||||
```
|
||||
|
||||
`FileCenterService`(Feign) + `FileCenterServiceFallbackFactory` 同步加:
|
||||
|
||||
```java
|
||||
@GetMapping("/qrcode/s3/by-url")
|
||||
Result<Map> uploadQrCodeToS3ByUrl(@RequestParam("codeId") String codeId,
|
||||
@RequestParam("qrUrl") String qrUrl,
|
||||
@RequestParam("suffix") String suffix);
|
||||
|
||||
@GetMapping("/files/s3/path")
|
||||
String getS3ViewUrlByPath(@RequestParam("path") String path);
|
||||
```
|
||||
|
||||
#### 2.5.3 业务控制器入口(order-center)
|
||||
|
||||
`QrCodeController` 新增:
|
||||
|
||||
```java
|
||||
@Operation(summary = "为已存在的二维码生成支付宝小程序码")
|
||||
@PostMapping("/{id}/alipay")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Result<QrCode> generateAlipayQrcode(@PathVariable Long id) throws Exception {
|
||||
QrCode q = qrCodeService.getById(id);
|
||||
Assert.notNull(q, "二维码不存在");
|
||||
Assert.hasText(q.getCodeId(), "codeId 缺失");
|
||||
Assert.hasText(q.getTenantId(),"tenantId 缺失");
|
||||
|
||||
String qrUrl = alipayQrcodeService.createMiniQrcode(
|
||||
q.getTenantId(), q.getCodeId(),
|
||||
alipayPagePath, alipayQueryKey, q.getName());
|
||||
|
||||
Result<Map> upload = fileCenterService.uploadQrCodeToS3ByUrl(
|
||||
q.getCodeId(), qrUrl, "png");
|
||||
if (upload.getResp_code() != 0) return Result.failed("支付宝二维码上传失败");
|
||||
|
||||
q.setAlipayFileId(upload.getData().get("fileId").toString());
|
||||
q.setAlipayFilePath(upload.getData().get("filePath").toString());
|
||||
qrCodeService.updateById(q);
|
||||
return Result.succeed(q, "支付宝二维码生成成功");
|
||||
}
|
||||
```
|
||||
|
||||
`list(...)` 末尾追加补 `alipayViewUrl` 的逻辑(见 §2.4.2)。
|
||||
|
||||
配置:
|
||||
|
||||
```properties
|
||||
# zlt-config 各 application-*.properties
|
||||
zlt.security.alipay.pagePath=pages/link/link
|
||||
zlt.security.alipay.queryKey=uuid
|
||||
```
|
||||
|
||||
#### 2.5.4 前端改造
|
||||
|
||||
**`QrcodeItem.tsx`** 新增字段:
|
||||
|
||||
```ts
|
||||
alipayFileId?: string;
|
||||
alipayFilePath?: string;
|
||||
alipayViewUrl?: string;
|
||||
```
|
||||
|
||||
**`services/orders.ts`** 新增:
|
||||
|
||||
```ts
|
||||
generateAlipayQrcode: async (id: number) => {
|
||||
const res = await request(`/api-order/qrcode/${id}/alipay`, { method: 'POST' });
|
||||
return (res as any).data || res;
|
||||
},
|
||||
```
|
||||
|
||||
**`QrcodeListModal.tsx`** 改动:
|
||||
- 新增列「支付宝二维码」(在现有「二维码」列右侧):
|
||||
- `alipayViewUrl` 存在 → 渲染缩略图,点击放大(复用现有 `previewOpen` Modal)。
|
||||
- 不存在 → 渲染 `<Button size="small">生成</Button>`,点击调 `generateAlipayQrcode(id)` 并刷新表格。
|
||||
- 新增/编辑表单 **不动**。当前业务字段仍由 `QrcodeTourismForm` 管,支付宝码是行级附属操作。
|
||||
- 操作列可加「重新生成支付宝码」入口(与列内按钮等价,便于已生成的码重置)。
|
||||
|
||||
### 2.6 风险点与回归项
|
||||
|
||||
| 风险 | 缓解 |
|
||||
|------|------|
|
||||
| `qr_code_url` 临时 URL 拉取期间过期 | 拿到后立即拉取并入 S3;任何步骤失败 → `@Transactional` 回滚,行上 `alipay_file_path` 保持原值 |
|
||||
| ISV 模式 `app_auth_token` 缺失 | `AlipayQrcodeService` 自动注入,与 `AlipayPayService` 一致 |
|
||||
| `describe` 限 16 字 | 服务里 `trim16()` 兜底 |
|
||||
| `url_param` 必须是已发布页面 | 配置项独立 (`zlt.security.alipay.pagePath`);上线前由开放平台审核保证 |
|
||||
| 旧数据兼容 | 新列允许 NULL;老数据 `alipayViewUrl=null`,前端按"未生成"展示 |
|
||||
| 文件中心新接口安全 | `/qrcode/s3/by-url` / `/files/s3/path` 仅供 Feign 内调,可由网关白名单或 IP 限制 |
|
||||
| 同一 codeId 重复点击"生成" | 接口幂等:每次会拿新 `qrCodeUrl`,覆盖 `alipay_file_path`;S3 旧对象不主动删,靠生命周期策略 |
|
||||
| S3 未配置导致 `uploadForS3` 抛错 | 控制器层捕获并返回 `Result.failed`,前端弹错;不影响微信码 |
|
||||
|
||||
### 2.7 上线步骤
|
||||
|
||||
1. 跑 `add_qrcode_alipay_fields.sql`。
|
||||
2. 部署 `zlt-alipay-spring-boot-starter`、`order-center`、`file-center`(一起,因 Feign 契约改动)。
|
||||
3. 在 `alipay_config` 录入 `type=MINI` 配置(appId / 私钥 / 支付宝公钥)。
|
||||
4. 支付宝开放平台开通能力 + 配置 `pages/link/link` 页面白名单。
|
||||
5. 后台对一条已有微信码的行点「生成支付宝码」,验证:
|
||||
- 列表新列出现支付宝缩略图,点击放大正常;
|
||||
- 用支付宝扫码进入小程序对应页,`options.uuid` 与微信扫码后取到的一致;
|
||||
- 进入业务流程(下单等)后端读取 `qrcodes.code_id` 命中**同一行**,价格 / 航线 / 设备 / 模板等均正确。
|
||||
|
||||
---
|
||||
|
||||
## 附录 A:相关 SQL/接口对照表
|
||||
|
||||
| 用途 | 微信 | 支付宝 |
|
||||
|------|------|--------|
|
||||
| 凭证来源 | Nacos `zlt.security.tencent.*`(全局) | DB `alipay_config(type=MINI, tenant_id=?)` |
|
||||
| 二维码接口 | `cgi-bin/wxaapp/createwxaqrcode` | `alipay.open.app.qrcode.create` |
|
||||
| 透传参数 | URL `?uuid=<codeId>` | `query_param=uuid=<codeId>` |
|
||||
| 接口返回 | `byte[]`(JPEG) | `qr_code_url`(PNG,需二次拉取) |
|
||||
| 存储后端 | OSS(智能路由 OSS↔MinIO) | **S3(MinIO,固定)** |
|
||||
| 存储文件名 | `<codeId>.jpeg` | `<codeId>.png` |
|
||||
| 业务表行 | 同一行 `qrcodes`(共享 `code_id` 与全部业务字段) | |
|
||||
| 行内字段 | `file_id` / `file_path` / `view_url`(transient) | `alipay_file_id` / `alipay_file_path` / `alipay_view_url`(transient) |
|
||||
| 写入入口 | `POST /qrcode/withPlan`(创建打卡点时同时生成) | `POST /qrcode/{id}/alipay`(按行追加) |
|
||||
| 读取入口(拼图 URL) | `GET /files/path`(智能路由) | `GET /files/s3/path`(直连 S3) |
|
||||
| 文件中心上传端点 | `GET /qrcode/{codeId}` | `GET /qrcode/s3/by-url?codeId=&qrUrl=&suffix=png` |
|
||||
|
||||
## 附录 B:核心文件清单(变更预期)
|
||||
|
||||
- 新增:
|
||||
- `zlt-business/order-center/src/main/resources/sql/add_qrcode_alipay_fields.sql`
|
||||
- `zlt-commons/zlt-alipay-spring-boot-starter/.../service/AlipayQrcodeService.java`
|
||||
- 修改:
|
||||
- `zlt-commons/zlt-alipay-spring-boot-starter/.../config/AlipayAutoConfiguration.java`(注册 Bean)
|
||||
- `zlt-business/file-center/.../service/IFileService.java`(新增 `uploadForS3(name, InputStream)` 重载)
|
||||
- `zlt-business/file-center/.../service/impl/AbstractIFileService.java`(实现新重载)
|
||||
- `zlt-business/file-center/.../controller/FileController.java`(新增 `/qrcode/s3/by-url`、`/files/s3/path`)
|
||||
- `zlt-business/order-center/.../feign/FileCenterService.java`(新增 Feign 方法)
|
||||
- `zlt-business/order-center/.../feign/fallback/FileCenterServiceFallbackFactory.java`
|
||||
- `zlt-business/order-center/.../controller/QrCodeController.java`(新增 `POST /qrcode/{id}/alipay`,`list` 补 `alipayViewUrl`)
|
||||
- `zlt-business/order-center/.../model/QrCode.java`(新增 `alipayFileId` / `alipayFilePath` / `alipayViewUrl`)
|
||||
- `zlt-config` 各 `application-*.properties`(`zlt.security.alipay.pagePath` / `queryKey`)
|
||||
- 前端:`QrcodeItem.tsx` / `QrcodeListModal.tsx` / `services/orders.ts`
|
||||
- 前端:`QrcodeItem.tsx` / `QrcodeTourismForm.tsx` / `QrcodeListModal.tsx` / `services/orders.ts`
|
||||
Reference in New Issue
Block a user