Files
cz-web/src/stores/modules/contract.ts
2026-10-01 17:24:57 +08:00

76 lines
1.7 KiB
TypeScript

import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { PageResult } from '@/types/api'
import type { Contract, ContractQuery } from '@/types/business/contract'
import {
getContractDetail,
getContractList,
getSignQr,
refreshSignQr,
} from '@/api/contract'
function createDefaultQuery(): ContractQuery {
return {
keyword: '',
page: 1,
pageSize: 8,
}
}
export const useContractStore = defineStore('contract', () => {
const query = ref<ContractQuery>(createDefaultQuery())
const list = ref<Contract[]>([])
const total = ref(0)
const current = ref<Contract | null>(null)
const signExpires = ref(272)
async function fetchList(): Promise<void> {
const result: PageResult<Contract> = await getContractList(query.value)
list.value = result.list
total.value = result.total
}
async function fetchDetail(id: string): Promise<void> {
current.value = await getContractDetail(id)
}
function setQuery(next: ContractQuery): void {
query.value = next
}
async function search(): Promise<void> {
query.value = { ...query.value, page: 1 }
await fetchList()
}
async function resetQuery(): Promise<void> {
query.value = createDefaultQuery()
await fetchList()
}
async function changePage(page: number): Promise<void> {
query.value = { ...query.value, page }
await fetchList()
}
async function fetchSignQr(id: string, refresh = false): Promise<void> {
const result = refresh ? await refreshSignQr(id) : await getSignQr(id)
signExpires.value = result.expiresInSeconds
}
return {
query,
list,
total,
current,
signExpires,
fetchList,
fetchDetail,
setQuery,
search,
resetQuery,
changePage,
fetchSignQr,
}
})