Files
venue_wx/pages/login/index.js

170 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// pages/login/index.js
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
showUnclickMask: false,
isGoHome: false,
isAgree: 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: '智慧云馆'
}
},
// 跳转到协议
pushAgreement() {
wx.navigateTo({
url: '/pages/agreement/index',
})
},
checkboxChange(e) {
this.setData({
isAgree: e.detail.value.length > 0
})
},
// 手机号一键登录/注册
onGetPhoneNumber(e) {
if (!this.data.isAgree) {
wx.showToast({
title: '请阅读并同意服务条款及隐私政策',
icon: 'none',
duration: 2000
})
return;
}
if (e.detail.errMsg === 'getPhoneNumber:fail user deny') {
return;
}
this.setData({ showUnclickMask: true });
let that = this;
// 前端获取到手机号授权 code 后,再去获取 wx.login 的 code
wx.login({
success: function (r) {
// 调用后端接口。
// 注意:这里复用了 memberLoginWXPhone。请让后端把接收参数改为 phoneCode 和 loginCode
// 或者您自己在 api.js 里新建一个专属的一键登录接口。后端收到 phoneCode 后去微信服换取真实手机号并完成注册登录。
app.$api.memberLoginWXPhone({
loginCode: r.code,
phoneCode: e.detail.code
}).then(res => {
if (!res.authorization) {
that.setData({ showUnclickMask: false });
wx.showToast({ title: '登录失败未返回Token', icon: 'none' });
return;
}
wx.setStorageSync('information', res.information);
wx.setStorageSync('accessToken', res.authorization.accessToken);
that.setData({ showUnclickMask: false });
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 })
}).catch(err => {
that.setData({ showUnclickMask: false });
});
},
fail: function (res) {
that.setData({ showUnclickMask: false });
}
})
}
})