对接小程序二维码

master
guochaojie 5 months ago
parent 88998e2432
commit 968e290a94

@ -0,0 +1,85 @@
package cc.yunxi.controller;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ApiModel("机柜扫码登录")
@Slf4j
@RestController("/qrcode")
public class QRCodeController {
private static final Map<String, String> FILE_CONTEXT = new ConcurrentHashMap<>();
// @ApiOperation("二维码校验文件")
// @GetMapping("/qrcode/{filename}")
// public void getQRCode(HttpServletResponse response, @PathVariable String filename) throws IOException {
// Resource resource = new ClassPathResource("static/" + filename);
//
// if (resource.exists() || resource.isReadable()) {
// // 设置响应头
// String contentType = null;
// try (InputStream inputStream = resource.getInputStream()) {
// contentType = "application/octet-stream";
// } catch (IOException e) {
// // 如果无法获取输入流,则回退到默认内容类型
// contentType = "application/octet-stream";
// }
//
// response.setContentType(contentType);
// response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"");
//
// try (InputStream inputStream = resource.getInputStream();
// OutputStream outputStream = response.getOutputStream()) {
//
// byte[] buffer = new byte[4096];
// int bytesRead = -1;
// while ((bytesRead = inputStream.read(buffer)) != -1) {
// outputStream.write(buffer, 0, bytesRead);
// }
// outputStream.flush();
// }
// } else {
// response.setStatus(HttpServletResponse.SC_NOT_FOUND);
// }
// }
@ApiOperation("二维码校验文件")
@GetMapping("/qrcode/{filename:.+}")
public ResponseEntity<String> getFile(@PathVariable String filename) {
if (StringUtils.hasText(filename)) {
String context = FILE_CONTEXT.get(filename);
if (StringUtils.hasText(context)) return ResponseEntity.ok(context);
}
Resource resource = new ClassPathResource("static/" + filename);
if (resource.exists() || resource.isReadable()) {
try {
Path path = resource.getFile().toPath();
String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
FILE_CONTEXT.put(filename, content);
return ResponseEntity.ok(content);
} catch (IOException e) {
e.printStackTrace();
log.error("读取校验文件失败: {}", e.getMessage());
}
}
return ResponseEntity.notFound().build();
}
}

@ -0,0 +1 @@
175babf4957dd6090b5b36b78cd51c78
Loading…
Cancel
Save