Files
pda-uniapp/api/auth.js
T
2026-09-02 10:26:43 +08:00

46 lines
1.3 KiB
JavaScript

import { API_ROOT } from '@/config/runtime.js'
function authRequest(path, data, options = {}) {
return new Promise((resolve, reject) => {
uni.request({
url: `${API_ROOT}${path}`,
method: 'POST',
data,
header: options.header,
timeout: options.timeout || 15000,
success(response) {
const body = response.data || {}
if (String(body.code ?? response.statusCode) !== '200') {
const error = new Error(body.message || '认证服务请求失败')
error.code = body.code || response.statusCode
error.requestId = body.requestId || response.header?.['X-Request-Id'] || response.header?.['x-request-id']
error.traceId = body.traceId || response.header?.['X-Trace-Id'] || response.header?.['x-trace-id']
reject(error)
return
}
resolve(body.data)
},
fail(error) {
const next = new Error(error.errMsg || '网络连接失败')
next.network = true
reject(next)
}
})
})
}
export function login(loginId, password) {
return authRequest('/auth/login', { loginId, password })
}
export function refresh(refreshToken) {
return authRequest('/auth/refresh', { refreshToken })
}
export function logout(refreshToken, accessToken) {
return authRequest('/auth/logout', { refreshToken }, {
header: accessToken ? { Authorization: `Bearer ${accessToken}` } : undefined,
timeout: 5000
})
}