// 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 }) }, // 微信一键登录 onWechatLogin() { if (!this.data.isAgree) { wx.showToast({ title: '请阅读并同意服务条款及隐私政策', icon: 'none', duration: 2000 }) return; } this.setData({ showUnclickMask: true }); let that = this; // 前端获取 code 后去换取 token wx.login({ success: function (r) { app.$api.memberLoginWX({ code: r.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 }); that.changeLoginStatus(); 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 }); } }) }, // 暂不登录,退回上一页 skipLogin() { let pages = getCurrentPages(); if (pages.length <= 1) { wx.redirectTo({ url: '/pages/home/index', }); } else { wx.navigateBack(); } } })