package com.train.controller; import com.train.common.Result; import com.train.service.WechatService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.io.File; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import java.util.UUID; /** * 分享物料:生成带参小程序码(沙龙/课程海报用)。 * 对齐 cfc(E:\cfc)ShareController#qrcode 的语义:{page, scene} → 小程序码图片。 * scene 里携带业务 id 与邀请码(如 id=12&ref=ABC123),落地页 onLoad 解析 scene 后建立推荐关系。 */ @Slf4j @Tag(name = "分享") @RestController @RequestMapping("/api/share") public class ShareController { /** 微信 getwxacodeunlimit 的 scene 最长 32 个可见字符 */ private static final int SCENE_MAX_LEN = 32; @Resource private WechatService wechatService; @Value("${upload.base-dir}") private String baseDir; /** * 生成分享小程序码。 * @param body { page: "pages/salon/list", scene: "id=12&ref=ABC123" } * @return { qrCodeUrl, scene };test-mode 或微信接口失败时 qrCodeUrl 为空串,前端降级为本地占位海报 */ @Operation(summary = "生成分享小程序码") @PostMapping("/qrcode") public Result> qrcode(@RequestBody Map body) { String page = body != null ? body.get("page") : null; String scene = body != null ? body.get("scene") : null; if (!StringUtils.hasText(page)) { page = "pages/salon/list"; } page = page.trim(); while (page.startsWith("/")) { page = page.substring(1); } if (!page.startsWith("pages/")) { return Result.error("page 参数非法"); } if (!StringUtils.hasText(scene)) { return Result.error("scene 参数不能为空"); } scene = fitScene(scene.trim()); Map row = new HashMap<>(); row.put("qrCodeUrl", ""); row.put("scene", scene); byte[] bytes = wechatService.generateWxacodeBytes(scene, page); if (bytes == null || bytes.length == 0) { // test-mode 或微信接口失败:不阻塞分享,前端用本地海报降级 return Result.success(row); } File dir = new File(baseDir); if (!dir.exists() && !dir.mkdirs()) { log.error("小程序码目录创建失败: {}", baseDir); return Result.success(row); } String filename = "share_" + UUID.randomUUID().toString().replace("-", "").substring(0, 16) + ".png"; try (FileOutputStream fos = new FileOutputStream(new File(dir, filename))) { fos.write(bytes); } catch (Exception e) { log.error("小程序码写入失败", e); return Result.success(row); } row.put("qrCodeUrl", "/uploads/" + filename); return Result.success(row); } /** * scene 超长时优先保留邀请码(ref=xxx),避免截断把推荐关系切断。 * 形如 id=12&ref=ABC123:超长则退化为 ref=ABC123(落地页仍能捕获邀请码)。 */ private String fitScene(String scene) { if (scene.length() <= SCENE_MAX_LEN) { return scene; } int idx = scene.indexOf("&ref="); if (idx >= 0) { String refPart = scene.substring(idx + 1); if (refPart.length() <= SCENE_MAX_LEN) { return refPart; } } return scene.substring(0, SCENE_MAX_LEN); } }