微信小程序-----自定义验证码实现

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

  1 <template>
  2   <div class="validate-code">
  3     <h3>验证码已发送至</h3>
  4     <div class="middle">
  5       <div class="tel">{{telPhone}}</div>
  6       <div class="right">
  7         <div class="timer" v-if="timer">({{count}}s)</div>
  8         <div class="txt-btn" v-else @click="getCode">重新获取验证码</div>
  9       </div>
 10     </div>
 11     <div class="code-wrap">
 12       <input type="number"
 13              placeholder="输入短信验证码"
 14              :maxlength="6"
 15              placeholder-style="color: #ccc;"
 16              @focus="handleFocus"
 17              @blur="handleBlur"
 18              v-model="validateCode">
 19       <div class="front-wrap" @click="getFocus">
 20         <div class="block">
 21           <i :class="{‘active‘: validateCode.length === 0 && hasFocused}"></i>
 22           {{validateArray[0]}}
 23         </div>
 24         <div class="block">
 25           <i :class="{‘active‘: validateCode.length === 1 && hasFocused}"></i>
 26           {{validateArray[1]}}
 27         </div>
 28         <div class="block">
 29           <i :class="{‘active‘: validateCode.length === 2 && hasFocused}"></i>
 30           {{validateArray[2]}}
 31         </div>
 32         <div class="block">
 33           <i :class="{‘active‘: validateCode.length === 3 && hasFocused}"></i>
 34           {{validateArray[3]}}
 35         </div>
 36         <div class="block">
 37           <i :class="{‘active‘: validateCode.length === 4 && hasFocused}"></i>
 38           {{validateArray[4]}}
 39         </div>
 40         <div class="block">
 41           <i :class="{‘active‘: validateCode.length === 5 && hasFocused}"></i>
 42           {{validateArray[5]}}
 43         </div>
 44       </div>
 45       <div class="tips" v-if="errMsg">{{errMsg}}</div>
 46     </div>
 47     <div class="btn" :class="{‘effective‘: validateCode.length === 6}" @click="bindPhone">登录</div>
 48   </div>
 49 </template>
 50
 51 <script>
 52   import fly from ‘@/http/config‘
 53
 54   export default {
 55     components: {},
 56     data () {
 57       return {
 58         telPhone: ‘‘,
 59         validateCode: ‘‘,
 60         errMsg: ‘‘,
 61         count: 60,
 62         timer: null,
 63         hasFocused: false
 64       }
 65     },
 66     computed: {
 67       validateArray () {
 68         return Array.from(this.validateCode);
 69       }
 70     },
 71     onShow () {
 72       this.errMsg = ‘‘;
 73       this.validateCode = ‘‘;
 74     },
 75     onReady () {
 76       this.telPhone = this.$root.$mp.query.telPhone;
 77       this.initTimer();
 78     },
 79     created () {
 80       //
 81     },
 82     methods: {
 83       handleFocus () {
 84         this.hasFocused = true;
 85         this.errMsg = ‘‘;
 86       },
 87       handleBlur() {
 88         this.hasFocused = false;
 89       },
 90
 91       getCode () {
 92         if (!this.timer) {
 93           this.getBindingVerifyCode()
 94         }
 95       },
 96       getBindingVerifyCode () {
 97         let _this = this
 98         fly.get(‘/2c/*********/*******‘, {
 99           phoneNum: this.telPhone
100         }).then((data) => {
101           wx.showToast({
102             title: data.message
103           })
104           _this.initTimer()
105         }, err => {
106           console.log(err)
107         })
108       },
109       bindPhone () {
110         let _this = this;
111         if (this.validateArray.length < 6) {
112           return
113         }
114         fly.get(‘/2c/*****/*******‘, {
115           phoneNum: this.telPhone,
116           verifyCode: this.validateCode
117         }).then((data) => {
118           wx.showToast({
119             title: data.message
120           })
121           wx.setStorage({
122             key: ‘phoneNum‘,
123             data: _this.telPhone
124           });
125           setTimeout(() => {
126             wx.reLaunch({
127               url: ‘/pages/index/main‘
128             });
129           }, 1000);
130         }, err => {
131           console.log(err);
132           _this.errMsg = err.message;
133           wx.showToast({
134             icon: ‘none‘,
135             title: err.message
136           });
137         })
138       },
139       initTimer () {
140         let _this = this
141         this.timer = setInterval(() => {
142           if (_this.count <= 0) {
143             _this.count = 60
144             clearInterval(_this.timer)
145             _this.timer = null
146           }
147           _this.$set({
148             ‘count‘: _this.count
149           })
150           _this.count--
151         }, 1000)
152       }
153     }
154   }
155 </script>
156
157
158 <style lang="scss" src="./index.scss">
159
160 </style>

