343 lines
9.1 KiB
JavaScript
343 lines
9.1 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: {
|
|
statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
|
|
phone: '',
|
|
captcha: '',
|
|
openId: '',
|
|
count: 60,
|
|
userInfoTemp: '',
|
|
rsaPublicKey: '',
|
|
showGetCaptcha: true,
|
|
authorize: false // 默认没有微信授权
|
|
},
|
|
|
|
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: '发送成功',
|
|
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',
|
|
})
|
|
},
|
|
goBack(){
|
|
wx.navigateBack()
|
|
},
|
|
|
|
// 注册
|
|
register() {
|
|
wx.navigateTo({
|
|
url: '../register/register',
|
|
})
|
|
},
|
|
// 微信登录
|
|
wxLogin(){
|
|
const that = this
|
|
wx.getStorage({
|
|
key: 'authorize',
|
|
success(result){
|
|
// 做过查看微信用户信息
|
|
if(result.data){ // 做过用户信息获取
|
|
wx.showToast({
|
|
title: '登录中...',
|
|
icon: 'loading'
|
|
})
|
|
wxBind((res)=>{
|
|
let params = {
|
|
openId: encrypt(that.data.rsaPublicKey,res.result.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.setStorageSync('authorize', true) // 设置有过微信授权
|
|
wx.setStorage({
|
|
key: 'avatarUrl',
|
|
data: result.avatarUrl
|
|
})
|
|
wx.switchTab({
|
|
url: '../home/home',
|
|
})
|
|
}else{ // 首次登录不能直接用微信快捷登录
|
|
wx.showToast({
|
|
title: '暂无授权手机号',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
})
|
|
})
|
|
}else{
|
|
that.getUserInfo()
|
|
}
|
|
},
|
|
fail(err){
|
|
// 没有做过查看微信用户信息
|
|
that.getUserInfo()
|
|
}
|
|
})
|
|
},
|
|
// 获取用户信息,微信登录
|
|
getUserInfo(){
|
|
const that = this
|
|
wx.showModal({
|
|
title: '温馨提示',
|
|
content: `亲,授权微信登录后才能正常使用小程序功能`,
|
|
success(res) {
|
|
if (res.confirm) {
|
|
wx.getUserProfile({
|
|
desc: '获取你的昵称、头像、地区及性别',
|
|
success(result){
|
|
let userInfoTemp = JSON.parse(result.rawData)
|
|
wx.setStorage({
|
|
key: 'userInfoTemp',
|
|
data: userInfoTemp
|
|
})
|
|
that.setData({
|
|
userInfoTemp
|
|
})
|
|
wx.showToast({
|
|
title: '登录中...',
|
|
icon: 'loading'
|
|
})
|
|
wxBind((res)=>{
|
|
let params = {
|
|
openId: encrypt(that.data.rsaPublicKey,res.result.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',
|
|
})
|
|
wx.setStorageSync('authorize', true) // 设置有过微信授权
|
|
wx.setStorageSync('avatarUrl', that.data.userInfoTemp.avatarUrl)
|
|
}else{ // 首次登录不能直接用微信快捷登录
|
|
wx.showToast({
|
|
title: '暂无授权手机号',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
})
|
|
})
|
|
},
|
|
fail(err){
|
|
wx.setStorageSync('authorize', false)
|
|
}
|
|
})
|
|
} else {
|
|
|
|
}
|
|
}
|
|
})
|
|
},
|
|
// 登录
|
|
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: encrypt(that.data.rsaPublicKey,res.result.openid),
|
|
rsaPublicKey: that.data.rsaPublicKey
|
|
}
|
|
// 需要判断是否绑定过微信,再进行绑定请求
|
|
UserApi.wxBind(params).then(res=>{})
|
|
})
|
|
},
|
|
// onLoad(){
|
|
// const that = this
|
|
// wx.getStorage({
|
|
// key: 'userInfoTemp',
|
|
// success(res){
|
|
// that.setData({
|
|
// userInfoTemp: res.data
|
|
// })
|
|
// }
|
|
// })
|
|
// },
|
|
/**
|
|
* 生命周期函数--监听页面显示
|
|
*/
|
|
onShow: function () {
|
|
const that = this
|
|
wx.getStorage({
|
|
key: 'userInfoTemp',
|
|
success(res){
|
|
that.setData({
|
|
userInfoTemp: res.data
|
|
})
|
|
}
|
|
})
|
|
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
|
|
})
|
|
})
|
|
}
|
|
})
|