78 lines
2.4 KiB
JavaScript
78 lines
2.4 KiB
JavaScript
import { hasRemoteConfig } from '@/utils/runtime-config.js'
|
|
import {
|
|
normalizeAppProfile,
|
|
normalizeProfileUpdatePayload,
|
|
normalizeRecommendationPreference
|
|
} from './profile-contract.js'
|
|
import {
|
|
createRequestError,
|
|
requestStrict
|
|
} from './request-client.js'
|
|
|
|
const requireRemoteProfile = (operation) => {
|
|
if (hasRemoteConfig()) return
|
|
const isRead = operation === '读取'
|
|
throw createRequestError(
|
|
`个人资料${operation}需要真实服务,当前本地预览不伪造${isRead ? '资料' : '保存成功'}`,
|
|
isRead ? 'REMOTE_READ_REQUIRED' : 'REMOTE_WRITE_REQUIRED'
|
|
)
|
|
}
|
|
|
|
const requireRemotePreference = (operation) => {
|
|
if (hasRemoteConfig()) return
|
|
throw createRequestError(
|
|
`个性化推荐偏好${operation}需要真实服务,当前本地预览不会伪造结果`,
|
|
operation === '读取' ? 'REMOTE_READ_REQUIRED' : 'REMOTE_WRITE_REQUIRED'
|
|
)
|
|
}
|
|
|
|
export const profileApi = {
|
|
async getProfile(requestOptions = {}) {
|
|
requireRemoteProfile('读取')
|
|
const profile = await requestStrict({
|
|
url: '/genealogy/app/auth/profile',
|
|
method: 'GET'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeAppProfile(profile)
|
|
},
|
|
|
|
async updateProfile(payload, requestOptions = {}) {
|
|
const profileChanges = normalizeProfileUpdatePayload(payload)
|
|
requireRemoteProfile('更新')
|
|
const profile = await requestStrict({
|
|
url: '/genealogy/app/auth/profile',
|
|
method: 'PUT',
|
|
data: profileChanges
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeAppProfile(profile)
|
|
},
|
|
|
|
async getRecommendationPreference(requestOptions = {}) {
|
|
requireRemotePreference('读取')
|
|
const preference = await requestStrict({
|
|
url: '/genealogy/app/recommendation-preference',
|
|
method: 'GET'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeRecommendationPreference(preference)
|
|
},
|
|
|
|
async updateRecommendationPreference(enabled, requestOptions = {}) {
|
|
if (typeof enabled !== 'boolean') throw new TypeError('个性化推荐开关必须是布尔值')
|
|
requireRemotePreference('写入')
|
|
const preference = await requestStrict({
|
|
url: '/genealogy/app/recommendation-preference',
|
|
method: 'PUT',
|
|
data: { enabled }
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeRecommendationPreference(preference)
|
|
}
|
|
}
|