vant做手机号图片验证和验证码验证

页面样式:

//用vant的dialog组件

<van-dialog
        v-model="showSMS"
        show-cancel-button
        confirm-button-text="取消"
        cancel-button-text="确定"
        :closeOnClickOverlay="true"
        @confirm="confirm"
        @cancel="cancel"
        :before-close="onBeforeClose"
      >
        <div class="dialog_layout">
          <van-row type="flex" justify="space-between">
            <van-col span="9"
              ><span style="margin-left: .6rem">手机号</span></van-col
            >
            <van-col span="9"
              ><span style="margin-right: 22px">{{ mobileNo }}</span></van-col
            >
          </van-row>

          <van-row type="flex">
            <van-col span="16"
              ><van-field
                v-model="picCode"
                maxlength="4"
                label="图形验证码"
                placeholder="输入验证码"
            /></van-col>
            <van-col span="8">
              <img
                class="refresh_code"
                @click="refreshCode"
                alt=""
                :src="picCodeUrl"
            /></van-col>
          </van-row>

          <van-row type="flex">
            <van-col span="16">
              <van-field
                v-model="smsCode"
                maxlength="6"
                label="验证码"
                placeholder="获取验证码"
              />
            </van-col>
            <van-col span="8">
              <span
                class="sms-send-btn"
                :class="{ 'not-can-send': smsNotCanSend }"
                @click="sendSMS"
                >{{ showText }}</span
              >
            </van-col>
          </van-row>
        </div>
      </van-dialog>

//data中声明数据
data:{
      picCode: "", //用户输入的图形验证码
      smsCode: "", //用户输入的短信验证码
      timer: 60,
      smsNotCanSend: false, //发送后禁止点击
      showSMS: false, //是否展示弹窗
      picCodeUrl: "", //图形验证码地址
      verifyCode: "", //图形验证码具体内容,如“ae86”
      mobileNo: "13618506674",
}

//computed中声明showText
 computed: {
    showText() {
      let text = "发送验证码";
      if (this.smsNotCanSend) {
        text = `重新获取(${this.timer})s`;
      }
      return text;
    },
  },



//methods中的点击事件
mmethods:{
 //点击确定
    cancel() {
      if (!this.smsCode) {
        this.$notify_err("短信验证码不能为空!");
        return false;
      }
    },
    confirm() {},
    sendSMS() {
      //去空格处理
      this.picCode = this.picCode.replace(/(^\s*)|(\s*$)/g, "");
      if (!this.picCode) {
        this.$notify_err("请输入图形验证码");
        return false;
      }
      //发送之后置灰,不能再次点击
      if (this.smsNotCanSend) {
        return false;
      }
      let params = {
        iamgeCode: this.picCode, //用户输入的图形验证码
        iamgeCodeByInterface: this.verifyCode, //接口返回的图形验证码
        operateType: "05", //操作类型
        platformType: "01", //平台类型
        phone: this.mobileNo,
      };
      console.log(JSON.stringify(params));
      this.$loading("正在获取验证码...");
        //下面调用验证码接收接口  根据接口路径自行修改
      sendVerificationCodeMessage(params).then((res) => {
        this.$cls();
        console.log(JSON.stringify(res));
        if (res.code === "0000") {
          if (res.data.state == true) {
            Toast("发送成功");
            this.setItime();
          } else {
            Toast("发送失败");
          }
        } else {
          this.$notify_err(res.msg);
        }
      });
    },
    onBeforeClose(action, done) {
      if (action === "cancel") {
        //点击确定不关闭
        return done(false);
      }
      if (action === "confirm") {
        return done();
      }
    },
    refreshCode() {
      let params = {};
      getPictureCaptchaInfo(params).then((res) => {
        if (res.code === "0000") {
          this.picCodeUrl = "data:image/jpg;base64," + res.data.pictureCaptcha;
          this.verifyCode = res.data.verifyCode;
        }
      });
      this.picCode = "";
    },
    setItime() {
      // 设置加载时间
      this.smsNotCanSend = true;
      var timeInter = setInterval(() => {
        if (this.timer == 0) {
          this.smsNotCanSend = false;
          this.timer = 60;
          clearInterval(timeInter);
        } else {
          this.timer--;
        }
      }, 1000);
    },
}


//页面加载时就调用图片验证码接口
 mounted() {
    this.refreshCode();
  },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值