37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
import { API_ROOT } from '@/config/runtime.js'
|
|
|
|
function authRequest(path, data) {
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: `${API_ROOT}${path}`,
|
|
method: 'POST',
|
|
data,
|
|
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(username, password) {
|
|
return authRequest('/auth/login', { username, password })
|
|
}
|
|
|
|
export function refresh(refreshToken) {
|
|
return authRequest('/auth/refresh', { refreshToken })
|
|
}
|