ShareController.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.train.controller;
  2. import com.train.common.Result;
  3. import com.train.service.WechatService;
  4. import io.swagger.v3.oas.annotations.Operation;
  5. import io.swagger.v3.oas.annotations.tags.Tag;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.util.StringUtils;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.RequestBody;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import javax.annotation.Resource;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.UUID;
  19. /**
  20. * 分享物料:生成带参小程序码(沙龙/课程海报用)。
  21. * 对齐 cfc(E:\cfc)ShareController#qrcode 的语义:{page, scene} → 小程序码图片。
  22. * scene 里携带业务 id 与邀请码(如 id=12&ref=ABC123),落地页 onLoad 解析 scene 后建立推荐关系。
  23. */
  24. @Slf4j
  25. @Tag(name = "分享")
  26. @RestController
  27. @RequestMapping("/api/share")
  28. public class ShareController {
  29. /** 微信 getwxacodeunlimit 的 scene 最长 32 个可见字符 */
  30. private static final int SCENE_MAX_LEN = 32;
  31. @Resource
  32. private WechatService wechatService;
  33. @Value("${upload.base-dir}")
  34. private String baseDir;
  35. /**
  36. * 生成分享小程序码。
  37. * @param body { page: "pages/salon/list", scene: "id=12&ref=ABC123" }
  38. * @return { qrCodeUrl, scene };test-mode 或微信接口失败时 qrCodeUrl 为空串,前端降级为本地占位海报
  39. */
  40. @Operation(summary = "生成分享小程序码")
  41. @PostMapping("/qrcode")
  42. public Result<Map<String, Object>> qrcode(@RequestBody Map<String, String> body) {
  43. String page = body != null ? body.get("page") : null;
  44. String scene = body != null ? body.get("scene") : null;
  45. if (!StringUtils.hasText(page)) {
  46. page = "pages/salon/list";
  47. }
  48. page = page.trim();
  49. while (page.startsWith("/")) {
  50. page = page.substring(1);
  51. }
  52. if (!page.startsWith("pages/")) {
  53. return Result.error("page 参数非法");
  54. }
  55. if (!StringUtils.hasText(scene)) {
  56. return Result.error("scene 参数不能为空");
  57. }
  58. scene = fitScene(scene.trim());
  59. Map<String, Object> row = new HashMap<>();
  60. row.put("qrCodeUrl", "");
  61. row.put("scene", scene);
  62. byte[] bytes = wechatService.generateWxacodeBytes(scene, page);
  63. if (bytes == null || bytes.length == 0) {
  64. // test-mode 或微信接口失败:不阻塞分享,前端用本地海报降级
  65. return Result.success(row);
  66. }
  67. File dir = new File(baseDir);
  68. if (!dir.exists() && !dir.mkdirs()) {
  69. log.error("小程序码目录创建失败: {}", baseDir);
  70. return Result.success(row);
  71. }
  72. String filename = "share_" + UUID.randomUUID().toString().replace("-", "").substring(0, 16) + ".png";
  73. try (FileOutputStream fos = new FileOutputStream(new File(dir, filename))) {
  74. fos.write(bytes);
  75. } catch (Exception e) {
  76. log.error("小程序码写入失败", e);
  77. return Result.success(row);
  78. }
  79. row.put("qrCodeUrl", "/uploads/" + filename);
  80. return Result.success(row);
  81. }
  82. /**
  83. * scene 超长时优先保留邀请码(ref=xxx),避免截断把推荐关系切断。
  84. * 形如 id=12&ref=ABC123:超长则退化为 ref=ABC123(落地页仍能捕获邀请码)。
  85. */
  86. private String fitScene(String scene) {
  87. if (scene.length() <= SCENE_MAX_LEN) {
  88. return scene;
  89. }
  90. int idx = scene.indexOf("&ref=");
  91. if (idx >= 0) {
  92. String refPart = scene.substring(idx + 1);
  93. if (refPart.length() <= SCENE_MAX_LEN) {
  94. return refPart;
  95. }
  96. }
  97. return scene.substring(0, SCENE_MAX_LEN);
  98. }
  99. }