这一段时间做小程序项目,使用的是mpvue的框架,需要自己实现验证码输入,模拟input的光标,上一个框输入后后一个框自动获取焦点,删除时从后往前依次删除。下图是整体效果:

 <template>
<div class="validate-code">
<h3>验证码已发送至</h3>
<div class="middle">
<div class="tel">{{telPhone}}</div>
<div class="right">
<div class="timer" v-if="timer">({{count}}s)</div>
<div class="txt-btn" v-else @click="getCode">重新获取验证码</div>
</div>
</div>
<div class="code-wrap">
<input type="number"
placeholder="输入短信验证码"
:maxlength="6"
placeholder-style="color: #ccc;"
@focus="handleFocus"
@blur="handleBlur"
v-model="validateCode">
<div class="front-wrap" @click="getFocus">
<div class="block">
<i :class="{'active': validateCode.length === 0 && hasFocused}"></i>
{{validateArray[0]}}
</div>
<div class="block">
<i :class="{'active': validateCode.length === 1 && hasFocused}"></i>
{{validateArray[1]}}
</div>
<div class="block">
<i :class="{'active': validateCode.length === 2 && hasFocused}"></i>
{{validateArray[2]}}
</div>
<div class="block">
<i :class="{'active': validateCode.length === 3 && hasFocused}"></i>
{{validateArray[3]}}
</div>
<div class="block">
<i :class="{'active': validateCode.length === 4 && hasFocused}"></i>
{{validateArray[4]}}
</div>
<div class="block">
<i :class="{'active': validateCode.length === 5 && hasFocused}"></i>
{{validateArray[5]}}
</div>
</div>
<div class="tips" v-if="errMsg">{{errMsg}}</div>
</div>
<div class="btn" :class="{'effective': validateCode.length === 6}" @click="bindPhone">登录</div>
</div>
</template> <script>
import fly from '@/http/config' export default {
components: {},
data () {
return {
telPhone: '',
validateCode: '',
errMsg: '',
count: 60,
timer: null,
hasFocused: false
}
},
computed: {
validateArray () {
return Array.from(this.validateCode);
}
},
onShow () {
this.errMsg = '';
this.validateCode = '';
},
onReady () {
this.telPhone = this.$root.$mp.query.telPhone;
this.initTimer();
},
created () {
//
},
methods: {
handleFocus () {
this.hasFocused = true;
this.errMsg = '';
},
handleBlur() {
this.hasFocused = false;
}, getCode () {
if (!this.timer) {
this.getBindingVerifyCode()
}
},
getBindingVerifyCode () {
let _this = this
fly.get('/2c/*********/*******', {
phoneNum: this.telPhone
}).then((data) => {
wx.showToast({
title: data.message
})
_this.initTimer()
}, err => {
console.log(err)
})
},
bindPhone () {
let _this = this;
if (this.validateArray.length < 6) {
return
}
fly.get('/2c/*****/*******', {
phoneNum: this.telPhone,
verifyCode: this.validateCode
}).then((data) => {
wx.showToast({
title: data.message
})
wx.setStorage({
key: 'phoneNum',
data: _this.telPhone
});
setTimeout(() => {
wx.reLaunch({
url: '/pages/index/main'
});
}, 1000);
}, err => {
console.log(err);
_this.errMsg = err.message;
wx.showToast({
icon: 'none',
title: err.message
});
})
},
initTimer () {
let _this = this
this.timer = setInterval(() => {
if (_this.count <= 0) {
_this.count = 60
clearInterval(_this.timer)
_this.timer = null
}
_this.$set({
'count': _this.count
})
_this.count--
}, 1000)
}
}
}
</script> <style lang="scss" src="./index.scss"> </style>

CSS文件

 .profile{
width: 100%;
height: 100%;
background: #f6f6f6;
.get-user-info {
width: 200px;
height: 50px;
}
.herder{
padding: 25px 10px;
background: #f25252;
display: flex;
justify-content: flex-start;
align-items: center;
.avatar-wrap{
width: 60px;
height: 60px;
overflow: hidden;
border-radius: 50%;
img{
width: 60px;
height: 60px;
}
}
.user-info{
color: #fff;
margin-left: 10px;
display: flex;
flex-direction: column;
justify-content: center;
h5{
font-size: 16px;
line-height: 16px;
margin-bottom: 10px;
}
p{
text-indent: 13px;
font-size: 14px;
line-height: 14px;
background: url("../../assets/images/profile/icon_phone.png") 0 50%/9px 14px no-repeat;
}
}
}
.content{
.main{
margin: 10px 0;
.content-item{
height: 50px;
padding: 0 10px;
display: flex;
background: #fff;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid #f6f6f6;
.left{
display: flex;
align-items: center;
img{
width: 15px;
height: 15px;
margin-right: 9px;
}
.title{
font-size: 15px;
color: #333;
}
}
.arrow{
display: flex;
justify-content: center;
align-items: center;
.phone{
font-size: 15px;
color: #399fda;
margin-right: 8px;
}
img{
width: 6px;
height: 9px;
}
}
}
}
.card-wrap{
background: #fff;
padding: 12px 10px;
.card{
height: 50px;
border-radius: 4px;
background: #ffdc69;
display: flex;
justify-content: space-between;
align-items: center;
position: relative;
.left{
span{
font-size: 15px;
color: #884600;
line-height: 50px;
}
img{
width: 34px;
height: 31px;
margin: 0 5px 0 10px;
vertical-align: middle;
}
}
.arrow{
display: flex;
justify-content: flex-start;
align-items: center;
img{
width: 4px;
height: 6px;
margin: 0 10px 0 3px;
}
span{
font-size: 13px;
color: #884600;
}
}
.icon-fixed{
position: absolute;
top:;
right:;
width: 27px;
height: 27px;
}
}
}
}
}

