Files
income_payments_applet/pages/phonePassword/phonePassword.js
T

202 lines
4.8 KiB
JavaScript

// pages/meeting/meeting.js
import UserApi from '../../assets/js/http/userApi'
import { encrypt } from '../../assets/js/jsencrypt/rsa'
// var countdown = 60; // 倒计时秒数
Page({
/**
* 页面的初始数据
*/
data: {
form: {
currdatetime: '',
requestCodeSuccess: true,
randCodeImage: '', // 验证码
rsaPublicKey: '', // 公钥
},
flag: false
},
back(){
wx.navigateBack({
delta: 1
})
},
// 改变验证码
handleChangeCheckCode() {
this.data.currdatetime = new Date().getTime()
UserApi.sendImage({time: this.data.currdatetime}).then(res=>{
if (res.success) {
this.setData({
"form.randCodeImage": res.result,
"form.requestCodeSuccess": true
})
} else {
this.setData({
"form.requestCodeSuccess": false
})
}
}).catch(() => {
this.setData({
"form.requestCodeSuccess": false
})
})
},
// 提交
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.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.captcha){
this.data.flag = false
wx.showToast({
title: '请输入验证码',
icon: 'none'
})
return
}else{
this.data.flag = true
}
console.log('form发生了submit事件,携带数据为:', e.detail.value)
// const time = new Date().getTime()
let params = {
username: encrypt(this.data.rsaPublicKey, e.detail.value.phone),
password: encrypt(this.data.rsaPublicKey, e.detail.value.password),
checkKey: this.data.currdatetime,
captcha: e.detail.value.captcha,
rsaPublicKey: this.data.rsaPublicKey,
}
if(this.data.flag){
UserApi.passWordLogin(params).then(res => {
if(res.success){
// 微信绑定
this.wxBind()
// 存token和userInfo
wx.setStorage({
key: 'X-Access-Token',
data: res.result.token
})
wx.setStorage({
key: 'userInfo',
data: res.result.userInfo
})
// 跳转首页
wx.switchTab({
url: '../home/home',
})
}else{
wx.showToast({
title: res.message,
duration: 2000
})
this.handleChangeCheckCode()
}
}).catch(err => {
wx.hideToast()
})
}
},
// 绑定微信
wxBind(){
// 获取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)
}
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
this.handleChangeCheckCode()
const that = this
wx.getStorage({
key: 'rsaPublicKey',
success (res) {
that.setData({
rsaPublicKey: res.data
})
},
fail: function(){
// 缓存中没有登录过默认app.json的登录页
}
})
}
})