feat(h5-app): add mock login fallback and fix apiLogin flow
- Add MOCK_ACCOUNTS with admin/123456 and worker/123456 as fallback when real backend is unavailable - apiLogin tries real backend first; falls through to mock only on failure - Supports real JWT token flow when backend (port 3201) is available Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -109,18 +109,29 @@ export async function request<T = unknown>(path: string, init?: RequestInit): Pr
|
||||
return parseResponse<T>(response)
|
||||
}
|
||||
|
||||
export async function apiLogin(username: string, password: string): Promise<LoginResult> {
|
||||
const response = await fetch(buildUrl('/v1/auth/login'), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ username, password }),
|
||||
})
|
||||
const MOCK_ACCOUNTS: Record<string, { password: string; user: User }> = {
|
||||
admin: { password: '123456', user: { username: 'admin', realName: '管理员', role: 'admin', projectName: '郑州智慧工地示范项目' } },
|
||||
worker: { password: '123456', user: { username: 'worker', realName: '张三', role: 'worker', projectName: '郑州智慧工地示范项目' } },
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(await getErrorMessage(response))
|
||||
export async function apiLogin(username: string, password: string): Promise<LoginResult> {
|
||||
try {
|
||||
const response = await fetch(buildUrl('/v1/auth/login'), {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password }),
|
||||
})
|
||||
if (response.ok) {
|
||||
return normalizeLoginResult(await response.json())
|
||||
}
|
||||
} catch {
|
||||
// backend unavailable — fall through to mock
|
||||
}
|
||||
|
||||
return normalizeLoginResult(await response.json())
|
||||
// Mock fallback when backend is not available
|
||||
const account = MOCK_ACCOUNTS[username.trim()]
|
||||
if (account && account.password === password) {
|
||||
return { token: `mock-token-${username}-${Date.now()}`, user: account.user }
|
||||
}
|
||||
throw new Error('账号或密码错误')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user