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

235 lines
5.7 KiB
JavaScript

// pages/mine/mine.js
import UserApi from '../../assets/js/http/userApi'
import { encrypt } from '../../assets/js/jsencrypt/rsa'
import { wxBind } from '../../assets/js/common'
var countdown = 60; // 倒计时秒数
Page({
data: {
phone: '',
captcha: '',
openId: '',
count: 60,
rsaPublicKey: '',
showGetCaptcha: true
},
bindInputPhone(e) {
this.data.phone = e.detail.value
this.setData({
phone: e.detail.value
})
},
// 获取验证码
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
})
}
}).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
},
// 手机号密码登录页
phonePassword(){
wx.navigateTo({
url: '../phonePassword/phonePassword',
})
},
// 微信登录
wxLogin(){
wx.showToast({
title: '登录中...',
icon: 'loading'
})
const that = this
wxBind((res)=>{
let params = {
openId: res.data.openid,
rsaPublicKey: that.data.rsaPublicKey
}
UserApi.wxLogin(params).then(res=>{
if(res.success) {// 已经用手机号登录过,此时用微信快捷登录时,把微信的个人信息存进缓存(微信信息在缓存的userInfoTemp)
wx.setStorageSync('userInfo', res.result.userInfo)
wx.setStorage({
key: 'X-Access-Token',
data: res.result.token
})
wx.switchTab({
url: '../home/home',
})
}else{ // 首次登录不能直接用微信快捷登录
wx.showToast({
title: '暂无授权手机号',
icon: 'none'
})
}
})
})
},
// 登录
formSubmit(e) {
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',
duration: 2000
})
return
}
if (e.detail.value.phone && e.detail.value.captcha) {
let params = {
username: encrypt(this.data.rsaPublicKey,e.detail.value.phone), // 手机号加密
captcha: e.detail.value.captcha,
rsaPublicKey: this.data.rsaPublicKey
}
// 请求短信登录start
UserApi.codeLogin(params).then(res => {
if (res.success) {
// 第一次登录系统,需要设置密码
if(res.message === "updatePassword") {
// 存临时token
wx.setStorage({
key: 'token-temp',
data: res.result.token
})
let userInfo = JSON.stringify(res.result.userInfo)
// 跳转设置密码页
wx.navigateTo({
url: `../setPassword/setPassword?userInfo=${userInfo}`,
})
}else{
// 存token和userInfo
wx.setStorage({
key: 'X-Access-Token',
data: res.result.token
})
wx.setStorage({
key: 'userInfo',
data: res.result.userInfo
})
// 绑定微信
this.wxBindPhone()
// 跳转首页
wx.switchTab({
url: '../home/home',
})
}
} else {
wx.showToast({
title: res.message,
icon: 'error',
duration: 2000
})
}
})
// 登录失败
} else {
wx.showToast({
title: '请输入正确的验证码',
icon: 'none',
duration: 2000
})
}
},
// 忘记密码
updateInfo(){
wx.navigateTo({
url: '../forgetPassword/forgetPassword',
})
},
// 绑定微信
wxBindPhone(){
const that = this
wxBind((res)=>{
let params = {
openId: res.data.openid,
rsaPublicKey: that.data.rsaPublicKey
}
// 需要判断是否绑定过微信,再进行绑定请求
UserApi.wxBind(params).then(res=>{})
})
},
onLoad(){
},
/**
* 生命周期函数--监听页面显示
*/
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
})
})
}
})