hn_cloud_web/smz-web/js/work/smallSubManagement/priceAdjustForm.js

177 lines
5.7 KiB
JavaScript
Raw Normal View History

2025-11-27 16:55:35 +08:00
var example = null;
var pers = null;
var form,laydate = null;
var lumpSumContractId = "";
function priceAdjustDetail(id){
lumpSumContractId = id;
layui.use(['layer', 'laydate', 'form'], function () {
form = layui.form;
form.render();
form.verify();
pers = checkPermission();
laydate = layui.laydate;
form.verify({
contractMoneyCheck: function (value, item) {
if (!value) return '请输入暂定协议价';
// 只允许数字和小数点,最多两位小数
if (!/^\d+(\.\d{1,2})?$/.test(value)) {
return '请输入正确的小写金额,最多两位小数';
}
// 金额范围限制(可选)
if (parseFloat(value) <= 0) {
return '金额必须大于0';
}
},
});
init(id);
form.on('submit(formDemo)', function (data) {
var contractMoney = $("#contractMoney").val();
var contractMoneyMax = convertCurrency(contractMoney);
// 构造最终对象
const submitData = {
id: lumpSumContractId, // 转换为整数
type: "4",
contractMoney: contractMoney,
contractMoneyMax: contractMoneyMax
};
// 提交到后端
$.ajax({
2025-12-09 18:44:29 +08:00
url: smz_ht_url + "/teamSmallBagDryTreaty/operContract", // 替换为你的实际接口
2025-11-27 16:55:35 +08:00
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(submitData),
success: function (res) {
if (res.res === 1) {
layer.msg("保存成功", { icon: 1, time: 2000 });
setTimeout("reloading()", 2001);
} else {
layer.msg(res.resMsg || "保存失败", { icon: 2, time: 2000 });
}
},
error: function (xhr, status, error) {
console.error("保存失败:", error);
layer.msg("网络错误,请重试", { icon: 5, time: 2000 });
}
});
return false; // 阻止表单刷新页面
});
});
}
function init(id) {
$.ajax({
type: 'get',
contentType: "application/x-www-form-urlencoded",
2025-12-09 18:44:29 +08:00
url: smz_ht_url + '/teamSmallBagDryTreaty/getDataDetail',
2025-11-27 16:55:35 +08:00
data: {"contractId":id},
dataType: 'json',
success: function (data) {
if (data.res === 1){
var contractMoney = data.obj.contractMoney;
$("#contractMoney").val(contractMoney);
layui.form.render();
}else{
layer.msg("未查询到", { icon: 2, time: 2000 });
}
},
error: function (xhr, status, error) {
console.error("请求失败:", error);
layer.msg("网络异常,请重试", { icon: 5, time: 2000 });
}
})
}
function convertCurrency(money) {
if (money === undefined || money === null || money === '') return '';
money = money.toString().trim();
if (!/^[\d]+(\.[\d]{0,2})?$/.test(money)) return '';
const cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
const cnUnits1 = ['', '拾', '佰', '仟'];
const cnUnits2 = ['', '万', '亿'];
let integerPart, decimalPart;
// 分割整数与小数部分
if (money.includes('.')) {
[integerPart, decimalPart] = money.split('.');
} else {
integerPart = money;
decimalPart = '00';
}
decimalPart = (decimalPart + '00').substring(0, 2);
// 补前导0至长度为4的倍数
while (integerPart.length % 4 !== 0) {
integerPart = '0' + integerPart;
}
let result = '';
let zeroFlag = false; // 是否需要加“零”
// 每4位一组处理
for (let i = 0; i < integerPart.length; i += 4) {
const section = integerPart.substr(i, 4);
let sectionResult = '';
let hasNonZero = false;
for (let j = 0; j < section.length; j++) {
const digit = parseInt(section[j], 10);
const unit = cnUnits1[section.length - j - 1];
if (digit !== 0) {
if (zeroFlag || (sectionResult === '' && result !== '')) {
sectionResult += '零';
}
sectionResult += cnNums[digit] + unit;
hasNonZero = true;
zeroFlag = false;
} else if (hasNonZero) {
zeroFlag = true;
}
}
// 添加亿/万单位
if (hasNonZero) {
const unitIndex = Math.floor((integerPart.length - i - 1) / 4);
result += sectionResult + cnUnits2[unitIndex];
zeroFlag = true; // 下一段可能需要加“零”
}
}
// 去除开头的“零”(仅当不是唯一字符时)
if (result.startsWith('零') && result.length > 1) {
result = result.substring(1);
}
// 全部为零的情况
if (result === '') {
result = '零';
}
// 添加“元”
result += '元';
// 处理角分
const jiao = decimalPart[0];
const fen = decimalPart[1];
if (jiao !== '0' || fen !== '0') {
result += cnNums[jiao] + '角' + cnNums[fen] + '分';
} else {
result += '整';
}
console.log("中文大写",result);
return result;
}
function reloading() {
var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
parent.layer.close(index); //再执行关闭
window.parent.location.reload();
}