优化 路由

This commit is contained in:
iVampireSP.com 2022-09-09 01:12:18 +08:00
parent 07fb29373f
commit a5f4facead
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 18 additions and 18 deletions

View File

@ -17,24 +17,26 @@ public function index()
public function call(Request $request, Module $module) public function call(Request $request, Module $module)
{ {
$this->validate($request, [ // $this->validate($request, [
'func' => 'required|string' // 'func' => 'required|string'
]); // ]);
$func = $request->func; // $func = $request->func;
// 不能让 func 的首个字符为 / // // 不能让 func 的首个字符为 /
if (Str::startsWith($func, '/')) { // if (Str::startsWith($func, '/')) {
$func = substr($func, 1); // $func = substr($func, 1);
} // }
$path = request()->path();
// 删除 modules/{module} 的部分
$path = substr($path, strlen('/api/modules/' . $module->id));
// 过滤除了 "/" 以外的特殊字符 // 过滤除了 "/" 以外的特殊字符
$func = preg_replace('/[^a-zA-Z0-9\/]/', '', $func); $path = preg_replace('/[^a-zA-Z0-9\/]/', '', $path);
// dd($func);
$method = Str::lower($request->method()); $method = Str::lower($request->method());
// 如果 method 为 post, 检查用户余额 // 如果 method 为 post, 检查用户余额
@ -47,7 +49,7 @@ public function call(Request $request, Module $module)
} }
$response = $module->remoteRequest($method, $func, $request->all()); $response = $module->remoteRequest($method, $path, $request->all());
if ($response['json'] === null && $response['body'] !== null) { if ($response['json'] === null && $response['body'] !== null) {
return response($response['body'], $response['status']); return response($response['body'], $response['status']);

View File

@ -53,12 +53,10 @@ public function remote($func, $requests)
// post, get, patch, delete 等请求 // post, get, patch, delete 等请求
public function remoteRequest($method, $func, $requests) public function remoteRequest($method, $path, $requests)
{ {
$http = Http::remote($this->api_token, $this->url) $http = Http::remote($this->api_token, $this->url)
->accept('application/json') ->accept('application/json');
->withHeaders(['X-Func' => $func]);
unset($requests['func']); unset($requests['func']);
@ -73,7 +71,7 @@ public function remoteRequest($method, $func, $requests)
$requests['user_id'] = $user['id']; $requests['user_id'] = $user['id'];
$response = $http->{$method}("functions/{$func}", $requests); $response = $http->{$method}("functions/{$path}", $requests);
$json = $response->json(); $json = $response->json();