Browse Source

feat: 完播统计

xdd 1 day ago
parent
commit
5e24a8e9e6

+ 50 - 0
fs-service/src/main/java/com/fs/utils/FileCacheService.java

@@ -0,0 +1,50 @@
+package com.fs.utils;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+@Service
+public class FileCacheService {
+
+    @Value("${getTuiImg}")
+    private String getTuiImg;
+
+    @Value("${getTuiFont}")
+    private String getTuiFont;
+
+    private byte[] templateBytes;
+    private byte[] fontBytes;
+
+    @PostConstruct
+    public void init() throws IOException {
+        // 缓存模板图片文件
+        this.templateBytes = Files.readAllBytes(Paths.get(getTuiImg));
+        // 缓存字体文件
+        this.fontBytes = Files.readAllBytes(Paths.get(getTuiFont));
+    }
+
+    /**
+     * 获取模板图片的 InputStream
+     */
+    public InputStream getTemplateStream() {
+        return new ByteArrayInputStream(templateBytes);
+    }
+
+    /**
+     * 获取字体文件的 InputStream
+     */
+    public InputStream getFontStream() {
+        return new ByteArrayInputStream(fontBytes);
+    }
+
+    // 可选:提供重新加载方法(如需热更新)
+    public void reload() throws IOException {
+        this.templateBytes = Files.readAllBytes(Paths.get(getTuiImg));
+        this.fontBytes = Files.readAllBytes(Paths.get(getTuiFont));
+    }
+}

+ 5 - 17
fs-user-app/src/main/java/com/fs/app/controller/store/UserScrmController.java

@@ -27,6 +27,7 @@ import com.fs.hisStore.vo.FsStoreOrderTuiVO;
 import com.fs.hisStore.vo.FsStoreProductRelationQueryVO;
 import com.fs.hisStore.vo.FsUserExtractVO;
 import com.fs.hisStore.vo.FsUserTuiVO;
+import com.fs.utils.FileCacheService;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import io.swagger.annotations.Api;
@@ -78,6 +79,8 @@ public class UserScrmController extends AppBaseController {
     private IFsUserCourseVideoService courseVideoService;
     @Autowired
     private IFsUserScrmService fsUserService;
+    @Autowired
+    private FileCacheService fileCacheService;
 
     /**
      * 获取用户信息
@@ -121,10 +124,6 @@ public class UserScrmController extends AppBaseController {
         return R.ok("认证成功");
     }
 
-    @Value("${getTuiImg}")
-    private String getTuiImg;
-    @Value("${getTuiFont}")
-    private String getTuiFont;
     @Login
     @ApiOperation("获取推荐海报")
     @GetMapping("/getTuiImg")
@@ -160,18 +159,7 @@ public class UserScrmController extends AppBaseController {
                 user.setUserCode(userMap.getUserCode());
                 log.info("成功生成并更新用户邀请码: {}", userMap.getUserCode());
             }
-            File templateSourceFile = new File(getTuiImg);
-            File fontSourceFile = new File(getTuiFont);
-
-            if (!templateSourceFile.exists() || !templateSourceFile.isFile()) {
-                log.error("海报模板文件不存在: {}", getTuiImg);
-                return R.error("海报模板文件不存在");
-            }
 
-            if (!fontSourceFile.exists() || !fontSourceFile.isFile()) {
-                log.error("字体文件不存在: {}", getTuiFont);
-                return R.error("字体文件不存在");
-            }
             String tempDir = System.getProperty("java.io.tmpdir") + File.separator + "poster_temp";
             File tempDirFile = new File(tempDir);
             if (!tempDirFile.exists()) {
@@ -183,7 +171,7 @@ public class UserScrmController extends AppBaseController {
 
             try {
                 log.info("开始加载海报模板图片");
-                templateStream = new FileInputStream(templateSourceFile);
+                templateStream = fileCacheService.getTemplateStream();
                 FileUtils.copyInputStreamToFile(templateStream, tempTemplateFile);
                 log.info("海报模板图片加载成功: {}", tempTemplateFile.getAbsolutePath());
                 if (!isValidImageFile(tempTemplateFile)) {
@@ -191,7 +179,7 @@ public class UserScrmController extends AppBaseController {
                     return R.error("模板图片文件无效");
                 }
                 log.info("开始加载字体文件");
-                fontStream = new FileInputStream(fontSourceFile);
+                fontStream = fileCacheService.getFontStream();
                 FileUtils.copyInputStreamToFile(fontStream, tempFontFile);
                 log.info("字体文件加载成功: {}", tempFontFile.getAbsolutePath());