// 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, vals:[], valsVerify: [] }, 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: 2 }).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 }, clearrepeatval(){ this.data.vals = [] this.setData({ password: '' }) return }, clearrepeatvalVerify(){ this.data.valsVerify = [] this.setData({ verifyPassword: '' }) return }, // 新密码的输入 handlePasswordInput(e) { //禁止输入中复制粘贴 // 获取当前数组 const vals = this.data.vals // 获取当前输入的值 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['password'] = 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) { 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 } 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 }) } }) } })