const app = getApp(); Page({ /** * 页面的初始数据 */ data: { user:'', //手机号码 password: '', //新密码 codeid: '', //验证码ID code: '', //验证码 text: '获取验证码', //倒计时 currentTime: 5, //倒计时 phone: '', //获取到的手机栏中的值 disabled: false, //按钮是否禁用 color: '', //获取验证码的颜色 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, //获取账号 Telephone: function (e) { var user = e.detail.value; console.log(user); this.setData({ user: user }); }, //获取密码 password: function (e) { var password = e.detail.value; console.log(password); this.setData({ password: password }); }, //获取验证码 getCode: function (e) { var code = e.detail.value; console.log(code); this.setData({ code: code }); }, // 获取验证码事件 getVerificationCode(res) { var that = this that.setData({ disabled: true, color: '#ccc', }) var phone = that.data.user; var currentTime = that.data.currentTime //把手机号跟倒计时值变例成js值 var warn = null; //warn为当手机号为空或格式不正确时提示用户的文字,默认为空 if (phone == '') { warn = "号码不能为空"; } else if (phone.trim().length != 11 || ! /^1[23456789]\d{9}$/.test(phone)) { warn = "手机号格式不正确"; } else { //验证号码格式正确走下面 // 获取验证码ajax let code_url = 'public/verificationcode'; let code_data = { mobile:this.data.user, type:2, source:1, }; app.postRequest(code_url, code_data, function (res) { if (res.code == 201) { // console.log(res.data.id); that.setData({ codeid: res.data.id // 微信ID }) //当手机号正确的时候提示用户短信验证码已经发送 wx.showToast({ title: '短信验证码已发送', icon: 'none', duration: 2000 }); return; } var _qrcode = res.msg getApp().Tips(_qrcode); }) //设置一分钟的倒计时 var interval = setInterval(function () { currentTime--; //每执行一次让倒计时秒数减一 that.setData({ text: "重新发送(" + currentTime + 's)', //按钮文字变成倒计时对应秒数 }) //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获取验证码的按钮恢复到初始化状态只改变按钮文字 if (currentTime <= 0) { clearInterval(interval) that.setData({ text: '重新发送', currentTime: 5, disabled: false, color: '#FF9BCA' }) } }, 1000); }; //判断 当提示错误信息文字不为空 即手机号输入有问题时提示用户错误信息 并且提示完之后一定要让按钮为可用状态 因为点击按钮时设置了只要点击了按钮就让按钮禁用的情况 if (warn != null) { wx.showModal({ title: '填写手机号码', content: warn, showCancel: false, }) that.setData({ disabled: false, color: '#FF9BCA' }) return; }; }, //完成事件 complete: function (e) { console.log(this) let user = this.data.user; //手机 let password = this.data.password; //密码 let code = this.data.code; //验证码 var complete = null; if (user == '') { complete = '请输入手机号码'; } else if (user.trim().length != 11 || ! /^1[23456789]\d{9}$/.test(user)) { complete = '手机号码格式不对'; } else if (password == '') { complete = '请设置密码'; } else if (code == '') { complete = '请输入验证码'; } else if (code.trim().length != 4 || ! /^[0-9]\d{3}$/.test(code)) { complete = '验证码4位数'; } else { var _this = this console.log(this.data.nickname) let income_url = 'public/getback'; let income_data = { user: this.data.user, //手机 password: this.data.password, //密码 code: this.data.code, //验证码 id: this.data.codeid, //验证码ID }; app.postRequest(income_url, income_data, function (res) { // console.log(income_data.photo); if (res.code == 201) { console.log(res.msg); wx.showModal({ title: '提示', content: res.msg, showCancel: false, }) wx.navigateTo({ // 跳转登陆页面 url: 'pages/Landpage/Land/Land' }) } else if (res.code == 301){ wx.showToast({ title: res.msg, icon: 'none', // 不显示图标 duration: 1500, }) } }) } //判断结束 // 完成事件 用来提示用户输入手机、密码、验证码 if (complete != null) { wx.showToast({ title: complete, icon: 'none', // 不显示图标 duration: 1500 }) return; }; }, })