当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序的注册页面包含倒计时验证码、获取用户信息

微信小程序的注册页面包含倒计时验证码、获取用户信息

2019年07月20日  | 移动技术网IT编程  | 我要评论

1、页面展示

2、wxml代码

<!--pages/register/register.wxml-->
<scroll-view>
 <image src='/images/register.png' mode='widthfix' class="topimage"></image>
 <view class='input-top'>
  <input id="telphone" maxlength='11' type="text" placeholder="请输入手机号" bindinput="inputphonenum"/>
  <text wx:if="{{!send}}" bindtap='sendmsg' class="sendmsg">获取验证码</text>
  <text wx:if="{{send}}" class="sendmsg" bindtap="sendmsg">{{second+"s"}}</text>
 </view>
 <view class="input-buttom">
  <input id="captcha" type="number" maxlength="4" placeholder="请输入4位验证码" bindinput="inputcode"/>
 </view>
 <button class="btn" open-type="getuserinfo" bindgetuserinfo="ongotuserinfo" lang="zh_cn">立即用伞</button>
 <view class='protocol'>
 <text class="protocol-left">点击立即用伞即表示同意</text>
 <text class="protocol-right">《共享雨伞租赁服务协议》</text>
 </view>
</scroll-view>

3、wxss代码

page{
 background: #f0eff4;
}
.topimage {
 width: 100%;
 height: auto;
}
.input-top, .input-buttom {
 font-size: 30rpx;
 padding-left: 30rpx;
 margin: 0rpx 20rpx;
 background-color: white;
 height: 70rpx;
}
.input-top {
 flex-direction: row;
 display: flex;
 justify-content: space-between;
 margin-bottom: 1px;
 margin-top: 20rpx;
}
#telphone, #captcha {
 height: 70rpx;
}
.sendmsg {
 line-height: 70rpx;
 margin-right: 30rpx;
 color: #f9b400;
}
.btn {
 margin: 0rpx 20rpx;
 background-color: #f9b400;
 color: white;
 margin-top: 20rpx;
 font-size: 30rpx;
 opacity:0.8
}
/* 下方协议开始 */
.protocol{
 text-align: center;
}
.protocol-left {
 color: #333;
 font-size: 25rpx;
}
.protocol-right {
 font-size: 23rpx;
 color: #f9b400;
}
/* 下方协议结束 */

4、js代码

page({
 //页面的初始数据
 data: {
 send: false, //是否已经发送验证码
 second: 120, //验证码有效时间
 phonenum: '', //用户输入的电话号码
 code: '', //用户输入的验证码
 },
 //当输入手机号码后,把数据存到data中
 inputphonenum: function(e) {
 let phonenum = e.detail.value;
 this.setdata({
  phonenum: phonenum,
 })
 },
 //前台校验手机号
 checkphonenum: function(phonenum) {
 let str = /^(1[3|5|8]{1}\d{9})$/;
 if (str.test(phonenum)) {
  return true;
 } else {
  //提示手机号码格式不正确
  wx.showtoast({
  title: '手机号格式不正确',
  image: '/images/warn.png',
  })
  return false;
 }
 },
 //调用发送验证码接口
 sendmsg: function() {
 var phonenum = this.data.phonenum;
 if (this.checkphonenum(phonenum)) {
  wx.request({
  url: '写自己的后台请求发送验证码访问url',
  method: 'post',
  data: {
   telphone: phonenum,
  },
  header: {
   "content-type": "application/x-www-form-urlencoded"
  },
  success: (res) => {
   if (获取验证码成功) {
   //开始倒计时
   this.setdata({
    send: true,
   })
   this.timer();
   } else {
   //提示获取验证码失败
   wx.showtoast({
    title: '获取验证码失败',
    image: '/images/warn.png',
   })
   }
  },
  })
 }
 },
 //一个计时器
 timer: function() {
 let promise = new promise((resolve, reject) => {
  let settimer = setinterval(
  () => {
   this.setdata({
   second: this.data.second - 1
   })
   if (this.data.second <= 0) {
   this.setdata({
    second: 60,
    send: false,
   })
   resolve(settimer)
   }
  }, 1000)
 })
 promise.then((settimer) => {
  clearinterval(settimer)
 })
 },
 //当输完验证码,把数据存到data中
 inputcode: function(e) {
 this.setdata({
  code: e.detail.value
 })
 },
 //点击立即用伞按钮后,获取微信用户信息存到后台
 //(问题缺陷:用户更改个人信息后,后台拿到的还是旧数据,不过用户信息最重要的还是openid和用户填写的手机号,其他都不重要)
 ongotuserinfo: function(e) {
 // todo 对数据包进行签名校验
 //前台校验数据
 if (this.data.phonenum === '' || this.data.code===''){
  wx.showtoast({
  title: "请填写手机号码和验证码",
  image: '/images/warn.png',
  })
 }
 //获取用户数据,(备注:我在用户一进入小程序就已经自动把openid获取到,然后放到缓存里)
 var userinfo = {
  nickname: e.detail.userinfo.nickname,
  avatarurl: e.detail.userinfo.avatarurl,
  gender: e.detail.userinfo.gender,
  phonenum: this.data.phonenum,
  openid: wx.getstoragesync('openid') 
 }
 //获取验证码
 var code = this.data.code;
 //用户信息存到后台
 wx.request({
  url: '写自己的后台请求注册url',
  method: 'post',
  data: {
  telphone: userinfo.phonenum,
  code: code,
  nickname: userinfo.nickname,
  gender: userinfo.gender,
  openid: userinfo.openid, 
  },
  header: {
  "content-type": "application/x-www-form-urlencoded"
  },
  success: (res) => {
  if (如果用户注册成功) {
   console.log("【用户注册成功】");
   wx.setstorage({
   key: "registered",
   data: true
   });
   wx.redirectto({
   url: '../user/user?orderstate=0'
   });
  } else {
   console.error("【用户注册失败】:" + res.data.resultmsg);
   wx.showtoast({
   title: res.data.resultmsg,
   image: '/images/warn.png',
   })
  }
  }
 })
 },
})

总结

以上所述是小编给大家介绍的微信小程序的注册页面包含倒计时验证码、获取用户信息,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网