// pages/changeTel/index.js const app = getApp(); Page({ /** * 页面的初始数据 */ data: { getmsg: "获取验证码", disabled: 0, //0不可点 telNum:"", oldTel:"", msgCode: "", btnDisable:false }, onLoad: function (options) { this.data.oldTel = options.tel; this.setData({ oldTel: options.tel }); }, showCha: function (e) { var tel = /^[1][3,4,5,6,7,8,9][0-9]{9}$/; if (e.target.dataset.tel == 1) { if (tel.test(e.detail.value)) { this.setData({ disabled: 1, telNum:e.detail.value }) } else { this.setData({ disabled: 0 }) } } }, inputMsgCode: function (e) { this.data.msgCode = e.detail.value; }, sendMsg: function () { var that = this; console.log(this.data.telNum == this.data.oldTel); if (this.data.telNum == this.data.oldTel){ app.dialog("","新手机号不可用!\r\n 原因:与原手机号相同。","知道了"); return; } that.setData({ disabled: 0 }) wx.request({ url: app.globalData.ip + '/commons/validateTelUniqueness', data: { userId: app.globalData.userId, tel: that.data.telNum }, header: app.globalData.header, method: "POST", success: function (res) { console.log(res); if (res.data.status == 200) { that.countDown(); that.sendMsgCode(); } else if (res.data.status == 9999) { // app.dialogNotLogin(); } else { // app.showTips(that, res.data.msg); app.dialog("","新手机号不可用!\r\n 原因:新手机号已绑定其它账号。","知道了"); that.setData({ disabled: 1 }) } } }) }, sendMsgCode: function() {//发送短信验证码 var that = this; wx.request({ url: app.globalData.ip + '/commons/sendMsgCode', data: { tel: that.data.telNum }, header: app.globalData.header, method: "GET", success: function (res) { console.log("发送短信验证码"); console.log(res); if (res.data.status == 200) { } else if (res.data.status == 9999) { // app.dialogNotLogin(); } else { } } }) }, countDown: function() { var that = this; var timer = 1; if (timer == 1) { timer = 0 // var that = this var time = 60 var inter = setInterval(function () { that.setData({ getmsg: time + "s", disabled: 0 }) time-- if (time < 0) { timer = 1 clearInterval(inter) that.setData({ getmsg: "重新获取", disabled: 1 }) } }, 1000) } }, postTel:function(){ var that = this; var tel = this.data.telNum; var tel1 = /^[1][3,4,5,6,7,8,9][0-9]{9}$/; if (!tel1.test(tel)){ app.showTips(that, "请输入正确手机号"); return false } if (that.data.msgCode.length != 4){ app.showTips(that, "请输入正确验证码"); return false } that.setData({ btnDisable:true }) wx.request({ url: app.globalData.ip + '/commons/validateMsgCode', data: { tel: that.data.telNum, code: that.data.msgCode, sessionId: app.globalData.sessionId }, header: app.globalData.header, method: "GET", success: function (res) { console.log(res); if (res.data.status == 200) { wx.showModal({ title: "", content: "更换新手机号为\r\n【"+tel+"】", cancelText: "取消", confirmColor: "#027AFF", confirmText: "确定", success: function (res) { if (res.confirm) { console.log('用户点击确定'); that.updateUserTel(); } else if (res.cancel) { that.setData({ btnDisable:false }) console.log('用户点击取消') } } }) } else if (res.data.status == 9999) { // app.dialogNotLogin(); app.showTips(that, res.data.msg); } else { app.showTips(that, res.data.msg); that.setData({ btnDisable: false }) } } }) }, updateUserTel: function() { var that = this; wx.request({ url: app.globalData.ip + '/user/updateUserTel', data: { tel: that.data.telNum }, header: app.globalData.header, method: "POST", success: function (res) { console.log(res); if (res.data.status == 200) { wx.redirectTo({ url: '../changeTelSuccess/index' }) } else if (res.data.status == 9999) { // app.dialogNotLogin(); } else { app.showTips(that, res.data.msg); } } }) }, })