const app = getApp(); import userApi from '../../../api/user'; Page({ verifyCodeTimer:null, /** * 页面的初始数据 */ data: { phone:'', isLogin:false, verifyCode:'', password:'', rePassword:'', codeLoading:false, getCodeBtnText:'获取验证码', waitingTime:app.verifyCodeWaitingTime, modifyLoading:false }, validator:{ phone:{ type:'phone',autoFocus:true,shake:true,message:'请输入正确的手机号码' }, verifyCode:[ {required:true,message:'请输入验证码',shake:true,autoFocus:true}, // {length:6,message:'请输入 6 位数验证码',shake:true,autoFocus:true} ], password:{minLength:6,message:'请输入至少 6 位新密码',shake:true,autoFocus:true}, rePassword:{minLength:true,message:'请输入确认新密码',shake:true,autoFocus:true}, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.setData({ phone:app.globalData.userInfo?.phone||'', isLogin:!!app.globalData.userInfo?.phone }) let time = wx.getStorageSync('password-verify-code-time'); if(time){ let remainTime = app.verifyCodeWaitingTime*1000 - ((new Date()).getTime() - time); if(remainTime>0){ this.setData({ waitingTime:parseInt(remainTime/1000), codeLoading:true }) this.startTimer(); }else{ wx.removeStorageSync('password-verify-code-time') } } }, getVerifyCode(){ if(this.data.codeLoading)return; this.setData({ codeLoading:true }); userApi.verifyCode(this.data.phone).then((data)=>{ this.setData({ getCodeBtnText:`${this.data.waitingTime}S` }); const time = new Date(); wx.setStorageSync('password-verify-code-time', time.getTime()); this.startTimer(); }) }, startTimer(){ if(this.data.waitingTime<=0){ this.setData({ codeLoading:false, getCodeBtnText:'获取验证码', waitingTime:app.verifyCodeWaitingTime }); wx.removeStorageSync('password-verify-code-time') }else{ this.setData({ getCodeBtnText:`${this.data.waitingTime--}S` }); this.verifyCodeTimer = setTimeout(this.startTimer,1000); } }, save(){ if(this.data.modifyLoading)return; const valid = app.validateForm(this.validator,this); if(valid.length==0){ if(this.data.password==this.data.rePassword){ this.setData({ rePasswordMessage:'' }); this.setData({ modifyLoading:true }); userApi.modifyPassword(this.data.phone,this.data.rePassword,this.data.verifyCode).then((data)=>{ wx.navigateBack({ success(){ wx.showToast({ icon:'success', title: '修改成功', }) } }) }).catch(()=>{ console.log(11111,'errro'); this.setData({ modifyLoading:false }) }) }else{ this.setData({ rePasswordMessage:'两次密码不一致' }) } } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { clearTimeout(this.verifyCodeTimer); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })