Home  >  Article  >  Web Front-end  >  How to optimize SMS verification

How to optimize SMS verification

php中世界最好的语言
php中世界最好的语言Original
2018-06-06 11:06:441128browse

This time I will show you how to optimize SMS verification and what are the precautions for SMS verification optimization. The following is a practical case, let’s take a look.

Usually when we register in projects, we often use the SMS verification function, but now many SMS verifications have the following problems, for example, when the SMS verification time is 60s,

1. When the button is clicked and the countdown has not reached 60s, refresh the browser and the verification code button can be clicked again

2. When the button is clicked, the countdown starts, for example, at 50s When I closed the browser, after 5 seconds, I opened it. At this time, the time countdown should be about 45 seconds, but when I reopened the browser, the button could be clicked again

To solve the above two problems, you need to write the time into localstorage. When the page is opened, go to localstorage to get it. I will post my solution here because there was a vue project a few days ago. This method is used, so I will write a vue method here

The html code in the component:

<p class="mtui-cell__ft" @click="getCode">
   <button class="mtui-vcode-btn mtui-text-center" v-if="flag">获取短信</button>
   <button class="mtui-vcode-btn mtui-text-center" v-if="!flag">剩余{{second}}s</button>
</p>

Here comes the key point

Define a few in the data Variables needed:

 second: 60,
 flag: true,
 timer: null // 该变量是用来记录定时器的,防止点击的时候触发多个setInterval

Method to obtain SMS verification:

getCode() {
   let that = this;
   if (that.flag) {
    that.flag = false;
    let interval = window.setInterval(function() {
     that.setStorage(that.second);
     if (that.second-- <= 0) {
      that.second = 60;
      that.flag = true;
      window.clearInterval(interval);
     }
    }, 1000);
   }
  }

Write and read localstorage:

     setStorage(parm) {
   localStorage.setItem("dalay", parm);
   localStorage.setItem("_time", new Date().getTime());
  },
  getStorage() {
   let localDelay = {};
   localDelay.delay = localStorage.getItem("dalay");
   localDelay.sec = localStorage.getItem("_time");
   return localDelay;
  }

Prevent page refresh from invalidating the verification code:

judgeCode() {
   let that = this;
   let localDelay = that.getStorage();
   let secTime = parseInt(
    (new Date().getTime() - localDelay.sec) / 1000
   );
   console.log(localDelay);
   if (secTime > localDelay.delay) {
    that.flag = true;
    console.log("已过期");
   } else {
    that.flag = false;
    let _delay = localDelay.delay - secTime;
    that.second = _delay;
    that.timer = setInterval(function() {
     if (_delay > 1) {
      _delay--;
      that.setStorage(_delay);
      that.second = _delay;
      that.flag = false;
     } else {
             
              // 此处赋值时为了让浏览器打开的时候,直接就显示剩余的时间
      that.flag = true;
      window.clearInterval(that.timer);
     }
    }, 1000);
   }
  }

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use vuex to operate state objects

How to use node to make a personal account robot

The above is the detailed content of How to optimize SMS verification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn