微信小程序-----自定义验证码实现
这一段时间做小程序项目,使用的是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;
}
}
}
}
}
微信小程序-----自定义验证码实现的更多相关文章
- 微信小程序——自定义导航栏
微信头部导航栏可能通过json配置: 但是有时候我们项目需求可能需要自定义头部导航栏,如下图所示: 现在具体说一下实现步骤及方法: 步骤: 1.在 app.json 里面把 "navigat ...
- 微信小程序自定义弹窗wcPop插件|仿微信弹窗样式
微信小程序自定义组件弹窗wcPop|小程序消息提示框|toast自定义模板弹窗 平时在开发小程序的时候,弹窗应用场景还是蛮广泛的,但是微信官方提供的弹窗比较有局限性,不能自定义修改.这个时候首先想到的 ...
- 微信小程序自定义 tabbar
一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件. 查阅了海量的博客和文档之后,亲自踩坑.总结了三种在不使用微信小程序原生 tab ...
- 微信小程序-自定义底部导航
代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- 微信小程序自定义tabbar的实现
微信小程序自定义tabbar的实现 目的:当采用微信的自定义tabbar组件的时候,切换的时候会出现闪屏的效果:当使用微信默认的tabbar的时候,限制了tabbar的数量以及灵活配置. 方案:自己动 ...
- 微信小程序 自定义导航组件 nav头部 全面屏设计
nav-dynamic 微信小程序自定义nav头部组件:适配全面屏设计: 实现功能 初始进入页面时,展示初始状态下的nav样式: 页面滚动时,监听页面滚动事件,展示滚动状态下的nav样式: 根据配置字 ...
- 微信小程序自定义组件,提示组件
微信小程序自定义组件,这里列举了一个常用的提示自定义组件,调用自定义组件中的方法和字段.仅供参考和学习. 编写组件: 在根目录下添加“components”目录,然后像添加Page页面一样添加自定义组 ...
- 微信小程序自定义Tabber,附详细源码
目录 1,前言 2,说明 3,核心代码 1,前言 分享一个完整的微信小程序自定义Tabber,tabber按钮可以设置为跳转页面,也可以设置为功能按钮.懒得看文字的可以直接去底部,博主分享了小程序代码 ...
- 微信小程序自定义导航栏
微信小程序需要自定义导航栏,特别是左上角的自定义设置,可以设置返回按钮,菜单按钮,配置如下: 1.在app.json的window属性中增加: navigationStyle:custom 顶部导航栏 ...
随机推荐
- PLSQL数据库无选项问题解决
添加如下ORACLE_HOME和OCI library 添加之后,关闭plsql再打开会出现如下界面: 其中database为空. 我当时也是在环境变量里面配置了TNS_ADMIN的环境变量为:如下 ...
- mongodb分组函数的使用(spring-data-mongodb)
这两天要做mongodb日志的模块,下面记录一下. 一. 首先要导入一批数据,使用springboot来完成. 配置mongodb的复制集:在application.yml文件中配置uri来完成 格式 ...
- 【最新最全】为 iOS 和 Android 的真机和模拟器编译 Luajit 库
编译 Luajit 库,的确是一个挑战.因为官网的教程,在当前版本的 Xcode 和 NDK 环境中,已经不适用了.以前只是编译了适用于真机的 Luajit 库.最近在尝试编译模拟器 Luajit 库 ...
- Java并发编程(八)线程间协作(上)
多线程并发执行时,不同的线程执行的内容之间可能存在一些依赖关系,比如线程一执行a()方法和c()方法,线程二执行b()方法,方法a()必须在方法b()之前执行,而方法c()必须在方法b()之后执行.这 ...
- 洛谷P3690 【模板】Link Cut Tree (LCT)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- C++练习 | 运算符重载练习
#include <iostream> #include <cmath> #include <cstring> #include <iomanip> u ...
- 含头结点的单链表C++实现(包含创建,查找,插入,追加,删除,反转,排序,合并,打印,清空,销毁等基本操作)
温馨提示:下面代码默认链表数据为字符型,本代码仅供参考,希望能对找到本随笔的人有所帮助! #include<iostream> using namespace std; typedef s ...
- HTML5常用标签及特殊字符表
*http://html5doctor.com/nav*http://html5doctor.com/article*http://html5doctor.com/section*http://htm ...
- Linux下更新git版本
查看git版本,卸载旧版本(如果没有安装git请直接到下一步) git --version yum remove git 安装依赖软件 yum install curl-devel expat-dev ...
- STM32串口一直进中断
调试过程中遇到了使用串口什么都没接却一直进中断,接串口线到电脑上测试又正常的问题. 网上有人说需要将USART的RX模式从输入浮空改成输入上拉,改后测试正常,问题解决. 分析可能是什么都不接时浮空模式 ...