feat(system): 优化微信小程序码获取接口,直接返回图片字节

- 修改getOrderQRCodeUnlimited接口,支持直接返回图片字节流
- 移除保存二维码图片到服务器文件的逻辑
- 在未登录时,通过响应码401返回错误提示
- 设置响应内容类型为image/png,禁用缓存,直接输出二维码图片数据
- 简化接口实现,提升性能与用户体验
This commit is contained in:
2026-08-31 18:20:32 +08:00
parent 539819d474
commit f751d17771
@@ -35,7 +35,9 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -965,14 +967,16 @@ public class WxLoginController extends BaseController {
return fail("获取失败", null); return fail("获取失败", null);
} }
@Operation(summary = "获取微信小程序码-数量极多的业务场景(支持自定义页面与场景值)") @Operation(summary = "获取微信小程序码-数量极多的业务场景(直接返回图片字节,支持自定义页面与场景值)")
@GetMapping("/getOrderQRCodeUnlimited/{orderNo}") @GetMapping("/getOrderQRCodeUnlimited/{orderNo}")
public ApiResult<?> getOrderQRCodeUnlimited(@PathVariable("orderNo") String orderNo, public void getOrderQRCodeUnlimited(@PathVariable("orderNo") String orderNo,
@RequestParam(required = false) String page, @RequestParam(required = false) String page,
@RequestParam(required = false) String scene) { @RequestParam(required = false) String scene,
HttpServletResponse response) throws IOException {
final User loginUser = getLoginUser(); final User loginUser = getLoginUser();
if(loginUser == null){ if(loginUser == null){
return fail("请先登录"); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "请先登录");
return;
} }
String apiUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + getAccessToken(loginUser.getTenantId()); String apiUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + getAccessToken(loginUser.getTenantId());
final HashMap<String, Object> map = new HashMap<>(); final HashMap<String, Object> map = new HashMap<>();
@@ -985,17 +989,12 @@ public class WxLoginController extends BaseController {
byte[] qrCode = HttpRequest.post(apiUrl) byte[] qrCode = HttpRequest.post(apiUrl)
.body(JSON.toJSONString(map)) .body(JSON.toJSONString(map))
.execute().bodyBytes(); .execute().bodyBytes();
System.out.println("qrCode = " + qrCode);
// 保存的文件名称 // 直接将小程序码图片字节写回响应,不依赖 file.websoft.top 文件服务器
final String fileName = CommonUtil.randomUUID8().concat(".png"); response.setContentType("image/png");
// 保存路径 response.setHeader("Cache-Control", "no-cache");
String filePath = getUploadDir().concat("qrcode/") + fileName; response.getOutputStream().write(qrCode);
File file = FileUtil.writeBytes(qrCode, filePath); response.getOutputStream().flush();
if (file != null) {
return success(config.getFileServer().concat("/qrcode/").concat(fileName));
}
return fail("获取失败", null);
} }