首頁 > 軟體

微信小程式手機號驗證碼登入的專案實現

2022-04-02 13:01:10

本文主要介紹了小程式手機號驗證碼登入,具體如下:

wxml:

<view class="content_bottom">
      <form bindsubmit="formSubmit">
        <view class="field">
          <label for="phone">手機號</label>
          <input class="int" name="phone" type="number" placeholder="請輸入手機號碼" placeholder-class="pla2" value='{{phone}}' bindinput='getPhoneValue'  />
        </view>
        <view class="field">
          <label for="phone">手機驗證碼</label>
          <view class='changeInfoName'>
            <input type="number" placeholder='請輸入驗證碼' bindinput='getCodeValue' placeholder-class="pla2" value='{[code]}' style='width:70%;' />
            <button class='codeBtn' style="background-color:{{pageBackgroundColor}}" bindtap='getVerificationCode' disabled='{{disabled}}'>{{codename}}</button>
          </view>
        </view>
        <view class="form_btn2">
          <button class="btn_login" type="primary"  formType="submit">下一步</button>
        </view>
      </form>
    </view>

js:

// pages/login/login.js
import http from '../../http/api';
import env from '../../http/evn.js';
Page({

  data: {
    phone: '',
    code: '',
    codename: '獲取驗證碼',
  },
  getPhoneValue: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getCodeValue: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
    //獲取驗證碼
    getVerificationCode() {
      this.getCode();
      var _this = this
      // _this.setData({
      //   disabled: true
      // })
    },
    getCode: function () {
      console.log(this.data.phone, '手機號')
      var _this = this;
      var myreg = /^(14[0-9]|13[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])d{8}$$/;
      if (this.data.phone == "") {
        wx.showToast({
          title: '手機號不能為空',
          icon: 'none',
          duration: 1000
        })
        return false;
      } else if (!myreg.test(this.data.phone)) {
        wx.showToast({
          title: '請輸入正確的手機號',
          icon: 'none',
          duration: 1000
        })
        return false;
      } else {
        _this.setData({
          disabled: true
        })
        http.sendsms({
          data: {
            phone: this.data.phone
          },
          success(res) {
            var bgColor = this.data.pageBackgroundColor == '#9db8db';
            _this.setData({
              pageBackgroundColor: bgColor
              //  iscode: res.data.data
            })
            var num = 60;
            var timer = setInterval(function () {
              num--;
              if (num <= 0) {
                clearInterval(timer);
                _this.setData({
                  codename: '重新傳送',
                  disabled: false
                })
              } else {
                _this.setData({
                  codename: num + "s"
                })
              }
            }, 1000)
          }
        })
      }
    },

})

到此這篇關於微信小程式手機號驗證碼登入的專案實現的文章就介紹到這了,更多相關小程式手機號驗證碼登入內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com