增加 模块间调用,改进 Host 显示
This commit is contained in:
parent
619d233ae2
commit
aab01d912b
@ -72,6 +72,25 @@ public function call(Request $request, Module $module)
|
||||
}
|
||||
|
||||
|
||||
public function exportCall(Request $request, Module $module)
|
||||
{
|
||||
$path = request()->path();
|
||||
|
||||
$path = substr($path, strlen('/remote/modules/' . $module->id));
|
||||
$path = preg_replace('/[^a-zA-Z0-9\/]/', '', $path);
|
||||
|
||||
$method = Str::lower($request->method());
|
||||
|
||||
$response = $module->moduleRequest($method, $path, $request->all());
|
||||
|
||||
if ($response['json'] === null && $response['body'] !== null) {
|
||||
return response($response['body'], $response['status']);
|
||||
}
|
||||
|
||||
return $this->remoteResponse($response['json'], $response['status']);
|
||||
}
|
||||
|
||||
|
||||
public function calcModule(Module $module)
|
||||
{
|
||||
// begin of this month
|
||||
|
@ -2,16 +2,31 @@
|
||||
|
||||
namespace App\Http\Controllers\Remote;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Host;
|
||||
use App\Models\User;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
//
|
||||
|
||||
|
||||
public function show(User $user) {
|
||||
public function show(User $user)
|
||||
{
|
||||
$transaction = new Transaction();
|
||||
|
||||
$user['drops'] = $transaction->getDrops($user['id']);
|
||||
$user['drops_rate'] = config('drops.rate');
|
||||
|
||||
return $this->success($user);
|
||||
}
|
||||
|
||||
public function hosts(Request $request, User $user)
|
||||
{
|
||||
$hosts = (new Host())->getUserHosts($request->module_id ?? null);
|
||||
|
||||
return $this->success($hosts);
|
||||
}
|
||||
}
|
||||
|
@ -10,10 +10,10 @@
|
||||
|
||||
class HostController extends Controller
|
||||
{
|
||||
public function index(Module $module)
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
$hosts = (new Host())->getUserHosts($module->id ?? null);
|
||||
$hosts = (new Host())->getUserHosts(auth()->id());
|
||||
|
||||
return $this->success($hosts);
|
||||
}
|
||||
|
@ -35,10 +35,10 @@ class Host extends Model
|
||||
|
||||
|
||||
// get user hosts
|
||||
public function getUserHosts($module_id) {
|
||||
|
||||
return Cache::remember('user_hosts_' . auth()->id(), 3600, function () use ($module_id) {
|
||||
return $this->thisUser($module_id)->with('module', function ($query) {
|
||||
public function getUserHosts($user_id = null)
|
||||
{
|
||||
return Cache::remember('user_hosts_' . $user_id ?? auth()->id(), 3600, function () use ($user_id) {
|
||||
return $this->where('user_id', $user_id)->with('module', function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
})->get();
|
||||
});
|
||||
|
@ -89,6 +89,27 @@ public function remoteRequest($method, $path, $requests)
|
||||
];
|
||||
}
|
||||
|
||||
public function moduleRequest($method, $path, $requests)
|
||||
{
|
||||
$http = Http::remote($this->api_token, $this->url)
|
||||
->accept('application/json');
|
||||
|
||||
unset($requests['func']);
|
||||
|
||||
$requests['module_id'] = auth('module')->id();
|
||||
|
||||
$response = $http->{$method}("exports/{$path}", $requests);
|
||||
|
||||
$json = $response->json();
|
||||
|
||||
$status = $response->status();
|
||||
return [
|
||||
'body' => $response->body(),
|
||||
'json' => $json,
|
||||
'status' => $status
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function remotePost($path = '', $data = [])
|
||||
|
@ -73,3 +73,21 @@
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// 模块间调用
|
||||
|
||||
$router->group(['prefix' => 'modules/{module}'], function () use ($router) {
|
||||
$controller = 'Remote\ModuleController@exportCall';
|
||||
$router->get('/{route:.*}/', $controller);
|
||||
$router->post('/{route:.*}/', $controller);
|
||||
$router->put('/{route:.*}/', $controller);
|
||||
$router->patch('/{route:.*}/', $controller);
|
||||
$router->delete('/{route:.*}/', $controller);
|
||||
});
|
||||
|
||||
|
||||
// 用户信息
|
||||
$router->get('users/{user}', [
|
||||
'uses' => '\App\Http\Controllers\Remote\UserController@show'
|
||||
]);
|
||||
|
Loading…
Reference in New Issue
Block a user