From 780ff45fdc167cadf4724c6c94530929b7445aab Mon Sep 17 00:00:00 2001
From: zwl <1051256694@qq.com>
Date: 星期一, 16 三月 2026 10:37:51 +0800
Subject: [PATCH] 完善许可证

---
 src/main/java/com/zy/system/controller/LicenseCreatorController.java |  191 ++++++++++++++++++++++++++++++-----------------
 1 files changed, 120 insertions(+), 71 deletions(-)

diff --git a/src/main/java/com/zy/system/controller/LicenseCreatorController.java b/src/main/java/com/zy/system/controller/LicenseCreatorController.java
index 0aafb3b..e949e1f 100644
--- a/src/main/java/com/zy/system/controller/LicenseCreatorController.java
+++ b/src/main/java/com/zy/system/controller/LicenseCreatorController.java
@@ -1,112 +1,161 @@
 package com.zy.system.controller;
 
-import com.core.common.Cools;
+import com.core.annotations.ManagerAuth;
 import com.core.common.R;
-import com.zy.system.entity.license.*;
+import com.zy.system.entity.LicenseInfos;
+import com.zy.system.entity.license.LicenseCheckListener;
+import com.zy.system.entity.license.LicenseUploadParam;
+import com.zy.system.entity.license.LicenseUtils;
+import com.zy.system.entity.license.LicenseVerify;
+import com.zy.system.entity.license.LicenseVerifyParam;
+import com.zy.system.service.LicenseInfosService;
+import com.zy.system.timer.LicenseTimer;
 import de.schlichtherle.license.LicenseContent;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.File;
