feat: 完成前端业务闭环与后端联调
This commit is contained in:
@@ -1,33 +1,25 @@
|
||||
import { listPreviewPublicGenealogies } from '@/data/preview/genealogies.js'
|
||||
import { hasRemoteConfig } from '@/utils/runtime-config.js'
|
||||
import {
|
||||
normalizeAppGenealogy,
|
||||
normalizeCreatedGenealogy,
|
||||
normalizeGenealogyCreatePayload,
|
||||
normalizeGenealogyOrderIds,
|
||||
normalizeGenealogyPathId,
|
||||
normalizeGenealogyDeletionTask,
|
||||
normalizeGenealogyPermanentDeletionCapability,
|
||||
normalizeGenealogyPermanentDeletionPayload,
|
||||
normalizeGenealogyQuota,
|
||||
normalizeGenealogySettings,
|
||||
normalizeGenealogyUpdatePayload,
|
||||
normalizeMyGenealogies,
|
||||
normalizePublicGenealogies,
|
||||
projectPreviewPublicGenealogies
|
||||
normalizePublicGenealogyQuery,
|
||||
normalizePublicGenealogies
|
||||
} from './genealogy-contract.js'
|
||||
import {
|
||||
createRequestError,
|
||||
requestStrict
|
||||
} from './request-client.js'
|
||||
|
||||
const requireRemoteGenealogy = (label, operation) => {
|
||||
if (hasRemoteConfig()) return
|
||||
throw createRequestError(
|
||||
`${label}${operation}需要真实服务,当前本地预览不会伪造结果`,
|
||||
operation === '读取' ? 'REMOTE_READ_REQUIRED' : 'REMOTE_WRITE_REQUIRED'
|
||||
)
|
||||
}
|
||||
|
||||
const writeGenealogyState = (genealogyId, action, label, requestOptions) => {
|
||||
requireRemoteGenealogy(label, '写入')
|
||||
const writeGenealogyState = (genealogyId, action, requestOptions) => {
|
||||
return requestStrict({
|
||||
url: `/genealogy/app/genealogies/${genealogyId}/${action}`,
|
||||
method: 'PUT'
|
||||
@@ -37,13 +29,11 @@ const writeGenealogyState = (genealogyId, action, label, requestOptions) => {
|
||||
}
|
||||
|
||||
export const genealogyApi = {
|
||||
async getPublicGenealogies(requestOptions = {}) {
|
||||
if (!hasRemoteConfig()) {
|
||||
return projectPreviewPublicGenealogies(listPreviewPublicGenealogies())
|
||||
}
|
||||
async getPublicGenealogies(query = {}, requestOptions = {}) {
|
||||
const publicGenealogyRows = await requestStrict({
|
||||
url: '/genealogy/app/genealogies/public',
|
||||
method: 'GET'
|
||||
method: 'GET',
|
||||
data: normalizePublicGenealogyQuery(query)
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
@@ -51,7 +41,6 @@ export const genealogyApi = {
|
||||
},
|
||||
|
||||
async getMyGenealogies(requestOptions = {}) {
|
||||
requireRemoteGenealogy('我的家谱', '读取')
|
||||
const genealogyRows = await requestStrict({
|
||||
url: '/genealogy/app/genealogies/mine',
|
||||
method: 'GET'
|
||||
@@ -63,7 +52,6 @@ export const genealogyApi = {
|
||||
|
||||
async saveMyGenealogyOrder(genealogyIds, requestOptions = {}) {
|
||||
const orderedIds = normalizeGenealogyOrderIds(genealogyIds)
|
||||
requireRemoteGenealogy('保存家谱排序', '写入')
|
||||
const confirmedGenealogies = normalizeMyGenealogies(await requestStrict({
|
||||
url: '/genealogy/app/genealogies/mine/order',
|
||||
method: 'PUT',
|
||||
@@ -81,7 +69,6 @@ export const genealogyApi = {
|
||||
},
|
||||
|
||||
async getGenealogyQuota(requestOptions = {}) {
|
||||
requireRemoteGenealogy('家谱配额', '读取')
|
||||
const quota = await requestStrict({
|
||||
url: '/genealogy/app/genealogies/quota',
|
||||
method: 'GET'
|
||||
@@ -93,7 +80,6 @@ export const genealogyApi = {
|
||||
|
||||
async createGenealogy(payload, requestOptions = {}) {
|
||||
const genealogyDraft = normalizeGenealogyCreatePayload(payload)
|
||||
requireRemoteGenealogy('创建家谱', '写入')
|
||||
const createdGenealogy = await requestStrict({
|
||||
url: '/genealogy/app/genealogies',
|
||||
method: 'POST',
|
||||
@@ -106,7 +92,6 @@ export const genealogyApi = {
|
||||
|
||||
async getGenealogySettings(genealogyId, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
requireRemoteGenealogy('家谱设置', '读取')
|
||||
const genealogySettings = await requestStrict({
|
||||
url: `/genealogy/app/genealogies/${normalizedGenealogyId}`,
|
||||
method: 'GET'
|
||||
@@ -119,7 +104,6 @@ export const genealogyApi = {
|
||||
async updateGenealogy(genealogyId, payload, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
const genealogyChanges = normalizeGenealogyUpdatePayload(payload)
|
||||
requireRemoteGenealogy('更新家谱', '写入')
|
||||
return requestStrict({
|
||||
url: `/genealogy/app/genealogies/${normalizedGenealogyId}`,
|
||||
method: 'PUT',
|
||||
@@ -135,7 +119,6 @@ export const genealogyApi = {
|
||||
const genealogy = await writeGenealogyState(
|
||||
normalizedGenealogyId,
|
||||
'archive',
|
||||
'家谱归档',
|
||||
requestOptions
|
||||
)
|
||||
return normalizeAppGenealogy(genealogy)
|
||||
@@ -146,24 +129,57 @@ export const genealogyApi = {
|
||||
const genealogy = await writeGenealogyState(
|
||||
normalizedGenealogyId,
|
||||
'restore',
|
||||
'家谱恢复',
|
||||
requestOptions
|
||||
)
|
||||
return normalizeAppGenealogy(genealogy)
|
||||
},
|
||||
|
||||
async sendPermanentDeletionCode(genealogyId, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
await requestStrict({
|
||||
url: `/genealogy/app/genealogies/${normalizedGenealogyId}/permanent-deletion/code`,
|
||||
method: 'POST'
|
||||
}, {
|
||||
requireData: false,
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return null
|
||||
},
|
||||
|
||||
async getPermanentDeletionCapability(genealogyId, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
const capability = await requestStrict({
|
||||
url: `/genealogy/app/genealogies/${normalizedGenealogyId}/permanent-deletion/capability`,
|
||||
method: 'GET'
|
||||
}, { requestController: requestOptions.requestController ?? null })
|
||||
return normalizeGenealogyPermanentDeletionCapability(capability)
|
||||
},
|
||||
|
||||
async deleteGenealogyPermanently(genealogyId, payload, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
const deletionTask = await requestStrict({
|
||||
url: `/genealogy/app/genealogies/${normalizedGenealogyId}/permanent-deletion`,
|
||||
method: 'POST',
|
||||
data: normalizeGenealogyPermanentDeletionPayload(payload)
|
||||
}, { requestController: requestOptions.requestController ?? null })
|
||||
return normalizeGenealogyDeletionTask(deletionTask, normalizedGenealogyId)
|
||||
},
|
||||
|
||||
async getOverview(genealogyId, requestOptions = {}) {
|
||||
const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId)
|
||||
requireRemoteGenealogy('家谱概览', '读取')
|
||||
const overview = normalizeAppGenealogy(await requestStrict({
|
||||
url: `/genealogy/app/genealogies/${normalizedGenealogyId}/overview`,
|
||||
const genealogyRows = await requestStrict({
|
||||
url: '/genealogy/app/genealogies/mine',
|
||||
method: 'GET'
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
}))
|
||||
if (overview.id !== normalizedGenealogyId) {
|
||||
throw createRequestError('家谱概览响应标识不匹配', 'GENEALOGY_RESPONSE_INVALID')
|
||||
})
|
||||
if (!Array.isArray(genealogyRows)) {
|
||||
throw createRequestError('家谱概览响应不是列表', 'GENEALOGY_OVERVIEW_RESPONSE_INVALID')
|
||||
}
|
||||
const overview = genealogyRows
|
||||
.map((item) => normalizeAppGenealogy(item))
|
||||
.find((item) => item.id === normalizedGenealogyId)
|
||||
if (!overview) throw createRequestError('当前家谱不在我的家谱中', 'GENEALOGY_OVERVIEW_RESPONSE_INVALID')
|
||||
return overview
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user