pub trait SzController: Send + Sync {
// Provided methods
fn render_json(
&self,
code: i32,
msg: impl Into<String>,
data: Value,
) -> Value { ... }
fn render_success(&self, msg: impl Into<String>, data: Value) -> Response { ... }
fn render_error(
&self,
msg: impl Into<String>,
data: Value,
code: i32,
) -> Response { ... }
fn post_data(
&self,
req: Request<Body>,
) -> impl Future<Output = Result<Value, String>> + Send { ... }
fn post_data_by_key(
&self,
req: Request<Body>,
key: &str,
) -> impl Future<Output = Result<Option<Value>, String>> + Send { ... }
fn get_data(&self, req: &Request<Body>) -> Value { ... }
fn get_data_by_key(&self, req: &Request<Body>, key: &str) -> Option<Value> { ... }
}Expand description
控制器基础 trait(对齐 PHP app\SzController)
业务控制器实现此 trait 即可获得 render_json / render_success / render_error
与 post_data / get_data 等方法。
§用法
use sz_rust_core::controller::SzController;
use sz_rust_core::response::ApiResponse;
use axum::body::Body;
use axum::http::Request;
use axum::response::Response;
use serde_json::json;
struct UserController;
impl SzController for UserController {}
impl UserController {
pub async fn info(&self, req: Request<Body>) -> Response {
let data = self.post_data(req).await.unwrap();
let name = data.get("name").cloned().unwrap_or(json!(null));
self.render_success("success", json!({"name": name}))
}
}Provided Methods§
Sourcefn render_json(&self, code: i32, msg: impl Into<String>, data: Value) -> Value
fn render_json(&self, code: i32, msg: impl Into<String>, data: Value) -> Value
返回封装后的 API 数据(对应 PHP renderJson)
严格遵循 PHP compact('code', 'msg', 'data') 的字段顺序:code → msg → data。
返回 Value::Object(而非 Response),便于调用方进一步处理。
§PHP 对齐
protected function renderJson($code = 1, $msg = '', $data = []) {
return compact('code', 'msg', 'data');
}Sourcefn render_success(&self, msg: impl Into<String>, data: Value) -> Response
fn render_success(&self, msg: impl Into<String>, data: Value) -> Response
返回操作成功 json(对应 PHP renderSuccess)
参数顺序:msg, data(与 PHP 一致,与 ApiResponse::success(data, msg) 相反)。
返回 Response,HTTP 200,Content-Type: application/json; charset=utf-8。
§PHP 对齐
protected function renderSuccess($msg = 'success', $data = []) {
return json($this->renderJson(1, $msg, $data));
}Sourcefn render_error(
&self,
msg: impl Into<String>,
data: Value,
code: i32,
) -> Response
fn render_error( &self, msg: impl Into<String>, data: Value, code: i32, ) -> Response
返回操作失败 json(对应 PHP renderError)
参数顺序:msg, data, code(与 PHP 一致)。
返回 Response,HTTP 200(业务失败 HTTP 仍 200,对齐 PHP 行为)。
§PHP 对齐
protected function renderError($msg = 'error', $data = [], $code = 0) {
return json($this->renderJson($code, $msg, $data));
}Sourcefn post_data(
&self,
req: Request<Body>,
) -> impl Future<Output = Result<Value, String>> + Send
fn post_data( &self, req: Request<Body>, ) -> impl Future<Output = Result<Value, String>> + Send
获取合并后的请求参数(对应 PHP postData() 无参形式)
合并 body + query,body 字段优先级高于 query。
对齐 PHP $this->request->param()(合并 POST + GET + route)。
§PHP 对齐
protected function postData($key = null) {
return $this->request->param(is_null($key) ? '' : $key . '/a');
}Sourcefn post_data_by_key(
&self,
req: Request<Body>,
key: &str,
) -> impl Future<Output = Result<Option<Value>, String>> + Send
fn post_data_by_key( &self, req: Request<Body>, key: &str, ) -> impl Future<Output = Result<Option<Value>, String>> + Send
获取合并参数中指定 key(对应 PHP postData($key))
§返回
Ok(Some(value)):字段存在Ok(None):字段不存在Err(String):body 读取或 JSON 解析失败
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".