mirror of
https://github.com/crmeb/CRMEB.git
synced 2026-10-28 21:40:50 +00:00
【程序目录】feat(utils): 添加响应助手函数并优化JSON处理
添加success和fail助手函数到common.php 优化Json类的类型提示和错误处理 重构set_http_type函数以更好处理URL协议转换
This commit is contained in:
parent
5ea6576c27
commit
a7dc05f7cf
@ -10,7 +10,7 @@ class Test
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user