refactor(frontend): merge authserver web login
This commit is contained in:
+25
-40
@@ -1,4 +1,5 @@
|
||||
import type { ApiResponse } from '@/types/api'
|
||||
import { getToken } from '@/utils/auth'
|
||||
|
||||
export interface LoginRequest {
|
||||
username: string
|
||||
@@ -19,53 +20,37 @@ export interface LoginResponse {
|
||||
user: UserInfo
|
||||
}
|
||||
|
||||
// Mock 数据
|
||||
const mockUser: UserInfo = {
|
||||
id: 1,
|
||||
username: 'admin',
|
||||
display_name: '王晓东',
|
||||
email: 'admin@qiniu.com',
|
||||
business_line: 'kodo',
|
||||
}
|
||||
|
||||
export const authApi = {
|
||||
// 登录
|
||||
login(_data: LoginRequest): Promise<ApiResponse<LoginResponse>> {
|
||||
// MVP 阶段使用 Mock 数据
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve({
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: {
|
||||
token: 'mock-jwt-token-' + Date.now(),
|
||||
expires_in: 3600,
|
||||
user: mockUser,
|
||||
},
|
||||
})
|
||||
}, 500)
|
||||
})
|
||||
return Promise.reject(new Error('password login is disabled'))
|
||||
},
|
||||
|
||||
// 登出
|
||||
logout(): Promise<ApiResponse<null>> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve({ code: 0, message: 'success', data: null })
|
||||
}, 200)
|
||||
})
|
||||
return Promise.resolve({ code: 0, message: 'success', data: null })
|
||||
},
|
||||
|
||||
// 获取用户信息
|
||||
getUserInfo(): Promise<ApiResponse<UserInfo>> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve({
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: mockUser,
|
||||
})
|
||||
}, 200)
|
||||
async getUserInfo(): Promise<ApiResponse<UserInfo>> {
|
||||
const token = getToken()
|
||||
const response = await fetch('/auth/api/v1/users/me', {
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
},
|
||||
})
|
||||
const data = await response.json().catch(() => ({}))
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || `HTTP ${response.status}`)
|
||||
}
|
||||
return {
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: {
|
||||
id: data.id,
|
||||
username: data.username,
|
||||
display_name: data.display_name || data.username,
|
||||
email: data.email || '',
|
||||
business_line: data.business_line || '',
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import axios, { type AxiosResponse, type AxiosError } from 'axios'
|
||||
import { getToken, removeToken } from '@/utils/auth'
|
||||
import { redirectToSSO } from '@/utils/sso'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import type { ApiResponse } from '@/types/api'
|
||||
import { codeMessageMap } from '@/types/api'
|
||||
@@ -60,7 +61,7 @@ request.interceptors.response.use(
|
||||
|
||||
if (status === 401) {
|
||||
removeToken()
|
||||
window.location.href = '/login'
|
||||
redirectToSSO()
|
||||
ElMessage.error(backendMessage || '未授权,请重新登录')
|
||||
} else {
|
||||
const message = backendMessage || httpMessageMap[status] || `请求失败 (${status})`
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import type { ApiResponse } from '@/types/api'
|
||||
import { getToken, removeToken } from '@/utils/auth'
|
||||
import { redirectToSSO } from '@/utils/sso'
|
||||
|
||||
export interface Subsystem {
|
||||
id: number
|
||||
@@ -15,7 +17,6 @@ export interface SSOResponse {
|
||||
expires_in: number
|
||||
}
|
||||
|
||||
// Mock 数据
|
||||
const mockSubsystems: Subsystem[] = [
|
||||
{
|
||||
id: 1,
|
||||
@@ -74,33 +75,59 @@ const mockSubsystems: Subsystem[] = [
|
||||
]
|
||||
|
||||
export const subsystemApi = {
|
||||
// 获取子系统列表
|
||||
getSubsystems(): Promise<ApiResponse<Subsystem[]>> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve({
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: mockSubsystems,
|
||||
})
|
||||
}, 300)
|
||||
return Promise.resolve({
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: mockSubsystems,
|
||||
})
|
||||
},
|
||||
|
||||
// 获取 SSO 跳转 URL
|
||||
getSSOUrl(id: number): Promise<ApiResponse<SSOResponse>> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
const subsystem = mockSubsystems.find((s) => s.id === id)
|
||||
resolve({
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: {
|
||||
sso_url: subsystem?.url + '/sso/callback?code=mock-code&state=mock-state',
|
||||
expires_in: 300,
|
||||
},
|
||||
})
|
||||
}, 200)
|
||||
async getSSOUrl(id: number): Promise<ApiResponse<SSOResponse>> {
|
||||
const subsystem = mockSubsystems.find((s) => s.id === id)
|
||||
if (!subsystem) {
|
||||
throw new Error('subsystem not found')
|
||||
}
|
||||
if (subsystem.name !== 'Wayne' && subsystem.name !== 'CloudDM') {
|
||||
return {
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: {
|
||||
sso_url: subsystem.url,
|
||||
expires_in: 300,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const token = getToken()
|
||||
const openApp = subsystem.name === 'CloudDM' ? 'clouddm' : 'wayne'
|
||||
const path = subsystem.name === 'CloudDM' ? '/auth/api/v1/clouddm/login' : '/auth/api/v1/wayen/login'
|
||||
const response = await fetch(path, {
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
},
|
||||
credentials: 'include',
|
||||
})
|
||||
const data = await response.json().catch(() => ({}))
|
||||
if (response.status === 401) {
|
||||
removeToken()
|
||||
redirectToSSO(openApp)
|
||||
throw new Error('unauthorized')
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || `HTTP ${response.status}`)
|
||||
}
|
||||
if (!data.target_url) {
|
||||
throw new Error(`${subsystem.name} 跳转地址为空`)
|
||||
}
|
||||
return {
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: {
|
||||
sso_url: data.target_url,
|
||||
expires_in: 300,
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user