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;