zycp-ddxcx/pages/login/bind/bind.js

161 lines
3.4 KiB
JavaScript

Page({
data: {
isToast: false,
openPhoneTips: false, //验证手机号弹出框
tipsMsg: '',
toastText: '',
phoneNumber: '',
show: true, //显示获取验证码
count: "", //剩余时间
timer: null,
code: '',
value: ''
},
onLoad() {
dd.setNavigationBar({
title: '绑定手机号',
backgroundColor: '#FFFFFF',
});
},
// 获取电话号码
bindKeyInput(e) {
this.setData({
phoneNumber: e.detail.value,
});
console.log(this.data.phoneNumber,111)
},
// 获取输入验证码
bindKeyCode(e) {
this.setData({
value: e.detail.value,
});
console.log(this.data.value,222)
},
// 倒计时
timeEV(){
if (!this.data.timer) {
this.setData({
count: 60,
show: false
})
let that = this;
const countDown = setInterval(() => {
if(that.data.count <= 1){
that.setData({
count:60,
show: true
})
clearInterval(countDown)
return
}
that.data.count --
that.setData({
count: that.data.count,
show: false
})
},1000);
}
},
// 获取验证码
changeCode(){
console.log(this.data.phoneNumber,123)
let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
if (this.data.phoneNumber == "") {
this.setData({
toastText: '请填写您的手机号!',
isToast: true
})
setTimeout(()=> {
this.setData({
isToast: false
})
}, 1000)
return false;
} else if (!reg_tel.test(this.data.phoneNumber)) {
this.setData({
toastText: '请正确填写您的手机号!',
isToast: true
})
setTimeout(()=> {
this.setData({
isToast: false
})
}, 1000)
return false;
}
let params = {
phone: this.data.phoneNumber
}
// 验证手机号是否存在
dd.$http.post('/api/user/has-phone-user',params).then(res=>{
console.log(res,'登录认证数据')
if(res.data.registered == 0){
this.timeEV();
this.sendCode();
}else if(res.data.registered == 1){
this.setData({
openPhoneTips: true,
tipsMsg: '该手机号已注册,请使用手机号登录或换绑其他手机号'
})
}
})
},
// 发送验证码
sendCode() {
let params = {
phone: this.data.phoneNumber,
type: "bind",
}
dd.$http.post('/api/common/send-sms-captcha',params).then(res=>{
console.log(res,'验证码数据')
this.setData({
code: res.data.code
})
})
},
// 绑定手机号
bindPhone(){
if(this.data.code == this.data.value){
let params = {
phone: this.data.phoneNumber,
sms_code: this.data.value,
}
dd.$http.post('/api/user/bind-phone',params).then(res=>{
console.log(res,'绑定数据')
if (res.code == 0) {
// 跳转页面
dd.redirectTo ({
url: '/pages/home/home'
})
}
})
}else{
this.setData({
value: '',
toastText: '验证码错误',
isToast: true
});
setTimeout(()=> {
this.setData({
isToast: false
})
}, 1000)
}
},
// 关闭弹窗
clearPhone(){
this.setData({
openPhoneTips: false,
})
},
});