Просмотр исходного кода

Merge branch 'dev' of http://git.izouma.com/xiongzhu/raex_back into dev

wangqifan 4 лет назад
Родитель
Сommit
1d88552b8e

+ 25 - 0
pom.xml

@@ -426,6 +426,31 @@
             <version>2.2.2</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>cdn20180510</artifactId>
+            <version>1.0.9</version>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>tea-openapi</artifactId>
+            <version>0.2.2</version>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>tea-console</artifactId>
+            <version>0.0.1</version>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>tea-util</artifactId>
+            <version>0.2.13</version>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>tea</artifactId>
+            <version>1.1.14</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 27 - 0
src/main/java/com/izouma/nineth/config/AliyunConfig.java

@@ -0,0 +1,27 @@
+package com.izouma.nineth.config;
+
+import com.aliyun.cdn20180510.Client;
+import com.aliyun.oss.OSSClient;
+import com.aliyun.teaopenapi.models.Config;
+import lombok.AllArgsConstructor;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
+
+@AllArgsConstructor
+@Configuration
+@EnableConfigurationProperties(AliyunProperties.class)
+public class AliyunConfig {
+
+    private AliyunProperties aliyunProperties;
+
+    @Bean
+    public Client cdnClient() throws Exception {
+        Config config = new Config()
+                .setAccessKeyId(aliyunProperties.getAccessKeyId())
+                .setAccessKeySecret(aliyunProperties.getAccessKeySecret());
+        config.endpoint = "cdn.aliyuncs.com";
+        return new Client(config);
+    }
+}

+ 14 - 0
src/main/java/com/izouma/nineth/config/AliyunProperties.java

@@ -0,0 +1,14 @@
+package com.izouma.nineth.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+@Data
+@ConfigurationProperties(prefix = "aliyun")
+public class AliyunProperties {
+    private String accessKeyId;
+    private String accessKeySecret;
+    private String ossBucketName;
+    private String ossEndPoint;
+    private String ossDomain;
+}

+ 1 - 0
src/main/java/com/izouma/nineth/security/WebSecurityConfig.java

