const app = getApp(); import userApi from '../../../api/user'; Page({ verifyCodeTimer:null, /** * 页面的初始数据 */ data: { phone:'', verifyCode:'', password:'', rePassword:'', codeLoading:false, getCodeBtnText:'获取验证码', waitingTime:app.verifyCodeWaitingTime, modifyLoading:false }, validator:{ verifyCode:[ {required:true,message:'请输入验证码'}, {length:6,message:'请输入 6 位数验证码'} ], password:{required:true,message:'请输入新密码'}, rePassword:{required:true,message:'请输入确认新密码'}, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { app.getUserInfo().then((data)=>{ this.setData({ phone:data.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(){ console.log(this.data.waitingTime); 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.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() { } })