package com.example.mini_program.controller; import com.example.mini_program.common.Result; import com.example.mini_program.service.WxService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import java.util.Map; @Slf4j @RestController @RequestMapping("/api/wx-mini") @RequiredArgsConstructor public class WxController { private final WxService wxService; /** * 生成小程序码 */ @PostMapping("/wxacode") public Result getWxacode(@RequestBody Map request) { String scene = request.get("scene"); String page = request.get("page"); String envVersion = request.get("envVersion"); if (scene == null || scene.trim().isEmpty()) { return Result.error("scene不能为空"); } page = (page != null && !page.trim().isEmpty()) ? page : "pages/scan/result/index"; Integer width = parseWidth(request.get("width")); if (envVersion == null || envVersion.trim().isEmpty()) { envVersion = wxService.getDefaultEnvVersion(); } try { String base64Image = wxService.getUnlimitedWxacode(scene, page, width, envVersion); return Result.success(base64Image); } catch (Exception e) { log.error("生成小程序码失败", e); return Result.error("生成小程序码失败: " + e.getMessage()); } } private Integer parseWidth(String widthStr) { if (widthStr == null || widthStr.trim().isEmpty()) { return 430; } try { int width = Integer.parseInt(widthStr); return width > 0 ? width : 430; } catch (NumberFormatException e) { log.warn("width参数格式错误: {}", widthStr); return 430; } } }