feat: 實作 POS API 整合功能,包含商品與銷售訂單同步及韌性機制
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 57s

This commit is contained in:
2026-02-06 11:56:29 +08:00
parent 906b094c18
commit 3fd333085b
30 changed files with 1120 additions and 22 deletions

View File

@@ -84,6 +84,24 @@ class TenantController extends Controller
{
$tenant = Tenant::with('domains')->findOrFail($id);
$tokens = [];
try {
tenancy()->initialize($tenant);
$user = \App\Modules\Core\Models\User::first();
if ($user) {
$tokens = $user->tokens()->orderBy('created_at', 'desc')->get(['id', 'name', 'last_used_at', 'created_at'])->map(function($token) {
return [
'id' => $token->id,
'name' => $token->name,
'last_used_at' => $token->last_used_at ? $token->last_used_at->format('Y-m-d H:i') : '未使用',
'created_at' => $token->created_at->format('Y-m-d H:i'),
];
});
}
} catch (\Exception $e) {
\Log::warning("Failed to fetch tokens for tenant {$id}: " . $e->getMessage());
}
return Inertia::render('Landlord/Tenant/Show', [
'tenant' => [
'id' => $tenant->id,
@@ -98,6 +116,7 @@ class TenantController extends Controller
'domain' => $d->domain,
])->toArray(),
],
'tokens' => $tokens,
]);
}
@@ -242,4 +261,58 @@ class TenantController extends Controller
return redirect()->back()->with('success', '樣式設定已更新');
}
/**
* 建立 API Token (用於 POS)
*/
public function createToken(Request $request, Tenant $tenant)
{
$request->validate([
'name' => 'required|string|max:50',
]);
try {
// 切換至租戶環境
tenancy()->initialize($tenant);
// 尋找超級管理員 (假設 ID 1, 或者根據 Role)
// 這裡簡單取第一個使用者,通常是 Admin
$user = \App\Modules\Core\Models\User::first();
if (!$user) {
return back()->with('error', '該租戶尚無使用者,無法建立 Token。');
}
// 建立 Token
$token = $user->createToken($request->name);
return back()->with('success', 'Token 建立成功')->with('new_token', $token->plainTextToken);
} catch (\Exception $e) {
\Log::error("Token creation failed: " . $e->getMessage());
return back()->with('error', 'Token 建立失敗');
} finally {
// tenancy()->end(); // Laravel Tenancy 自動處理 scope 結束? 通常 Controller request life-cycle?
// Landlord controller is Central. Tenancy initialization persists for request.
// We should explicit end if we want to be safe, but redirect ends request anyway.
}
}
/**
* 撤銷 API Token
*/
public function revokeToken(Request $request, Tenant $tenant, string $tokenId)
{
try {
tenancy()->initialize($tenant);
$user = \App\Modules\Core\Models\User::first();
if ($user) {
$user->tokens()->where('id', $tokenId)->delete();
}
return back()->with('success', 'Token 已撤銷');
} catch (\Exception $e) {
return back()->with('error', 'Token 撤銷失敗');
}
}
}