uni-app 自定义支付密码键盘

1.新建最贱 payKeyboard .vue

javascript 复制代码
<template>
	<view class="page-total" v-show="isShow">
		<view class="key-list">
			<view class="list" v-for="(item,index) in keyList" 
			:class="{'special':item.keyCode==190||item.keyCode==8,'payMoney':item.keyCode==88}"
			@click="onKeyList(item,index)"
			:key="item.keyCode">
				<text>{{item.key}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isShow: false,
				keyList: [
					{
						key: 1,
						en: '',
						keyCode: 49,
					},{
						key: 2,
						en: 'ABC',
						keyCode: 50,
					},{
						key: 3,
						en: 'ABC',
						keyCode: 51,
					},{
						key: 4,
						en: 'ABC',
						keyCode: 52,
					},{
						key: 5,
						en: 'ABC',
						keyCode: 53,
					},
					
					{
						key: 6,
						en: 'ABC',
						keyCode: 54,
					},{
						key: 7,
						en: 'ABC',
						keyCode: 55,
					},{
						key: 8,
						en: 'ABC',
						keyCode: 56,
					},
					{
						key: '删除',
						en: 'Del',
						keyCode: 8,
					},
					{
						key: 9,
						en: 'ABC',
						keyCode: 57,
					},{
						key: 0,
						en: 'ABC',
						keyCode: 48,
					},{
						key: '确定',
						en: 'Pay',
						keyCode: 88,
					},
				],
				keyIndex: -1,
			};
		},
		props:{
			passwrdType: {
				type: String,
				default: 'pay'
			}
		},
		methods:{
			show(){
				this.isShow = true;
			},
			hide(){
				this.isShow = false;
			},
			/**
			 * 密码键盘按下
			 * @param {Object} item
			 * @param {Number} index
			 */
			onKeyList(item,index){
				let KeyInfo = item;	
				// 删除键
				if(KeyInfo.keyCode === 8 && this.keyIndex > -1){
					this.keyIndex--;
				}
				// 不是删除键
				if(KeyInfo.keyCode != 8){
					if(this.passwrdType == 'pay' && this.keyIndex >= 5){
						console.log('键盘');
						this.keyIndex = -1;
						return;
					}else{
						this.keyIndex = -1;
					}
					this.keyIndex++;
				}
				KeyInfo.index = this.keyIndex;
				this.$emit('KeyInfo',KeyInfo);
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'cc-payKeyboard.scss';
</style>

2.引用

<PayKeyboard ref="codeKeyboard" passwrdType="pay" @KeyInfo="KeyInfo">

javascript 复制代码
import PayKeyboard  from "@/components/payKeyboard.vue"

<view>
<view>
<text v-show="AffirmStatus === 1">请设置6位支付密码</text>
<text v-show="AffirmStatus >= 2">请确认6位支付密码</text>
</view>
<view class="pay-password" @click="onPayUp" >
			<view class="list">
				<text v-show="passwordArr.length >= 1">●</text>
			</view>
			<view class="list">
				<text v-show="passwordArr.length >= 2">●</text>
			</view>
			<view class="list">
				<text v-show="passwordArr.length >= 3">●</text>
			</view>
			<view class="list">
				<text v-show="passwordArr.length >= 4">●</text>
			</view>
			<view class="list">
				<text v-show="passwordArr.length >= 5">●</text>
			</view>
			<view class="list">
				<text v-show="passwordArr.length >= 6">●</text>
			</view>
		</view>
	</view>


const codeKeyboard = ref(null)
	const passwordArr = ref([])
	const newPasswordArr = ref("")
	const AffirmStatus = ref("")
	const passwordArr = ref([])
//支付密码
	function onPayUp() {
		codeKeyboard.value.show();
	}
	/**
	 * 支付键盘回调
	 * @param {Object} val
	 */
	function KeyInfo(val) {
		if (val.index >= 6) {
			return;
		}
		// 判断是否输入的是删除键
		if (val.keyCode === 8) {
			// 删除最后一位
			passwordArr.value.splice(val.index + 1, 1)
		}
		// 判断是否输入的是.
		else if (val.keyCode == 190) {
			// 输入.无效
		} else {
			if(passwordArr.value.length<=6){
				passwordArr.value.push(val.key);
			}
		}
		if (val.keyCode == 88) {
			let pass = parseInt(passwordArr.value.join(""));
			console.log(pass)
			
		}
		// 判断是否等于6
	if (this.passwordArr.length === 6) {
		if (AffirmStatus.value === 1) {

			setTimeout(() => {
				passwordArr.value = [];
				AffirmStatus.value = AffirmStatus.value + 1;
				codeKeyboard..value.hide()
			}, 500)
		}
	}
	// 判断到哪一步了
	if (AffirmStatus.value === 1) {
		oldPasswordArr.value = parseInt(passwordArr.value.join(""));
	} else if (AffirmStatus.value >= 2) {
		afPasswordArr.value = parseInt(passwordArr.value.join(""));
		if (this.passwordArr.length === 6) {
		}
	}


	this.$forceUpdate();
	}
相关推荐
2分钟速写快排2 小时前
孔乙己的茴字有多少种写法?|AI 对话实时通信的三种实现
前端·javascript·github
用户0044729538259 小时前
练歌镜:让眼镜听懂“循环这一句”
javascript
yuyuxun112 小时前
协同过滤的校园二手物品智能推荐系统的设计与实现 毕业设计-附源码40082
javascript·vue.js·spring boot·python·数据分析·django·课程设计
总要学点什么15 小时前
第105篇:三维特效-流动图片墙体效果——让箭头沿墙长滚,它俩竟是同一份代码
开发语言·javascript·ecmascript·gis·webgl·材质·cesium
Agent世界16 小时前
用 CDP 做发布自动化:四道机器闸门与产物验收
前端·javascript
总要学点什么16 小时前
第104篇:三维特效-流动墙体效果——让贴图沿墙流动,它却只肯走一条对角线
开发语言·javascript·ecmascript·gis·webgl·材质·cesium
程序员大阳17 小时前
副队长ES6+教程(3)--解构赋值
前端·javascript·es6·赋值·解构
Hello_Pyhx17 小时前
Hookshot v2.1.0:用键鼠玩《时之笛》,把操控和画面调顺手
计算机外设
神膘护体小月半17 小时前
css - 元素背景图由 hover 触发,初次加载时闪动的解决办法
前端·javascript·css
程序员大阳17 小时前
副队长ES6+教程(2)--let 与 const
前端·javascript·es6·const·let·var