Files
AGuestSaas/pages/login/login.js
2020-12-27 09:54:49 +08:00

233 lines
5.4 KiB
JavaScript

/**
* Web唐明明
* 一个梦想做木雕手艺人的程序员
*/
var phoneOutTime
Page({
/**
* 页面的初始数据
*/
data: {
code : "", //解密code
userInfo : false, //处理页面弹窗状态
parent_id : 0, //分享人用户id
loginWechat : true, //登录方式
codeTime : 60, //短信重新获取倒计时
codeBtnStat : false, //获取验证码按钮
getCodeLay : false, //获取短信验证层
codeImg : "", //图形验证码
codeImgKey : "", //图形验证码key
phone : "", //用户输入的手机号码
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(){
this.wxLogin()
},
/**
* 图形验证码
*/
showImgCode(){
if(this.data.phone == ""){
wx.showToast({
title: "请输入手机号码",
icon : "none"
})
return
}
this.setData({
getCodeLay: !this.data.getCodeLay
})
if(this.data.getCodeLay) this.getCodeImg()
},
/**
* 验证码
*/
getCodeImg(){
wx.$api.auth.getImgCode().then(res=>{
this.setData({
codeImg : res.img,
codeImgKey : res.key
})
})
},
/**
* 获取短信验证码
*/
getPhoneCode(e){
if(e.detail.value.imgcode == ""){
wx.showToast({
title: "请输入验证码",
icon : "none"
})
return
}
wx.$api.auth.phoneCode({
mobileNo : this.data.phone,
captcha_key : this.data.codeImgKey,
captcha : e.detail.value.imgcode
}).then(res=>{
wx.showToast({
title: res,
icon : "none"
})
// 移出弹出层
this.showImgCode()
// 更新按钮状态
this.outTime()
this.setData({
codeBtnStat: true
})
})
},
/**
* 获取短信倒计时
*/
outTime(){
let outTime = this.data.codeTime
phoneOutTime = setInterval(()=>{
if(outTime <= 60 && outTime > 0){
outTime--
this.setData({
codeTime: outTime
})
}else{
clearInterval(phoneOutTime)
this.setData({
codeTime : 60,
codeBtnStat : false
})
}
},1000)
},
/**
* 手机号码登录
*/
loginForm(e){
wx.$api.auth.phoneLogin({
mobileNo : e.detail.value.mobileNo,
code : e.detail.value.code,
parent_id : this.data.parent_id
}).then(token=>{
// 存储登录信息
wx.setStorageSync('token', token.access_token)
if(token.has_nickname){
// 返回上一页
wx.navigateBack()
}else{
this.setData({
userInfo: !token.has_nickname
})
}
})
},
/**
* 获取用户code
*/
wxLogin(){
wx.login({
success: res=>{
this.setData({
code: res.code
})
}
})
},
/**
* 用户输入手机号码
*/
userPhone(e){
this.setData({
phone: e.detail.value
})
},
/**
* 切换登录方式
*/
loginWayTab(){
this.setData({
loginWechat: !this.data.loginWechat
})
},
/**
* 手机号码授权
*/
PhoneNumber(e){
if(!e.detail.encryptedData){
this.wxLogin()
return
}
wx.$api.auth.authPhone({
code : this.data.code,
iv : e.detail.iv,
encryptedData : e.detail.encryptedData,
parent_id : this.data.parent_id
}).then(token=>{
// 存储登录信息
wx.setStorageSync('token', token.access_token)
if(token.has_nickname){
// 返回上一页
wx.navigateBack()
}else{
this.setData({
userInfo: !token.has_nickname
})
}
}).catch(()=>{
this.wxLogin()
})
},
/**
* 完善用户信息
*/
userInfo(e){
if(e.detail.auth){
wx.$api.auth.authInfo({
nickname : e.detail.userInfo.nickName,
avatar : e.detail.userInfo.avatarUrl
}).then(res=>{
// 返回上一页
wx.navigateBack()
})
}else{
wx.removeStorage({
key : token,
success : ()=>{
this.setData({
userInfo: false
})
}
})
}
},
/**
* 页面被卸载了
*/
onUnload(){
clearInterval(phoneOutTime)
this.setData({
codeTime : 60,
codeBtnStat : false
})
}
})