namespace app\common\response;
use app\common\ApiErrCode;
use think\facade\Log;
/**
* Trait JsonResponse
* @package app\http\response
*/
trait JsonResponse
{
/**
* 成功时返回的数据
* @param $message
* @param $data
*/
public function jsonSuccessData($data = ''){
return $this->jsonResponse(ApiErrCode::success['code'],ApiErrCode::success['msg'],$data);
}
/**
* 错误时返回的数据
* @param $code
* @param $message
* @param $data
*/
public function jsonData($code,$message,$data = ''){
Log::error([
'code'=>$code,
'message'=>$message,
'data'=>$data
]);
return $this->jsonResponse($code,$message,$data);
}
/**
* 接口返回数据结构
* @param $code
* @param $message
* @param $data
*/
private function jsonResponse($code,$message,$data){
$content = [
'code'=>$code,
'msg'=>$message,
'data'=>$data,
'timestamp'=>time()
];
return json($content);
}
}
https://www.likecs.com/show-308249141.html