Problem solving of global variable caching in WeChat applet development

php中世界最好的语言
Release: 2018-06-05 13:48:16
Original
4072 people have browsed it

This problem arises because, in the input event, I have not found a better way to monitor the keyboard retraction. It is also for a better user experience, so the ensuing problem is global variables. , after the user exits the page, the global variable is cached by WeChat, which causes a bug that the user fails to modify the mobile phone number after entering it once.

let Btel = '',//防止用户恶意修改手机号
    Byzm = '';

handleTel(e) {// 这里修改用户输入的手机号
        this.setData({
            tel:e.detail.value 
        })
    },
    handleYzm(e) {
        this.setData({
            Yzm:e.detail.value
        })
        Byzm = e.detail.value;        let psd = this.data.psd,
            yzm = this.data.Yzm;
        yzm == psd ? this.setData({isYzm:0}):'';
    },    handleGetYzm() {        let reg = common.telReg,
            val = this.data.tel;    
        if (!reg.test(val)) {
               wx.showModal({
                content:'请输入正确的手机号',
                showCancel:false,
                confirmColor:'#3cc51f',//默认值为#3cc51f
                success:res =>{                    if(res.confirm){
                        this.setData({
                            tel:''
                        })         
                    }
                }
            })
        }else {//发送验证码的时候用全局变量的手机号
            Btel = val;// 这里是全局的手机号    
            this.setData({
                isReset : true,
                isNoClick: true
            })
            const data ={
                tel:val//传的是全局变量 
            };
            utils.sendRequest(api.YanZhengMa, data, this.handleGetYzmSucc.bind(this));
            //button 定时器            let time = setInterval(()=>{            let phoneCode = this.data.time;
                phoneCode --
                this.setData({
                    time : phoneCode
                })                if(phoneCode == 0){
                     clearInterval(time)
                     this.setData({
                        isReset : false,
                        isNoClick: false,
                        time:60
                     })
                }
            },1000)
        }
        
            
    },

//然后这一步是校验了用户在请求完验证码接口后 有没有修改手机好 然后保存handleSave() {        let name = this.data.name,
            telNum = this.data.tel,
            yzm = Byzm,
            status = this.data.isYzm,
            card = wx.getStorageSync('UserCard');        let timestamp= new Date().getTime();        if (yzm == '') {
            wx.showModal({
                content:'请输入验证码.',
                showCancel:false,
                confirmColor:'#3cc51f'
            })            return false;
        }        if (name!=''&&telNum!='') {            if(Btel != telNum) {
                utils.showModal('手机号发生变化,请重新获取验证码。');
            }else {
                const data ={
                distribution_id:card.distribution_id,
                post:{
                    user_name:name,
                    user_tel:Btel,
                    user_code:yzm
                },
                user_id:card.user_id,
                password:yzm+timestamp
                };
                utils.sendRequest(api.BindTel, data, this.handleSaveTel.bind(this));
            }
        }else {
            utils.showModal('请填写完整信息哟');
        }
    },

// 然后在 保存成功之后 用户点击确定  清空 全局变量  也可以在隐藏和卸载的生命周期里面清空全局变量。
handleSaveTel(res) {        if (res.data.error == 0) {            let go = this.data.go,
                id = res.data.data.id,
                lv = res.data.data.level;
            wx.showModal({
                content:'绑定成功~',
                showCancel:false,
                confirmColor:'#3cc51f',//默认值为#3cc51f
                success:res =>{                    if(res.confirm){
                        Byzm = '';//对小程序全局变量缓存进行清除                        if (go) {
                            wx.redirectTo({
                              url: '/pages/user/cash/cash'
                            })
                        }else {                            if (id != 0) {                                let card = wx.getStorageSync('UserCard');
                                card.distribution_id = id;
                                card.distribution_level = lv;
                                wx.setStorageSync('UserCard',card);
                                wx.setStorageSync('seller', true)
                            }
                            wx.switchTab({
                              url: '/pages/user/index'
                            })
                        }         
                    }
                }
            }) 
            return false
        }else {
            utils.showModal(res.data.err_msg);
        }
    }
Copy after login

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 Map objects in WeChat mini program development

How to use pull-up loading in WeChat mini program development accomplish

The above is the detailed content of Problem solving of global variable caching in WeChat applet development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!