-import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Base64;
 import java.util.Date;
 
 /**
- *
- * 鐢ㄤ簬鐢熸垚璇佷功鏂囦欢锛屼笉鑳芥斁鍦ㄧ粰瀹㈡埛閮ㄧ讲鐨勪唬鐮侀噷
+ * 璁稿彲璇佽繍琛屾椂绠$悊
  */
 @RestController
 @RequestMapping("/license")
 public class LicenseCreatorController {
 
-    @Value("${license.licensePath}")
-    private String licensePath;
-
+    @Value("${license.subject}")
+    private String licenseSubject;
+    @Value("${license.publicAlias}")
+    private String publicAlias;
+    @Value("${license.storePass}")
+    private String storePass;
+    @Value("${license.publicKeysStorePath}")
+    private String publicKeysStorePath;
     @Autowired
     private LicenseCheckListener licenseCheckListener;
+    @Autowired
+    private LicenseTimer licenseTimer;
+    @Autowired
+    private LicenseInfosService licenseInfosService;
 
     /**
-     * 鑾峰彇鏈嶅姟鍣ㄧ‖浠朵俊鎭�
-     * @param osName 鎿嶄綔绯荤粺绫诲瀷锛屽鏋滀负绌哄垯鑷姩鍒ゆ柇
+     * 鑾峰彇璇锋眰鐮併��
      */
-    @RequestMapping(value = "/getServerInfos",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
-    public LicenseCheck getServerInfos(@RequestParam(value = "osName",required = false) String osName) {
-        //鎿嶄綔绯荤粺绫诲瀷
-        if(Cools.isEmpty(osName)){
-            osName = System.getProperty("os.name");
-        }
-        osName = osName.toLowerCase();
+    @ManagerAuth(value = ManagerAuth.Auth.NONE)
+    @RequestMapping(value = "/getRequestCode", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
+    public R getRequestCode() {
+        return R.ok(LicenseUtils.buildRequestCode(licenseSubject));
+    }
 
-        AbstractServerInfos abstractServerInfos = null;
-
-        //鏍规嵁涓嶅悓鎿嶄綔绯荤粺绫诲瀷閫夋嫨涓嶅悓鐨勬暟鎹幏鍙栨柟娉�
-        if (osName.startsWith("windows")) {
-            abstractServerInfos = new WindowsServerInfos();
-        } else if (osName.startsWith("linux")) {
-//            abstractServerInfos = new LinuxServerInfos();
-        }else{//鍏朵粬鏈嶅姟鍣ㄧ被鍨�
-            abstractServerInfos = new WindowsServerInfos();
-        }
-
-        return abstractServerInfos.getServerInfos();
+    /**
+     * 鑾峰彇绯荤粺閰嶇疆銆�
+     */
+    @ManagerAuth(value = ManagerAuth.Auth.NONE)
+    @RequestMapping(value = "/getServerInfos", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
+    public Object getServerInfos(@RequestParam(value = "osName", required = false) String osName) {
+        return LicenseUtils.getServerInfos();
     }
 
     /**
      * 鑾峰彇璁稿彲璇佹湁鏁堟湡澶╂暟
      */
-    @RequestMapping(value = "/getLicenseDays")
+    @ManagerAuth(value = ManagerAuth.Auth.NONE)
+    @RequestMapping(value = "/getLicenseDays", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
     public R getLicenseDays() {
-        LicenseVerify licenseVerify = new LicenseVerify();
-        LicenseContent verifyInfo = licenseVerify.getVerifyInfo();
-        if (verifyInfo == null) {
-            return R.error();
+        int licenseDays = licenseTimer.getLicenseDays();
+        if (!licenseTimer.getSystemSupport()) {
+            licenseDays = -1;
         }
-
-        Date start = new Date();
-        Date end = verifyInfo.getNotAfter();
-        Long starTime = start.getTime();
-        Long endTime = end.getTime();
-        Long num = endTime - starTime;//鏃堕棿鎴崇浉宸殑姣鏁�
-        int day = (int) (num / 24 / 60 / 60 / 1000);
-        return R.ok().add(day);
+        return R.ok(licenseDays);
     }
 
-    @RequestMapping(value = "/updateLicense")
-    public R updateLicense(@RequestParam("file") MultipartFile[] files){
-        MultipartFile file = files[0];
-
-        String licensePathFileName = this.getClass().getClassLoader().getResource(licensePath).getPath();
-        File licensePathFile = new File(licensePathFileName);
-        //鏈嶅姟鍣ㄧ淇濆瓨鐨勬枃浠跺璞�
-        File serverFile = new File(licensePathFile.getPath());
-        if (serverFile.exists()) {
-            try {
-                serverFile.delete();//瀛樺湪鏂囦欢锛屽垹闄�
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
+    @ManagerAuth(value = ManagerAuth.Auth.NONE)
+    @RequestMapping(value = "/updateLicense", consumes = MediaType.APPLICATION_JSON_VALUE, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
+    public R updateLicense(@RequestBody LicenseUploadParam param){
+        if (param == null || param.getLicense() == null || param.getLicense().trim().isEmpty()) {
+            return R.error("璁稿彲璇佸唴瀹逛笉鑳戒负绌�");
         }
+        return saveAndActivateLicense(param.getLicense().trim());
+    }
 
+    @ManagerAuth(value = ManagerAuth.Auth.NONE)
+    @RequestMapping(value = "/updateLicense", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
+    public R updateLicense(@RequestParam("file") MultipartFile[] files) {
+        if (files == null || files.length == 0 || files[0] == null || files[0].isEmpty()) {
+            return R.error("璁稿彲璇佹枃浠朵笉鑳戒负绌�");
+        }
         try {
-            //鍒涘缓鏂囦欢
-            serverFile.createNewFile();
-            //灏嗕笂浼犵殑鏂囦欢鍐欏叆鍒版湇鍔″櫒绔枃浠跺唴
-            file.transferTo(serverFile);
-        } catch (IOException e) {
-            e.printStackTrace();
+            String licenseBase64 = Base64.getEncoder().encodeToString(files[0].getBytes());
+            return saveAndActivateLicense(licenseBase64);
+        } catch (Exception e) {
+            return R.error("璁稿彲璇佽鍙栧け璐�");
         }
-
-        //閲嶆柊鍔犺浇璁稿彲璇�
-        boolean loadedLicense = licenseCheckListener.loadLicense();
-        if (loadedLicense) {
-            return R.ok();
-        }
-        return R.error("璁稿彲璇佹洿鏂板け璐�");
     }
 
-}
\ No newline at end of file
+    @ManagerAuth(value = ManagerAuth.Auth.NONE)
+    @RequestMapping(value = "/activate", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
+    public R activate() {
+        licenseTimer.timer();
+        if (!licenseTimer.getSystemSupport()) {
+            return R.error("璁稿彲璇佹縺娲诲け璐�");
+        }
+        return R.ok();
+    }
+
+    @ManagerAuth(value = ManagerAuth.Auth.NONE)
+    @RequestMapping(value = "/getProjectName", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
+    public R getProjectName() {
+        return R.ok(licenseSubject);
+    }
+
+    private R saveAndActivateLicense(String licenseBase64) {
+        LicenseVerifyParam verifyParam = buildVerifyParam();
+        LicenseVerify licenseVerify = new LicenseVerify();
+        LicenseContent install = licenseVerify.install(verifyParam, licenseBase64);
+        if (install == null) {
+            return R.error("璁稿彲璇佸唴瀹规棤鏁�");
+        }
+
+        LicenseInfos licenseInfos = new LicenseInfos();
+        licenseInfos.setLicense(licenseBase64);
+        licenseInfos.setLicenseTime(formatLicenseTime(install));
+        licenseInfos.setRequestCode(LicenseUtils.buildRequestCode(licenseSubject));
+        licenseInfos.setCreateTime(new Date());
+        if (!licenseInfosService.insert(licenseInfos)) {
+            return R.error("璁稿彲璇佷繚瀛樺け璐�");
+        }
+
+        boolean loadedLicense = licenseCheckListener.loadLicense(false);
+        if (!loadedLicense) {
+            return R.error("璁稿彲璇佹縺娲诲け璐�");
+        }
+        licenseTimer.verify();
+        if (!licenseTimer.getSystemSupport()) {
+            return R.error("璁稿彲璇佹牎楠屽け璐�");
+        }
+        return R.ok();
+    }
+
+    private LicenseVerifyParam buildVerifyParam() {
+        LicenseVerifyParam param = new LicenseVerifyParam();
+        param.setSubject(licenseSubject);
+        param.setPublicAlias(publicAlias);
+        param.setStorePass(storePass);
+        param.setPublicKeysStorePath(publicKeysStorePath);
+        return param;
+    }
+
+    private String formatLicenseTime(LicenseContent install) {
+        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        return format.format(install.getNotBefore()) + "  -  " + format.format(install.getNotAfter());
+    }
+
+}

--
Gitblit v1.9.1