feat: 联调对话功能
This commit is contained in:
+50
-4
@@ -58,15 +58,36 @@ function readErrorMessage(data: unknown, fallback: string) {
|
||||
return fallback
|
||||
}
|
||||
|
||||
function request<T>(url: string, data: unknown, method: 'POST' | 'GET' = 'POST'): Promise<T> {
|
||||
function readAccessToken() {
|
||||
try {
|
||||
return uni.getStorageSync('clinical-thinking-access-token') || ''
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
function createRequestHeaders(includeAuth = false) {
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (!includeAuth) return headers
|
||||
|
||||
const token = readAccessToken()
|
||||
if (token) {
|
||||
headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
|
||||
return headers
|
||||
}
|
||||
|
||||
function request<T>(url: string, data: unknown, method: 'POST' | 'GET' = 'POST', includeAuth = false): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: `${API_BASE_URL}${url}`,
|
||||
method,
|
||||
timeout: 10000,
|
||||
header: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
header: createRequestHeaders(includeAuth),
|
||||
data,
|
||||
success: response => {
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
@@ -114,10 +135,35 @@ export type InstitutionRecord = {
|
||||
is_trial: boolean
|
||||
}
|
||||
|
||||
export type InstitutionInfo = {
|
||||
id: number
|
||||
code: string
|
||||
name: string
|
||||
type: string
|
||||
level: string
|
||||
province: string
|
||||
city: string
|
||||
banner_url: string
|
||||
}
|
||||
|
||||
export type DepartmentRecord = {
|
||||
id: number
|
||||
name: string
|
||||
category: string
|
||||
}
|
||||
|
||||
export function fetchInstitutions(): Promise<InstitutionRecord[]> {
|
||||
return request<InstitutionRecord[]>('/user/institution_list/', null, 'GET')
|
||||
}
|
||||
|
||||
export function fetchInstitutionInfo(): Promise<InstitutionInfo> {
|
||||
return request<InstitutionInfo>('/user/institution_info/', null, 'GET', true)
|
||||
}
|
||||
|
||||
export function fetchMyDepartments(): Promise<DepartmentRecord[]> {
|
||||
return request<DepartmentRecord[]>('/user/my_departments/', null, 'GET', true)
|
||||
}
|
||||
|
||||
export function loginWithCode(payload: LoginCodePayload): Promise<LoginResponse> {
|
||||
return request<LoginResponse>('/user/auth/login-code/', payload).then(response => {
|
||||
if (isLoginResponse(response)) return response
|
||||
|
||||
Reference in New Issue
Block a user