Files
income_payments_applet/pages/setPassword/setPassword.js
T
2021-10-28 11:08:01 +08:00

235 lines
6.3 KiB
JavaScript

// pages/meeting/meeting.js
import UserApi from '../../assets/js/http/userApi'
import { encrypt } from '../../assets/js/jsencrypt/rsa'
import { wxBind } from '../../assets/js/common'
Page({
/**
* 页面的初始数据
*/
data: {
password: '', // token
newPassword: '', // 密码
verifyPassword: '', // 确认密码
valsNew: [],
valsVerify: [],
rsaPublicKey: '',
flag: false,
show: true, // 默认显示的是闭眼睛
showVerify: true
},
showPassword(){
this.setData({
show: !this.data.show
})
},
showPasswordVerify(){
this.setData({
showVerify: !this.data.showVerify
})
},
clearrepeatval(){
this.data.valsNew = []
wx.showToast({
title: '禁止复制粘贴请手动输入',
icon: 'none',
duration: 2000
})
this.setData({
newPassword: ''
})
return
},
clearrepeatvalVerify(){
this.data.valsVerify = []
wx.showToast({
title: '禁止复制粘贴请手动输入',
icon: 'none',
duration: 2000
})
this.setData({
verifyPassword: ''
})
return
},
// 新密码的输入
handlePasswordInput(e) { //禁止输入中复制粘贴
// 获取当前数组
const vals = this.data.valsNew
// 获取当前输入的值
const currentval = e.detail.value.replace(/\s+/g, "")
if(currentval){ // 如果值存在追加到vals中
vals.push(currentval)
}
if(vals.length == 1 ){
const v = vals[vals.length-1].length
if(v > 1){ // 如果第一个的长度就大于1 说明是粘贴过来的
this.clearrepeatval()
}
}
if(vals.length > 1){
// 如果值数组大于1 就获取最后一个值长度和倒数第二个值长度
// 比较值长度差如果大于1 则粘贴的
const lastv = vals[vals.length-1].length // 获取最后一个值的长度
const secondv = vals[vals.length-2].length // 获取倒数第二个值得长度
const dis = lastv - secondv
if(dis>1){
this.clearrepeatval()
}
}
// 最后赋值
this.data['newPassword'] = currentval
},
// 确认新密码的输入
handleVerifyPasswordInput(e) { //禁止输入中复制粘贴
// 获取当前数组
const vals = this.data.valsVerify
// 获取当前输入的值
const currentval = e.detail.value.replace(/\s+/g, "")
if(currentval){ // 如果值存在追加到vals中
vals.push(currentval)
}
if(vals.length == 1 ){
const v = vals[vals.length-1].length
if(v > 1){ // 如果第一个的长度就大于1 说明是粘贴过来的
this.clearrepeatvalVerify()
}
}
if(vals.length > 1){
// 如果值数组大于1 就获取最后一个值长度和倒数第二个值长度
// 比较值长度差如果大于1 则粘贴的
const lastv = vals[vals.length-1].length // 获取最后一个值的长度
const secondv = vals[vals.length-2].length // 获取倒数第二个值得长度
const dis = lastv - secondv
if(dis>1){
this.clearrepeatvalVerify()
}
}
// 最后赋值
this.data['verifyPassword'] = currentval
},
// 登录
formSubmit(e) {
const that = this
if(!e.detail.value.newPassword){
this.data.flag = false
wx.showToast({
title: '请输入密码',
icon: 'none'
})
return
} else {
let pattern = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,.\/]).{8,}$/
if(!pattern.test(e.detail.value.newPassword)){
wx.showToast({
title: '密码由8位及以上数字、大小写字母和特殊符号组成!',
icon: 'none'
})
this.data.flag = false
return
} else {
this.data.flag = true
}
}
if(!e.detail.value.verifyPassword){
this.data.flag = false
wx.showToast({
title: '请输入确认密码',
icon: 'none'
})
return
} else {
let pattern = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,.\/]).{8,}$/
if(!pattern.test(e.detail.value.verifyPassword)){
wx.showToast({
title: '密码由8位及以上数字、大小写字母和特殊符号组成!',
icon: 'none'
})
this.data.flag = false
return
} else {
this.data.flag = true
}
}
if(e.detail.value.newPassword === e.detail.value.verifyPassword) {
this.data.flag = true
} else {
this.data.flag = false
wx.showToast({
title: '密码不一致',
icon: 'none'
})
return
}
if (this.data.flag) {
let params = {
password: this.data.password, // token
newPassword: encrypt(this.data.rsaPublicKey, e.detail.value.newPassword), // 密码
verifyPassword: encrypt(this.data.rsaPublicKey, e.detail.value.verifyPassword), // 确认密码
rsaPublicKey: this.data.rsaPublicKey
}
// 请求短信登录start
UserApi.setPassword(params).then(res => {
if (res.success) {
// 成功设置密码,跳转首页
wx.setStorage({
key: 'X-Access-Token',
data: that.data.password
})
wx.setStorage({
key: 'userInfo',
data: this.data.userInfo
})
// 绑定微信
this.wxBindPhone()
wx.switchTab({
url: '../home/home',
})
} else {
wx.showToast({
title: res.message,
icon: 'error',
duration: 2000
})
}
})
}
},
// 绑定微信
wxBindPhone(){
const that = this
wxBind((res)=>{
let params = {
openId: res.data.openid,
rsaPublicKey: that.data.rsaPublicKey
}
// 需要判断是否绑定过微信,再进行绑定请求
UserApi.wxBind(params).then(res=>{})
})
},
onLoad(options){
// this.data.userInfo = JSON.parse(options.userInfo)
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
const that = this
wx.getStorage({
key: 'rsaPublicKey',
success (res) {
that.setData({
rsaPublicKey: res.data
})
}
})
wx.getStorage({
key: 'token-temp',
success (res) {
that.setData({
password: res.data
})
}
})
}
})