123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- Page({
- data: {
- isLogin: false, // 登录状态
- userInfo: {}, // 存储用户信息(头像和昵称)
- },
- // 页面加载时获取缓存的昵称和头像
- onLoad() {
- const storedUserInfo = wx.getStorageSync('userinfo'); // 获取缓存中的用户信息
- if (storedUserInfo) {
- // 如果缓存中有用户信息,更新页面上的数据
- this.setData({
- userInfo: storedUserInfo, // 更新用户信息
- isLogin: true, // 设置登录状态为 true
- });
- }
- },
- // 获取用户头像
- getAvatar(e) {
- const avatarUrl = e.detail.avatarUrl;
- this.setData({
- 'userInfo.avatarUrl': avatarUrl
- });
- // 保存头像到缓存
- let userInfo = wx.getStorageSync('userinfo') || {};
- userInfo.avatarUrl = avatarUrl;
- wx.setStorageSync('userinfo', userInfo);
- },
- // 获取用户昵称
- getName(e) {
- const nickName = e.detail.value;
- this.setData({
- 'userInfo.nickName': nickName
- });
- // 保存昵称到缓存
- let userInfo = wx.getStorageSync('userinfo') || {};
- userInfo.nickName = nickName;
- wx.setStorageSync('userinfo', userInfo);
- },
- onShow: function() {
-
- if (typeof this.getTabBar === 'function' && this.getTabBar()) {
- this.getTabBar().setData({
- selected: 3 //这个数字是当前页面在tabBar中list数组的索引
- })
- }
- },
- // 显示登录弹窗
- gologin() {
- this.setData({
- isHidden: false
- });
- },
- // 取消登录弹窗
- potNo() {
- this.setData({
- isHidden: true
- });
- },
- // 确认登录弹窗
- popYes() {
- const { avatarUrl, nickName } = this.data.userInfo;
- if (!avatarUrl || !nickName) {
- wx.showToast({
- icon: 'error',
- title: '请获取头像和昵称',
- });
- return;
- }
- // 保存头像和昵称到缓存
- wx.setStorageSync('userinfo', this.data.userInfo);
- this.setData({
- isLogin: true, // 设置登录状态为 true
- isHidden: true, // 隐藏弹窗
- });
- },
- goToThreshold() {
- wx.navigateTo({
- url: '/pages/threshold/threshold'
- });
- },
- /// 点击退出登录
- tuichu() {
- // 清除缓存
- wx.clearStorageSync();
- // 重置登录状态
- this.setData({
- isLogin: false,
- userInfo: {}
- });
- // 跳转到登录页面
- wx.redirectTo({
- url: '/pages/RoleSelectionPage/RoleSelectionPage' // 登录页面的路径
- });
- }
- });
|