DomainOrderService.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package com.izouma.nineth.service;
  2. import com.alibaba.excel.util.StringUtils;
  3. import com.google.zxing.WriterException;
  4. import com.izouma.nineth.domain.DomainOrder;
  5. import com.izouma.nineth.domain.FileObject;
  6. import com.izouma.nineth.domain.User;
  7. import com.izouma.nineth.dto.PageQuery;
  8. import com.izouma.nineth.enums.CollectionStatus;
  9. import com.izouma.nineth.enums.OrderStatus;
  10. import com.izouma.nineth.enums.PayMethod;
  11. import com.izouma.nineth.exception.BusinessException;
  12. import com.izouma.nineth.repo.DomainOrderRepo;
  13. import com.izouma.nineth.repo.UserRepo;
  14. import com.izouma.nineth.service.storage.StorageService;
  15. import com.izouma.nineth.utils.ImageUtils;
  16. import com.izouma.nineth.utils.JpaUtils;
  17. import com.izouma.nineth.utils.SecurityUtils;
  18. import lombok.AllArgsConstructor;
  19. import org.apache.commons.lang3.RandomStringUtils;
  20. import org.springframework.data.annotation.Transient;
  21. import org.springframework.data.domain.Page;
  22. import org.springframework.data.domain.PageRequest;
  23. import org.springframework.data.domain.Pageable;
  24. import org.springframework.data.domain.Sort;
  25. import org.springframework.stereotype.Service;
  26. import javax.imageio.ImageIO;
  27. import java.awt.*;
  28. import java.awt.image.BufferedImage;
  29. import java.io.ByteArrayInputStream;
  30. import java.io.ByteArrayOutputStream;
  31. import java.io.IOException;
  32. import java.io.InputStream;
  33. import java.math.BigDecimal;
  34. import java.text.SimpleDateFormat;
  35. import java.time.LocalDateTime;
  36. import java.util.*;
  37. import java.util.List;
  38. import java.util.regex.Matcher;
  39. import java.util.regex.Pattern;
  40. @Service
  41. @AllArgsConstructor
  42. public class DomainOrderService {
  43. private DomainOrderRepo domainOrderRepo;
  44. private ContentAuditService contentAuditService;
  45. private UserRepo userRepo;
  46. private AssetService assetService;
  47. private SysConfigService sysConfigService;
  48. private StorageService storageService;
  49. private RockRecordService rockRecordService;
  50. public Page<DomainOrder> all(PageQuery pageQuery) {
  51. return domainOrderRepo
  52. .findAll(JpaUtils.toSpecification(pageQuery, DomainOrder.class), JpaUtils.toPageRequest(pageQuery));
  53. }
  54. public boolean isContainChinese(String str) {
  55. if (StringUtils.isEmpty(str)) {
  56. throw new BusinessException("sms context is empty!");
  57. }
  58. Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
  59. Matcher m = p.matcher(str);
  60. if (m.find()) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. public DomainOrder create(Long userId, String domain, BigDecimal price, Long year) {
  66. List<DomainOrder> notPaidOrders = domainOrderRepo.findAllByUserIdAndOrderStatus(userId, OrderStatus.NOT_PAID);
  67. Long superUserId = Long.valueOf(sysConfigService.getString("domain_superUserId"));
  68. if (notPaidOrders.size() > 0) {
  69. throw new BusinessException("已存在未支付订单,不可继续下单");
  70. }
  71. if (domain.contains(".uni")) {
  72. throw new BusinessException("域名后缀不符合规定.");
  73. }
  74. if (!superUserId.equals(SecurityUtils.getAuthenticatedUser().getId())) {
  75. if (isContainChinese(domain)) {
  76. throw new BusinessException("禁止注册中文域名");
  77. }
  78. if (domain.length() < 9) {
  79. throw new BusinessException("四位及以下域名只能官方创建。");
  80. }
  81. }
  82. String realName;
  83. int dotIndex = domain.indexOf(".");
  84. realName = domain.substring(0, dotIndex);
  85. Map<String, Object> checkResult = check(realName);
  86. if (!(Boolean) checkResult.get("result")) {
  87. throw new BusinessException(checkResult.get("reason").toString());
  88. }
  89. BigDecimal singlePrice = sysConfigService.getBigDecimal("domain_price");
  90. if (singlePrice.multiply(BigDecimal.valueOf(year)).compareTo(price) != 0) {
  91. throw new BusinessException("价格不符");
  92. }
  93. // LocalDateTime startTime = LocalDateTime.of(2023, 2, 10, 17, 0, 0);
  94. // if (LocalDateTime.now().isBefore(startTime)) {
  95. // throw new BusinessException("时间不符");
  96. // }
  97. // if (price.compareTo(BigDecimal.valueOf(40L)) != 0) {
  98. // throw new BusinessException("订单价格与配置不符,请重新下单.");
  99. // }
  100. User user = userRepo.findById(userId).orElseThrow(new BusinessException("未找到用户"));
  101. if (domain.contains(".")) {
  102. int dotIndex1 = domain.indexOf(".");
  103. domain = domain.substring(0, dotIndex1);
  104. }
  105. DomainOrder domainOrder = new DomainOrder();
  106. // domainOrder.setPic(Collections.singletonList(fileObject));
  107. domainOrder.setPicName(domain);
  108. domainOrder.setPrice(price);
  109. domainOrder.setDomainName((domain + ".nft").toLowerCase());
  110. domainOrder.setYears(year);
  111. domainOrder.setStatus(CollectionStatus.PENDING);
  112. domainOrder.setOrderStatus(OrderStatus.NOT_PAID);
  113. domainOrder.setUserId(user.getId());
  114. domainOrder.setUserAvatar(user.getAvatar());
  115. domainOrder.setUserName(user.getNickname());
  116. return domainOrderRepo.save(domainOrder);
  117. }
  118. public Map<String, Object> check(String domain) {
  119. Map<String, Object> result = new HashMap<>();
  120. Long superUserId = Long.valueOf(sysConfigService.getString("domain_superUserId"));
  121. String visibleDomain = domain;
  122. if (domain.contains(".uni")) {
  123. result.put("result", false);
  124. result.put("reason", "禁止使用.uni");
  125. return result;
  126. }
  127. if (!domain.contains(".nft")) {
  128. visibleDomain = domain + ".nft";
  129. }
  130. List<String> keywords = Arrays.asList(sysConfigService.getString("domain_keyword").split(","));
  131. if (!superUserId.equals(SecurityUtils.getAuthenticatedUser().getId())) {
  132. if (keywords.stream().anyMatch(keyword -> org.apache.commons.lang.StringUtils.equals(keyword, domain))) {
  133. result.put("result", false);
  134. result.put("reason", "包含敏感关键字");
  135. return result;
  136. }
  137. if (visibleDomain.length() < 9 || visibleDomain.length() > 20) {
  138. result.put("result", false);
  139. result.put("reason", "域名长度不合规");
  140. return result;
  141. }
  142. if (!contentAuditService.auditText(domain)) {
  143. result.put("result", false);
  144. result.put("reason", "该域名内容不合规");
  145. return result;
  146. }
  147. }
  148. Integer count = domainOrderRepo
  149. .countAllByDomainNameEqualsAndOrderStatusNot(visibleDomain, OrderStatus.CANCELLED);
  150. if (count > 0) {
  151. result.put("result", false);
  152. result.put("reason", "该域名已被注册");
  153. return result;
  154. }
  155. result.put("result", true);
  156. result.put("reason", "可以注册");
  157. return result;
  158. }
  159. public List<Map<String, Object>> search(String domain) {
  160. if (domain.contains(".")) {
  161. int dotIndex = domain.indexOf(".");
  162. domain = domain.substring(0, dotIndex);
  163. }
  164. Pageable pageable = PageRequest.of(0, 10, Sort.by("createdAt").descending());
  165. List<DomainOrder> used = domainOrderRepo.searchUsedDomain("%" + domain + "%", OrderStatus.CANCELLED, pageable)
  166. .getContent();
  167. String n = domain.substring(domain.length() - 1);
  168. List<Map<String, Object>> recommend = new ArrayList<>();
  169. for (int i = 0; i < 100; i++) {
  170. if (i != 0) {
  171. domain = domain + n;
  172. }
  173. Map<String, Object> checkResult = check(domain);
  174. if ((Boolean) checkResult.get("result")) {
  175. Map<String, Object> sold = new HashMap<>();
  176. sold.put("domain", (domain + ".nft").toLowerCase());
  177. sold.put("sold", false);
  178. recommend.add(sold);
  179. }
  180. if (recommend.size() > 2) {
  181. break;
  182. }
  183. }
  184. List<Map<String, Object>> result = new ArrayList<>(recommend);
  185. used.forEach(domainOrder -> {
  186. Map<String, Object> sold = new HashMap<>();
  187. if (!domainOrder.getDomainName().contains(".uni")) {
  188. sold.put("domain", domainOrder.getDomainName().toLowerCase());
  189. sold.put("endTime", domainOrder.getEndTime());
  190. sold.put("sold", true);
  191. result.add(sold);
  192. }
  193. if (result.size() > 9) {
  194. }
  195. });
  196. return result;
  197. }
  198. @Transient
  199. public void notify(Long id, PayMethod payMethod, String transactionId) throws FontFormatException, IOException, WriterException {
  200. DomainOrder domainOrder = domainOrderRepo.findById(id).orElseThrow(new BusinessException("未找到星图"));
  201. if (!domainOrder.getOrderStatus().equals(OrderStatus.NOT_PAID)) {
  202. throw new BusinessException("订单已经处理");
  203. }
  204. BufferedImage img = domainImg(domainOrder.getDomainName());
  205. ByteArrayOutputStream os = new ByteArrayOutputStream();
  206. ImageIO.write(img, "jpg", os);
  207. InputStream input = new ByteArrayInputStream(os.toByteArray());
  208. String path = "image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
  209. + RandomStringUtils.randomAlphabetic(8) + ".jpg";
  210. String realUrl = storageService.uploadFromInputStream(input, path);
  211. FileObject fileObject = new FileObject();
  212. fileObject.setName(domainOrder.getDomainName());
  213. fileObject.setType("image/jpeg");
  214. fileObject.setUrl(realUrl);
  215. domainOrder.setOrderStatus(OrderStatus.FINISH);
  216. domainOrder.setPayMethod(payMethod);
  217. domainOrder.setPic(Collections.singletonList(fileObject));
  218. domainOrder.setTransactionId(transactionId);
  219. domainOrder.setStatus(CollectionStatus.SUCCESS);
  220. domainOrder.setCreateAssetId(createAsset(domainOrder));
  221. domainOrder.setEndTime(LocalDateTime.now().plusYears(domainOrder.getYears()));
  222. domainOrderRepo.save(domainOrder);
  223. rockRecordService.addRock(domainOrder.getUserId(), domainOrder.getPrice(), "购买");
  224. }
  225. public void cancel(DomainOrder domainOrder) {
  226. domainOrder.setOrderStatus(OrderStatus.CANCELLED);
  227. domainOrder.setStatus(CollectionStatus.FAIL);
  228. domainOrderRepo.save(domainOrder);
  229. }
  230. public Long createAsset(DomainOrder domainOrder) {
  231. return assetService.createAsset(domainOrder, userRepo.findById(domainOrder.getUserId())
  232. .orElseThrow(new BusinessException("无用户记录")), null, BigDecimal.ZERO, "域名", null, false).getId();
  233. }
  234. public BufferedImage domainImg(String domain) throws IOException, FontFormatException, WriterException {
  235. String domainName;
  236. if (domain.contains(".")) {
  237. int dotIndex = domain.indexOf(".");
  238. domainName = domain.substring(0, dotIndex).toUpperCase();
  239. } else {
  240. domainName = domain.toUpperCase();
  241. }
  242. InputStream is1 = this.getClass()
  243. .getResourceAsStream("/font/VonwaonBitmap_16pxLite.ttf");
  244. Font font1 = Font.createFont(Font.TRUETYPE_FONT, is1);
  245. is1.close();
  246. InputStream is2 = this.getClass()
  247. .getResourceAsStream("/font/VonwaonBitmap_12pxLite.ttf");
  248. Font font2 = Font.createFont(Font.TRUETYPE_FONT, is2);
  249. is2.close();
  250. int length = domainName.length();
  251. BufferedImage shareImg;
  252. if (length <= 2) {
  253. InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_jing.png");
  254. shareImg = ImageIO.read(is3);
  255. is3.close();
  256. } else if (length <= 4) {
  257. InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_lv.png");
  258. shareImg = ImageIO.read(is3);
  259. is3.close();
  260. } else {
  261. InputStream is3 = this.getClass().getResourceAsStream("/static/img/png_zi.png");
  262. shareImg = ImageIO.read(is3);
  263. is3.close();
  264. }
  265. BufferedImage result = new BufferedImage(shareImg.getWidth(), shareImg.getHeight(), BufferedImage.TYPE_INT_RGB);
  266. Graphics2D g = result.createGraphics();
  267. g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  268. // g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  269. // g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  270. g.setComposite(AlphaComposite.SrcOver);
  271. g.drawImage(shareImg, 0, 0, null);
  272. // BufferedImage avatarImg = ImageUtils.makeRoundedCorner(ImageUtils.scale(ImageIO.read(new URL(user.getAvatar())),
  273. // 80, 80,
  274. // ImageUtils.Fit.COVER), 40);
  275. // g.drawImage(avatarImg, 334, 136, null);
  276. int domainLength = domainName.length();
  277. if (domainLength > 10) {
  278. g.setColor(new Color(255, 255, 255));
  279. Font topFont = font1.deriveFont(Font.PLAIN, 130f);
  280. Font downFont = font2.deriveFont(Font.BOLD, 36f);
  281. int subIndex = domainLength / 3;
  282. String str1 = domainName.substring(0, subIndex);
  283. String str2 = domainName.substring(subIndex, subIndex + subIndex);
  284. String str3 = domainName.substring(subIndex + subIndex, domainLength);
  285. ImageUtils.drawCenteredString(g, str1, new Rectangle(0, 180, shareImg
  286. .getWidth(), 86), topFont);
  287. ImageUtils.drawCenteredString(g, str2, new Rectangle(0, 300, shareImg
  288. .getWidth(), 86), topFont);
  289. ImageUtils.drawCenteredString(g, str3, new Rectangle(0, 420, shareImg
  290. .getWidth(), 86), topFont);
  291. ImageUtils.drawCenteredString(g, ".NFT", new Rectangle(0, 540, shareImg
  292. .getWidth(), 86), topFont);
  293. g.setColor(new Color(255, 255, 255));
  294. ImageUtils.drawCenteredString(g, domain, new Rectangle(0, 650, shareImg
  295. .getWidth(), 12), downFont);
  296. }
  297. if (domainLength > 5 & domainLength <= 10) {
  298. g.setColor(new Color(255, 255, 255));
  299. Font topFont = font1.deriveFont(Font.PLAIN, 190f);
  300. Font downFont = font2.deriveFont(Font.BOLD, 36f);
  301. int subIndex = domainLength / 2;
  302. String str1 = domainName.substring(0, subIndex);
  303. String str2 = domainName.substring(subIndex, domainLength);
  304. ImageUtils.drawCenteredString(g, str1, new Rectangle(0, 180, shareImg
  305. .getWidth(), 86), topFont);
  306. ImageUtils.drawCenteredString(g, str2, new Rectangle(0, 350, shareImg
  307. .getWidth(), 86), topFont);
  308. ImageUtils.drawCenteredString(g, ".NFT", new Rectangle(0, 520, shareImg
  309. .getWidth(), 86), topFont);
  310. g.setColor(new Color(255, 255, 255));
  311. ImageUtils.drawCenteredString(g, domain, new Rectangle(0, 650, shareImg
  312. .getWidth(), 12), downFont);
  313. }
  314. if (domainLength <= 5) {
  315. g.setColor(new Color(255, 255, 255));
  316. Font topFont = font1.deriveFont(Font.PLAIN, 240f);
  317. Font downFont = font2.deriveFont(Font.BOLD, 36f);
  318. ImageUtils.drawCenteredString(g, domainName, new Rectangle(17, 220, shareImg
  319. .getWidth(), 86), topFont);
  320. ImageUtils.drawCenteredString(g, ".NFT", new Rectangle(-10, 420, shareImg
  321. .getWidth(), 86), topFont);
  322. g.setColor(new Color(255, 255, 255));
  323. ImageUtils.drawCenteredString(g, domain, new Rectangle(0, 620, shareImg
  324. .getWidth(), 12), downFont);
  325. }
  326. //二维码
  327. // QRCodeWriter qrCodeWriter = new QRCodeWriter();
  328. // Map<EncodeHintType, Object> hints = new HashMap<>();
  329. // hints.put(EncodeHintType.MARGIN, 2);
  330. // BitMatrix bitMatrix = qrCodeWriter
  331. // .encode(env.getProperty("general.host") + "/wx/share?invitor=" + user.getId(),
  332. // BarcodeFormat.QR_CODE, 252, 252, hints);
  333. // g.drawImage(MatrixToImageWriter.toBufferedImage(bitMatrix), 250, 386, null);
  334. return result;
  335. }
  336. }