Browse Source

中文域名生成图片 + 盲盒导入

wangqifan 3 years ago
parent
commit
a97a74c7bb

+ 2 - 0
src/main/java/com/izouma/nineth/repo/CollectionRepo.java

@@ -126,6 +126,8 @@ public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpec
 
     List<Collection> findByNameLike(String name);
 
+    List<Collection> findAllByPrefixNameAndDelFalse(String prefix);
+
     List<Collection> findByStockGreaterThan(int stock);
 
     @Query("update Collection c set c.stock = ?2 where c.id = ?1")

+ 106 - 70
src/main/java/com/izouma/nineth/service/DomainOrderService.java

@@ -379,36 +379,60 @@ public class DomainOrderService {
         } else {
             domainName = domain.toUpperCase();
         }
-        InputStream is1 = this.getClass()
-                .getResourceAsStream("/font/VonwaonBitmap_16pxLite.ttf");
-        Font font1 = Font.createFont(Font.TRUETYPE_FONT, is1);
-        is1.close();
-        InputStream is2 = this.getClass()
-                .getResourceAsStream("/font/VonwaonBitmap_12pxLite.ttf");
-        Font font2 = Font.createFont(Font.TRUETYPE_FONT, is2);
-        is2.close();
+        Pattern p;
+        p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
+        Matcher m = p.matcher(domain);
+        boolean chinese = m.find();
+        Font font1;
+        Font font2;
+        if (!chinese) {
+            InputStream is1 = this.getClass()
+                    .getResourceAsStream("/font/VonwaonBitmap_16pxLite.ttf");
+            font1 = Font.createFont(Font.TRUETYPE_FONT, is1);
+            is1.close();
+            InputStream is2 = this.getClass()
+                    .getResourceAsStream("/font/VonwaonBitmap_12pxLite.ttf");
+            font2 = Font.createFont(Font.TRUETYPE_FONT, is2);
+            is2.close();
+        } else {
+            InputStream is1 = this.getClass()
+                    .getResourceAsStream("/font/尔雅荣耀手书.ttf");
+            font1 = Font.createFont(Font.TRUETYPE_FONT, is1);
+            is1.close();
+            InputStream is2 = this.getClass()
+                    .getResourceAsStream("/font/尔雅荣耀手书.ttf");
+            font2 = Font.createFont(Font.TRUETYPE_FONT, is2);
+            is2.close();
+        }
+
         int length = domainName.length();
         BufferedImage shareImg;
