恢复修改的数据

This commit is contained in:
wangzzpeng
2026-09-28 10:47:49 +08:00
parent f1e64ef864
commit 43c2dc0d8d
57 changed files with 4557 additions and 1642 deletions

View File

@@ -1,41 +1,197 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { BusinessType, PageResult } from '@/types/api'
import type { Case, CaseQuery } from '@/types/business/cases'
import { getCaseCounts, getCaseDetail, getCaseList, toggleFavorite as toggleFavoriteApi } from '@/api/cases'
import type {
BusinessIndustryOption,
Case,
CaseQuery,
CaseTypeOption,
ProjectCaseStatisticsVo,
ProjectCaseUserBo,
ScaleLimit,
} from '@/types/business/cases'
import { collectCase, getCaseCategories, getCaseDetail, getCaseList } from '@/api/cases'
import { toCase } from '@/utils/caseVo'
import type { CaseTypeNameMap } from '@/utils/caseVo'
/**
* 案例库数据(列表 / 筛选 / 分页 / 详情 / 收藏)
*
* 后端接口(ProjectCaseUserController,前缀 /web/projectCase):
* - GET /list 列表(分页 + 跟随筛选条件的统计)
* - GET /categories 筛选面板选项(业务类型统计 + 所属行业 + 项目规模档位)
* - GET /{id} 详情(含项目背景富文本与附件,并回填 isFavorite)
* - POST /collect 收藏 / 取消收藏(toggle)
*
* ⚠️ 筛选面板**除完成时间外**的选项全部由后端下发,且**都来自 GET /categories**(一次请求):
* - 业务类型:projectCaseStatisticsVo.projectCaseCounts(按项目提报表补全类型全集)
* - 案例总数:projectCaseStatisticsVo.total
* - 所属行业:businessIndustry
* - 项目规模:upperAndLowerLimits(label 已由后端拼好)
* 完成时间后端无对应选项接口,由前端按快捷区间 + 日期选择器生成。
*
* ⚠️ 业务类型**下发的是提报主键 id**(不是类型名):案例表 business_type 列实测存的就是
* 该 id,传类型名后端按原值匹配不到(实测 total=0)。同一份 id→名称 字典还用于把列表 /
* 详情里的 id 翻译成展示名(见 typeNameById),否则标签会全部退化成「其他」。
*
* 后端 VO 与页面字段的差异(命名、业务类型取值、富文本、数值单位)由 utils/caseVo 统一对齐。
*/
/** 每页条数(案例库为三列卡片栅格,9 = 3 行) */
const PAGE_SIZE = 9
/** 结果数预探测的防抖时长(筛选面板改动后即时回显「N 个结果」) */
const PROBE_DELAY = 300
function createDefaultQuery(): CaseQuery {
return {
category: 'all',
categories: [],
industries: [],
scales: [],
dateRange: null,
favoriteOnly: false,
keyword: '',
page: 1,
pageSize: 8,
pageSize: PAGE_SIZE,
}
}
/** 案例库数据(列表 / 筛选 / 分页 / 收藏) */
/** 筛选面板给的是 yyyy-MM-dd,后端 Date 入参按 yyyy-MM-dd HH:mm:ss 补全 */
function withTime(date: string, time: string): string {
return date.length === 10 ? `${date} ${time}` : date
}
function trimmed(raw: string | null | undefined): string {
return (raw ?? '').trim()
}
/**
* 业务类型统计 → 筛选面板选项。
*
* <p>value 取**提报主键 id**(下发 businessTypes 用),label 取业务类型名称(展示用)。
* 不按前端 BusinessType 枚举归一化,选项一律用后端下发的原值。</p>
*/
function toTypeOptions(stats: ProjectCaseStatisticsVo | null): CaseTypeOption[] {
return (stats?.projectCaseCounts ?? [])
.map((item) => ({
value: String(item.id ?? '').trim(),
label: trimmed(item.businessType),
count: item.caseCount ?? 0,
}))
.filter((item) => item.value.length > 0 && item.label.length > 0)
}
/** 业务类型统计 → 「提报 id → 类型名」字典(供 utils/caseVo 翻译列表/详情的 business_type) */
function toTypeNameMap(stats: ProjectCaseStatisticsVo | null): CaseTypeNameMap {
const map: CaseTypeNameMap = {}
for (const item of stats?.projectCaseCounts ?? []) {
const id = String(item.id ?? '').trim()
const name = trimmed(item.businessType)
if (id.length > 0 && name.length > 0) map[id] = name
}
return map
}
export const useCasesStore = defineStore('cases', () => {
const query = ref<CaseQuery>(createDefaultQuery())
const list = ref<Case[]>([])
const total = ref(0)
const counts = ref<Partial<Record<BusinessType, number>>>({})
/** 待提交条件的结果数(筛选面板改动后即时探测,仅用于「确定筛选(N 个结果)」) */
const previewTotal = ref(0)
/** 业务类型筛选项(接口下发:GET /categories 的统计;value=提报 id) */
const typeOptions = ref<CaseTypeOption[]>([])
/** 业务类型字典:提报 id → 类型名(列表/详情标签翻译 + 首页精选共用) */
const typeNameById = ref<CaseTypeNameMap>({})
/** 所属行业筛选项(接口下发:GET /categories) */
const industryOptions = ref<BusinessIndustryOption[]>([])
/** 项目规模筛选项(接口下发:GET /categories,label 由后端拼好) */
const scaleOptions = ref<ScaleLimit[]>([])
/** 案例总数(全量口径,用于业务类型「全部」行角标) */
const caseTotal = ref(0)
const currentCase = ref<Case | null>(null)
let probeTimer: ReturnType<typeof setTimeout> | null = null
/** 页面筛选条件 → 后端查询条件(值均由接口下发,原样回传) */
function buildQuery(input: CaseQuery): {
bo: ProjectCaseUserBo
pageNum: number
pageSize: number
} {
const bo: ProjectCaseUserBo = {}
if (input.categories.length > 0) {
// categories 存的已是提报主键 id(CaseTypeOption.value),直接下发
bo.businessTypes = [...input.categories]
}
if (input.industries.length > 0) {
bo.businessIndustries = [...input.industries]
}
if (input.scales.length > 0) {
// 整档对象下发(后端接收 List<UpperAndLowerLimitsModel>),浅拷贝避免把响应式对象传出去
bo.scales = input.scales.map((item) => ({ ...item }))
}
if (input.dateRange) {
bo.beginTime = withTime(input.dateRange[0], '00:00:00')
bo.endTime = withTime(input.dateRange[1], '23:59:59')
}
if (input.favoriteOnly) bo.isFavorite = 1
const keyword = input.keyword.trim()
if (keyword) bo.key = keyword
return { bo, pageNum: input.page, pageSize: input.pageSize }
}
/**
* 筛选面板选项:一次 GET /categories 取回三组 + 业务类型字典。
*
* <p>后端 CaseSelectionModel 里同时带 projectCaseStatisticsVo,所以业务类型选项与案例总数
* 无需再单独打一次 /list —— 这里的所有选项都是「全量口径」,不随当前筛选条件收缩。</p>
*/
async function fetchCategories(): Promise<void> {
const result = await getCaseCategories()
const stats = result.projectCaseStatisticsVo
typeOptions.value = toTypeOptions(stats)
typeNameById.value = toTypeNameMap(stats)
caseTotal.value = stats?.total ?? 0
industryOptions.value = (result.businessIndustry ?? []).filter((item) => trimmed(item.name).length > 0)
scaleOptions.value = result.upperAndLowerLimits ?? []
}
/**
* 确保业务类型字典已就绪(幂等)。
*
* <p>首页「案例库精选」的标签翻译同样依赖该字典 —— 从首页直接进入时,
* 案例库页自身的 fetchCategories 还没跑过,故由调用方(首页 store)显式确保。</p>
*/
async function ensureCategories(): Promise<void> {
if (typeOptions.value.length === 0) await fetchCategories()
}
/** 列表取数 */
async function fetchCases(): Promise<void> {
const result: PageResult<Case> = await getCaseList(query.value)
list.value = result.list
total.value = result.total
const { bo, pageNum, pageSize } = buildQuery(query.value)
const result = await getCaseList(bo, pageNum, pageSize)
list.value = (result.projectCasePage?.rows ?? []).map((vo) => toCase(vo, typeNameById.value))
total.value = result.projectCasePage?.total ?? 0
previewTotal.value = total.value
}
async function fetchCounts(): Promise<void> {
counts.value = await getCaseCounts()
/** 待提交条件的结果数(防抖,避免每次点击都打接口) */
async function probeTotal(): Promise<void> {
const { bo } = buildQuery(query.value)
const result = await getCaseList(bo, 1, 1)
previewTotal.value = result.projectCasePage?.total ?? 0
}
function scheduleProbe(): void {
if (probeTimer) clearTimeout(probeTimer)
probeTimer = setTimeout(() => void probeTotal(), PROBE_DELAY)
}
/** 面板改条件(只改待提交条件,不取数;取数由「确定筛选」触发) */
function setQuery(next: CaseQuery): void {
query.value = next
scheduleProbe()
}
/** 确定筛选(重置页码后取数) */
@@ -55,29 +211,61 @@ export const useCasesStore = defineStore('cases', () => {
await fetchCases()
}
async function fetchCaseDetail(id: string): Promise<void> {
currentCase.value = await getCaseDetail(id)
/** 「收藏列表」开关:只筛已收藏案例 */
async function toggleFavoriteOnly(): Promise<void> {
query.value = { ...query.value, favoriteOnly: !query.value.favoriteOnly, page: 1 }
await fetchCases()
}
/** 案例详情(后端查不到时返回 null,页面据此展示空态) */
async function fetchCaseDetail(id: string): Promise<void> {
const vo = await getCaseDetail(id)
currentCase.value = vo ? toCase(vo, typeNameById.value) : null
}
/**
* 收藏 / 取消收藏。
*
* <p>后端为 toggle 语义且不返回最新状态,这里按调用成功的结果本地翻转,
* 避免再调一次详情接口(该接口会自增预览量)。</p>
*/
async function toggleFavorite(id: string): Promise<void> {
const updated = await toggleFavoriteApi(id)
await collectCase(id)
const index = list.value.findIndex((item) => item.id === id)
if (index >= 0) list.value[index] = updated
if (currentCase.value?.id === id) currentCase.value = updated
if (index >= 0) {
const target = list.value[index]
const delta = target.isFavorite ? -1 : 1
list.value[index] = { ...target, isFavorite: !target.isFavorite, favoriteCount: target.favoriteCount + delta }
}
if (currentCase.value?.id === id) {
const delta = currentCase.value.isFavorite ? -1 : 1
currentCase.value = {
...currentCase.value,
isFavorite: !currentCase.value.isFavorite,
favoriteCount: currentCase.value.favoriteCount + delta,
}
}
}
return {
query,
list,
total,
counts,
previewTotal,
typeOptions,
typeNameById,
industryOptions,
scaleOptions,
caseTotal,
currentCase,
fetchCases,
fetchCounts,
fetchCategories,
ensureCategories,
setQuery,
search,
resetQuery,
changePage,
toggleFavoriteOnly,
fetchCaseDetail,
toggleFavorite,
}