CSS文件

  1 .profile{
  2   width: 100%;
  3   height: 100%;
  4   background: #f6f6f6;
  5   .get-user-info {
  6     width: 200px;
  7     height: 50px;
  8   }
  9   .herder{
 10     padding: 25px 10px;
 11     background: #f25252;
 12     display: flex;
 13     justify-content: flex-start;
 14     align-items: center;
 15     .avatar-wrap{
 16       width: 60px;
 17       height: 60px;
 18       overflow: hidden;
 19       border-radius: 50%;
 20       img{
 21         width: 60px;
 22         height: 60px;
 23       }
 24     }
 25     .user-info{
 26       color: #fff;
 27       margin-left: 10px;
 28       display: flex;
 29       flex-direction: column;
 30       justify-content: center;
 31       h5{
 32         font-size: 16px;
 33         line-height: 16px;
 34         margin-bottom: 10px;
 35       }
 36       p{
 37         text-indent: 13px;
 38         font-size: 14px;
 39         line-height: 14px;
 40         background: url("../../assets/images/profile/icon_phone.png") 0 50%/9px 14px no-repeat;
 41       }
 42     }
 43   }
 44   .content{
 45     .main{
 46       margin: 10px 0;
 47       .content-item{
 48         height: 50px;
 49         padding: 0 10px;
 50         display: flex;
 51         background: #fff;
 52         justify-content: space-between;
 53         align-items: center;
 54         border-bottom: 1px solid #f6f6f6;
 55         .left{
 56           display: flex;
 57           align-items: center;
 58           img{
 59             width: 15px;
 60             height: 15px;
 61             margin-right: 9px;
 62           }
 63           .title{
 64             font-size: 15px;
 65             color: #333;
 66           }
 67         }
 68         .arrow{
 69           display: flex;
 70           justify-content: center;
 71           align-items: center;
 72           .phone{
 73             font-size: 15px;
 74             color: #399fda;
 75             margin-right: 8px;
 76           }
 77           img{
 78             width: 6px;
 79             height: 9px;
 80           }
 81         }
 82       }
 83     }
 84     .card-wrap{
 85       background: #fff;
 86       padding: 12px 10px;
 87       .card{
 88         height: 50px;
 89         border-radius: 4px;
 90         background: #ffdc69;
 91         display: flex;
 92         justify-content: space-between;
 93         align-items: center;
 94         position: relative;
 95         .left{
 96           span{
 97             font-size: 15px;
 98             color: #884600;
 99             line-height: 50px;
100           }
101           img{
102             width: 34px;
103             height: 31px;
104             margin: 0 5px 0 10px;
105             vertical-align: middle;
106           }
107         }
108         .arrow{
109           display: flex;
110           justify-content: flex-start;
111           align-items: center;
112           img{
113             width: 4px;
114             height: 6px;
115             margin: 0 10px 0 3px;
116           }
117           span{
118             font-size: 13px;
119             color: #884600;
120           }
121         }
122         .icon-fixed{
123           position: absolute;
124           top: 0;
125           right: 0;
126           width: 27px;
127           height: 27px;
128         }
129       }
130     }
131   }
132 }

原文地址:https://www.cnblogs.com/cangqinglang/p/9314112.html

时间: 2024-08-29 18:25:41

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

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

代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html 1.基本需求. 实现用户自定义底部导航 2.案例目录结构 二.程序实现具体步骤 1.自定义nav.wxml代码 <template name="nav"> <view class=&quo

值得记录的(五)- 微信小程序自定义 tabbar

