shinnosuke
4 天以前 2c01718e732f7c4842bc4711484bbd59345caf49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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)
}