微信小程序-----自定义验证码实现的更多相关文章

  1. 微信小程序——自定义导航栏

    微信头部导航栏可能通过json配置: 但是有时候我们项目需求可能需要自定义头部导航栏,如下图所示: 现在具体说一下实现步骤及方法: 步骤: 1.在 app.json 里面把 "navigat ...

  2. 微信小程序自定义弹窗wcPop插件|仿微信弹窗样式

    微信小程序自定义组件弹窗wcPop|小程序消息提示框|toast自定义模板弹窗 平时在开发小程序的时候,弹窗应用场景还是蛮广泛的,但是微信官方提供的弹窗比较有局限性,不能自定义修改.这个时候首先想到的 ...

  3. 微信小程序自定义 tabbar

    一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件. 查阅了海量的博客和文档之后,亲自踩坑.总结了三种在不使用微信小程序原生 tab ...

  4. 微信小程序-自定义底部导航

    代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  5. 微信小程序自定义tabbar的实现

    微信小程序自定义tabbar的实现 目的:当采用微信的自定义tabbar组件的时候,切换的时候会出现闪屏的效果:当使用微信默认的tabbar的时候,限制了tabbar的数量以及灵活配置. 方案:自己动 ...

  6. 微信小程序 自定义导航组件 nav头部 全面屏设计

    nav-dynamic 微信小程序自定义nav头部组件:适配全面屏设计: 实现功能 初始进入页面时,展示初始状态下的nav样式: 页面滚动时,监听页面滚动事件,展示滚动状态下的nav样式: 根据配置字 ...

  7. 微信小程序自定义组件,提示组件

    微信小程序自定义组件,这里列举了一个常用的提示自定义组件,调用自定义组件中的方法和字段.仅供参考和学习. 编写组件: 在根目录下添加“components”目录,然后像添加Page页面一样添加自定义组 ...

  8. 微信小程序自定义Tabber,附详细源码

    目录 1,前言 2,说明 3,核心代码 1,前言 分享一个完整的微信小程序自定义Tabber,tabber按钮可以设置为跳转页面,也可以设置为功能按钮.懒得看文字的可以直接去底部,博主分享了小程序代码 ...

  9. 微信小程序自定义导航栏

    微信小程序需要自定义导航栏,特别是左上角的自定义设置,可以设置返回按钮,菜单按钮,配置如下: 1.在app.json的window属性中增加: navigationStyle:custom 顶部导航栏 ...

随机推荐

  1. JBDC—③数据库连接池的介绍、使用和配置

    首先要知道数据库连接(Connection对象)的创建和关闭是非常浪费系统资源的,如果是使用常规的数据库连接方式来操作数据库,当用户变多时,每次访问数据库都要创建大量的Connnection对象,使用 ...

  2. [JLOI2014]松鼠的新家(线段树,树链剖分)

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...

  3. 【PTA 天梯赛】L2-028 秀恩爱分得快(模拟)

    古人云:秀恩爱,分得快. 互联网上每天都有大量人发布大量照片,我们通过分析这些照片,可以分析人与人之间的亲密度.如果一张照片上出现了 K 个人,这些人两两间的亲密度就被定义为 1/K.任意两个人如果同 ...

  4. ajax与jsonp定义及使用方法

    ajax 定义 ajax技术的目的是让javascript发送http请求,与后台通信,获取数据和信息. ajax通信的过程不会影响后续javascript的执行,从而实现异步. 同步和异步 现实生活 ...

  5. CSDN强制登录的解决办法

    这个网站的吃相越来越恶心,最近使用发现他竟然强制登录了,这样的网站我是不可能登录的,要一大堆身份信息,但是,某些时候有需要看一些别人的文章怎么办呢, ​ 似乎节操与便利必须选一个,还好CSDN这样的网 ...

  6. python多进程详解和协程

    1.由于python多线程适合于多IO操作,但不适合于cpu计算型工作,这时候可以通过多进程实现.python多进程简单实用 # 多进程,可以cpu保持一致,python多线程适合多io.对于高cpu ...

  7. 搭建iSCSI共享IPSAN

    iSCSI(internet SCSI)是一个供硬件设备使用的.可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行SCSI协议,使其能够在诸如高速千兆以太网上进行路由选择. ...

  8. 用k8s构建生产环境下应用服务

    1.生成镜像 见https://www.cnblogs.com/mushou/p/9713741.html,把测试成熟的应用添加到tomcat镜像生成新的镜像,用ansible部署到集群的几点服务器中 ...

  9. Mybash实现

    Mybash实现 知识储备: feof是C语言标准库函数,其原型在stdio.h中,其功能是检测流上的文件结束符,如果文件结束,则返回非0值,否则返回0,文件结束符只能被clearerr()清除. 创 ...

  10. # 20155224 2016-2017-2《Java程序设计》课程总结

    20155224 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:我所期望的师生关系 预备作业2:我的技能与C语言学习 预备作业3:Linux的初步学习, ...