初始
This commit is contained in:
97
pages/mine/mine.js
Normal file
97
pages/mine/mine.js
Normal file
@@ -0,0 +1,97 @@
|
||||
// pages/mine/mine.js
|
||||
const app = getApp();
|
||||
|
||||
Page({
|
||||
data: {
|
||||
userInfo: null,
|
||||
isLogin: false,
|
||||
menuList: [
|
||||
{ id: 'favorite', icon: 'star-o', title: '我的收藏', url: '/pages/favorite/favorite' },
|
||||
{ id: 'history', icon: 'underway-o', title: '浏览历史', url: '/pages/history/history' },
|
||||
{ id: 'feedback', icon: 'comment-o', title: '意见反馈', url: '/pages/feedback/feedback' },
|
||||
{ id: 'about', icon: 'info-o', title: '关于我们', url: '/pages/about/about' },
|
||||
{ id: 'setting', icon: 'setting-o', title: '设置', url: '/pages/setting/setting' }
|
||||
]
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.checkLoginStatus();
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.checkLoginStatus();
|
||||
// 初始化 TabBar 选中状态
|
||||
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
|
||||
this.getTabBar().init();
|
||||
}
|
||||
},
|
||||
|
||||
// 检查登录状态
|
||||
checkLoginStatus() {
|
||||
const isLogin = app.checkLogin();
|
||||
const userInfo = app.globalData.userInfo;
|
||||
this.setData({ isLogin, userInfo });
|
||||
},
|
||||
|
||||
// 获取用户信息(微信登录)
|
||||
onGetUserInfo() {
|
||||
wx.getUserProfile({
|
||||
desc: '用于完善用户资料',
|
||||
success: (res) => {
|
||||
const userInfo = res.userInfo;
|
||||
app.globalData.userInfo = userInfo;
|
||||
this.setData({ userInfo, isLogin: true });
|
||||
|
||||
// 模拟设置token
|
||||
app.setToken('mock_token_' + Date.now());
|
||||
|
||||
wx.showToast({
|
||||
title: '登录成功',
|
||||
icon: 'success'
|
||||
});
|
||||
},
|
||||
fail: () => {
|
||||
wx.showToast({
|
||||
title: '已取消授权',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 点击菜单项
|
||||
onMenuTap(e) {
|
||||
const { url, id } = e.currentTarget.dataset;
|
||||
|
||||
// 某些功能需要登录
|
||||
if (['favorite', 'history'].includes(id) && !this.data.isLogin) {
|
||||
wx.showToast({
|
||||
title: '请先登录',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
wx.navigateTo({ url });
|
||||
},
|
||||
|
||||
// 退出登录
|
||||
onLogout() {
|
||||
wx.showModal({
|
||||
title: '提示',
|
||||
content: '确定要退出登录吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
app.clearToken();
|
||||
app.globalData.userInfo = null;
|
||||
this.setData({ isLogin: false, userInfo: null });
|
||||
|
||||
wx.showToast({
|
||||
title: '已退出登录',
|
||||
icon: 'success'
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user