所属分类:php教程
这个问题的出现是因为,在input的事件,我没有找到更好的能监听键盘收回的方法,也是为了更好的用户体验吧,所以随之而来的问题就是全局变量,在赋值之后用户退出这个页面,全局变量被微信缓存了,然后造成,用户输入过一次之后,修改手机号失败的bug。
程序员必备接口测试调试工具:立即使用
Apipost = Postman + Swagger + Mock + Jmeter
Api设计、调试、文档、自动化测试工具
后端、前端、测试,同时在线协作,内容实时同步
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 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 =
''
;
//对<a href="https://www.zzsucai.com/search-0-482-1.html" class="keylink" target="_blank">小程序</a>全局变量缓存进行清除 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);
}
}
登录后复制
相信看了本文案例你已经掌握了方法,更多精彩请关注zzsucai.com其它相关文章!
推荐阅读:
微信小程序开发中怎样使用Map对象
微信小程序开发上拉加载如何实现
以上就是微信小程序开发中全局变量缓存的问题处理的详细内容,更多请关注zzsucai.com其它相关文章!