- 微信小程序前端上送数据,在小程序中,我们需要使用wx.login获取用户的登录凭证code,然后调用wx.getUserInfo获取用户的基本信息。接下来,我们将数据上送到后台。
-
// app.js App({ onLaunch: function () { // 登录 wx.login({ success: (res) => { if (res.code) { // 保存code到本地缓存,可以用作后续操作 wx.setStorageSync('code', res.code); } else { console.log('登录失败!' + res.errMsg); } } }); } });
// 某个页面的JS文件 Page({ data: { userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo') }, onLoad: function () { if (wx.getStorageSync('userInfo')) { this.setData({ userInfo: wx.getStorageSync('userInfo'), hasUserInfo: true }); } else if (this.data.canIUse){ // 由于getUserInfo是网络请求,可能会在Page.onLoad之后才返回 // 所以此处加入callback以防止这种情况 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }); } } else { // 在没有open-type=getUserInfo版本的兼容处理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo; this.setData({ userInfo: res.userInfo, hasUserInfo: true }); } }); } }, getUserInfo: function (e) { console.log(e); app.globalData.userInfo = e.detail.userInfo; this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }); // 上送数据到后台 this.uploadData(); }, uploadData: function () { const data = { id: 1, // 假设这是您要上传的数据的ID name: 'item1', userInfo: this.data.userInfo }; wx.request({ url: 'https://your-backend-url/upload-data', // 您的后台接口地址 method: 'POST', data: JSON.stringify(data), header: { 'content-type': 'application/json' }, success: function (res) { if (res.statusCode === 200) { console.log('上传成功'); } else { console.log('上传失败'); } }, fail: function (err) { console.log('上传失败', err); } }); } });
- 后台批量生成二维码、条形码图片插入Excel:在后台,我们需要接收前端发送的数据,将其保存到数据库,并生成二维码、条形码图片插入Excel。
@RestController @RequestMapping("/upload-data") public class DataUploadController { @Autowired private ItemService itemService; @PostMapping public ResponseEntity<?> uploadData(@RequestBody Item item) { // 保存数据到数据库 itemService.save(item); // 生成二维码和条形码图片 BufferedImage qrCodeImage = generateQRCodeImage(item.getId().toString()); BufferedImage barcodeImage = generateBarcodeImage(item.getId().toString()); // 将图片插入Excel Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Codes"); Row row = sheet.createRow(0); Cell qrCodeCell = row.createCell(0); Cell barcodeCell = row.createCell(1); // 将图片转换为Base64编码的字符串 String qrCodeBase64 = imageToBase64String(qrCodeImage, "png"); String barcodeBase64 = imageToBase64String(barcodeImage, "png"); // 设置单元格的值为Base64编码的图片 qrCodeCell.setCellValue(qrCodeBase64); barcodeCell.setCellValue(barcodeBase64); // 将Excel文件保存到内存中 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); workbook.close(); // 将内存中的Excel文件转换为Base64编码的字符串 String excelBase64 = Base64.getEncoder().encodeToString(outputStream.toByteArray()); // 返回Excel文件的Base64编码 return ResponseEntity.ok().body("data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + excelBase64); } private BufferedImage generateQRCodeImage(String content) throws WriterException { int width = 200; int height = 200; String charset = "UTF-8"; Map<EncodeHintType, Object> hintMap = Map.of(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hintMap); return MatrixToImageWriter.toBufferedImage(matrix); } private BufferedImage generateBarcodeImage(String content) throws WriterException { int width = 400; int height = 100; String charset = "UTF-8"; Map<EncodeHintType, Object> hintMap = Map.of(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); BitMatrix matrix = new Code128Writer().encode(content, BarcodeFormat.CODE_128, width, height, hintMap); return MatrixToImageWriter.toBufferedImage(matrix); } private String imageToBase64String(BufferedImage image, String format) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(image, format, outputStream); byte[] bytes = outputStream.toByteArray(); return Base64.getEncoder().encodeToString(bytes); } }
- 后台批量生成二维码、条形码图片插入Excel在后台,您可以使用Spring Boot框架实现。首先,您需要添加相关依赖,例如:
-
<groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency><dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.1</version> </dependency><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.1.0</version> </dependency><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.1.0</version> </dependency>
- 文件上传,为了将生成的Excel文件保存到服务器,我们需要实现一个文件上传接口。在这个例子中,我们将使用Spring Boot的MultipartFile类来处理文件上传。
-
@RestController @RequestMapping("/upload-excel") public class ExcelUploadController { @Autowired private ItemService itemService; @PostMapping public ResponseEntity<?> uploadExcel(@RequestParam("file") MultipartFile file) throws IOException { // 读取Excel文件 Workbook workbook = new XSSFWorkbook(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); // 处理Excel文件中的数据 for (int i = 1; i <= lastRowNum; i++) { Row row = sheet.getRow(i); if (row != null) { Cell qrCodeCell = row.getCell(0); Cell barcodeCell = row.getCell(1); // 解析Base64编码的图片 String qrCodeBase64 = qrCodeCell.getStringCellValue(); String barcodeBase64 = barcodeCell.getStringCellValue(); byte[] qrCodeBytes = Base64.getDecoder().decode(qrCodeBase64.split(",")[1]); byte[] barcodeBytes = Base64.getDecoder().decode(barcodeBase64.split(",")[1]); // 保存图片到文件系统 File qrCodeFile = new File("path/to/qr-code.png"); File barcodeFile = new File("path/to/barcode.png"); FileOutputStream qrCodeOutputStream = new FileOutputStream(qrCodeFile); FileOutputStream barcodeOutputStream = new FileOutputStream(barcodeFile); qrCodeOutputStream.write(qrCodeBytes); barcodeOutputStream.write(barcodeBytes); qrCodeOutputStream.close(); barcodeOutputStream.close(); // 将图片路径保存到数据库 Item item = new Item(); item.setName(qrCodeFile.getName()); item.setQrCodePath(qrCodeFile.getAbsolutePath()); item.setBarcodePath(barcodeFile.getAbsolutePath()); itemService.save(item); } } // 返回成功响应 return ResponseEntity.ok().body("文件上传成功"); } }
微信小程序前端上送数据后台批量生成二维码和条形码图片插入excel,再通过wx.downloadFile下载excel
最新推荐文章于 2024-09-06 09:56:23 发布