Skip to content

Commit

Permalink
支持jsonp输出
Browse files Browse the repository at this point in the history
移除 successDate 输出方法
  • Loading branch information
webguosai committed Sep 27, 2022
1 parent 66c13ae commit c459b42
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
67 changes: 55 additions & 12 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,60 @@

class Response
{
public static function success($message = '', $data = [])
{
static::response(0, $message, $data);
/**
* 输出类型
*/
protected static $outputType = 'json';

/**
* 设置输出类型
*
* @param $type
*/
public static function setType($type) {
self::$outputType = $type;
}

public static function successData($var = [], $data = false, $message = '')
/**
* 成功
*
* @param string $message
* @param array $data
* @param array $var
*/
public static function success($message = '', $data = [], $var = [])
{
static::response(0, $message, $data, $var);
}

/**
* 失败
*
* @param string $message
* @param int $code
* @param int $httpCode
*/
public static function error($message = '', $code = 1, $httpCode = 200)
{
//http状态根据需要来指定 401(未授权) 406(不接受)
static::response($code, $message, [], [], $httpCode);
}

// 用于cli下输出
public static function cliResponse($message = '', $status = 'info') {
/**
* cli下输出
*
* @param string $message
* @param string $status
*/
public static function cliResponse($message = '', $status = 'info')
{
/**
* 颜色值对照
* https://blog.yzmcms.com/php/219.html
* @see:https://blog.yzmcms.com/php/219.html
*/
if ($status == 'info') {
$fontColor = 34; //37=白色
} elseif ($status == 'error') {
// 默认白色
$fontColor = 34;
if ($status == 'error') {
$fontColor = 31;
} elseif ($status == 'success') {
$fontColor = 32;
Expand All @@ -41,6 +70,15 @@ public static function cliResponse($message = '', $status = 'info') {
echo "\e[;{$fontColor}m[". date('Y-m-d H:i:s')."] {$message} \e[0m\e[0m\n";
}

/**
* 响应
*
* @param $code
* @param string $message
* @param array $data
* @param array $var
* @param int $httpCode
*/
protected static function response($code, $message = '', $data = [], $var = [], $httpCode = 200)
{
$json = [
Expand All @@ -50,14 +88,19 @@ protected static function response($code, $message = '', $data = [], $var = [],

$json['data'] = $data;

if ($var !== []) {
if (!empty($var)) {
$json = $json + $var;
}

Header::setHttpCode($httpCode);
Header::setJson();

echo json_encode($json, JSON_UNESCAPED_UNICODE);
if (self::$outputType === 'jsonp') {
$callback = !empty($_REQUEST['callback']) ? htmlspecialchars($_REQUEST['callback']) : 'jsonp_' . time() . mt_rand(100, 999);
echo $callback . '(' . json_encode($json, JSON_UNESCAPED_UNICODE) . ')';
} else {
echo json_encode($json, JSON_UNESCAPED_UNICODE);
}
exit;
}
}
3 changes: 2 additions & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,9 @@
//dump($ip);

/** 响应类测试 **/
//Response::setType('jsonp');
//Response::error('6666', 88, 200);

//Response::success('6666', 88, ['a' => true]);

/** 获取随机内容 **/
//var_dump(\Webguosai\Helper\Str::random(10));
Expand Down

0 comments on commit c459b42

Please sign in to comment.