feat: 完成前端业务闭环与后端联调
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
import { assertSmsCode } from '@/utils/auth/verification.js'
|
||||
import { resolveRuntimeMode, runtimeConfig } from '@/utils/runtime-config.js'
|
||||
import { runtimeConfig } from '@/utils/runtime-config.js'
|
||||
import {
|
||||
assertAuthVerificationOperation,
|
||||
assertPasswordHash,
|
||||
assertWechatAuthorizationCode,
|
||||
normalizeOptionalReferralCode,
|
||||
normalizeOptionalValidToken,
|
||||
requireRemoteAuth,
|
||||
saveLogin
|
||||
} from './auth-contract.js'
|
||||
import { normalizeOptionalText } from './request-normalizers.js'
|
||||
import { normalizePhoneChangePayload } from './profile-contract.js'
|
||||
import {
|
||||
createRequestError,
|
||||
requestAuth,
|
||||
requestAuthVoid,
|
||||
requestStrict
|
||||
@@ -17,7 +19,6 @@ import {
|
||||
|
||||
export const authApi = {
|
||||
async getCaptchaRequirement({ operationCode, subject }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
return requestAuth({
|
||||
url: `/genealogy/app/auth/verification/${encodeURIComponent(assertAuthVerificationOperation(operationCode))}/require`,
|
||||
method: 'GET',
|
||||
@@ -26,7 +27,6 @@ export const authApi = {
|
||||
},
|
||||
|
||||
async sendSmsCode({ operationCode, phone, validToken }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
const normalizedValidToken = normalizeOptionalValidToken(validToken)
|
||||
return requestAuthVoid({
|
||||
url: `/genealogy/app/auth/sms/${encodeURIComponent(assertAuthVerificationOperation(operationCode))}/code`,
|
||||
@@ -41,7 +41,6 @@ export const authApi = {
|
||||
},
|
||||
|
||||
async loginWithPassword({ phone, passwordHash, validToken }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
const normalizedValidToken = normalizeOptionalValidToken(validToken)
|
||||
const loginSession = await requestAuth({
|
||||
url: '/genealogy/app/auth/login',
|
||||
@@ -58,7 +57,6 @@ export const authApi = {
|
||||
},
|
||||
|
||||
async loginWithSms({ phone, smsCode }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
const loginSession = await requestAuth({
|
||||
url: '/genealogy/app/auth/login/sms',
|
||||
method: 'POST',
|
||||
@@ -72,9 +70,39 @@ export const authApi = {
|
||||
return saveLogin(loginSession)
|
||||
},
|
||||
|
||||
async registerWithPassword({ phone, passwordHash, smsCode, nickName }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
async loginWithWechat({ code }, requestOptions = {}) {
|
||||
const loginResult = await requestAuth({
|
||||
url: '/genealogy/app/auth/login/wechat',
|
||||
method: 'POST',
|
||||
data: { code: assertWechatAuthorizationCode(code) }
|
||||
}, requestOptions)
|
||||
if (loginResult?.status === 'ACCOUNT_BINDING_REQUIRED') {
|
||||
throw createRequestError(
|
||||
'该微信尚未绑定账号,请先用手机号登录,再到“账号与安全”绑定微信',
|
||||
'ACCOUNT_BINDING_REQUIRED'
|
||||
)
|
||||
}
|
||||
if (loginResult?.status !== 'AUTHENTICATED' || !loginResult.login) {
|
||||
throw createRequestError('微信登录响应无效', 'AUTH_RESPONSE_INVALID')
|
||||
}
|
||||
return saveLogin(loginResult.login)
|
||||
},
|
||||
|
||||
async bindWechat({ code }, requestOptions = {}) {
|
||||
await requestStrict({
|
||||
url: '/genealogy/app/auth/wechat/bind',
|
||||
method: 'POST',
|
||||
data: { code: assertWechatAuthorizationCode(code) }
|
||||
}, {
|
||||
requireData: false,
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return null
|
||||
},
|
||||
|
||||
async registerWithPassword({ phone, passwordHash, smsCode, nickName, referralCode }, requestOptions = {}) {
|
||||
const normalizedNickName = normalizeOptionalText(nickName, '昵称')
|
||||
const normalizedReferralCode = normalizeOptionalReferralCode(referralCode)
|
||||
const loginSession = await requestAuth({
|
||||
url: '/genealogy/app/auth/register',
|
||||
method: 'POST',
|
||||
@@ -84,14 +112,14 @@ export const authApi = {
|
||||
phone,
|
||||
password: assertPasswordHash(passwordHash),
|
||||
smsCode: assertSmsCode(smsCode),
|
||||
...(normalizedNickName ? { nickName: normalizedNickName } : {})
|
||||
...(normalizedNickName ? { nickName: normalizedNickName } : {}),
|
||||
...(normalizedReferralCode ? { referralCode: normalizedReferralCode } : {})
|
||||
}
|
||||
}, requestOptions)
|
||||
return saveLogin(loginSession)
|
||||
},
|
||||
|
||||
async resetPassword({ phone, passwordHash, smsCode }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
return requestAuthVoid({
|
||||
url: '/genealogy/app/auth/password/reset',
|
||||
method: 'PUT',
|
||||
@@ -106,7 +134,6 @@ export const authApi = {
|
||||
},
|
||||
|
||||
async changePassword({ oldPasswordHash, newPasswordHash }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
await requestStrict({
|
||||
url: '/genealogy/app/auth/password',
|
||||
method: 'PUT',
|
||||
@@ -122,7 +149,6 @@ export const authApi = {
|
||||
},
|
||||
|
||||
async deactivateAccount({ smsCode }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
await requestStrict({
|
||||
url: '/genealogy/app/auth/account/deactivate',
|
||||
method: 'POST',
|
||||
@@ -135,7 +161,6 @@ export const authApi = {
|
||||
},
|
||||
|
||||
async logout(requestOptions = {}) {
|
||||
if (resolveRuntimeMode() !== 'remote') return null
|
||||
await requestStrict({
|
||||
url: '/genealogy/app/auth/logout',
|
||||
method: 'DELETE'
|
||||
@@ -147,7 +172,6 @@ export const authApi = {
|
||||
},
|
||||
|
||||
async changePhone(payload, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
await requestStrict({
|
||||
url: '/genealogy/app/auth/phone',
|
||||
method: 'PUT',
|
||||
|
||||
Reference in New Issue
Block a user