//手机号码相关
export default {
    data() {
        return {
            phonePattern: /^[1][3,4,5,7,8,9][0-9]{9}$/,
            isSend: false,
            msgCode: '',
            sendNum: 60,
            time: 60,
            timer: null
        };
    },
    methods: {
        sendMsg(phone) {
            // this.isSend = true;
            this.checkTime(60);
            this.$http
                .get('/sms/sendVerify', {
                    phone: phone
                })
                .then(res => {
                    this.msgCode = res;
                    this.$message.success('发送成功');
                })
                .catch(e => {
                    if (e) {
                        this.$message.error(e.error);
                    }
                    this.checkTime(0);
                });
        },
        setTime(num) {
            this.sendNum = num;
            if (this.timer) {
                clearTimeout(this.timer);
            }

            if (num <= 0) {
                this.isSend = false;
                return;
            } else {
                this.timer = setTimeout(() => {
                    this.setTime(num - 1);
                }, 1000);
            }
        },
        checkTime() {
            this.time = 60;
            let i = setInterval(() => {
                this.time--;
                if (this.time === 0) {
                    clearInterval(i);
                }
            }, 1000);
        },
        verifyMsg(phone, code) {
            return this.$http
                .get('/sms/verify', {
                    phone: phone,
                    code: code
                })
                .catch(e => {
                    if (e) {
                        this.$message.error(e.error);
                    }
                    return Promise.reject();
                });
        },
        loginByPhone(phone, psd) {
            return this.$http
                .post('/auth/phonePwdLogin', {
                    phone: phone,
                    password: psd
                })
                .catch(e => {
                    if (e) {
                        this.$message.error(e.error);
                    }
                    return Promise.reject();
                })
                .then(res => {
                    localStorage.setItem('nineToken', res);
                    return this.$store.dispatch('getUserInfo');
                });
        }
    }
};
