import { getApiEndpoint } from '../config/api.uts'
|
import { request, HttpError } from './http.uts'
|
import { clearSession, getSession, setSession } from './session.uts'
|
|
const consentStorageKey = 'talent-card-agreement-consent'
|
|
export const recordAgreementConsent = (version: string) => {
|
uni.setStorageSync(consentStorageKey, { version: version, agreedAt: Date.now() })
|
}
|
|
export const loginWithDevelopmentSession = (onSuccess: () => void, onError: (error: HttpError) => void) => {
|
request<any>({ path: getApiEndpoint('authDevelopmentSession'), method: 'POST', skipAuth: true }, (data: any) => {
|
if (data == null || data.accessToken == null || data.expireAt == null || data.talentId == null) {
|
onError({ code: 'INVALID_DEVELOPMENT_SESSION', message: '开发测试会话创建失败', statusCode: 200, traceId: '' })
|
return
|
}
|
setSession({ accessToken: data.accessToken as string, refreshToken: data.refreshToken != null ? data.refreshToken as string : '', expireAt: data.expireAt as number, talentId: '' + data.talentId })
|
onSuccess()
|
}, onError)
|
}
|
|
export const loginWithPhoneCode = (phoneCode: string, onSuccess: () => void, onError: (error: HttpError) => void) => {
|
if (phoneCode.length == 0) {
|
onError({ code: 'PHONE_AUTH_REQUIRED', message: '请重新授权手机号', statusCode: 0, traceId: '' })
|
return
|
}
|
const consent: any = uni.getStorageSync(consentStorageKey)
|
if (consent == null || consent.agreedAt == null) {
|
onError({ code: 'AGREEMENT_REQUIRED', message: '请先阅读并同意相关协议', statusCode: 0, traceId: '' })
|
return
|
}
|
uni.login({
|
provider: 'weixin',
|
success: (loginResult: any) => {
|
const endpoint = getApiEndpoint('authLogin')
|
request<any>({
|
path: endpoint,
|
method: 'POST',
|
skipAuth: true,
|
data: {
|
wechatCode: loginResult.code,
|
phoneCode: phoneCode,
|
agreementVersion: consent.version,
|
agreedAt: consent.agreedAt
|
}
|
}, (data: any) => {
|
if (data == null || data.accessToken == null || data.expireAt == null) {
|
onError({ code: 'INVALID_LOGIN_RESPONSE', message: '登录响应缺少会话信息', statusCode: 200, traceId: '' })
|
return
|
}
|
setSession({ accessToken: data.accessToken as string, refreshToken: data.refreshToken != null ? data.refreshToken as string : '', expireAt: data.expireAt as number, talentId: data.talentId != null ? '' + data.talentId : '' })
|
onSuccess()
|
}, onError)
|
},
|
fail: () => onError({ code: 'WECHAT_LOGIN_FAILED', message: '微信登录失败,请稍后重试', statusCode: 0, traceId: '' })
|
})
|
}
|
|
// 刷新时只接受服务端返回的新会话,不沿用已经过期的 accessToken。
|
export const refreshSession = (onSuccess: () => void, onError: (error: HttpError) => void) => {
|
const current = getSession()
|
if (current == null || current.refreshToken.length == 0) {
|
clearSession()
|
onError({ code: 'REFRESH_TOKEN_MISSING', message: '登录已失效,请重新登录', statusCode: 401, traceId: '' })
|
return
|
}
|
request<any>({ path: getApiEndpoint('authRefresh'), method: 'POST', skipAuth: true, data: { refreshToken: current.refreshToken } }, (data: any) => {
|
if (data == null || data.accessToken == null || data.expireAt == null) {
|
clearSession()
|
onError({ code: 'INVALID_REFRESH_RESPONSE', message: '登录刷新失败,请重新登录', statusCode: 200, traceId: '' })
|
return
|
}
|
setSession({ accessToken: data.accessToken as string, refreshToken: data.refreshToken != null ? data.refreshToken as string : current.refreshToken, expireAt: data.expireAt as number, talentId: data.talentId != null ? '' + data.talentId : current.talentId })
|
onSuccess()
|
}, (error) => {
|
clearSession()
|
onError(error)
|
})
|
}
|
|
export const logout = (onSuccess: () => void, onError: (error: HttpError) => void) => {
|
const endpoint = getApiEndpoint('authLogout')
|
if (endpoint.length == 0) {
|
clearSession()
|
uni.removeStorageSync(consentStorageKey)
|
onSuccess()
|
return
|
}
|
request<any>({ path: endpoint, method: 'POST' }, () => { clearSession(); uni.removeStorageSync(consentStorageKey); onSuccess() }, onError)
|
}
|