feat[通用模块]:getViewUrlByName在文件存储中fileName,小程序二维码生成并上传逻辑完善,二维码管理后台功能实现以及其他逻辑优化
This commit is contained in:
+33
-14
@@ -4,10 +4,10 @@ import com.central.common.model.FileInfo;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.file.service.IFileService;
|
||||
import com.central.file.utils.HexToJpeg;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.collections.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
@@ -17,6 +17,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -30,14 +31,17 @@ public class FileController {
|
||||
@Autowired
|
||||
private IFileService fileService;
|
||||
|
||||
// @Value("tencent.accessTokenUrl")
|
||||
private String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx30a3c8de94a85584&secret=e64d6e3dcfe88d0daee50e77f5a2ee69";
|
||||
@Value("${zlt.security.tencent.accessTokenUrl}")
|
||||
private String accessTokenUrl;
|
||||
|
||||
// @Value("tencent.getwxacodeUrl")
|
||||
private String getwxacodeUrl = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=";
|
||||
@Value("${zlt.security.tencent.getwxacodeUrl}")
|
||||
private String getwxacodeUrl;
|
||||
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
@Value("${zlt.security.tencent.pagePath}")
|
||||
private Object pagePath;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* 根据fileType选择上传方式
|
||||
@@ -74,7 +78,13 @@ public class FileController {
|
||||
*/
|
||||
@GetMapping("/files")
|
||||
public PageResult<FileInfo> findFiles(@RequestParam Map<String, Object> params) {
|
||||
return fileService.findList(params);
|
||||
PageResult<FileInfo> pageResult = fileService.findList(params);
|
||||
if(pageResult.getCount() >0){
|
||||
for (FileInfo fileInfo: pageResult.getData()){
|
||||
fileInfo.setUrl(getViewUrlByName(fileInfo.getName()));
|
||||
}
|
||||
}
|
||||
return pageResult;
|
||||
}
|
||||
|
||||
@GetMapping("/files/{id}")
|
||||
@@ -109,7 +119,7 @@ public class FileController {
|
||||
FileInfo fileInfo = fileService.upload(codeId+".jpeg",getQRCode(accessToken,codeId));
|
||||
map.put("codeId", codeId);
|
||||
map.put("fileId", fileInfo.getId());
|
||||
map.put("filePath", fileInfo.getPath());
|
||||
map.put("fileName", fileInfo.getName());
|
||||
return Result.succeed(map);
|
||||
}
|
||||
|
||||
@@ -149,17 +159,26 @@ public class FileController {
|
||||
* @return
|
||||
*/
|
||||
public InputStream getQRCode(String accessToken, String codeId) {
|
||||
String url = getwxacodeUrl + accessToken; // 确保这里拼接正确,可能还需要其他参数
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
params.put("path", "/pages/link/link?uuid=" + codeId);
|
||||
HttpEntity<Map> requestEntity = new HttpEntity<>(params, headers);
|
||||
ResponseEntity<String> responseEntity = restTemplate.exchange(getwxacodeUrl+accessToken, HttpMethod.POST, requestEntity, String.class);
|
||||
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("path", pagePath + codeId);
|
||||
HttpEntity<Map> requestEntity = new HttpEntity<>(requestBody, headers);
|
||||
ResponseEntity<byte[]> responseEntity = restTemplate.exchange(
|
||||
getwxacodeUrl+accessToken, // 假设path参数是这样传递的
|
||||
HttpMethod.POST, // 或者POST,取决于API要求
|
||||
requestEntity,
|
||||
byte[].class); // 直接请求二进制数据
|
||||
|
||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||
return new ByteArrayInputStream(HexToJpeg.hexStringToByteArray(responseEntity.getBody()));
|
||||
}else {
|
||||
throw new RuntimeException();
|
||||
byte[] qrCodeBytes = responseEntity.getBody();
|
||||
// 这里可以直接将字节数组转换为InputStream并返回,或者上传到OSS
|
||||
InputStream qrCodeStream = new ByteArrayInputStream(qrCodeBytes);
|
||||
return qrCodeStream; // 如果确实需要返回InputStream,则这样返回
|
||||
} else {
|
||||
throw new RuntimeException("Failed to fetch QR code: " + responseEntity.getStatusCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,16 @@ public class HexToJpeg {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static byte[] hexStringToByteArray(String hex) {
|
||||
|
||||
// 检查字符串长度是否为偶数
|
||||
if (hex.length() % 2 != 0) {
|
||||
throw new IllegalArgumentException("Invalid hex string length: " + hex.length());
|
||||
}
|
||||
int len = hex.length();
|
||||
byte[] data = new byte[len / 2];
|
||||
for (int i = 0; i < len; i += 2) {
|
||||
// 这里应该是安全的,因为我们已经检查了字符串长度是偶数
|
||||
data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
|
||||
+ Character.digit(hex.charAt(i + 1), 16));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user