From a7dc05f7cf3f45f2e20b2c786970208cfe16c17d Mon Sep 17 00:00:00 2001 From: sugar1569 Date: Tue, 27 Jan 2026 12:14:43 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=A8=8B=E5=BA=8F=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E3=80=91feat(utils):=20=E6=B7=BB=E5=8A=A0=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E5=8A=A9=E6=89=8B=E5=87=BD=E6=95=B0=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?JSON=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加success和fail助手函数到common.php 优化Json类的类型提示和错误处理 重构set_http_type函数以更好处理URL协议转换 --- crmeb/app/adminapi/controller/Test.php | 2 +- crmeb/app/common.php | 54 +++++++++++++++++++--- crmeb/crmeb/utils/Json.php | 64 +++++++++++++++++++++++--- 3 files changed, 105 insertions(+), 15 deletions(-) diff --git a/crmeb/app/adminapi/controller/Test.php b/crmeb/app/adminapi/controller/Test.php index 1aa25d887..9117cbdcc 100644 --- a/crmeb/app/adminapi/controller/Test.php +++ b/crmeb/app/adminapi/controller/Test.php @@ -10,7 +10,7 @@ class Test { public function index() { - + } } diff --git a/crmeb/app/common.php b/crmeb/app/common.php index f9dd25fce..d6c387aa9 100644 --- a/crmeb/app/common.php +++ b/crmeb/app/common.php @@ -38,7 +38,34 @@ if (!function_exists('crmebLog')) { Log::write($msg, 'crmeb'); } } - +if (!function_exists('success')) { + /** + * 响应助手函数 + * @param mixed $msg 响应消息 + * @param array|null $data 响应数据 + * @param array|null $replace 消息替换数组 + * @return \think\Response + * @see \crmeb\utils\Json::success() + */ + function success($msg = 'success', ?array $data = null, ?array $replace = []) + { + return app('json')->success($msg, $data, $replace); + } +} +if (!function_exists('fail')) { + /** + * 失败响应助手函数 + * @param mixed $msg 响应消息 + * @param array|null $data 响应数据 + * @param array|null $replace 消息替换数组 + * @return \think\Response + * @see \crmeb\utils\Json::fail() + */ + function fail($msg = 'fail', ?array $data = null, ?array $replace = []) + { + return app('json')->fail($msg, $data, $replace); + } +} if (!function_exists('getWorkerManUrl')) { /** @@ -287,14 +314,27 @@ if (!function_exists('set_http_type')) { * @param int $type 0 返回https 1 返回 http * @return string */ - function set_http_type($url, $type = 0) + function set_http_type(string $url, int $type = 0): string { - $domainTop = substr($url, 0, 5); - if ($type) { - if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url)); - } else { - if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url)); + // 基本验证 + if (empty($url)) { + return $url; } + + // 检查是否是完整 URL + $is_full_url = (strpos($url, '://') !== false); + + if ($is_full_url) { + // 处理完整 URL + if ($type) { + // 转换为 HTTP + $url = preg_replace('/^https:/i', 'http:', $url); + } else { + // 转换为 HTTPS + $url = preg_replace('/^http:/i', 'https:', $url); + } + } + return $url; } diff --git a/crmeb/crmeb/utils/Json.php b/crmeb/crmeb/utils/Json.php index 9147cbe60..73c8e87d5 100644 --- a/crmeb/crmeb/utils/Json.php +++ b/crmeb/crmeb/utils/Json.php @@ -20,21 +20,44 @@ use think\Response; */ class Json { + /** + * @var int + */ private $code = 200; + /** + * @var array + */ private $header = []; + /** + * 设置响应状态码 + * @param int $code + * @return self + */ public function code(int $code): self { $this->code = $code; return $this; } + /** + * 设置响应头 + * @param array $header + * @return self + */ public function header(array $header): self { $this->header = $header; return $this; } - + /** + * 生成响应 + * @param int $status + * @param string $msg + * @param array|null $data + * @param array|null $replace + * @return Response + */ public function make(int $status, string $msg, ?array $data = null, ?array $replace = []): Response { $res = compact('status', 'msg'); @@ -44,15 +67,26 @@ class Json if (is_numeric($res['msg'])) { $res['code'] = $res['msg']; - $res['msg'] = getLang($res['msg'], $replace); + try { + // 尝试根据状态码获取语言包消息 + $res['msg'] = function_exists('getLang') ? getLang($res['msg'], $replace) : $res['msg']; + } catch (\Exception $e) { + // 保持原消息,避免语言包错误影响响应 + } } - - return Response::create($res, 'json', $this->code)->header($this->header); } - + /** + * 成功响应 + * @param string|array $msg 响应消息 + * @param array|null $data 响应数据 + * @param array|null $replace 消息替换数组 + * @return \think\Response + * @see \crmeb\utils\Json::make() + */ public function success($msg = 'success', ?array $data = null, ?array $replace = []): Response { + // 如果消息是数组,将其作为数据处理 if (is_array($msg)) { $data = $msg; $msg = 'success'; @@ -61,18 +95,34 @@ class Json return $this->make(200, $msg, $data, $replace); } + /** + * 失败响应 + * @param string|array $msg 响应消息 + * @param array|null $data 响应数据 + * @param array|null $replace 消息替换数组 + * @return \think\Response + * @see \crmeb\utils\Json::make() + */ public function fail($msg = 'fail', ?array $data = null, ?array $replace = []): Response { + // 如果消息是数组,将其作为数据处理 if (is_array($msg)) { $data = $msg; $msg = 'fail'; } - return $this->make(400, $msg, $data, $replace); } - + /** + * 自定义状态响应 + * @param string $status 状态码 + * @param string|array $msg 响应消息 + * @param array|null $result 响应数据 + * @return \think\Response + * @see \crmeb\utils\Json::success() + */ public function status($status, $msg, $result = []) { + // 转换状态码为大写 $status = strtoupper($status); if (is_array($msg)) { $result = $msg;