// pages/change_mobile/change_mobile.js var app = getApp(); const util = require('../../utils/utils.js'); let timer1 = null; var arr = []; Page({ /** * 页面的初始数据 */ data: { smsVal: '', //输入的短信验证码 phoneVal: '', //输入的手机号码 code: '获取验证码', //获取短信验证码 currentTime: 60, //倒计时秒数 codeDisabled: false, //是否禁用按钮 timer: null, //定时器 codeIshow: true,//获取验证码是否显示 }, onLoad: function (options) { // this.setData({ // phoneVal: my.getStorageSync('userInfo').mobile // }) }, //验证手机号 ckPhone(e) { this.setData({ phoneVal: e.detail.value }) }, //验证短信验证码 ckSms(e) { this.setData({ smsVal: e.detail.value }) }, change: function () { my.navigateTo({ url: '/pages/mine/mine', }) }, //获取验证码 getCode: util.throttle(function () { let self = this let currentTime = self.data.currentTime let timer = self.data.timer if (self.data.phoneVal.length != 11) { my.showToast({ content: '请输入正确的手机号', icon: 'none', duration: 2000 }) } else { // 调用短信验证码接口 let data = { mobile: this.data.phoneVal, type: 2 } app.request('/verification-code', data, 'POST').then(res => { console.log(res) if (res.status == 200) { if (!timer) { timer = setInterval(() => { if (currentTime > 0 && currentTime <= 60) { currentTime--; } if (currentTime !== 0) { self.setData({ code: "重新发送" + "(" + currentTime + ")", codeDisabled: true, }) } else { clearInterval(timer); self.setData({ code: '获取验证码', codeDisabled: false, currentTime: 60, timer: null, }) } }, 1000) } } }) //60秒倒计时 } }, 1000), verify: util.throttle(function () { let reg = 11 && /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/; let reg1 = /^\d{4}$/ if (!reg1.test(this.data.smsVal)) { my.showToast({ content: '请输入短信验证码', icon: 'none', duration: 2000 }) } if (!reg.test(this.data.phoneVal)) { my.showToast({ content: '请输入正确的手机号', icon: 'none', duration: 2000 }) return; } else if (!this.data.smsVal) { my.showToast({ content: '短信验证码不能为空', icon: 'none', duration: 2000 }) return; } else { if (app.globalData.req) { let data = { mobile: this.data.phoneVal, code: this.data.smsVal } app.request('/user/bind-mobile', data, 'POST', app.globalData.req).then(res => { if (res.status == 200) { my.showToast({ content: '换绑成功', icon: 'none' }) } }).catch(err => { if (err.status == 422) { console.log(res) my.showToast({ content: res.data.errors.mobile[0], icon: 'none', duration: 2000 }) } }) } else { my.showToast({ content: '您的操作过于频繁,请稍后再试~', icon: 'none' }) } } }, 1000), //获取手机号 getPhoneNumber: function (e) { let that = this my.getPhoneNumber({ success: (res) => { let encryptedData = JSON.parse(res.response).response; //userObject.phoneInfo = res.response; console.log(res, 'xying'); //手机号解密请求 // phoneAuth(res.response); let data = { session_key: encryptedData } app.request('/user/bind-wechat-mobile', data, 'POST', app.globalData.req).then(res => { if (res.status == 200) { console.log(res.data, '手机号') if (res.data.is_bind_mobile == true) { my.showToast({ content: '当前手机号已绑定', icon: 'none' }) return } else { that.setData({ phoneVal: res.data.user.mobile, // codeIshow: false }) } } }) } }) }, onReady: function () { }, onShow: function () { }, onHide: function () { }, onUnload: function () { }, onPullDownRefresh: function () { }, onReachBottom: function () { }, onShareAppMessage: function () { }, })