-        if (length == 1) {
-            InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_jing.png");
-            shareImg = ImageIO.read(is3);
-            is3.close();
-        } else if (length == 2) {
-            InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_lv.png");
-            shareImg = ImageIO.read(is3);
-            is3.close();
-        } else if (length == 3) {
-            InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_b.png");
-            shareImg = ImageIO.read(is3);
-            is3.close();
-        } else if (length == 4) {
-            InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_h.png");
+        if (chinese) {
+            InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_baijiaxing.png");
             shareImg = ImageIO.read(is3);
             is3.close();
         } else {
-            InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_zi.png");
-            shareImg = ImageIO.read(is3);
-            is3.close();
+            if (length == 1) {
+                InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_jing.png");
+                shareImg = ImageIO.read(is3);
+                is3.close();
+            } else if (length == 2) {
+                InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_lv.png");
+                shareImg = ImageIO.read(is3);
+                is3.close();
+            } else if (length == 3) {
+                InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_b.png");
+                shareImg = ImageIO.read(is3);
+                is3.close();
+            } else if (length == 4) {
+                InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_h.png");
+                shareImg = ImageIO.read(is3);
+                is3.close();
+            } else {
+                InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_zi.png");
+                shareImg = ImageIO.read(is3);
+                is3.close();
+            }
         }
 
 
@@ -427,56 +451,68 @@ public class DomainOrderService {
 
 
         int domainLength = domainName.length();
-        if (domainLength > 10) {
-            g.setColor(new Color(255, 255, 255));
-            Font topFont = font1.deriveFont(Font.PLAIN, 130f);
-            Font downFont = font2.deriveFont(Font.BOLD, 36f);
-            int subIndex = domainLength / 3;
-            String str1 = domainName.substring(0, subIndex);
-            String str2 = domainName.substring(subIndex, subIndex + subIndex);
-            String str3 = domainName.substring(subIndex + subIndex, domainLength);
-            ImageUtils.drawCenteredString(g, str1, new Rectangle(0, 180, shareImg
-                    .getWidth(), 86), topFont);
-            ImageUtils.drawCenteredString(g, str2, new Rectangle(0, 300, shareImg
+        if (chinese) {
+            g.setColor(new Color(255, 241, 187));
+            Font topFont = font1.deriveFont(Font.PLAIN, 180f);
+            Font downFont = font2.deriveFont(Font.PLAIN, 30f);
+            ImageUtils.drawCenteredString(g, domainName, new Rectangle(5, 200, shareImg
                     .getWidth(), 86), topFont);
-            ImageUtils.drawCenteredString(g, str3, new Rectangle(0, 420, shareImg
-                    .getWidth(), 86), topFont);
-            ImageUtils.drawCenteredString(g, ".NFT", new Rectangle(0, 540, shareImg
+            ImageUtils.drawCenteredString(g, ".NFT", new Rectangle(-10, 370, shareImg
                     .getWidth(), 86), topFont);
             g.setColor(new Color(255, 255, 255));
-            ImageUtils.drawCenteredString(g, domain, new Rectangle(0, 650, shareImg
-                    .getWidth(), 12), downFont);
-        }
-        if (domainLength > 5 & domainLength <= 10) {
-            g.setColor(new Color(255, 255, 255));
-            Font topFont = font1.deriveFont(Font.PLAIN, 190f);
-            Font downFont = font2.deriveFont(Font.BOLD, 36f);
-            int subIndex = domainLength / 2;
-            String str1 = domainName.substring(0, subIndex);
-            String str2 = domainName.substring(subIndex, domainLength);
-            ImageUtils.drawCenteredString(g, str1, new Rectangle(0, 180, shareImg
-                    .getWidth(), 86), topFont);
-            ImageUtils.drawCenteredString(g, str2, new Rectangle(0, 350, shareImg
-                    .getWidth(), 86), topFont);
-            ImageUtils.drawCenteredString(g, ".NFT", new Rectangle(0, 520, shareImg
-                    .getWidth(), 86), topFont);
-            g.setColor(new Color(255, 255, 255));
-            ImageUtils.drawCenteredString(g, domain, new Rectangle(0, 650, shareImg
-                    .getWidth(), 12), downFont);
-        }
-        if (domainLength <= 5) {
-            g.setColor(new Color(255, 255, 255));
-            Font topFont = font1.deriveFont(Font.PLAIN, 240f);
-            Font downFont = font2.deriveFont(Font.BOLD, 36f);
-            ImageUtils.drawCenteredString(g, domainName, new Rectangle(17, 220, shareImg
-                    .getWidth(), 86), topFont);
-            ImageUtils.drawCenteredString(g, ".NFT", new Rectangle(-10, 420, shareImg
-                    .getWidth(), 86), topFont);
-            g.setColor(new Color(255, 255, 255));
-            ImageUtils.drawCenteredString(g, domain, new Rectangle(0, 620, shareImg
+            ImageUtils.drawCenteredString(g, domain, new Rectangle(0, 600, shareImg
                     .getWidth(), 12), downFont);
+        } else {
+            if (domainLength > 10) {
+                g.setColor(new Color(255, 255, 255));
+                Font topFont = font1.deriveFont(Font.PLAIN, 130f);
+                Font downFont = font2.deriveFont(Font.BOLD, 36f);
+                int subIndex = domainLength / 3;
+                String str1 = domainName.substring(0, subIndex);
+                String str2 = domainName.substring(subIndex, subIndex + subIndex);
+                String str3 = domainName.substring(subIndex + subIndex, domainLength);
+                ImageUtils.drawCenteredString(g, str1, new Rectangle(0, 180, shareImg
+                        .getWidth(), 86), topFont);
+                ImageUtils.drawCenteredString(g, str2, new Rectangle(0, 300, shareImg
+                        .getWidth(), 86), topFont);
+                ImageUtils.drawCenteredString(g, str3, new Rectangle(0, 420, shareImg
+                        .getWidth(), 86), topFont);
+                ImageUtils.drawCenteredString(g, ".NFT", new Rectangle(0, 540, shareImg
+                        .getWidth(), 86), topFont);
+                g.setColor(new Color(255, 255, 255));
+                ImageUtils.drawCenteredString(g, domain, new Rectangle(0, 650, shareImg
+                        .getWidth(), 12), downFont);
+            }
+            if (domainLength > 5 & domainLength <= 10) {
+                g.setColor(new Color(255, 255, 255));
+                Font topFont = font1.deriveFont(Font.PLAIN, 190f);
+                Font downFont = font2.deriveFont(Font.BOLD, 36f);
+                int subIndex = domainLength / 2;
+                String str1 = domainName.substring(0, subIndex);
+                String str2 = domainName.substring(subIndex, domainLength);
+                ImageUtils.drawCenteredString(g, str1, new Rectangle(0, 180, shareImg
+                        .getWidth(), 86), topFont);
+                ImageUtils.drawCenteredString(g, str2, new Rectangle(0, 350, shareImg
+                        .getWidth(), 86), topFont);
+                ImageUtils.drawCenteredString(g, ".NFT", new Rectangle(0, 520, shareImg
+                        .getWidth(), 86), topFont);
+                g.setColor(new Color(255, 255, 255));
+                ImageUtils.drawCenteredString(g, domain, new Rectangle(0, 650, shareImg
+                        .getWidth(), 12), downFont);
+            }
+            if (domainLength <= 5) {
+                g.setColor(new Color(255, 255, 255));
+                Font topFont = font1.deriveFont(Font.PLAIN, 240f);
+                Font downFont = font2.deriveFont(Font.BOLD, 36f);
+                ImageUtils.drawCenteredString(g, domainName, new Rectangle(17, 220, shareImg
+                        .getWidth(), 86), topFont);
+                ImageUtils.drawCenteredString(g, ".NFT", new Rectangle(-10, 420, shareImg
+                        .getWidth(), 86), topFont);
+                g.setColor(new Color(255, 255, 255));
+                ImageUtils.drawCenteredString(g, domain, new Rectangle(0, 620, shareImg
+                        .getWidth(), 12), downFont);
+            }
         }
-
         //二维码
 //        QRCodeWriter qrCodeWriter = new QRCodeWriter();
 //        Map<EncodeHintType, Object> hints = new HashMap<>();

BIN
src/main/resources/font/尔雅荣耀手书.ttf


BIN
src/main/resources/static/img/png_baijiaxing.png


+ 43 - 14
src/test/java/com/izouma/nineth/service/CollectionServiceTest.java

@@ -5,12 +5,16 @@ import com.alibaba.fastjson.JSON;
 import com.izouma.nineth.ApplicationTests;
 import com.izouma.nineth.domain.BlindBoxItem;
 import com.izouma.nineth.domain.Collection;
+import com.izouma.nineth.domain.DomainOrder;
 import com.izouma.nineth.domain.FileObject;
 import com.izouma.nineth.dto.CreateBlindBox;
 import com.izouma.nineth.dto.TestExcelDTO;
+import com.izouma.nineth.enums.CollectionStatus;
 import com.izouma.nineth.enums.CollectionType;
+import com.izouma.nineth.enums.OrderStatus;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.CollectionRepo;
+import com.izouma.nineth.repo.DomainOrderRepo;
 import com.izouma.nineth.repo.PrivilegeOptionRepo;
 import com.izouma.nineth.repo.UserRepo;
 import com.izouma.nineth.service.storage.StorageService;
@@ -29,6 +33,7 @@ import org.springframework.boot.test.context.SpringBootTest;
 import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
 import java.io.*;
+import java.math.BigDecimal;
 import java.nio.file.Files;
 import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
@@ -56,6 +61,8 @@ class CollectionServiceTest extends ApplicationTests {
     private SysConfigService    sysConfigService;
     @Autowired
     private DomainOrderService  domainOrderService;
+    @Autowired
+    private DomainOrderRepo     domainOrderRepo;
 
     @Test
     void toDTO() {
@@ -67,10 +74,10 @@ class CollectionServiceTest extends ApplicationTests {
     public void batchUpload() throws IOException {
         AtomicInteger num = new AtomicInteger(1);
         List<BlindBoxItem> items = new ArrayList<>();
-        String jsonStr = FileUtils.readFileToString(new File("/Users/qiufangchao/Desktop/mugen_mu.json"), "UTF-8");
-        Arrays.stream(new File("/Users/qiufangchao/Desktop/mugen_mu").listFiles())
-                .filter(f -> !f.getName().contains(".DS_Store"))
-                .parallel().forEach(file -> {
+        String jsonStr = FileUtils.readFileToString(new File("D:/nft/nixingzhe.json"), "UTF-8");
+        String totalString = "王,李,张,刘,陈,杨,赵,黄,周,吴,徐,孙,胡,朱,高,林,何,郭,马,罗";
+        List<String> strings = new ArrayList<>(Arrays.asList(totalString.split(",")));
+        strings.forEach(file -> {
             try {
 //                        String name = file.getName();
 //                        Pattern p = Pattern.compile("(\\d+)");
@@ -80,21 +87,43 @@ class CollectionServiceTest extends ApplicationTests {
 
                 Collection collection = JSON.parseObject(jsonStr, Collection.class);
                 collection.setId(null);
-                collection.setName("MUGEN无限:未央宗#" + num.getAndIncrement());
+                collection.setType(CollectionType.DOMAIN);
+                collection.setName("RID元宇宙域名 " + file + ".nft");
                 collection.setStock(1);
                 collection.setTotal(1);
+                collection.setPrefixName("RIDCX");
                 collection.setSale(0);
-                collection.setMinterId(5868950L);
                 collection.setOnShelf(false);
                 collection.setSalable(false);
 //                        String thumbPath = "https://cdn.raex.vip/thumb_image/mahjongman/" + file.getName()
 //                                .substring(0, file.getName().lastIndexOf(".")) + ".jpg";
+                BufferedImage img = domainOrderService.domainImg(file + ".nft");
+                ByteArrayOutputStream os = new ByteArrayOutputStream();
+                ImageIO.write(img, "jpg", os);
+                InputStream input = new ByteArrayInputStream(os.toByteArray());
+                String path = "image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
+                        + RandomStringUtils.randomAlphabetic(8) + ".jpg";
+                String realUrl = storageService.uploadFromInputStream(input, path);
+                FileObject fileObject = new FileObject();
+                fileObject.setName(file + ".nft");
+                fileObject.setType("image/jpeg");
+                fileObject.setUrl(realUrl);
                 collection.setPic(Collections
-                        .singletonList(new FileObject("", "https://cdn.raex.vip/nft/mugen_mu/" + file
-                                .getName(), null, "image/png")));
+                        .singletonList(fileObject));
 
-                collectionRepo.save(collection);
-                System.out.println("保存成功" + collection.getId());
+                DomainOrder domainOrder = new DomainOrder();
+                domainOrder.setOrderStatus(OrderStatus.FINISH);
+                domainOrder.setStatus(CollectionStatus.SUCCESS);
+                domainOrder.setPic(Collections
+                        .singletonList(fileObject));
+                domainOrder.setYears(1L);
+                domainOrder.setPrice(BigDecimal.valueOf(29.9));
+                domainOrder.setPicName(file);
+                domainOrder.setDomainName(file + ".nft");
+                domainOrder.setEndTime(LocalDateTime.now());
+                domainOrderRepo.save(domainOrder);
+//                collectionRepo.save(collection);
+                System.out.println("保存成功"               + collection.getId());
 
                 items.add(BlindBoxItem
                         .builder()
@@ -114,22 +143,22 @@ class CollectionServiceTest extends ApplicationTests {
     public void createBlindBox() throws Exception {
 //        List<Long> arr = Arrays.asList(6862110L, 6862511L, 6862516L, 6862533L, 6862689L, 6862792L, 6862867L, 6863009L, 6863047L,
 //                6863188L, 6863438L, 6863449L, 6863588L, 6863608L, 6863671L, 6863745L, 6863760L, 6863938L, 6863954L, 6864008L, 6864081L);
-        List<Collection> items = collectionRepo.findByNameLike("MUGEN无限:未央宗#%")
+        List<Collection> items = collectionRepo.findAllByPrefixNameAndDelFalse("RIDCX")
                 .stream()
                 .filter(collection -> collection.getCreatedAt().isAfter(LocalDateTime.of(2022, 6, 28, 0, 0)))
                 .limit(450L)
                 .collect(Collectors.toList());
 
-        String jsonStr = FileUtils.readFileToString(new File("/Users/qiufangchao/Desktop/mugen_mu.json"), "UTF-8");
+        String jsonStr = FileUtils.readFileToString(new File("D:/nft/nixingzhe.json"), "UTF-8");
         Collection blindBox = JSON.parseObject(jsonStr, Collection.class);
         blindBox.setType(CollectionType.BLIND_BOX);
         blindBox.setId(null);
         blindBox.setOnShelf(false);
         blindBox.setSalable(false);
-        blindBox.setMinterId(5868950L);
+//        blindBox.setMinterId(5868950L);
         blindBox.setNoSoldOut(true);
         blindBox.setMaxCount(1);
-        blindBox.setName("MUGEN无限未央宗数字艺术品盲盒");
+        blindBox.setName("百家姓测试盲盒");
         blindBox.setPic(Arrays
                 .asList(new FileObject(null, "https://cdn.raex.vip/nft/2022-04-16-19-24-54WTJLmsmR.jpg", null, "image/jpeg")));
         collectionService.createBlindBox(new CreateBlindBox(blindBox, items.stream().map(i -> {

+ 1 - 1
src/test/java/com/izouma/nineth/service/StatisticServiceTest.java

@@ -52,6 +52,6 @@ public class StatisticServiceTest {
 
     @Test
     public void test() {
-        cacheService.clearWeekTop();
+        cacheService.clearOrderPriceTrendV2();
     }
 }

+ 1 - 1
src/test/java/com/izouma/nineth/service/UserServiceTest.java

@@ -273,7 +273,7 @@ public class UserServiceTest extends ApplicationTests {
 
     @Test
     public void shareImg() throws IOException, FontFormatException, WriterException {
-        BufferedImage img = domainOrderService.domainImg("thomeboydontkill.nft");
+        BufferedImage img = domainOrderService.domainImg(".nft");
         ImageIO.write(img, "jpg", new File("1.jpg"));
     }
 }