// pages/mine/index.js const app = getApp(); const AuthUtil = require("../../utils/auth"); Page({ /** * 页面的初始数据 */ data: { userInfo: {}, isLoggedIn: false, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) {}, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ onShow() { // 每次页面显示时更新用户信息 this.updateUserInfo(); }, /** * 生命周期函数--监听页面隐藏 */ onHide() {}, /** * 生命周期函数--监听页面卸载 */ onUnload() {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ onShareAppMessage() {}, /** * 更新用户信息 */ updateUserInfo() { // 从认证工具获取用户信息 const userInfo = AuthUtil.getUserInfo(); const isLoggedIn = AuthUtil.hasToken() && app.globalData.isLoggedIn; this.setData({ userInfo: userInfo || {}, isLoggedIn: isLoggedIn, }); }, /** * 跳转到编辑个人信息页面 */ goToEditProfile() { if (!this.data.isLoggedIn) { wx.showToast({ title: '请先登录', icon: 'none' }); return; } wx.navigateTo({ url: '/pages/edit-profile/index' }); }, /** * 处理注销登录 */ handleLogout() { wx.showModal({ title: "确认注销", content: "您确定要退出登录吗?", success: (res) => { if (res.confirm) { // 使用认证工具清除登录状态 AuthUtil.clearAuth(); // 提示用户 wx.showToast({ title: "已注销登录", icon: "success", }); // 跳转到登录页 setTimeout(() => { wx.reLaunch({ url: "/pages/login/index", }); }, 1500); } }, }); }, });