phone-code.js 4.3 KB
// pages/index/phone-code/phone-code.js
const app = getApp();
var interval = null //倒计时函数
Page({

    /**
     * 页面的初始数据
     */
    data: {
        fun_id: 2,
        time: '获取验证码', //倒计时
        currentTime: 61,
        phone_number: '',
        verification_code: '',
    },
    phoneInput(e) {
        this.setData({phone_number: e.detail.value})
    },
    codeInput(e) {
        this.setData({verification_code: e.detail.value})
    },
    getCode: function (options) {
        var that = this;
        var currentTime = that.data.currentTime;
        interval = setInterval(function () {
            currentTime--;
            that.setData({
                time: currentTime + '秒'
            })
            if (currentTime <= 0) {
                clearInterval(interval);
                that.setData({
                    time: '重新发送',
                    currentTime: 61,
                    disabled: false
                })
            }
        }, 1000);
        let url = '/user/Profile/getCode';
        let params = {
            mobile: that.data.phone_number,
        };
        let header = {
            "XX-token": wx.getStorageSync('token')
        };
        app.post(url, params,header).then((res) => {
            console.log(res);
            that.setData({verification_code: res.code})
            // console.log(this.data.this_week_test_info);
        })
    },
    getVerificationCode() {
        var that = this;
        if(that.data.phone_number !== '') {
            that.getCode();
            that.setData({
                disabled: true
            })
        }else {
            that.setData({
                disabled: false
            });
            wx.showToast({title: '请输入手机号!',icon: 'none'})
        }
    },
    //确认绑定
    formSubmit(e) {
        console.log(e.detail);
        const self = this;
        if(self.data.phone_number === '') {
            wx.showToast({title: '请输入手机号!',icon: 'none'})
        }else if(self.data.verification_code === '') {
            wx.showToast({title: '请输入验证码!',icon: 'none'})
        }else {
            let url = '/user/Profile/bindingMobile';
            let params = {
                _type: 1,//1,手机号绑定成功
                formId: e.detail.formId,
                mobile: e.detail.value.phone_number,
                verification_code: e.detail.value.verification_code,
            };
            let header = {
                "XX-token": wx.getStorageSync('token')
            };
            app.post(url, params,header).then((res) => {
                console.log('绑定结果',res);
                app.globalData.is_bingding = 1;
                if(self.data.from_index) {
                    wx.switchTab({
                        url: '/pages/index/index?'
                    })
                }else if(self.data.from_clock) {
                    wx.switchTab({
                        url: '/pages/clock/clock'
                    })
                }else if(self.data.from_my) {
                    wx.switchTab({
                        url: '/pages/my/my'
                    })
                }

                // self.setData({past_test_info: res})
                // console.log(self.data.this_week_test_info);
            });
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            from_index: options.from_index?options.from_index:'',
            from_clock: options.from_clock?options.from_clock:'',
            from_my: options.from_my?options.from_my:'',
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})