@@ -121,6 +121,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/sandpay/**").permitAll()
                 .antMatchers("/hmpay/**").permitAll()
                 .antMatchers("/order/calcSettle").permitAll()
+                .antMatchers("/ossNotify").permitAll()
                 // all other requests need to be authenticated
                 .anyRequest().authenticated().and()
                 // make sure we use stateless session; session won't be used to

+ 58 - 0
src/main/java/com/izouma/nineth/service/SandPayService.java

@@ -302,6 +302,64 @@ public class SandPayService {
         return res.getJSONObject("body").getString("credential");
     }
 
+    @Cacheable(value = "sandPayQuick", key = "#orderId")
+    public String payGiftQuick(Long orderId) {
+        GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
+        if (order.getStatus() != OrderStatus.NOT_PAID) {
+            throw new BusinessException("订单状态错误");
+        }
+        JSONObject extend = new JSONObject();
+        extend.put("type", "gift");
+        extend.put("id", orderId);
+
+        JSONObject res = requestQuick(orderId.toString(), order.getGasPrice(), "转增" + order.getAssetId(),
+                "转增" + order.getAssetId(), 180, extend.toJSONString(),
+                generalProperties.getHost() + "/9th/");
+        if (res == null)
+            throw new BusinessException("下单失败,请稍后再试");
+
+        if (!"000000".equals(res.getJSONObject("head").getString("respCode"))) {
+            String msg = res.getJSONObject("head").getString("respMsg");
+            if (msg.contains("超限")) {
+                throw new BusinessException("超过商户单日额度");
+            }
+            if (msg.contains("商户状态")) {
+                throw new BusinessException("超过商户单日额度");
+            }
+            throw new BusinessException(msg);
+        }
+        return res.getJSONObject("body").getString("credential");
+    }
+
+    @Cacheable(value = "sandPayQuick", key = "#orderId")
+    public String payMintQuick(Long orderId) {
+        MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
+        if (order.getStatus() != MintOrderStatus.NOT_PAID) {
+            throw new BusinessException("订单状态错误");
+        }
+        JSONObject extend = new JSONObject();
+        extend.put("type", "mintOrder");
+        extend.put("id", orderId);
+
+        JSONObject res = requestQuick(orderId.toString(), order.getGasPrice(),
+                "铸造活动:" + order.getMintActivityId(), "铸造活动:" + order.getMintActivityId(),
+                180, extend.toJSONString(), generalProperties.getHost() + "/9th/");
+        if (res == null)
+            throw new BusinessException("下单失败,请稍后再试");
+
+        if (!"000000".equals(res.getJSONObject("head").getString("respCode"))) {
+            String msg = res.getJSONObject("head").getString("respMsg");
+            if (msg.contains("超限")) {
+                throw new BusinessException("超过商户单日额度");
+            }
+            if (msg.contains("商户状态")) {
+                throw new BusinessException("超过商户单日额度");
+            }
+            throw new BusinessException(msg);
+        }
+        return res.getJSONObject("body").getString("credential");
+    }
+
     @Cacheable(value = "sandPay", key = "#orderId")
     public String payGiftOrder(Long orderId) {
         GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));

+ 8 - 10
src/main/java/com/izouma/nineth/service/storage/AliStorageService.java

@@ -2,7 +2,9 @@ package com.izouma.nineth.service.storage;
 
 import com.aliyun.oss.OSSClient;
 import com.aliyun.oss.model.ObjectMetadata;
+import com.izouma.nineth.config.AliyunProperties;
 import com.izouma.nineth.exception.BusinessException;
+import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -17,15 +19,10 @@ import java.net.URL;
 @Data
 @Service
 @Slf4j
-@EnableConfigurationProperties
-@ConfigurationProperties(prefix = "aliyun")
+@AllArgsConstructor
 @ConditionalOnProperty(name = "storage.provider", havingValue = "aliyun")
 public class AliStorageService implements StorageService {
-    private String accessKeyId;
-    private String accessKeySecret;
-    private String ossBucketName;
-    private String ossEndPoint;
-    private String ossDomain;
+    private AliyunProperties aliyunProperties;
 
     @Override
     public String uploadFromInputStream(InputStream inputStream, String path) {
@@ -55,16 +52,17 @@ public class AliStorageService implements StorageService {
     }
 
     private String upload(InputStream inputStream, String path) {
-        OSSClient client = new OSSClient(ossEndPoint, accessKeyId, accessKeySecret);
+        OSSClient client = new OSSClient(aliyunProperties.getOssEndPoint(), aliyunProperties.getAccessKeyId(),
+                aliyunProperties.getAccessKeySecret());
         ObjectMetadata metadata = new ObjectMetadata();
-        client.putObject(ossBucketName, path, inputStream, metadata);
+        client.putObject(aliyunProperties.getOssBucketName(), path, inputStream, metadata);
         client.shutdown();
         try {
             inputStream.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
-        return ossDomain + "/" + path;
+        return aliyunProperties.getOssDomain() + "/" + path;
     }
 
 }

+ 13 - 0
src/main/java/com/izouma/nineth/web/FileUploadController.java

@@ -3,6 +3,7 @@ package com.izouma.nineth.web;
 import com.izouma.nineth.domain.FileObject;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.service.storage.StorageService;
+import com.izouma.nineth.utils.DateTimeUtils;
 import com.izouma.nineth.utils.ImageUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FileUtils;
@@ -26,6 +27,8 @@ import java.awt.image.BufferedImage;
 import java.io.*;
 import java.net.URLConnection;
 import java.text.SimpleDateFormat;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.regex.Pattern;
 
@@ -205,4 +208,14 @@ public class FileUploadController {
                 .orElseThrow(new BusinessException("找不到fbx文件"));
         return new FileObject(fbxFile.getName(), fbxUrl, null, "fbx");
     }
+
+    @PostMapping("/user")
+    public String uploadFile(@RequestParam("file") MultipartFile file, @RequestParam String type) throws IOException {
+        String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("")
+                .toLowerCase().replace("jpeg", "jpg");
+        String path = "user/" + type + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
+                + RandomStringUtils.randomAlphabetic(8)
+                + "." + ext;
+        return uploadFile(file, path, true, 300, 300);
+    }
 }

+ 30 - 0
src/main/java/com/izouma/nineth/web/OSSAuditController.java

@@ -0,0 +1,30 @@
+package com.izouma.nineth.web;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.aliyun.cdn20180510.Client;
+import com.aliyun.cdn20180510.models.RefreshObjectCachesRequest;
+import com.aliyun.cdn20180510.models.RefreshObjectCachesResponse;
+import com.aliyun.teaopenapi.models.Config;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@Slf4j
+@AllArgsConstructor
+public class OSSAuditController {
+
+    private Client cdnClient;
+
+    @PostMapping("/ossNotify")
+    public String ossNotify(String checksum, String content) throws Exception {
+        JSONObject jsonObject = JSON.parseObject(content);
+        RefreshObjectCachesRequest refreshObjectCachesRequest = new RefreshObjectCachesRequest();
+        refreshObjectCachesRequest.setObjectPath("cdn.raex.vip/" + jsonObject.getString("object"));
+        RefreshObjectCachesResponse resp = cdnClient.refreshObjectCaches(refreshObjectCachesRequest);
+        log.info(JSON.toJSONString(resp, true));
+        return "ok";
+    }
+}

+ 12 - 0
src/main/java/com/izouma/nineth/web/OrderPayControllerV2.java

@@ -72,6 +72,12 @@ public class OrderPayControllerV2 {
         return "AlipayHtml";
     }
 
+    @RequestMapping(value = "/gift/sandQuick", method = RequestMethod.GET, produces = "text/html")
+    @ResponseBody
+    public String payGiftQuick(@RequestParam Long id) {
+        return sandPayService.payGiftQuick(id);
+    }
+
     @RequestMapping(value = "/mint/alipay", method = RequestMethod.GET)
     @ResponseBody
     public String payMintOrderAlipayH5(Long id) {
@@ -85,4 +91,10 @@ public class OrderPayControllerV2 {
         model.addAttribute("orderId", id);
         return "AlipayHtml2";
     }
+
+    @RequestMapping(value = "/mint/sandQuick", method = RequestMethod.GET, produces = "text/html")
+    @ResponseBody
+    public String payMintQuick(@RequestParam Long id) {
+        return sandPayService.payMintQuick(id);
+    }
 }

+ 24 - 0
src/test/java/com/izouma/nineth/CDNTest.java

@@ -0,0 +1,24 @@
+package com.izouma.nineth;
+
+
+import com.alibaba.fastjson.JSON;
+import com.aliyun.cdn20180510.Client;
+import com.aliyun.cdn20180510.models.RefreshObjectCachesRequest;
+import com.aliyun.cdn20180510.models.RefreshObjectCachesResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+@Slf4j
+public class CDNTest extends ApplicationTests {
+    @Autowired
+    private Client cdnClient;
+
+    @Test
+    public void testCdnRefresh() throws Exception {
+        RefreshObjectCachesRequest refreshObjectCachesRequest = new RefreshObjectCachesRequest();
+        refreshObjectCachesRequest.setObjectPath("cdn.raex.vip/user/avatar/2022-04-27-16-37-09dvFjvMgW.jpg");
+        RefreshObjectCachesResponse resp = cdnClient.refreshObjectCaches(refreshObjectCachesRequest);
+        log.info(JSON.toJSONString(resp, true));
+    }
+}