230 lines
5.9 KiB
TypeScript
230 lines
5.9 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import type { PageResult } from '@/types/api'
|
|
import type {
|
|
Bill,
|
|
BillQuery,
|
|
FinanceStats,
|
|
Invoice,
|
|
InvoiceQuery,
|
|
InvoiceTitle,
|
|
InvoiceTitlePayload,
|
|
PayMethod,
|
|
PayOrderResponse,
|
|
Payment,
|
|
PaymentResult,
|
|
} from '@/types/business/finance'
|
|
import {
|
|
applyInvoice as applyInvoiceApi,
|
|
createAlipayPageOrder as createAlipayPageOrderApi,
|
|
createInvoiceTitle,
|
|
createPayment as createPaymentApi,
|
|
createWechatNativeOrder as createWechatNativeOrderApi,
|
|
deleteInvoiceTitle,
|
|
exportReport as exportReportApi,
|
|
getBillDetail,
|
|
getBillList,
|
|
getCheckoutBills,
|
|
getFinanceStats,
|
|
getInvoiceList,
|
|
getInvoiceTitles,
|
|
getPlanPayResult,
|
|
getPayResult,
|
|
setDefaultTitle as setDefaultTitleApi,
|
|
switchPaymentMethod as switchPaymentMethodApi,
|
|
updateInvoiceTitle,
|
|
} from '@/api/finance'
|
|
|
|
function createDefaultBillQuery(): BillQuery {
|
|
return { keyword: '', status: 'all', page: 1, pageSize: 6 }
|
|
}
|
|
|
|
function createDefaultInvoiceQuery(): InvoiceQuery {
|
|
return { keyword: '', status: 'all', page: 1, pageSize: 6 }
|
|
}
|
|
|
|
export const useFinanceStore = defineStore('finance', () => {
|
|
const stats = ref<FinanceStats | null>(null)
|
|
|
|
const list = ref<Bill[]>([])
|
|
const total = ref(0)
|
|
const query = ref<BillQuery>(createDefaultBillQuery())
|
|
const currentBill = ref<Bill | null>(null)
|
|
|
|
const payment = ref<Payment | null>(null)
|
|
const result = ref<PaymentResult | null>(null)
|
|
|
|
const checkoutBills = ref<Bill[]>([])
|
|
const checkoutLoading = ref(false)
|
|
const payOrder = ref<PayOrderResponse | null>(null)
|
|
|
|
const invoices = ref<Invoice[]>([])
|
|
const invoiceTotal = ref(0)
|
|
const invoiceQuery = ref<InvoiceQuery>(createDefaultInvoiceQuery())
|
|
|
|
const titles = ref<InvoiceTitle[]>([])
|
|
|
|
async function fetchStats(period?: string): Promise<void> {
|
|
stats.value = await getFinanceStats(period)
|
|
}
|
|
|
|
async function fetchList(): Promise<void> {
|
|
const pageData: PageResult<Bill> = await getBillList(query.value)
|
|
list.value = pageData.list
|
|
total.value = pageData.total
|
|
}
|
|
|
|
function setQuery(next: BillQuery): void {
|
|
query.value = next
|
|
}
|
|
|
|
async function search(): Promise<void> {
|
|
query.value = { ...query.value, page: 1 }
|
|
await fetchList()
|
|
}
|
|
|
|
async function resetQuery(): Promise<void> {
|
|
query.value = createDefaultBillQuery()
|
|
await fetchList()
|
|
}
|
|
|
|
async function changePage(page: number): Promise<void> {
|
|
query.value = { ...query.value, page }
|
|
await fetchList()
|
|
}
|
|
|
|
async function fetchDetail(id: string): Promise<void> {
|
|
currentBill.value = await getBillDetail(id)
|
|
}
|
|
|
|
async function createPayment(billId: string, method: PayMethod): Promise<void> {
|
|
payment.value = await createPaymentApi(billId, method)
|
|
}
|
|
|
|
async function switchPaymentMethod(billId: string, method: PayMethod): Promise<void> {
|
|
payment.value = await switchPaymentMethodApi(billId, method)
|
|
}
|
|
|
|
async function fetchResult(billId: string, requirementId = ''): Promise<void> {
|
|
result.value = requirementId
|
|
? await getPlanPayResult(requirementId, billId)
|
|
: await getPayResult(billId)
|
|
}
|
|
|
|
async function loadCheckout(requirementId: string, ids: string[] = []): Promise<void> {
|
|
checkoutLoading.value = true
|
|
try {
|
|
checkoutBills.value = await getCheckoutBills(requirementId, ids)
|
|
} finally {
|
|
checkoutLoading.value = false
|
|
}
|
|
}
|
|
|
|
async function payByWechatNative(orderNo: string, amount: number, billIds: string[]): Promise<void> {
|
|
payOrder.value = await createWechatNativeOrderApi(orderNo, amount, billIds)
|
|
}
|
|
|
|
async function payByAlipayPage(orderNo: string, amount: number, billIds: string[]): Promise<void> {
|
|
payOrder.value = await createAlipayPageOrderApi(orderNo, amount, billIds)
|
|
}
|
|
|
|
function clearPayOrder(): void {
|
|
payOrder.value = null
|
|
}
|
|
|
|
async function fetchInvoices(): Promise<void> {
|
|
const pageData: PageResult<Invoice> = await getInvoiceList(invoiceQuery.value)
|
|
invoices.value = pageData.list
|
|
invoiceTotal.value = pageData.total
|
|
}
|
|
|
|
function setInvoiceQuery(next: InvoiceQuery): void {
|
|
invoiceQuery.value = next
|
|
}
|
|
|
|
async function searchInvoices(): Promise<void> {
|
|
invoiceQuery.value = { ...invoiceQuery.value, page: 1 }
|
|
await fetchInvoices()
|
|
}
|
|
|
|
async function changeInvoicePage(page: number): Promise<void> {
|
|
invoiceQuery.value = { ...invoiceQuery.value, page }
|
|
await fetchInvoices()
|
|
}
|
|
|
|
async function applyInvoice(id: string): Promise<void> {
|
|
const updated = await applyInvoiceApi(id)
|
|
const index = invoices.value.findIndex((item) => item.id === id)
|
|
if (index >= 0) invoices.value[index] = updated
|
|
}
|
|
|
|
async function fetchTitles(): Promise<void> {
|
|
titles.value = await getInvoiceTitles()
|
|
}
|
|
|
|
async function createTitle(payload: InvoiceTitlePayload): Promise<void> {
|
|
await createInvoiceTitle(payload)
|
|
await fetchTitles()
|
|
}
|
|
|
|
async function updateTitle(id: string, payload: InvoiceTitlePayload): Promise<void> {
|
|
await updateInvoiceTitle(id, payload)
|
|
await fetchTitles()
|
|
}
|
|
|
|
async function deleteTitle(id: string): Promise<void> {
|
|
await deleteInvoiceTitle(id)
|
|
await fetchTitles()
|
|
}
|
|
|
|
async function setDefaultTitle(id: string): Promise<void> {
|
|
titles.value = await setDefaultTitleApi(id)
|
|
}
|
|
|
|
async function exportReport(): Promise<void> {
|
|
await exportReportApi()
|
|
}
|
|
|
|
return {
|
|
stats,
|
|
list,
|
|
total,
|
|
query,
|
|
currentBill,
|
|
payment,
|
|
result,
|
|
checkoutBills,
|
|
checkoutLoading,
|
|
payOrder,
|
|
invoices,
|
|
invoiceTotal,
|
|
invoiceQuery,
|
|
titles,
|
|
fetchStats,
|
|
fetchList,
|
|
setQuery,
|
|
search,
|
|
resetQuery,
|
|
changePage,
|
|
fetchDetail,
|
|
createPayment,
|
|
switchPaymentMethod,
|
|
fetchResult,
|
|
loadCheckout,
|
|
payByWechatNative,
|
|
payByAlipayPage,
|
|
clearPayOrder,
|
|
fetchInvoices,
|
|
setInvoiceQuery,
|
|
searchInvoices,
|
|
changeInvoicePage,
|
|
applyInvoice,
|
|
fetchTitles,
|
|
createTitle,
|
|
updateTitle,
|
|
deleteTitle,
|
|
setDefaultTitle,
|
|
export: exportReport,
|
|
}
|
|
})
|