init
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* Web API 响应处理类
|
||||
* 提供静态方法用于JSON格式输出和终止脚本
|
||||
*/
|
||||
|
||||
namespace Stemcell\Commerce\Model;
|
||||
|
||||
class WebapiResponse
|
||||
{
|
||||
/**
|
||||
* 输出JSON格式的数据并终止脚本
|
||||
*
|
||||
* @param array $data 要输出的数据
|
||||
* @return mixed
|
||||
*/
|
||||
public static function json(array $data)
|
||||
{
|
||||
// 设置HTTP响应头为JSON格式
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
$result = [];
|
||||
if (isset($data['error'])) {
|
||||
$result['status'] = false;
|
||||
$result['message'] = $data['error'];
|
||||
} else {
|
||||
$result['status'] = true;
|
||||
$result['data'] = $data;
|
||||
}
|
||||
// 输出JSON格式化的数据
|
||||
echo json_encode($result);
|
||||
|
||||
// 终止脚本执行
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user