From f645540906e6e3dcd203da1419d35fa306bc6e11 Mon Sep 17 00:00:00 2001 From: lion <120344285@qq.com> Date: Mon, 8 Jun 2026 11:33:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=91=E9=A2=9D=E5=A4=A7=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/XyDialog/index.vue | 3 +- src/utils/index.js | 147 +++++---- .../contract/components/printPaymentForm.vue | 281 ++++++++++++++++-- src/views/contract/contractList.vue | 29 ++ src/views/finance/paymentRegistrationList.vue | 23 +- 5 files changed, 383 insertions(+), 100 deletions(-) diff --git a/src/components/XyDialog/index.vue b/src/components/XyDialog/index.vue index 6a40925..28adccd 100644 --- a/src/components/XyDialog/index.vue +++ b/src/components/XyDialog/index.vue @@ -160,10 +160,11 @@ export default { { if (type === 'form') return ($scopedSlots.footerContent ? $scopedSlots.footerContent() : footerRender()) if (type === 'normal') { + if ($scopedSlots.footerContent) return $scopedSlots.footerContent() return (
- +
) } diff --git a/src/utils/index.js b/src/utils/index.js index a4c843f..7114f75 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -227,85 +227,100 @@ export function buildTree(data, pid = 0) { return tree } +/** + * 将 0–9999 转为中文(含拾/佰/仟,不含万/亿) + */ +function convertSection(section) { + const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] + const units = ['', '拾', '佰', '仟'] + if (section === 0) return '' + let str = '' + let zeroFlag = false + let started = false + for (let i = 3; i >= 0; i--) { + const d = Math.floor(section / Math.pow(10, i)) % 10 + if (d === 0) { + if (started) zeroFlag = true + } else { + if (zeroFlag) { + str += '零' + zeroFlag = false + } + if (i === 1 && d === 1 && Math.floor(section / 100) === 0) { + str += units[i] + } else { + str += digits[d] + units[i] + } + started = true + } + } + return str +} + /** * 数字转中文大写金额 - * @param {number} num - 要转换的数字 + * @param {number|string} num - 要转换的数字 * @returns {string} 中文大写金额 */ export function numberToChinese(num) { - if (num === 0) { + const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] + const bigUnits = ['', '万', '亿', '兆'] + + const n = parseFloat(String(num).replace(/[¥¥\s,,]/g, '')) + if (!Number.isFinite(n) || n === 0) { return '零元整' } - const units = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万'] - const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] - const [integer, decimal] = num.toString().split('.') + const fixed = n.toFixed(2) + const [intStr, decStr] = fixed.split('.') + let intNum = parseInt(intStr, 10) + + const groups = [] + let temp = intNum + while (temp > 0) { + groups.push(temp % 10000) + temp = Math.floor(temp / 10000) + } + let result = '' - let intNum = parseInt(integer) - - if (intNum === 0) { - result = '零' - } else { - let i = 0 - let lastDigit = null - let hasZero = false - - while (intNum > 0) { - const digit = intNum % 10 - - if (digit === 0) { - // 只有当后面有非零数字时才添加"零" - if (!hasZero && lastDigit !== 0) { - result = '零' + result - hasZero = true - } - } else { - let unit = units[i] - - // 处理"拾"的特殊情况:当十位是1时,不写"壹拾",只写"拾" - if (i === 1 && digit === 1) { - result = unit + result - } else { - result = digits[digit] + unit + result - } - hasZero = false - } - - lastDigit = digit - intNum = Math.floor(intNum / 10) - i++ + let needZero = false + for (let i = 0; i < groups.length; i++) { + const section = groups[i] + if (section === 0) { + if (result) needZero = true + continue } - - // 清理末尾的零 - result = result.replace(/零+$/, '') - // 清理连续的零 - result = result.replace(/零+/g, '零') - } - - // 先加上"元"单位 - result += '元' - - // 处理小数部分 - if (decimal) { - const decimalNum = parseInt(decimal) - if (decimalNum > 0) { - const jiao = Math.floor(decimalNum / 10) - const fen = decimalNum % 10 - - if (jiao > 0) { - result += digits[jiao] + '角' - } - if (fen > 0) { - result += digits[fen] + '分' - } - return result + const sectionStr = convertSection(section) + let prefix = sectionStr + bigUnits[i] + if (needZero) { + result = prefix + '零' + result + needZero = false + } else { + const lower = groups.slice(0, i).reduce((sum, g, idx) => sum + g * Math.pow(10000, idx), 0) + const padZero = i > 0 && lower > 0 && section < 1000 + result = prefix + (padZero ? '零' : '') + result } } - - // 如果没有小数部分,添加"整" - if (!decimal || parseInt(decimal) === 0) { + + if (!result) { + result = '零' + } + result += '元' + + const jiao = parseInt(decStr[0] || '0', 10) + const fen = parseInt(decStr[1] || '0', 10) + if (jiao === 0 && fen === 0) { result += '整' + } else { + if (jiao > 0) { + result += digits[jiao] + '角' + } else if (fen > 0) { + result += '零' + } + if (fen > 0) { + result += digits[fen] + '分' + } } - + return result } \ No newline at end of file diff --git a/src/views/contract/components/printPaymentForm.vue b/src/views/contract/components/printPaymentForm.vue index e35ad45..08ed430 100644 --- a/src/views/contract/components/printPaymentForm.vue +++ b/src/views/contract/components/printPaymentForm.vue @@ -1,7 +1,13 @@