const app = getApp() Page({ data: { frameStyle: { useTop: true, name: '系统', leftArrow: false, useBar: false }, }, toCommon: function (e) { const { route } = e.currentTarget.dataset; if (route) wx.navigateTo({ url: `/pages/${route}` }); }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, // 监听用户是否登录 watchLogin: async function () { // 监听用户是否登录, wx.getStorage({ key: 'user', success: async res => { if (res.data) wx.redirectTo({ url: '/pages/test/index' }) }, fail: async res => { wx.login({ success: async (arr) => { if (app.globalData?.wxInfo?.openid) { await this.toLogin(app.globalData?.wxInfo?.openid); return; } const { code: js_code } = arr; const aee = await app.$get('/wechat/api/login/app', { js_code: js_code, config: 'newCourtApp' }); if (aee.errcode === 0 && aee.data?.openid) { const openid = aee.data.openid; app.globalData.wxInfo = { openid }; await this.toLogin(openid); } else { wx.showToast({ title: `${aee.errmsg}`, icon: 'fail', duration: 2000 }); wx.redirectTo({ url: '/pages/register/index' }); } }, }); } }) }, async toLogin(openid) { const res = await app.$post('/user/wxAppLogin', { openid }); if (app.$checkRes(res)) { const { data } = res; // 没有用户,去注册 if (!data) wx.redirectTo({ url: '/pages/register/index' }); // 有用户,存起来,跳转 else { wx.setStorageSync('user', data); wx.redirectTo({ url: '/pages/school/index' }); } } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { const that = this; // 监听用户是否登录 that.watchLogin(); }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })