Files
income_payments_applet/pages/register/register.js
T

718 lines
16 KiB
JavaScript

// pages/register/register.js
import UserApi from '../../assets/js/http/userApi'
import {appletName} from "../../assets/js/project.config"
import {
phoneReg,
postcode,
compareDate,
passwordReg,
isEmail,
} from '../../utils/validate'
import {
encrypt
} from '../../assets/js/jsencrypt/rsa'
var countdown = 60; // 倒计时秒数
Page({
/**
* 页面的初始数据
*/
data: {
statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
showGetCaptcha: true, // 默认显示 获取验证阿门
phone: '',
appletName:appletName,
password: '',
confirmPassword: '',
captcha: '',
count: 60,
rsaPublicKey: '',
show: true, // 默认显示密码框
confirmshow: true, // 确认密码的显示
currentTab: 1, // 当前到哪一步
selectedCompany: {}, // 选中的企业
sexList: [{
name: '男',
val: 1
}, {
name: '女',
val: 2
}],
prePageData: {}, // 上一页的数据
currentInfo: {}, // 当前页的数据
},
// 提交 提交完成之后 要清除缓存中 存入的企业信息 并返回登录页
formSubmit(e) {
// 再次校准姓名,
if (this.data.currentInfo.name === '' || this.data.currentInfo.name === null || this.data.currentInfo.name === undefined) {
wx.showToast({
title: '请输入姓名',
icon: 'none',
duration: 800
})
return
}
// 邮政编码
if (this.data.currentInfo.postalCode === '' || this.data.currentInfo.postalCode === null || this.data.currentInfo.postalCode === undefined) {
wx.showToast({
title: '请输入邮政编码',
icon: 'none',
duration: 800
})
return
} else if (this.data.currentInfo.postalCode && !postcode(this.data.currentInfo.postalCode)) {
wx.showToast({
title: '请输入正确的邮政编码',
icon: 'none',
duration: 800
})
return
}
// 手机号
// if (this.data.currentInfo.phone === '' || this.data.currentInfo.phone === null || this.data.currentInfo.phone === undefined) {
// wx.showToast({
// title: '请输入手机号',
// icon: 'none',
// duration: 800
// })
// return
// } else if (this.data.currentInfo.phone && !phoneReg(this.data.currentInfo.phone)) {
// wx.showToast({
// title: '请输入正确的手机号',
// icon: 'none',
// duration: 800
// })
// return
// }
// 性别
if (this.data.currentInfo.gender === '' || this.data.currentInfo.gender === null || this.data.currentInfo.gender === undefined) {
wx.showToast({
title: '请选择性别',
icon: 'none',
duration: 800
})
return
}
// 通讯地址
if (this.data.currentInfo.contactAddress === '' || this.data.currentInfo.contactAddress === null || this.data.currentInfo.contactAddress === undefined) {
wx.showToast({
title: '请输入通讯地址',
icon: 'none',
duration: 800
})
return
}
// 再次校准email
if (this.data.currentInfo.email === '' || this.data.currentInfo.email === null || this.data.currentInfo.email === undefined) {
wx.showToast({
title: '请输入邮箱',
icon: 'none',
duration: 800
})
return
} else if (this.data.currentInfo.email && !isEmail(this.data.currentInfo.email)) {
wx.showToast({
title: '请输入正确的邮箱',
icon: 'none',
duration: 800
})
return
}
let formData = {}
let currentPageInfo = Object.assign({},this.data.currentInfo,this.data.selectedCompany)
// this.data.prePageData,
currentPageInfo.post = e.detail.value.post.trim()
currentPageInfo.remarks = e.detail.value.remarks.trim()
// 上一步的的数据加密
formData.rsaPublicKey = this.data.rsaPublicKey
formData.verifyCode =this.data.prePageData.verifyCode
formData.phone = encrypt(this.data.rsaPublicKey,this.data.prePageData.phone),
formData.password = encrypt(this.data.rsaPublicKey,this.data.prePageData.password),
formData.verifyPassword = encrypt(this.data.rsaPublicKey,this.data.prePageData.verifyPassword),
formData.payContactsManagement = currentPageInfo
// formData.payContactsManagement
console.log('表单提交的数据',formData);
UserApi.registerUser(formData).then(res => {
console.log(res);
if(res.success){
wx.showToast({
title: res.result,
icon:'none',
duration:800
})
setTimeout( () => {
wx.navigateTo({
url: '../login/login',
success(){
wx.removeStorage({
key: 'selectedCompany',
})
}
})
},800)
}
}).catch(err => {
wx.showToast({
title: err.message,
icon:none,
duration:800
})
})
},
changeGender(e){
console.log(e);
this.setData({
'currentInfo.gender':e.detail.value
})
},
// 下一步
nextStep() {
// 再次校准手机号
if (this.data.prePageData.phone === '' || this.data.prePageData.phone === null || this.data.prePageData.phone === undefined) {
wx.showToast({
title: '请输入手机号',
icon: 'none',
duration: 800
})
return
} else if (this.data.prePageData.phone && !phoneReg(this.data.prePageData.phone)) {
wx.showToast({
title: '请输入正确的手机号',
icon: 'none',
duration: 800
})
return
}
// 再次校准验证码
if (this.data.prePageData.verifyCode === '' || this.data.prePageData.verifyCode === null || this.data.prePageData.verifyCode === undefined) {
wx.showToast({
title: '请输入验证码',
icon: 'none',
duration: 800
})
return
}
// 再次校准密码
if (this.data.prePageData.password === '' || this.data.prePageData.password === null || this.data.prePageData.password === undefined) {
wx.showToast({
title: '请输入密码',
icon: 'none',
duration: 800
})
return
} else if (this.data.prePageData.password && !passwordReg(this.data.prePageData.password)) {
wx.showToast({
title: '密码至少八位,需包含大小写字母、数字、特殊符号',
icon: 'none',
duration: 2000
})
return
}
// 再次校准确认密码
if (this.data.prePageData.verifyPassword === '' || this.data.prePageData.verifyPassword === null || this.data.prePageData.verifyPassword === undefined) {
wx.showToast({
title: '请输入确认密码',
icon: 'none',
duration: 800
})
return
} else if (this.data.prePageData.password && this.data.prePageData.password !== this.data.prePageData.verifyPassword) {
wx.showToast({
title: '密码与确认密码不一致',
icon: 'none',
duration: 800
})
return
}
this.setData({
currentTab: 2
})
},
// 上一步
preStep() {
this.setData({
currentTab: 1
})
},
// 选择企业
selectCompany() {
wx.navigateTo({
url: '../companyList/companyList?page=register&nolimit=true',
})
},
goBack() {
wx.navigateBack()
},
// 密码框与文本框的切换
showPassword() {
this.setData({
show: !this.data.show,
})
},
showConfirmPassword() {
this.setData({
confirmshow: !this.data.confirmshow,
})
},
bindInputPhone(e) {
this.data.phone = e.detail.value
this.setData({
phone: e.detail.value
})
},
// 获取验证码
async getVerCode() {
console.log(this.data.phone);
// let flag = await this.checkPhoneOnly(this.data.value)
if(!this.data.phone){
// wx.showToast({
// title: '手机号已存在',
// icon: 'none',
// duration: 2000
// })
return
}
countdown = 60
if (/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(this.data.phone)) {
UserApi.sendCode({
phone: this.data.phone,
codeType: 3
}).then(res => {
// 获取成功,倒计时 start
if (res.success) {
this.countDown()
wx.showToast({
title: '发送成功',
icon: 'none',
duration: 2000
})
} else {
wx.showToast({
title: res.message,
icon: 'none',
duration: 2000
})
}
}).catch(err => {
wx.showToast({
title: '获取失败',
icon: 'none',
duration: 2000
})
})
} else {
wx.showToast({
title: '请输入正确的手机号',
icon: 'none',
duration: 2000
})
}
},
// 倒计时
countDown() {
if (countdown === 0) {
this.setData({
showGetCaptcha: true
})
countdown = 60;
return;
} else {
this.setData({
showGetCaptcha: false
})
countdown--;
this.setData({
count: countdown
})
}
setTimeout(() => {
this.countDown()
}, 1000)
// 倒计时 end
},
// 校验手机号
async validatePhone(e) {
let value = e.detail.value
this.setData({
phone: value,
'prePageData.phone': value
})
let {
message,
errmsg
} = e.currentTarget.dataset
if (value === '' || value === undefined) {
wx.showToast({
title: message,
icon: 'none',
duration: 800
})
return
} else if (phoneReg(value)) {
let flag = await this.checkPhoneOnly(value)
console.log(flag);
if(flag){
this.setData({
'prePageData.phone': value,
'phone':value
})
}else{
wx.showToast({
title: '手机号已存在',
icon: 'none',
duration: 800
})
this.setData({
'phone': ''
})
}
} else {
wx.showToast({
title: errmsg,
icon: 'none',
duration: 800
})
}
},
// 判断手机号是否存在
checkPhoneOnly(value){
return new Promise(resolve => {
// 校验手机号是否存在
const param = {
confusionCode: 'pay_contacts_management_phone',
fieldVal: value,
encrypt: 1
}
UserApi.checkPhoneOnly(param).then(res => {
resolve(res.success)
})
})
},
// 校验验证码
validateVerCode(e) {
let value = e.detail.value
this.setData({
'prePageData.verifyCode': value
})
let {
message,
errmsg
} = e.currentTarget.dataset
if (value === '' || value === undefined) {
wx.showToast({
title: message,
icon: 'none',
duration: 800
})
return
} else {
this.setData({
captcha: value,
'prePageData.verifyCode': value
})
}
},
// 校验姓名
validateName(e) {
let value = e.detail.value
this.setData({
'currentInfo.name': value,
})
let {
message,
} = e.currentTarget.dataset
if (value === '' || value === undefined) {
wx.showToast({
title: message,
icon: 'none',
duration: 800
})
return
} else {
this.setData({
'currentInfo.name': value
})
}
},
// 第二步的校验手机号
validatePhoneCurent(e) {
let value = e.detail.value
this.setData({
'currentInfo.phone': value
})
let {
message,
errmsg
} = e.currentTarget.dataset
if (value === '' || value === undefined) {
wx.showToast({
title: message,
icon: 'none',
duration: 800
})
return
} else if (phoneReg(value)) {
this.setData({
'currentInfo.phone': value
})
} else {
wx.showToast({
title: errmsg,
icon: 'none',
duration: 800
})
}
},
// 校验邮政编码
validatePostalCode(e) {
let value = e.detail.value
this.setData({
'currentInfo.postalCode': value
})
let {
message,
errmsg
} = e.currentTarget.dataset
if (value === '' || value === undefined) {
wx.showToast({
title: message,
icon: 'none',
duration: 800
})
return
} else if (postcode(value)) {
this.setData({
'currentInfo.postalCode': value
})
} else {
wx.showToast({
title: errmsg,
icon: 'none',
duration: 800
})
}
},
// 校验邮箱
validateEmail(e) {
let value = e.detail.value
this.setData({
'currentInfo.email': value
})
let {
message,
errmsg
} = e.currentTarget.dataset
if (value === '' || value === undefined) {
wx.showToast({
title: message,
icon: 'none',
duration: 800
})
return
} else if (isEmail(value)) {
this.setData({
'currentInfo.email': value
})
} else {
wx.showToast({
title: errmsg,
icon: 'none',
duration: 800
})
}
},
// 校验通讯地址
validateContactAddress(e) {
let value = e.detail.value
this.setData({
'currentInfo.contactAddress': value,
})
let {
message,
} = e.currentTarget.dataset
if (value === '' || value === undefined) {
wx.showToast({
title: message,
icon: 'none',
duration: 800
})
return
} else {
this.setData({
'currentInfo.contactAddress': value
})
}
},
// 校验密码
validatePsd(e) {
console.log(e);
let value = e.detail.value
this.setData({
password: value,
'prePageData.password': value
})
let {
message,
errmsg
} = e.currentTarget.dataset
if (value === '' || value === undefined) {
wx.showToast({
title: message,
icon: 'none',
duration: 2000
})
return
} else if (passwordReg(value)) {
this.setData({
password: value,
'prePageData.password': value
})
} else {
wx.showToast({
title: errmsg,
icon: 'none',
duration: 2000
})
}
},
// 校验确认密码
validateConfirmPsd(e) {
console.log(e);
let value = e.detail.value
this.setData({
'prePageData.verifyPassword': value
})
let {
message,
errmsg
} = e.currentTarget.dataset
if (value === '' || value === undefined) {
wx.showToast({
title: message,
icon: 'none',
duration: 800
})
return
} else if (e.detail.value === this.data.password) {
this.setData({
confirmPassword: value,
'prePageData.verifyPassword': value
})
} else {
wx.showToast({
title: errmsg,
icon: 'none',
duration: 800
})
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
UserApi.getRSAPublicKey().then(res => {
if (res.success) {
this.setData({
rsaPublicKey: res.result
})
wx.setStorage({
key: 'rsaPublicKey',
data: res.result
})
}else{
wx.showToast({
title: res.message,
icon: 'none',
duration: 2000
})
}
}).catch(err => {
wx.showToast({
title: '加载失败',
icon: 'none',
duration: 2000
})
})
// const that = this
// wx.getStorage({
// key:'selectedCompany',
// success(res){
// console.log(res);
// that.setData({
// 'selectedCompany.companyName':res.data.companyName,
// 'selectedCompany.companyId':res.data.id
// })
// }
// })
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})