Files
venue_wx/pages/login/index.js
2021-05-27 22:43:12 +08:00

173 lines
3.3 KiB
JavaScript

// pages/login/index.js
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
showUnclickMask: false,
isGoHome: false,
isLogin: false,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function() {
let pages = getCurrentPages();
this.setData({
isGoHome: pages.length == 1
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function() {
if (this.data.isLogin) {
this.pushRoute()
return;
}
let that = this;
this.setData({
showUnclickMask: false
})
},
pushRoute() {
let route = wx.getStorageSync('history');
wx.removeStorageSync('history');
if (!route || route == '/pages/login/index') {
return wx.redirectTo({
url: '/pages/mine/index',
})
}
wx.redirectTo({
url: route,
})
},
changeLoginStatus() {
this.setData({
isLogin: true
})
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {
return {
title: '智慧云馆'
}
},
// 登录
loginClick() {
wx.navigateTo({
url: '/pages/loginIndex/index',
})
},
// 注册
registerClick(e) {
wx.redirectTo({
url: '/pages/register/index?codeType=' + 2,
})
},
// 微信登录
wxLogin(e) {
if (e.detail.errMsg == 'getUserInfo:fail auth deny') {
return;
}
this.setData({
showUnclickMask: true
})
let that = this;
wx.getUserProfile({
desc: '用于完善会员资料',
success: (res) => {
wx.login({
success: function(r){
app.$api.memberLoginWX({
avatar: res.userInfo.avatarUrl,
code: r.code,
nickname: res.userInfo.nickName
}).then(res => {
wx.setStorageSync('information', res.information);
if (!res.authorization) {
wx.redirectTo({
url: '/pages/register/index?codeType=' + 1,
})
return;
}
wx.setStorageSync('accessToken', res.authorization.accessToken);
let route = wx.getStorageSync('history');
wx.removeStorageSync('history');
if (!route || route == '/pages/login/index') {
return wx.redirectTo({
url: '/pages/mine/index',
})
}
wx.redirectTo({
url: route,
})
}, err => {
that.setData({
showUnclickMask: false
})
});
},
fail: function(res) {
that.setData({
showUnclickMask: false
})
},
})
},
fail: (res) => {
that.setData({
showUnclickMask: false
})
},
})
},
})