調整心跳 要回送後台 20250603

This commit is contained in:
allen.yan 2025-06-03 17:29:50 +08:00
parent 129d5f56ce
commit 290029388a
5 changed files with 41 additions and 10 deletions

View File

@ -8,6 +8,8 @@ use App\Http\Requests\ReceiveRoomStatusDataRequest;
use App\Services\TcpSocketClient;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use App\Services\ApiClient;
use App\Models\User;
use App\Models\Branch;
use App\Models\Room;
use App\Models\MachineStatus;
@ -198,6 +200,27 @@ class RoomControlController extends Controller
$room->touch(); // 更新 updated_at
$room->save();
}
$externalUrl = $branch->external_ip;
$parsed = parse_url($externalUrl);
$hostParts = explode('.', $parsed['host']);
if (count($hostParts) >= 3) {
$mainDomain = implode('.', array_slice($hostParts, 1));
} else {
$mainDomain = $parsed['host'];
}
$mainDomainUrl = $parsed['scheme'] . '://' . $mainDomain;
try {
$user = \App\Models\User::find(2); // 你可改為 config 或動態決定
if ($user && $user->api_plain_token) {
$client = new ApiClient($mainDomainUrl,$user->api_plain_token);
$client->post('/api/room/heartbeat', $validated);
}
} catch (\Throwable $e) {
logger()->error('❌ Failed to forward machine status: ' . $e->getMessage());
}
return ApiResponse::success([
'data' => MachineStatus::create($validated),

View File

@ -7,7 +7,7 @@ use Illuminate\Foundation\Http\FormRequest;
/**
* @OA\Schema(
* schema="ReceiveRoomStatusDataRequest",
* required={"branch_name","hostname", "ip", "status"},
* required={"branch_name","hostname", "ip"},
* @OA\Property(property="branch_name", type="string", example="測試"),
* @OA\Property(property="hostname", type="string", example="PC101"),
* @OA\Property(property="ip", type="string", example="192.168.XX.XX"),

View File

@ -85,9 +85,8 @@ class RoomDetailModal extends Component
$this->sendErrorNotification('api', 'API token is missing.');
return false;
}
$apiClient = new ApiClient();
$response = $apiClient->setToken($token)->post('/room/sendSwitch', $data);
$apiClient = new ApiClient($this->branch->external_ip,$token);
$response = $apiClient->post('/api/room/sendSwitch', $data);
if ($response->failed()) {
$this->sendErrorNotification('api', 'API request failed: ' . $response->body());
return false;

View File

@ -8,10 +8,21 @@ class ApiClient
protected string $baseUrl;
protected string $token;
public function __construct(string $token = null)
public function __construct(string $baseUrl = null,string $token = null)
{
$this->baseUrl = config('services.room_api.base_url', 'https://ktv.test/api');
$this->token = $token ?? config('services.room_api.token','');
$this->baseUrl = $baseUrl;
$this->token = $token;
}
public function set(string $baseUrl = null,string $token = null):self
{
$this->baseUrl = $baseUrl;
$this->token = $token;
return $this;
}
public function setBaseUrl():self
{
$this->baseUrl = $baseUrl;
return $this;
}
public function setToken(string $token): self
{

View File

@ -13,9 +13,7 @@ return [
| a conventional file to locate the various service credentials.
|
*/
'room_api' => [
'base_url' => env('ROOM_API_BASE_URL', env('APP_URL') . '/api'),
],
'postmark' => [
'token' => env('POSTMARK_TOKEN'),
],