I
I
Ivan Belovikov2019-12-28 16:54:49
JavaScript
Ivan Belovikov, 2019-12-28 16:54:49

How to make a button to repeat sending SMS?

Good day!
I have sending sms and checking the pin code and I need to make the repeat button 1 attempt, and you can only use it every 60 seconds.
Here is my code, please help me with it:

Auth.vue

<template>
  <f7-page no-toolbar no-navbar no-swipeback login-screen v-if="isSocketConnected">
    <f7-login-screen-title>Авторизация</f7-login-screen-title>
    <f7-list form v-if="!show">
      <f7-input
        label="Введите номер телефона"
        type="text"
        placeholder="Ваш номер"
        :value="phone"
        v-model="phone"
      ></f7-input>
    </f7-list>
    <f7-list form v-if="show">
      <f7-input
        label="Введите пин-код из смс"
        type="text"
        placeholder="Введите пин-код из смс"
        :value="pin"
        v-model="pin"
      ></f7-input>
    </f7-list>
    <f7-list>
      <f7-list-button @click="signIn" v-if="!show">Продолжить</f7-list-button>
      <f7-list-button @click="confirm" v-if="show">Подтвердить</f7-list-button>
      <f7-link @click="resend" v-if="show && chance > 0">Повторить {{ time > 0 ? `через ${time} секунд` : null }}</f7-link>
    </f7-list>
  </f7-page>
</template>

<script>
  import { mapGetters } from 'vuex'

  export default {
    computed: {
      ...mapGetters([
       'isSocketConnected',
       'phone',
       'pin',
       'user'
     ])
    },
    data() {
      return {
        phone: '',
        pin: '',
        show: false,
        time: 60,
        chance: 1
      };
    },
    methods: {
      signIn () {
        if (this.isValid(this.phone) === false){
          this.$f7.alert('Некорректный номер телефона. Уберите лишние знаки, проверьте номер.', 'Tick')
        } else {
          // Парсим номер
          const phone = this.phone.replace(/\D/g, "") + ''
          // генерируем код
          const gpc = require('generate-pincode')
          let pin_code = gpc(4)
          // Сохраняем
          this.$store.state.phone = phone
          this.$store.state.pin = pin_code
          // Переключаем на режим подтверждения
          this.show = true
          // Отправка смс
          const msg = `Ваш пин-код: ${this.$store.state.pin}`
          this.sendMsg (this.$store.state.phone, msg)
          console.log(msg)
        }
      },
      confirm () {
        if (this.pin === this.$store.state.pin) {
          /* Сохранение или вход юзера */
          // Проверка есть ли юзер
          try {
            const user = Vue.$cookies.get('token') === undefined ? true : false
            //this.$store.state.user = user
          } catch (e) {
            this.$f7.alert(e, 'Ошибка')
          }
        } else {
          this.$f7.alert('Повторите попытку вводу.', 'Неправильный пин-код')
        }
      },
      resend () {
        if (this.time < 1) {
          const msg = `Ваш пин-код: ${this.$store.state.pin}`
          this.sendMsg (this.$store.state.phone, msg)
          this.chance--
        }
      },
      isValid (p) {
        const digits = p.replace(/\D/g, "")
        return digits.length === 11 ? true : false
        console.log(digits)
      },
      sendMsg (phone, msg) {
        const SMSru = require('sms_ru')
        const sms = new SMSru('***')
        sms.sms_send({
          to: this.$store.state.phone,
          text: msg
        }, function(e){
          console.log('send')
        })
      }
    },
  };
</script>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Maltsev, 2020-12-30
@arat1337

var start =  null;
function timer(){
    
    if (start==null){
        
        start = Math.floor(Date.now() / 1000);
        timer();
    }else if(Math.floor(Date.now() / 1000) - start>59){
        alert('STOP');
    }else setTimeout('timer()',1000);
     
}
//run timer();

Y
Yakim Kornev, 2019-12-28
@sergo87konev

js to help, display the button after the specified seconds

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question