json([ 'success' => true, 'data' => $data, 'total' => $total, 'msg' => 'success', 'message' => 'success', 'code' => 200, ]); } /** * 返回成功数据 - 对象格式 * @param object $data * @param int $total * @return JsonResponse */ public function SuccessObject($data = new \stdClass(), int $total = 0): JsonResponse { return response()->json([ 'success' => true, 'data' => $data, 'total' => $total, 'msg' => 'success', 'message' => 'success', 'code' => 200, ]); } /** * 返回分页数据 * @param array $data * @param int $total * @return JsonResponse */ public function SuccessPage($data = [], int $total = 0): JsonResponse { return response()->json([ 'success' => true, 'data' => [ 'total' => $total, 'list' => $data ], 'message' => 'success', 'code' => 200, ]); } /** * 返回错误信息 * @param string $msg * @param int $code * @param int $status * @return JsonResponse */ public function Field(string $msg = 'fail', int $code = 0, int $status = 200): JsonResponse { return response()->json([ 'success' => false, 'data' => null, 'msg' => $msg, 'message' => $msg, 'code' => $code, ], $status); } /** * 流式返回数据 * @param string $msg * @param int $code * @return StreamedResponse */ public function FieldStream(string $msg = 'success', int $code = 0): StreamedResponse { return response()->stream(function () use ($msg, $code) { $responseStream = array( 'conversation_id' => 0, 'content' => '', 'reasoning_content' => '', 'messages' =>$msg, 'code' => $code, 'finish_reason' => 'stop', 'use_token' => 0 ); echo 'data: ' . json_encode($responseStream,JSON_UNESCAPED_UNICODE) ."\n\n"; ob_flush(); flush(); }, 200,[ 'Content-Type' => 'text/event-stream', 'X-Accel-Buffering' => 'no', ]); } /** * 安全获取字符串参数 * @param string $key 参数键 * @param string $default 默认值 * @return string */ protected function getStringParam(string $key, string $default = ''): string { return (string) (request()->input($key) ?? $default); } /** * 安全获取整数参数 * @param string $key 参数键 * @param int $default 默认值 * @return int */ protected function getIntParam(string $key, int $default = 0): int { return (int) (request()->input($key) ?? $default); } /** * 安全获取数组参数 * @param string $key 参数键 * @param array $default 默认值 * @return array */ protected function getArrayParam(string $key, array $default = []): array { $value = request()->input($key, $default); if (is_string($value)){ try { $value = json_decode($value, true); } catch (\Exception $e) { $value = explode(',', $value); if (!is_array($value) || empty($value)){ $value = $default; } } } return is_array($value) ? $value : $default; } }