一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件. 查阅了海量的博客和文档之后,亲自踩坑.总结了三种在不使用微信小程序原生 tabbar的情况下自制 tabbar 的方法.并说说这几种方法各自的特色. 类 navigator 跳转方式 类 navigator 跳转方式是我自己起的名称,因为它的实现思路就是这个样子的.期初参考 微信小程序自定义tabBar组件开发 这篇博文的思路.进行了这种方式的尝试,并为后续提供了解决思路.在

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

微信小程序自定义组件弹窗wcPop|小程序消息提示框|toast自定义模板弹窗 平时在开发小程序的时候,弹窗应用场景还是蛮广泛的,但是微信官方提供的弹窗比较有局限性,不能自定义修改.这个时候首先想到的是自定义组件化开发,就是把弹出框封装成一个组件,然后多处调用. 解决了小程序开发自定义弹窗出现后,遮罩层下的页面仍可以滚动的方法: 给遮罩层的最外层view中加入catchtouchmove="preventTouchMove" 即可解决该遮罩层点透问题. 根据需要还可以自定义多个按钮及事

微信小程序自定义导航栏(wx_custom_navigation_bar) 自定义返回键、首页键,动态设置标题,响应式组件

微信小程序自定义导航栏 navigation bar 返回键 首页 github: https://github.com/chen-yt/wx_custom_navigation_bar https://github.com/Superman2113/wx_custom_navigation_bar 代码 navbar组件 navbar.wxml <view class="navbar" style="{{'height: ' + navigationBarHeight

微信小程序-自定义QQ版下拉刷新

最近给别个公司做技术支持,要实现微信小程序上拉刷新与下拉加载更多 微信给出的接口不怎么友好,最终想实现效果类似QQ手机版 ,一共3种下拉刷新状态变化,文字+图片+背景颜色 最终实现后的效果(这里提示有个不同点就是,自定义了导航条,并且下拉的时候,自定义导航条必须固定) 小程序实现下拉加载2种方式: 1. 简单粗暴,直接开启enablePullDownRefresh,开启全局下拉刷新 2.利用scroll-view组件 简单分析下2种方式的利与弊 enablePullDownRefresh方式 优

微信小程序发送验证码短信SDK及文档

微信小程序验证码版SDK增加了对短信验证码的管理,开发者无需生成管理验证码,该SDK可直接生成.存储.发送.校验. 官网 SDK下载地址 demo ? 1.安装下载后的SDK只包含一个zhenzisms.js文件,可直接放在utils文件夹中,然后通过require引入,如下: var zhenzisms = require('../../utils/zhenzisms.js');2.配置域名在微信公众平台-小程序管理中配置域名https://smsdeveloper.zhenzikj.com,

微信小程序 自定义标题栏

微信小程序可以设置自定义标题栏,可以针对不同页面单独设置1. 在页面文件 .json 文件中,设置如下属性,自定义导航栏 和 导航栏样式"navigationStyle": "custom","navigationBarTextStyle": "white", 特别是第二个 navigationBarTextStyle 属性 可以将胶囊样式调整为透明效果 2. 设置自定义导航栏布局 样式 动效逻辑等 3. 划重点!!!导航栏中

微信小程序自定义组件的使用以及调用自定义组件中的方法

在写小程序的时候,有时候页面的内容过多,逻辑比较复杂,如果全部都写在一个页面的话,会比较繁杂,代码可读性比较差,也不易于后期代码维护,这时候可以把里面某部分功能抽出来,单独封装为一个组件,也就是通常说的自定义组件,自定义组件类似于页面,它有wxml 模版.wxss 样式和js文件,然后在页面中使用该自定义组件即可. 例如,我的自定义组件代码结构是这样的: myComponent文件就是我所创的自定义组件,myComponent.wxml文件代码为: <view class="inner&q

微信小程序自定义tabbar的问题

个人感觉小程序的tab样式自定义的能力有所欠缺,不够美观,于是今天自己diy了一个tab 测试的时候发现,无论是使用navigator跳转(会出现点击的效果)还是用bindtap(触摸),因为没有定义tabbar, 跳转的方式都只能是页面重定向的,导致tab页上的ABCD互相跳转的时候,每次都会重新渲染视图,由此带来的 视觉效果很不好,没有系统tabbar跳转地自然,万般无奈之下,只好用回系统自带的tabbar. 如果你有好的方法能够解决自定义tabbar跳转重新渲染的问题,烦请告知,在此谢过!