36 lines
1.2 KiB
Plaintext
36 lines
1.2 KiB
Plaintext
import { API_BASE_URL, wait, requestJson } from '@/services/api.uts'
|
|||
|
|
export { API_BASE_URL, saveSession } from '@/services/api.uts'
|
||
|
|
|
||
|
|
export async function requestSmsCode(phone: string): Promise<boolean> {
|
||
|
|
if (API_BASE_URL.length == 0) {
|
||
|
|
await wait(350)
|
||
|
|
return phone.length == 11
|
||
|
|
}
|
||
|
|
await requestJson('/auth/sms-code', 'POST', { phone })
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function loginWithSms(phone: string, code: string): Promise<string> {
|
||
|
|
if (API_BASE_URL.length == 0) {
|
||
|
|
await wait(300)
|
||
|
|
if (code.length != 6) throw new Error('验证码应为 6 位数字')
|
||
|
|
return 'demo-sms-token'
|
||
|
|
}
|
||
|
|
const result = await requestJson('/auth/sms-login', 'POST', { phone, code })
|
||
|
|
const token = result['token'] as string | null
|
||
|
|
if (token == null || token.length == 0) throw new Error('登录响应缺少 token')
|
||
|
|
return token
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function loginWithPassword(phone: string, password: string): Promise<string> {
|
||
|
|
if (API_BASE_URL.length == 0) {
|
||
|
|
await wait(300)
|
||
|
|
return 'demo-password-token'
|
||
|
|
}
|
||
|
|
const result = await requestJson('/auth/password-login', 'POST', { phone, password })
|
||
|
|
const token = result['token'] as string | null
|
||
|
|
if (token == null || token.length == 0) throw new Error('登录响应缺少 token')
|
||
|
|
return token
|
||
|
|
}
|
||
|
|
|