Files
income_payments_applet/pages/forgetPassword/forgetPassword.js
T
2021-10-22 18:03:14 +08:00

258 lines
6.2 KiB
JavaScript

// pages/mine/mine.js
import UserApi from '../../assets/js/http/userApi'
import { encrypt } from '../../assets/js/jsencrypt/rsa'
var countdown = 60; // 倒计时秒数
Page({
data: {
phone: '',
captcha: '',
password: '',
confirmPassword: '',
count: 60,
rsaPublicKey: '',
showGetCaptcha: true,
show: true, // 默认显示的是闭眼睛
showVerify: true,
},
bindInputPhone(e) {
this.setData({
phone: e.detail.value
})
},
showPassword(){
this.setData({
show: !this.data.show
})
},
showPasswordVerify(){
this.setData({
showVerify: !this.data.showVerify
})
},
// 获取验证码
getVerCode() {
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: 1
}).then(res => {
// 获取成功,倒计时 start
if (res.success) {
this.countDown()
wx.showToast({
title: '发送成功'+ res.result,
icon: 'none',
duration: 2000
})
} else {
wx.showToast({
title: res.message,
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
},
// 确认
formSubmit(e) {
if(!e.detail.value.phone){
this.data.flag = false
wx.showToast({
title: '请输入手机号',
icon: 'none'
})
return
} else {
let pattern = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
if(!pattern.test(e.detail.value.phone)){
wx.showToast({
title: '请输入正确的手机号!',
icon: 'none'
})
this.data.flag = false
return
} else {
this.data.flag = true
}
}
if(!e.detail.value.verifyCode){
this.data.flag = false
wx.showToast({
title: '请输入验证码',
icon: 'none'
})
return
} else {
this.data.flag = true
}
if(!e.detail.value.password){
this.data.flag = false
wx.showToast({
title: '请输入新密码',
icon: 'none'
})
return
} else {
let pattern = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,.\/]).{8,}$/
if(!pattern.test(e.detail.value.password)){
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.password === e.detail.value.verifyPassword) {
this.data.flag = true
} else {
this.data.flag = false
wx.showToast({
title: '密码不一致',
icon: 'none'
})
return
}
let params = {
phone: encrypt(this.data.rsaPublicKey,e.detail.value.phone), // 手机号加密
password: encrypt(this.data.rsaPublicKey, e.detail.value.password), // 新密码加密
verifyPassword: encrypt(this.data.rsaPublicKey, e.detail.value.verifyPassword), // 确认新密码加密
verifyCode: e.detail.value.verifyCode,
rsaPublicKey: this.data.rsaPublicKey
}
// 修改密码
UserApi.forgetPassword(params).then(res => {
wx.showToast({
title: '加载中...',
icon: 'loading'
})
if (res.success) {
wx.showToast({
title: '修改成功',
duration: 2000
})
setTimeout(() => {
// 跳转登录页
wx.redirectTo({
url: '../login/login',
})
}, 2000);
} else {
wx.showToast({
title: res.message,
icon: 'none',
duration: 2000
})
}
}).catch(err => {
wx.showToast({
title: err.message,
icon: 'none',
duration: 2000
})
})
},
// 绑定微信
wxBind(){
const that = this
// 获取openid进行手机号和微信的绑定
wx.login({
success (res) {
if (res.code) {
//发起网络请求
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
appid: 'wx24914e866df035fb',
secret: '0d2830f26f57722716249f2b2236f409',
js_code: res.code,
grant_type: 'authorization_code'
},
success (res) { // 成功拿到openId
// 微信和该账号绑定
let params = {
openId: res.openId,
rsaPublicKey: that.data.rsaPublicKey
}
UserApi.wxBind(params).then(res=>{})
}
})
} else {
console.log('授权失败!' + res.errMsg)
}
}
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
UserApi.getRSAPublicKey().then(res => {
if (res.success) {
this.setData({
rsaPublicKey: res.result
})
wx.setStorage({
key: 'rsaPublicKey',
data: res.result
})
}
})
}
})