bind.vue 5.51 KB
<template>
  <div v-if="dialogEnable" class="third-bind-wrapper">
    <div class="bind-dialog">
      <p class="bind-title">为了提供有效服务<br>请关联手机号</p>
      <div class="under-row">
        <div class="left-row-title">
          <span @click="chooseArea">{{code}}</span>
          <span class="iconfont icondownn"></span>
        </div>
        <CubeInput class="bind-input" :class="phone ? '' : 'bind-input-phone'" v-model="phone"></CubeInput>
      </div>
      <div class="under-row">
        <div class="left-row-title">
          验证码
        </div>
        <CubeInput class="bind-input" :class="smsCode ? '' : 'bind-input-code'" v-model="smsCode"></CubeInput>
        <CubeButton class="send-sms-btn" :disabled="!!sendBtnText" @click="sendSMS">{{sendBtnText || '获取验证码'}}</CubeButton>
      </div>
      <div class="submit-row">
        <CubeButton class="bind-btn" :disabled="submitDisable" @click="bindSubmit">登录</CubeButton>
      </div>
    </div>
  </div>
</template>

<script>

import { Button, Input } from 'cube-ui';
import { mapActions, mapState, mapMutations } from 'vuex';

export default {
  name: 'ThirdBind',
  data() {
    return {
      showBind: false,
      code: '+86',
      phone: '',
      smsCode: '',
      sendBtnText: '',
      binded: false
    }
  },
  computed: {
    dialogEnable() {
      return this.showBind && !this.binded;
    },
    submitDisable() {
      return !(this.phone && this.smsCode);
    }
  },
  watch: {
    smsCode(newVal, oldVal) {
      // 解决ios自动填充验证码输入两遍的问题
      if (this.$yoho.isiOS && newVal.length > 4 && newVal.length - oldVal.length > 1 && newVal.length % 2 === 0) {
        let half = newVal.length / 2;

        if (newVal.substring(0, half) === newVal.substring(half)) {
          setTimeout(() => {
            this.smsCode = newVal.substring(0, half);
          }, 0);
        }
      }
    }
  },
  methods: {
    ...mapActions(['sendBindSms', 'submitThirdBind']),
    ...mapMutations(['SET_LOGIN_INFO']),
    open() {
      this.bindCode = this.$route.query.bind_code;
      this.phone = '';
      this.smsCode = '';
      this.clearSendTimer();
      this.show();
    },
    close() {
      this.showBind = false;
    },
    show() {
      this.showBind = true;
    },
    toast(msg, time = 2000) {
      this.$createToast && this.$createToast({
        txt: msg,
        type: 'txt',
        time
      }).show();
    },
    chooseArea() {
      this.$router.push({
        name: 'passport.area'
      });
    },
    changeArea(code) {
      code && (this.code = code);
    },
    sendSMS() {
      let total = 60;

      this.timer = setInterval(() => {
        if (--total) {
          this.sendBtnText = '重发' + total;
        } else {
          this.clearSendTimer();
        }
      }, 1000);
      this.sendBtnText = '重发' + total;

      this.sendBindSms({
        mobile: this.phone,
        bindCode: this.bindCode
      }).then(res => {
        if (res.code !== 200) {
          this.clearSendTimer();
          this.toast(res.message || '发送失败');
        }
      });
    },
    clearSendTimer() {
      this.sendBtnText = '';
      clearInterval(this.timer);
    },
    bindSubmit() {
      if (this.phone && this.smsCode) {
        this.submitThirdBind({
          mobile: this.phone,
          code: this.smsCode,
          bindCode: this.bindCode
        }).then(res => {
          if (res.code === 200) {
            this.binded = true;
            this.close();
            this.SET_LOGIN_INFO({ uid: 999999999999 }); // 设置登录状态
          } else {
            this.toast(res.message);
          }
        });
      } else {
        this.toast(this.phone ?  '请输入短信验证码' : '请输入手机号');
      }
    }
  },
  components: {
    CubeInput: Input,
    CubeButton: Button
  }
};
</script>

<style lang="scss" scoped>
.third-bind-wrapper {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  z-index: 99;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.bind-dialog {
  width: 670px;
  padding: 64px 48px 80px;
  font-size: 28px;
  box-sizing: border-box;
  background-color: #fff;
}

.bind-title {
  font-size: 48px;
  line-height: 66px;
  font-weight: 500;
  margin-bottom: 32px;
  text-align: center;
}

.under-row {
  line-height: 120px;
  border-bottom: 1px solid #eaeaea;
  display: flex;
  justify-content: center;
  align-items: center;

  .left-row-title {
    width: 150px;
    flex-shrink: 0;
    color: #999;
  }

  .iconfont {
    font-size: 28px;
  }
}

.bind-input {
  flex-grow: 1;

  &:after {
    width: auto;
    height: auto;
    border: 0;
    font-size: 28px;
    line-height: 40px;
    transform: scale(1);
    top: 50%;
    margin-top: -20px;
    color: #ccc;
    font-weight: 300;
  }

  /deep/ .cube-input-field {
    font-size: 20px;
    padding: 10px 0;
    color: #000;
    font-weight: 500;
  }
}

.bind-input-phone:after {
  content: "请输入手机号";
}

.bind-input-code:after {
  content: "请输入验证码";
}

.send-sms-btn {
  width: 180px;
  height: 54px;
  line-height: 54px;
  font-size: 28px;
  padding: 0;
  text-align: right;
  background: none;
  color: #000;
  margin-left: 10px;

  &.cube-btn_disabled {
    color: #999;
  }

  &:after {
    border: 0;
  }
}

.submit-row {
  margin-top: 80px;
}

.bind-btn {
  height: 90px;
  font-size: 28px;
  background: #022c46;
  border-radius: 46px;

  &.cube-btn_disabled {
    background: #ccc;
  }

  &:after {
    border: 0;
  }
}
</style>