Explorar o código

refactor(core): 优化核心函数调用与类型声明

- 将 LogisiticsInformationResponse 的 jsonSerialize 返回类型从 mixed 改为 array
- 在 BaseJob 中增加默认执行逻辑分支,提高代码健壮性
- 为 secret_password 函数添加 RandomException 异常声明
- 移除 Helper 类中冗余的 use 语句和重复实现,统一调用全局函数
- 简化 success_response、secret_password 和 extension_path 方法实现逻辑
runphp hai 3 meses
pai
achega
563fdd6020
Modificáronse 4 ficheiros con 9 adicións e 51 borrados
  1. 1 1
      src/Contracts/LogisticsInformationResponse.php
  2. 3 50
      src/Helper.php
  3. 3 0
      src/Job/BaseJob.php
  4. 2 0
      src/functions.php

+ 1 - 1
src/Contracts/LogisticsInformationResponse.php

@@ -23,7 +23,7 @@ class LogisticsInformationResponse implements JsonSerializable
     {
     }
 
-    public function jsonSerialize(): mixed
+    public function jsonSerialize(): array
     {
         return [
             'extension_id' => $this->extensionID,

+ 3 - 50
src/Helper.php

@@ -4,9 +4,7 @@ declare(strict_types=1);
 namespace SixShop\Core;
 
 use SixShop\Core\Exception\LogicException;
-use SixShop\Core\Response\Xml;
 use SixShop\Core\Service\CoreService;
-use think\Container;
 use think\Paginator;
 use think\Response;
 
@@ -23,23 +21,7 @@ final class Helper
      */
     public static function success_response(mixed $data = [], string $status = 'ok', int $code = 200, string $msg = 'success', string $type = 'json', string $xslt = ''): Response
     {
-        if ($xslt) {
-            $type = 'xml';
-        }
-        $responseData = [
-            'code' => $code,
-            'status' => $status,
-            'msg' => $msg,
-            'data' => $data
-        ];
-        if ($type == 'xml') {
-            /* @var Xml $response */
-            $response = Container::getInstance()->invokeClass(Xml::class, [$responseData, 200]);
-            $response = $response->options(['root_node' => 'root', 'xslt' => $xslt]);
-        } else {
-            $response = Response::create($responseData, $type);
-        }
-        return $response;
+        return success_response($data, $status, $code, $msg, $type, $xslt);
     }
 
     /**
@@ -143,26 +125,7 @@ final class Helper
      */
     public static function secret_password(int $length = 16): string
     {
-        // 确保密码包含各种字符类型
-        $lowercase = 'abcdefghijklmnopqrstuvwxyz';
-        $uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
-        $numbers = '0123456789';
-        $specialChars = '!@#$%^&*()_+-=[]{}|;:,.<>?';
-
-        // 至少包含每种类型的一个字符
-        $password = $lowercase[random_int(0, strlen($lowercase) - 1)];
-        $password .= $uppercase[random_int(0, strlen($uppercase) - 1)];
-        $password .= $numbers[random_int(0, strlen($numbers) - 1)];
-        $password .= $specialChars[random_int(0, strlen($specialChars) - 1)];
-
-        // 剩余字符随机生成
-        $allChars = $lowercase . $uppercase . $numbers . $specialChars;
-        for ($i = 4; $i < $length; $i++) {
-            $password .= $allChars[random_int(0, strlen($allChars) - 1)];
-        }
-
-        // 打乱字符顺序
-        return str_shuffle($password);
+        return secret_password($length);
     }
 
     /**
@@ -173,17 +136,7 @@ final class Helper
      */
     public static function extension_path(string $extensionID = ''): string
     {
-        if (!$extensionID) {
-            return CoreService::$extensionPath;
-        }
-        if (isset(CoreService::$extensionComposerMap[$extensionID]['name'])) {
-            $extensionName = CoreService::$extensionComposerMap[$extensionID]['name'];
-            $versions = Plugin::getInstalledSixShopExtensions()['versions'];
-            if (isset($versions[$extensionName]['install_path'])) {
-                return realpath($versions[$extensionName]['install_path']) . DIRECTORY_SEPARATOR;
-            }
-        }
-        return CoreService::$extensionPath . $extensionID . DIRECTORY_SEPARATOR;
+        return extension_path($extensionID);
     }
 
     /**

+ 3 - 0
src/Job/BaseJob.php

@@ -61,6 +61,9 @@ abstract class BaseJob
             if (method_exists($this, 'execute')) {
                 // 执行任务
                 $result = $this->execute($data);
+            } else {
+                // 默认执行逻辑
+                $result = $data;
             }
 
             // 后置处理

+ 2 - 0
src/functions.php

@@ -3,6 +3,7 @@ declare(strict_types=1);
 
 namespace SixShop\Core;
 
+use Random\RandomException;
 use SixShop\Core\Exception\LogicException;
 use SixShop\Core\Response\Xml;
 use SixShop\Core\Service\CoreService;
@@ -131,6 +132,7 @@ function build_tree_options(
  * 生成随机密码
  * @param int $length 密码长度
  * @return string 生成的密码
+ * @throws RandomException
  */
 function secret_password(int $length = 16): string
 {