Commit f5a76b3d authored by tangtuo's avatar tangtuo

nft证书接口编写

parent 9a4346df
...@@ -5,6 +5,7 @@ import cn.dev33.satoken.session.SaSession; ...@@ -5,6 +5,7 @@ import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.SaLoginModel; import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.SaTokenInfo; import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpLogic; import cn.dev33.satoken.stp.StpLogic;
import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
...@@ -13,6 +14,7 @@ import java.util.List; ...@@ -13,6 +14,7 @@ import java.util.List;
* *
* @author kong * @author kong
*/ */
@Component
public class StpAdminUtil { public class StpAdminUtil {
/** /**
......
...@@ -84,6 +84,10 @@ ...@@ -84,6 +84,10 @@
<groupId>com.github.pagehelper</groupId> <groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId> <artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
......
package com.fzm.common.entity.vo;
import cn.hutool.core.date.DateUtil;
import com.fzm.common.entity.Nft;
import com.fzm.common.entity.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @author tangtuo
* @date 2021/7/1 10:35
*/
@Data
public class NftCertificateVo {
@ApiModelProperty("主键")
private Integer id;
@ApiModelProperty("名称")
private String name;
@ApiModelProperty("发行人")
private String publisher;
@ApiModelProperty("发行人地址")
private String wallet;
@ApiModelProperty("剧本hash")
private String fileHash;
@ApiModelProperty("nft哈希")
private String nftHash;
@ApiModelProperty("存证时间")
private Date publishTime;
@ApiModelProperty("二维码")
private String qrCode;
@ApiModelProperty("剧本审核状态 0-未审核 1-已审核")
private Integer examineStatus;
@ApiModelProperty("版权证明: 0-未认证 1-已认证")
private Integer copyrightStatus;
public NftCertificateVo(Nft nft, User user, String qrCode) {
this.name = nft.getName();
this.fileHash = nft.getFileHash();
this.nftHash = nft.getNftHash();
this.id = nft.getId();
this.publisher = user.getNickname();
this.publishTime = nft.getPublishTime();
this.wallet=user.getWallet();
this.qrCode = qrCode;
this.examineStatus = 0;
this.copyrightStatus = 0;
}
}
package com.fzm.common.model;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import com.google.zxing.LuminanceSource;
public class BufferedImageLuminanceSource extends LuminanceSource {
private final BufferedImage image;
private final int left;
private final int top;
public BufferedImageLuminanceSource(BufferedImage image) {
this(image, 0, 0, image.getWidth(), image.getHeight());
}
public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
super(width, height);
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width > sourceWidth || top + height > sourceHeight) {
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
}
for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
}
}
}
this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top;
}
public byte[] getRow(int y, byte[] row) {
if (y < 0 || y >= getHeight()) {
throw new IllegalArgumentException("Requested row is outside the image: " + y);
}
int width = getWidth();
if (row == null || row.length < width) {
row = new byte[width];
}
image.getRaster().getDataElements(left, top + y, width, 1, row);
return row;
}
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
return matrix;
}
@Override
public boolean isCropSupported() {
return true;
}
@Override
public LuminanceSource crop(int left, int top, int width, int height) {
return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
}
@Override
public boolean isRotateSupported() {
return true;
}
@Override
public LuminanceSource rotateCounterClockwise() {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose();
int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
}
}
\ No newline at end of file
package com.fzm.common.utils;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.fzm.common.model.BufferedImageLuminanceSource;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import sun.misc.BASE64Encoder;
public class QRCodeUtil {
private static final String CHARSET = "utf-8";
private static final String FORMAT_NAME = "JPG";
// 二维码尺寸
private static final int QRCODE_SIZE = 300;
// LOGO宽度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
// 插入图片
QRCodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
System.err.println("" + imgPath + " 该文件不存在!");
return;
}
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
/**
* @param content 二维码内容
* @param imgPath 嵌入二维码的图片路径
* @param needCompress 是否压缩
* @return
* @throws Exception
*/
public static String encode(String content, String imgPath, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
try {
ImageIO.write(image, "png", baos);//写入流中
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = baos.toByteArray();//转换成字节
BASE64Encoder encoder = new BASE64Encoder();
String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
System.out.println("值为:" + "data:image/jpg;base64," + png_base64);
return "data:image/jpg;base64," + png_base64;
}
// 被注释的方法
/*
* public static void encode(String content, String destPath, boolean
* needCompress) throws Exception { QRCodeUtil.encode(content, null, destPath,
* needCompress); }
*/
public static void encode(String content) throws Exception {
QRCodeUtil.encode(content, null, false);
}
public static void encode(String content, String imgPath, OutputStream output, boolean needCompress)
throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
ImageIO.write(image, FORMAT_NAME, output);
}
public static void encode(String content, OutputStream output) throws Exception {
QRCodeUtil.encode(content, null, output, false);
}
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
public static String decode(String path) throws Exception {
return QRCodeUtil.decode(new File(path));
}
}
\ No newline at end of file
...@@ -40,7 +40,6 @@ ...@@ -40,7 +40,6 @@
<artifactId>joying-common</artifactId> <artifactId>joying-common</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -7,6 +7,7 @@ import com.fzm.common.constant.SystemConstant; ...@@ -7,6 +7,7 @@ import com.fzm.common.constant.SystemConstant;
import com.fzm.common.entity.Nft; import com.fzm.common.entity.Nft;
import com.fzm.common.entity.User; import com.fzm.common.entity.User;
import com.fzm.common.entity.vo.CollectionNftVo; import com.fzm.common.entity.vo.CollectionNftVo;
import com.fzm.common.entity.vo.NftCertificateVo;
import com.fzm.common.entity.vo.NftVo; import com.fzm.common.entity.vo.NftVo;
import com.fzm.common.enums.ResultCode; import com.fzm.common.enums.ResultCode;
import com.fzm.common.exception.GlobalException; import com.fzm.common.exception.GlobalException;
...@@ -15,6 +16,7 @@ import com.fzm.common.service.CategoryService; ...@@ -15,6 +16,7 @@ import com.fzm.common.service.CategoryService;
import com.fzm.common.service.NftService; import com.fzm.common.service.NftService;
import com.fzm.common.service.UserService; import com.fzm.common.service.UserService;
import com.fzm.common.utils.OssUtil; import com.fzm.common.utils.OssUtil;
import com.fzm.common.utils.QRCodeUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
...@@ -49,6 +51,12 @@ public class NftController { ...@@ -49,6 +51,12 @@ public class NftController {
@Resource @Resource
private CategoryService categoryService; private CategoryService categoryService;
/**
* 存证二维码跳转地址
* todo 修改成动态前端路由
*/
private static final String path = "https://chain.33.cn/document/60";
@SaCheckLogin @SaCheckLogin
@PostMapping("/publish") @PostMapping("/publish")
...@@ -144,4 +152,21 @@ public class NftController { ...@@ -144,4 +152,21 @@ public class NftController {
} }
@SaCheckLogin
@GetMapping("/certificate/{id}")
@ApiOperation(value = "nft证书")
public ResponseModel<NftCertificateVo> certificate(@PathVariable Integer id) throws Exception {
Nft nft = nftService.getById(id);
if (nft == null) {
throw GlobalException.newException(ResultCode.DATA_ERROR, "没找到此nft的详情");
}
int userId = StpUtil.getLoginIdAsInt();
if (!nft.getUserId().equals(userId)) {
throw GlobalException.newException(ResultCode.FORBIDDEN, "您无权查看别人的nft证书");
}
User user = userService.getById(userId);
String qrCode = QRCodeUtil.encode(path, null, false);
NftCertificateVo vo = new NftCertificateVo(nft, user, qrCode);
return ResponseModel.success(vo);
}
} }
...@@ -93,6 +93,12 @@ ...@@ -93,6 +93,12 @@
<artifactId>pagehelper-spring-boot-starter</artifactId> <artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version> <version>1.3.0</version>
</dependency> </dependency>
<!-- 二维码生成工具 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment