174 lines
3.0 KiB
JavaScript
174 lines
3.0 KiB
JavaScript
// pages/loginIndex/index.js
|
|
const app = getApp();
|
|
let timer;
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
isFocus: '',
|
|
body: {
|
|
phone: '',
|
|
code: ''
|
|
},
|
|
isGetCode: false,
|
|
code: '获取验证码',
|
|
showUnclickMask: false,
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面加载
|
|
*/
|
|
onLoad: function (options) {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
*/
|
|
onReady: function () {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面显示
|
|
*/
|
|
onShow: function () {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面隐藏
|
|
*/
|
|
onHide: function () {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面卸载
|
|
*/
|
|
onUnload: function () {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
*/
|
|
onPullDownRefresh: function () {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面上拉触底事件的处理函数
|
|
*/
|
|
onReachBottom: function () {
|
|
|
|
},
|
|
|
|
// 输入框获取聚焦时
|
|
inputFocus(event) {
|
|
let key = event.currentTarget.dataset.type;
|
|
this.setData({
|
|
isFocus: key
|
|
})
|
|
},
|
|
|
|
// 输入框失去焦点时
|
|
inputBlur(event) {
|
|
let key = event.currentTarget.dataset.type;
|
|
this.setData({
|
|
isPhoneFocus: ''
|
|
})
|
|
},
|
|
|
|
// 输入框输入时
|
|
inputChange(event) {
|
|
let key = event.currentTarget.dataset.type;
|
|
this.data.body[key] = event.detail.value;
|
|
},
|
|
|
|
// 获取验证码
|
|
getCode() {
|
|
if (this.data.isGetCode) {
|
|
return;
|
|
}
|
|
if (!this.data.body.phone) {
|
|
wx.showToast({
|
|
title: '请输入手机号',
|
|
icon: 'none',
|
|
})
|
|
return;
|
|
}
|
|
this.data.isGetCode = true;
|
|
|
|
app.$api.smsLogin({
|
|
mobile: this.data.body.phone
|
|
}).then(res => {
|
|
clearTimeout(timer);
|
|
this.countdown(60);
|
|
}, err => {
|
|
this.data.isGetCode = false;
|
|
})
|
|
},
|
|
|
|
// 倒计时
|
|
countdown(val) {
|
|
if (val == 0) {
|
|
this.data.isGetCode = false;
|
|
clearTimeout(timer);
|
|
return;
|
|
}
|
|
let that = this;
|
|
timer = setTimeout(function () {
|
|
val--
|
|
that.countdown(val);
|
|
that.setData({
|
|
code: val == 0 ? '获取验证码' : val + ' s'
|
|
})
|
|
}, 1000)
|
|
},
|
|
|
|
// 跳转到协议
|
|
pushAgreement() {
|
|
wx.navigateTo({
|
|
url: '/pages/agreement/index',
|
|
})
|
|
},
|
|
|
|
// 登录
|
|
loginClick() {
|
|
if (!this.data.body.code) {
|
|
wx.showToast({
|
|
title: '请输入验证码',
|
|
icon: 'none',
|
|
})
|
|
return;
|
|
}
|
|
this.setData({
|
|
showUnclickMask: true
|
|
})
|
|
let that = this;
|
|
app.$api.memberLogin({
|
|
captcha: this.data.body.code,
|
|
mobile: this.data.body.phone
|
|
}).then(res => {
|
|
wx.setStorageSync('accessToken', res.authorization.accessToken);
|
|
that.setData({
|
|
showUnclickMask: false
|
|
})
|
|
const pages = getCurrentPages();
|
|
let page = pages[pages.length - 2];
|
|
page.changeLoginStatus();
|
|
wx.navigateBack();
|
|
// return wx.redirectTo({
|
|
// url: '/pages/login/index',
|
|
// })
|
|
}, err => {
|
|
that.setData({
|
|
showUnclickMask: false
|
|
})
|
|
})
|
|
},
|
|
|
|
}) |