优化 路由
This commit is contained in:
parent
07fb29373f
commit
a5f4facead
@ -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']);
|
||||||
|
@ -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();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user