优化 路由
This commit is contained in:
parent
07fb29373f
commit
a5f4facead
@ -17,24 +17,26 @@ public function index()
|
||||
|
||||
public function call(Request $request, Module $module)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'func' => 'required|string'
|
||||
]);
|
||||
// $this->validate($request, [
|
||||
// 'func' => 'required|string'
|
||||
// ]);
|
||||
|
||||
$func = $request->func;
|
||||
// $func = $request->func;
|
||||
|
||||
// 不能让 func 的首个字符为 /
|
||||
if (Str::startsWith($func, '/')) {
|
||||
$func = substr($func, 1);
|
||||
}
|
||||
// // 不能让 func 的首个字符为 /
|
||||
// if (Str::startsWith($func, '/')) {
|
||||
// $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 为 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) {
|
||||
return response($response['body'], $response['status']);
|
||||
|
@ -53,12 +53,10 @@ public function remote($func, $requests)
|
||||
|
||||
|
||||
// post, get, patch, delete 等请求
|
||||
public function remoteRequest($method, $func, $requests)
|
||||
public function remoteRequest($method, $path, $requests)
|
||||
{
|
||||
$http = Http::remote($this->api_token, $this->url)
|
||||
->accept('application/json')
|
||||
->withHeaders(['X-Func' => $func]);
|
||||
|
||||
->accept('application/json');
|
||||
|
||||
unset($requests['func']);
|
||||
|
||||
@ -73,7 +71,7 @@ public function remoteRequest($method, $func, $requests)
|
||||
|
||||
$requests['user_id'] = $user['id'];
|
||||
|
||||
$response = $http->{$method}("functions/{$func}", $requests);
|
||||
$response = $http->{$method}("functions/{$path}", $requests);
|
||||
|
||||
$json = $response->json();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user