haomingming 5ba50e4e61 优化
2023-03-28 20:52:22 +08:00

160 lines
4.0 KiB
JavaScript

import { API } from './../../utils/network/api'
const api = new API()
const app = getApp()
Page({
data: {
navbarData: {
showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
title: '手机号登录', //导航栏 中间的标题
},
checked: false,
phone: "",
sms: "",
btn_msg: "获取验证码",
btn_disabled: false,
login_disabled: true,
btn_color: "#ccc"
},
onChange(event) {
this.setData({
checked: event.detail,
});
},
phone_change(e){
let phone = e.detail.value;
this.setData({
phone: e.detail.value
})
let phone_reg = /^1[3-9]\d{9}$/;
if(phone_reg.test(phone)){
this.setData({
btn_color: "#3CC7C0"
})
}else{
this.setData({
btn_color: "#ccc"
})
}
},
getCodePhone(e){
let phone_reg = /^1[3-9]\d{9}$/;
let phone = this.data.phone;
//判断用户输入的真实姓名是否为空
if(phone.length == 0){
wx.showToast({title: '手机号不能为空', icon:'error'})
return false;
}else if(!phone_reg.test(phone)){//检测姓名的格式是否匹配
wx.showToast({title: '手机号输入有误', icon:'error'})
return false;
}
if(!this.data.checked){
wx.showToast({
title: '请同意协议',
icon: "error"
})
return;
}
this.setData({
btn_disabled: true
})
//获取手机号验证码
var params = {};
params.phone = this.data.phone;
params.scene = 1;
api.getCodePhone(params).then(response => {
console.log(response);
this.beginDaoJiShi();
this.setData({
login_disabled: false
})
}).catch(errors => {
this.setData({
btn_disabled: false
})
console.error(errors);
})
},
mobileLogin(e){
let phone_reg = /^1[3-9]\d{9}$/;
let phone = this.data.phone;
let sms = this.data.sms;
//判断用户输入的真实姓名是否为空
if(phone.length == 0){
wx.showToast({title: '手机号不能为空', icon:'error'})
return false;
}else if(!phone_reg.test(phone)){//检测姓名的格式是否匹配
wx.showToast({title: '手机号输入有误', icon:'error'})
return false;
}
if(sms.length == 0){
wx.showToast({title: '验证码不能为空', icon:'error'})
return false;
}
if(!this.data.checked){
wx.showToast({
title: '请同意协议',
icon: "error"
})
return;
}
let usertype = wx.getStorageSync('usertype');
wx.login({
success: (res) => {
console.log(res)
if (res.code) {
var params = {};
params.phone = this.data.phone;
params.code = this.data.sms;
params.user_type = usertype;
params.wx_code = res.code;
api.mobileLogin(params).then(response => {
console.log(response);
wx.setStorageSync('AUTH_TOKEN_'+usertype, response.data.token);
wx.setStorageSync('user_id', response.data.user_id);
wx.setStorageSync('client_user_id', response.data.client_user_id);
app.globalData.config.userID = response.data.user_id;
app.imInit();
wx.switchTab({
url: wx.getStorageSync('next_url')
})
}).catch(errors => {console.error(errors);})
} else {
wx.showToast({
title: '登录失败',
icon: "error"
})
}
},
})
},
beginDaoJiShi(){
console.log("开始倒计时")
let _this = this;
let time = 60;
let time_index = setInterval(function(){
console.log("time: ", time);
if(time > 1){
time--;
_this.setData({
btn_msg: time+" 秒后获取",
btn_disabled: true
})
}else{
_this.setData({
btn_msg: "重新获取验证码",
btn_disabled: false
})
clearInterval(time_index);
}
},1000)
}
})