let money = ''; export default { name: "keyBoard", props: { title: { default: "确认", type: String, }, hideInputNumber: { default: false, type: Boolean, }, btnColor: { default: "green", }, }, data() { return { money: "", Cdel: "", Time: "", }; }, watch: { money(val) { this.$emit("update:money", val) }, }, methods: { touchstart() { this.Time = setInterval(() => { if (money == "") { clearInterval(); } money = money.substring(0, money.length - 1); }, 200); }, touchend() { clearInterval(this.Time); }, //处理按键 _handleKeyPress(e) { let num = 0 // #ifdef MP-ALIPAY num = e.target.targetDataset.num; // #endif // #ifdef MP-WEIXIN || H5 num = e.target.dataset.num; // #endif //不同按键处理逻辑 // -1 代表无效按键,直接返回 if (num == -1) return false; switch (String(num)) { //小数点 case ".": this._handleDecimalPoint(); break; //删除键 case "D": this._handleDeleteKey(); break; //清空键 case "C": this._handleClearKey(); break; //确认键 case "S": this._handleConfirmKey(num); break; default: this._handleNumberKey(num); break; } if (String(num) != "S") { this.$emit("oninputBind", money); //提交参数 } }, //处理小数点函数 _handleDecimalPoint() { //如果包含小数点,直接返回 if (money.indexOf(".") > -1) return false; //如果小数点是第一位,补0 if (!money.length) money = "0."; //如果不是,添加一个小数点 else money = money + "."; }, //处理删除键 _handleDeleteKey() { let S = money; //如果没有输入,直接返回 if (!S.length) return false; //否则删除最后一个 money = S.substring(0, S.length - 1); }, //处理清空键 _handleClearKey() { money = ""; }, //处理数字 _handleNumberKey(num) { if (money && money.length == 6) { uni.showToast({ title: '最多6位数字', icon: 'none' }); return; } if (num == undefined || num == 'undefined' || num < 0) { return; } let S = money; //如果有小数点且小数点位数不小于2 if (S.indexOf(".") > -1 && S.substring(S.indexOf(".") + 1).length < 2) money = S + num; //没有小数点 if (!(S.indexOf(".") > -1)) { //如果第一位是0,只能输入小数点 if (num == 0 && S.length == 0) money = "0."; else { if (S.length && Number(S.charAt(0)) === 0) return; money = S + num; } } }, //提交正确的金额 _handleConfirmKey(S) { //未输入 if (!S.length || S == 0) { uni.showToast({ title: "请输入正确的数值", icon: "none", duration: 1000, }); return false; } //将 8. 这种转换成 8.00 if (S.indexOf(".") > -1 && S.indexOf(".") == S.length - 1) S = Number(S.substring(0, S.length - 1)).toFixed(2); //保留两位 S = Number(S).toFixed(2); //提交参数 this.$emit("confirmEvent", S); }, }, }