Commit e7cfee60 by chenwq

新增对账单导入功能,包含导入对账单对话框及相关逻辑

1 parent b3ef5ba2
...@@ -200,6 +200,18 @@ export function getMarketTransferBankAccounts(data) { ...@@ -200,6 +200,18 @@ export function getMarketTransferBankAccounts(data) {
return request(data, "post") return request(data, "post")
} }
// 导入对账单
export function importReconciliationBill(data) {
data.method = 'dj.api.smartcarlife.reconciliationBill.import'
// base64 文件内容较大,按现有签名规则排除 file 字段。
return request(data, "post", ["file"])
}
// 查询导入进度
export function getImportExcelProcess(data) {
data.method = 'dj.api.common.importExcel.async.queryProcess'
return request(data, "post")
}
// 应收应付列表 // 应收应付列表
export function getJdBillList(data) { export function getJdBillList(data) {
data.method = 'dj.smartcarlife.api.jdbill.page' data.method = 'dj.smartcarlife.api.jdbill.page'
......
export const IMPORT_BILL_FILE_RULE = {
extensions: [".xls", ".xlsx"],
maxSizeMb: 10,
maxCount: 1,
accept: ".xls,.xlsx"
}
export const IMPORT_BILL_TYPE_OPTIONS = [
{ value: "1", label: "流量方对账单" },
{ value: "2", label: "服务商对账单" }
]
// 导入弹窗统一使用 glsx-combi-form,这里集中维护表单结构,避免组件里散落配置。
export function buildImportBillFormData() {
return [
{
label: "对账单类型",
name: "billType",
type: "select",
optionList: IMPORT_BILL_TYPE_OPTIONS,
optionId: "value",
optionLabel: "label",
isClear: true
},
{
label: "对账单文件",
name: "fileName",
addHtml: "fileHtml"
}
]
}
// 后端要求 file 字段为“文件后缀@base64内容”,不要把完整 dataURL 传过去。
export function buildImportBillPayload({ billType, fileName, base64 }) {
const extension = fileName.split(".").pop()
return {
billType,
file: `${extension}@${base64}`
}
}
// 轮询接口返回 100% 后才算结束,再根据失败数量判断成功或失败。
export function getImportPollStatus(result = {}) {
if (result.progress === 100 && Number(result.failNum) === 0) {
return "success"
}
if (result.progress === 100 && Number(result.failNum) > 0) {
return "failed"
}
return "processing"
}
export function getImportUuid(result) {
if (typeof result === "string") {
return result
}
return result?.uuid || result?.data?.uuid || result?.data || ""
}
export function normalizeImportPollResult(result) {
return Array.isArray(result) ? result[0] || {} : result || {}
}
...@@ -98,6 +98,7 @@ ...@@ -98,6 +98,7 @@
<glsk-form-dialog v-if="billSplitVisible" :selectVisible="billSplitVisible" :dialogConfig="billSplitDialogConfig" label-width="200px" :formRules="billSplitRules" :editData="billSplitEditData" :editFormData="billSplitEditFormData" :baseData="baseData" @handleClose="splitDialogVisible" @handleConfirm="handleSplitConfirm"></glsk-form-dialog> <glsk-form-dialog v-if="billSplitVisible" :selectVisible="billSplitVisible" :dialogConfig="billSplitDialogConfig" label-width="200px" :formRules="billSplitRules" :editData="billSplitEditData" :editFormData="billSplitEditFormData" :baseData="baseData" @handleClose="splitDialogVisible" @handleConfirm="handleSplitConfirm"></glsk-form-dialog>
<CreateServiceDialog :account-visible="accountVisible" v-if="accountVisible" :scene="7" :editForm="serviceInfo" @custTmpl="handleCustTmpl" @handleConfirm="handleServiceConfirm" @accountClose="createServiceVisible" /> <CreateServiceDialog :account-visible="accountVisible" v-if="accountVisible" :scene="7" :editForm="serviceInfo" @custTmpl="handleCustTmpl" @handleConfirm="handleServiceConfirm" @accountClose="createServiceVisible" />
<market-transfer-dialog v-if="marketTransferVisible" :marketTransferVisible="marketTransferVisible" :info="merchantInfo" @handleConfirm="handleMarketTransferConfirm" @dialogClose="handleMarketTransferVisible"></market-transfer-dialog> <market-transfer-dialog v-if="marketTransferVisible" :marketTransferVisible="marketTransferVisible" :info="merchantInfo" @handleConfirm="handleMarketTransferConfirm" @dialogClose="handleMarketTransferVisible"></market-transfer-dialog>
<import-bill-dialog v-if="importBillVisible" :importBillVisible="importBillVisible" @handleConfirm="handleImportBillConfirm" @dialogClose="handleImportBillVisible"></import-bill-dialog>
<tmpl-drawer v-if="tmplDrawerVisible" :tmplDrawerVisible="tmplDrawerVisible" :type="tmplInfo.type" :tmplSelectList="tmplInfo.tmplSelectList" @handleConfirm="handleCustConfirm" @handleClose="handleCustVisible"></tmpl-drawer> <tmpl-drawer v-if="tmplDrawerVisible" :tmplDrawerVisible="tmplDrawerVisible" :type="tmplInfo.type" :tmplSelectList="tmplInfo.tmplSelectList" @handleConfirm="handleCustConfirm" @handleClose="handleCustVisible"></tmpl-drawer>
</div> </div>
</template> </template>
...@@ -116,6 +117,7 @@ import RecordTransfer from "../components/RecordTransfer" ...@@ -116,6 +117,7 @@ import RecordTransfer from "../components/RecordTransfer"
import EditPayReceive from "../components/EditPayReceive" import EditPayReceive from "../components/EditPayReceive"
import CreateServiceDialog from "../components/CreateServiceDialog" import CreateServiceDialog from "../components/CreateServiceDialog"
import MarketTransferDialog from "../components/MarketTransferDialog" import MarketTransferDialog from "../components/MarketTransferDialog"
import ImportBillDialog from "../components/ImportBillDialog"
import tmplDrawer from "../components/TmplDrawer" import tmplDrawer from "../components/TmplDrawer"
export default { export default {
name: 'AccountStateManage', name: 'AccountStateManage',
...@@ -129,6 +131,7 @@ export default { ...@@ -129,6 +131,7 @@ export default {
EditPayReceive, EditPayReceive,
CreateServiceDialog, CreateServiceDialog,
MarketTransferDialog, MarketTransferDialog,
ImportBillDialog,
tmplDrawer tmplDrawer
}, },
data() { data() {
...@@ -308,6 +311,7 @@ export default { ...@@ -308,6 +311,7 @@ export default {
}], }],
accountVisible: false, accountVisible: false,
marketTransferVisible: false, marketTransferVisible: false,
importBillVisible: false,
serviceInfo: {}, serviceInfo: {},
tmplDrawerVisible: false, tmplDrawerVisible: false,
tmplInfo: { tmplInfo: {
...@@ -717,6 +721,20 @@ export default { ...@@ -717,6 +721,20 @@ export default {
handleMarketTransferConfirm() { handleMarketTransferConfirm() {
this.handleMarketTransferVisible() this.handleMarketTransferVisible()
this.$refs.recordTable.refreshTable(false, true) this.$refs.recordTable.refreshTable(false, true)
},
// 导入对账单
handleImportBill() {
this.handleImportBillVisible()
},
handleImportBillVisible() {
this.importBillVisible = !this.importBillVisible
},
handleImportBillConfirm() {
this.handleImportBillVisible()
this.$refs.recordTable.refreshTable(false, true)
} }
} }
} }
......
<template>
<d2-dialog
title="对账单导入"
:is-visible="importBillVisible"
class="import-bill-dialog"
width="800px"
:btnOkLabel="importing ? '导入中' : '确定'"
@closeDialog="handleClose"
@confirmDialog="handleConfirm"
>
<glsx-combi-form
ref="importFormRef"
label-width="130px"
default-width="560px"
:isBlock="isBlock"
:form-ref="importChildRef"
:form-obj="form"
:form-data="formData"
:form-rules="formRules"
>
<template slot="fileHtml">
<div class="import-file-box">
<el-button size="small" type="primary" plain class="select-file-btn" :disabled="importing">
选择文件
<input
:key="fileInputKey"
ref="fileInput"
type="file"
class="file-input"
:accept="fileRule.accept"
@change="handleFileChange"
/>
</el-button>
<div v-if="form.fileName" class="file-name-box">
<i class="el-icon-document"></i>
<span class="file-name">{{ form.fileName }}</span>
<i v-if="!importing" class="el-icon-close file-remove" @click="removeFile"></i>
</div>
<div class="file-tip">支持 xls、xlsx,文件不超过10M,最多上传1个。</div>
<div v-if="importing || progress > 0" class="progress-text">
导入进度:{{ progress }}%
</div>
<div v-if="failUrl" class="fail-file">
导入失败,
<el-button type="text" @click="handleDownloadFailFile">下载失败文件</el-button>
</div>
</div>
</template>
</glsx-combi-form>
</d2-dialog>
</template>
<script>
import { downLoadFile } from "@/common/util"
import { readFileAsDataURL } from "@/libs/file"
import { startPolling } from "@/libs/interval"
import { getImportExcelProcess, importReconciliationBill } from "@api/accountManage"
import {
IMPORT_BILL_FILE_RULE,
buildImportBillFormData,
buildImportBillPayload,
getImportPollStatus,
getImportUuid,
normalizeImportPollResult
} from "../accountStateManage/importBill"
export default {
name: "ImportBillDialog",
props: {
importBillVisible: {
type: Boolean,
default: false
}
},
data() {
return {
isBlock: true,
importChildRef: "importChildRef",
fileRule: IMPORT_BILL_FILE_RULE,
fileInputKey: Date.now(),
form: {
billType: "",
fileName: "",
file: null
},
formData: buildImportBillFormData(),
formRules: {
billType: [{ required: true, message: "请选择对账单类型", trigger: "change" }],
fileName: [{ required: true, message: "请选择需要导入的对账单文件", trigger: "change" }]
},
importing: false,
polling: null,
progress: 0,
failUrl: ""
}
},
beforeDestroy() {
this.stopPolling()
},
methods: {
handleClose() {
if (this.importing) {
this.$message.warning("正在导入中,请稍后")
return
}
this.stopPolling()
this.$emit("dialogClose")
},
handleFileChange(event) {
const file = event.target.files && event.target.files[0]
this.resetFileInput()
if (!file || !this.validateFile(file)) {
return
}
this.failUrl = ""
this.progress = 0
this.form.file = file
this.form.fileName = file.name
this.validateField("fileName")
},
removeFile() {
this.form.file = null
this.form.fileName = ""
this.validateField("fileName")
},
validateFile(file) {
const fileName = file.name.toLowerCase()
const validType = this.fileRule.extensions.some(ext => fileName.endsWith(ext))
if (!validType) {
this.$message.error("仅支持上传 xls、xlsx 文件")
return false
}
if (file.size / 1024 / 1024 > this.fileRule.maxSizeMb) {
this.$message.error(`文件大小不能超过${this.fileRule.maxSizeMb}M`)
return false
}
return true
},
handleConfirm() {
if (this.importing) {
return
}
this.$refs.importFormRef.$refs[this.importChildRef].validate(async valid => {
if (!valid) {
return
}
await this.submitImport()
})
},
async submitImport() {
try {
this.importing = true
this.failUrl = ""
this.progress = 0
// 直接转 base64 后提交给导入接口。
const event = await readFileAsDataURL(this.form.file)
const base64 = event?.target?.result?.split(",")?.[1] || ""
const payload = buildImportBillPayload({
billType: this.form.billType,
fileName: this.form.fileName,
base64
})
const result = await importReconciliationBill(payload)
const uuid = getImportUuid(result)
if (!uuid) {
this.$message.error("导入任务创建失败")
this.importing = false
return
}
this.startImportPolling(uuid)
} catch (error) {
this.importing = false
}
},
startImportPolling(uuid) {
this.stopPolling()
const requestFn = () => getImportExcelProcess({ fileUuidList: [uuid] })
const stopCondition = result => {
// 查询结果可能是数组或对象,先统一格式再判断进度。
const pollResult = normalizeImportPollResult(result)
this.progress = pollResult.progress || 0
const status = getImportPollStatus(pollResult)
if (status === "success") {
this.importing = false
this.$message.success("导入成功")
this.$emit("handleConfirm")
return true
}
if (status === "failed") {
this.importing = false
this.failUrl = pollResult.url || ""
this.$message.error("导入失败,请查看失败文件")
return true
}
return false
}
this.polling = startPolling(requestFn, 1000, stopCondition)
},
stopPolling() {
if (this.polling) {
this.polling()
this.polling = null
}
},
resetFileInput() {
this.fileInputKey = Date.now()
},
validateField(field) {
this.$nextTick(() => {
this.$refs.importFormRef?.$refs?.[this.importChildRef]?.validateField(field)
})
},
handleDownloadFailFile() {
if (this.failUrl) {
downLoadFile(this.failUrl)
}
}
}
}
</script>
<style scoped>
.import-bill-dialog .import-file-box {
line-height: 1;
}
.select-file-btn {
position: relative;
overflow: hidden;
}
.file-input {
position: absolute;
top: 0;
right: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
.file-name-box {
display: inline-flex;
align-items: center;
max-width: 560px;
margin-left: 10px;
color: #606266;
vertical-align: middle;
}
.file-name {
max-width: 240px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
margin: 0 6px;
}
.file-remove {
cursor: pointer;
}
.file-tip,
.progress-text,
.fail-file {
margin-top: 10px;
color: #909399;
line-height: 18px;
}
.fail-file {
color: #f56c6c;
}
</style>
import {
IMPORT_BILL_FILE_RULE,
IMPORT_BILL_TYPE_OPTIONS,
buildImportBillFormData,
buildImportBillPayload,
getImportPollStatus
} from "@/views/financeManage/accountStateManage/importBill"
describe("importBill helpers", () => {
it("limits import file to one Excel file no larger than 10M", () => {
expect(IMPORT_BILL_FILE_RULE.extensions).toEqual([".xls", ".xlsx"])
expect(IMPORT_BILL_FILE_RULE.accept).toBe(".xls,.xlsx")
expect(IMPORT_BILL_FILE_RULE.maxSizeMb).toBe(10)
expect(IMPORT_BILL_FILE_RULE.maxCount).toBe(1)
})
it("keeps bill type options fixed", () => {
expect(IMPORT_BILL_TYPE_OPTIONS).toEqual([
{ value: "1", label: "流量方对账单" },
{ value: "2", label: "服务商对账单" }
])
})
it("builds glsx-combi-form data with bill type before file", () => {
const names = buildImportBillFormData().map(item => item.name)
expect(names).toEqual(["billType", "fileName"])
})
it("builds import payload as extension at base64", () => {
const payload = buildImportBillPayload({
billType: "1",
fileName: "demo.xlsx",
base64: "AAA111"
})
expect(payload).toEqual({
billType: "1",
file: "xlsx@AAA111"
})
})
it("normalizes poll status", () => {
expect(getImportPollStatus({ progress: 50, failNum: 0 })).toBe("processing")
expect(getImportPollStatus({ progress: 100, failNum: 0 })).toBe("success")
expect(getImportPollStatus({ progress: 100, failNum: 2 })).toBe("failed")
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!