Files
venue_wx/pages/register/index.js
2020-06-21 16:27:58 +08:00

315 lines
7.1 KiB
JavaScript

// pages/register/index.js
const app = getApp();
let timer = '';
let codeType = 0;
let changeType = '';
Page({
/**
* 页面的初始数据
*/
data: {
isFocus: '',
body: {},
code: "获取验证码",
isGetCode: false,
ageList: [{
label: '男',
value: 0
}, {
label: '女',
value: 1
}, {
label: '不选择',
value: 2
}],
isShowSexPopup: false,
sexLabel: '选择性别(选填)',
changeFrom: {},
unClick: false,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
let that = this;
changeType = options.changeType ? options.changeType : '';
codeType = options.codeType;
this.setData({
changeFrom: options
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {
return {
title: '智慧云馆'
}
},
// 输入框获取聚焦时
inputFocus(event) {
let key = event.currentTarget.dataset.type;
this.setData({
isFocus: key
})
},
// 输入框输入时
inputChange(event) {
let key = event.currentTarget.dataset.type;
this.data.body[key] = event.detail.value;
},
// 输入框失去焦点时
inputBlur(event) {
let key = event.currentTarget.dataset.type;
this.setData({
isFocus: ''
})
},
// 获取验证码
getCode() {
if (this.data.isGetCode) {
return;
}
if (!this.data.body.phone) {
wx.showToast({
title: '请输入手机号',
icon: 'none',
})
return;
}
this.data.isGetCode = true;
if (this.data.changeFrom.codeType != 3) {
app.$api.smsRegister({
mobile: this.data.body.phone,
'type': codeType
}).then(res => {
clearTimeout(timer);
this.countdown(60);
}, err => {
this.data.isGetCode = false;
})
}
if (this.data.changeFrom.codeType == 3) {
app.$api.memberSmsUpdate({
mobile: this.data.body.phone,
}).then(res => {}, err => {})
}
},
// 倒计时
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)
},
// 点击选择性别
changeSex() {
this.setData({
isShowSexPopup: true
})
},
// 取消选择性别
cancelSex() {
this.setData({
isShowSexPopup: false
})
},
// 选择性别
selectedSex(e) {
this.data.body.sex = e.detail.value;
this.setData({
isShowSexPopup: false,
sexLabel: e.detail.label == '不选择' ? "选择性别(选填)" : e.detail.label
})
},
// 注册
registerClick(e) {
if (this.data.unClick) {
return;
}
if (!this.data.body.phone) {
wx.showToast({
title: '请输入手机号',
icon: 'none',
})
return;
}
if (!this.data.body.code) {
wx.showToast({
title: '请输入验证码',
icon: 'none',
})
return;
}
if (this.data.body.age > 100) {
wx.showToast({
title: '输入年龄不得超过100',
icon: 'none',
})
return;
}
this.data.unClick = true;
if (codeType == 2) {
app.$api.memberRegister({
address: this.data.body.address ? this.data.body.address : '',
age: this.data.body.age ? this.data.body.age : '',
captcha: this.data.body.code ? this.data.body.code : '',
mobile: this.data.body.phone ? this.data.body.phone : '',
name: this.data.body.name ? this.data.body.name : '',
sex: this.data.body.sex ? this.data.body.sex : '',
}).then(res => {
this.data.unClick = false;
wx.setStorageSync('accessToken', res.authorization.accessToken);
// if (wx.getStorageSync('history').indexOf("basketballGym") > -1) {
wx.redirectTo({
url: '/pages/myAccount/index?isPass=' + 1,
})
// } else {
// wx.redirectTo({
// url: '/pages/facialCapturing/index?isPass=' + 1,
// })
// }
}, err => {
this.data.unClick = false;
})
} else if (codeType == 1) {
app.$api.memberLoginWXPhone({
avatar: wx.getStorageSync('information').avatar,
nickname: wx.getStorageSync('information').nickname,
openId: wx.getStorageSync('information').openId,
address: this.data.body.address ? this.data.body.address : '',
age: this.data.body.age ? this.data.body.age : '',
captcha: this.data.body.code ? this.data.body.code : '',
mobile: this.data.body.phone ? this.data.body.phone : '',
name: this.data.body.name ? this.data.body.name : '',
sex: this.data.body.sex ? this.data.body.sex : '',
}).then(res => {
this.data.unClick = false;
wx.setStorageSync('accessToken', res.authorization.accessToken);
// if (wx.getStorageSync('history').indexOf("basketballGym") > -1) {
wx.redirectTo({
url: '/pages/myAccount/index?isPass=' + 1,
})
// } else {
// wx.redirectTo({
// url: '/pages/facialCapturing/index?isPass=' + 1,
// })
// }
}, err => {
this.data.unClick = false;
})
}
},
// 保存修改
saveClick() {
let body = {};
if (this.data.changeFrom.label == "phone") {
if (!this.data.body.code) {
wx.showToast({
title: '请输入验证码',
icon: 'none',
})
return;
}
body.mobile = this.data.body.phone ? this.data.body.phone : this.data.changeFrom.value;
body.captcha = this.data.body.code;
} else {
if (this.data.changeFrom.label == 'age' && this.data.body.age > 100) {
wx.showToast({
title: '输入年龄不得超过100',
icon: 'none',
})
return;
}
if (this.data.changeFrom.label == 'nickname' && this.data.body.nickname === '') {
wx.showToast({
title: '请输入昵称',
icon: 'none',
})
return;
}
body[this.data.changeFrom.label] = this.data.body[this.data.changeFrom.label] != undefined ? this.data.body[this.data.changeFrom.label] : this.data.changeFrom.value
}
app.$api.memberUpdateInformation(body).then(res => {
wx.showToast({
title: '保存成功',
duration: 2000
});
setTimeout(function() {
wx.navigateBack({
delta: 1,
})
}, 2000);
}, err => {
})
}
})