微信小程序-----自定义验证码实现
这一段时间做小程序项目,使用的是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 顶部导航栏 ...
随机推荐
- oo第二次总结作业
OO电梯作业总结 这三周的作业和课堂内容以及OS的课上内容都相同,都是关于多线程方面的知识.在这次作业中由浅入深布置了三项多线程电梯方面的作业,让我们在实践中感受了多线程的工作原理以及各项需要注意的要 ...
- Django实现支付宝支付(沙箱)
1.安装SDK 点击右侧沙箱当面付接入指导,之后可以看到一个下载SDK的按钮,点击后,会有python的SDK下载链接,但还是属于公测中,也可以通过在cmd里输入以下代码来安装. pip instal ...
- 第三月 day03.笔记
函数在调用的时候回形成一个私有作用域,内部变量不会被外面访问,这种保护机制叫做闭包,这就意味着函数调用完了,这个函数形成的栈内存就会被销毁,但有时候我们不希望被销毁. * 函数归属谁和他的调用没有关系 ...
- node.js的http模块创建基本Web服务器
首先下载node.js模块.终端执行命令 npm i node -g 引入http核心模块 const http =require('http') 引入文件系统模块 const fs =require ...
- 在mac上使用tar.gz安装mysql
官方: download: https://dev.mysql.com/downloads/mysql/ mysql参考文档:https://dev.mysql.com/doc/ 环境: macOS ...
- 课时9.HTML发展史(了解)
这个图片里的时间不用都记住,只需要记住一些特殊的,1993年,1995年(在W3C接手以后,才有了真正意义上的标准),1999年这几个时间 WHATWG的目的是推广HTML的标准,HTML5是浏览器厂 ...
- 【Memcached】原理、体系架构、基本操作及路由算法
1. 什么是Memcached 要了解Memcached首先要到官网上去看官方对它的描述.Memcached的官网网站是:http://memcached.org/,官方对Memcached的描述如下 ...
- 关于flume的filechannel的 full 问题
事务启动以后,批量向事务Transaction的一个putList的尾部写入,putlist是一个LinkedBlockingDeque . 事务提交的时候, 把putlist中的event批量移除, ...
- 如何通过审计安全事件日志检测密码喷洒(Password Spraying)攻击
许多渗透测试人员和攻击者通常都会使用一种被称为“密码喷洒(Password Spraying)”的技术来进行测试和攻击.对密码进行喷洒式的攻击,这个叫法很形象,因为它属于自动化密码猜测的一种.这种针对 ...
- 20155220 实验三 敏捷开发与XP实践 实验报告
20155220 实验三 敏捷开发与XP实践 实验报告 实验内容 XP基础 XP核心实践 相关工具 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim ...