const config = require('../config/config.js'); const apiClient = require('../utils/api-client.js'); Component({ data: { selected: 0, color: '#999999', selectedColor: '#ffffff', showCameraAction: false, // 拍照弹窗显示状态 cameraActive: false, // 发布按钮激活状态 list: [ { pagePath: '/pages/map/map', text: '发现', iconPath: '/images/index/location.png', selectedIconPath: '/images/index/location-active.png' }, { pagePath: '/pages/circle/circle', text: '圈子', iconPath: '/images/index/circle.png', selectedIconPath: '/images/index/circle-active.png' }, { pagePath: '/subpackages/media/camera/camera', iconPath: '/images/index/phone.png', text: '发布', selectedIconPath: '/images/index/phone-active.png' }, { pagePath: '/pages/message/message', text: '聊天', iconPath: '/images/index/message.png', selectedIconPath: '/images/index/message-active.png' }, { pagePath: '/pages/social/friends/friends', text: '好友', iconPath: '/images/index/friend.png', selectedIconPath: '/images/index/friend-active.png' } ] }, /** * 用户点击右上角分享 */ onShareAppMessage(options) { // options.from 可以是 'button' 或 'menu' // 'button' 表示通过页面内的分享按钮触发 // 'menu' 表示通过右上角菜单的分享按钮触发 return { title: 'Find Me', // 分享标题 path: '/custom-tab-bar/index/index', // 分享路径,必须是以 / 开头的完整路径 imageUrl: '/images/findme-logo.png', // 分享图标,可以是本地图片或网络图片 success(res) { // 分享成功后的回调 console.log('分享成功', res); // 可以在这里添加统计代码等 }, fail(res) { // 分享失败后的回调 console.log('分享失败', res); } } }, attached() { // 已通过CSS调整适配所有设备 try { const counts = wx.getStorageSync('unreadCounts') || {}; const friends = Number(counts.friends || 0); const messages = Number(counts.messages || 0); if (friends || messages) { this.setData({ friendsBadge: friends, messagesBadge: messages }); } } catch (_) {} }, methods: { // 对外暴露:设置好友请求角标数量 setFriendsBadge(count) { const n = Number(count) || 0; if (this.data.friendsBadge === n) return; this.setData({ friendsBadge: n < 0 ? 0 : n }); }, // 对外暴露:设置消息未读角标数量 setMessagesBadge(count) { const n = Number(count) || 0; if (this.data.messagesBadge === n) return; this.setData({ messagesBadge: n < 0 ? 0 : n }); }, // 清除所有角标 clearBadges() { this.setData({ friendsBadge: 0, messagesBadge: 0 }); }, switchTab(e) { const data = e.currentTarget.dataset; const url = data.path; const index = data.index; // 获取当前页面信息 const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; // 检查是否是"发布"按钮(索引2)- 直接跳转到编辑页面 if (index === 2) { // 切换发布按钮激活状态 this.setData({ cameraActive: !this.data.cameraActive }); // 检查登录状态 const app = getApp(); if (!app.globalData.isLoggedIn) { wx.navigateTo({ url: '/pages/login/login?from=camera' }); return; } // 已登录时直接跳转到编辑页面 wx.navigateTo({ url: '/subpackages/media/edits/edits', fail: (err) => { console.error('跳转编辑页面失败:', err); wx.showToast({ title: '跳转失败,请重试', icon: 'none' }); // 跳转失败时重置激活状态 this.setData({ cameraActive: false }); } }); return; } // 切换到其他按钮时,取消发布按钮激活状态 if (this.data.cameraActive) { this.setData({ cameraActive: false }); } // 先更新选中状态(其他 tabBar 页面) this.setData({ selected: index }); // 检查是否是"发现"按钮(索引0) if (index === 0) { // 检查当前是否已经在地图页面 if (currentPage.route === 'pages/map/map') { // 如果已经在地图页面,直接打开地点收藏栏 currentPage.onOpenLocationFavoriteBar(); } else { const app = getApp(); // 如果不在地图页面,先跳转到地图页面,然后通过全局变量标记需要打开收藏栏 wx.switchTab({ url, success: () => { // 设置全局变量,让地图页面加载后知道需要打开收藏栏 app.globalData.needOpenLocationFavoriteBar = true; } }); } return; } // 检查是否是"圈子"按钮(索引1) if (index === 1) { // 检查登录状态 const app = getApp(); if (!app.globalData.isLoggedIn) { wx.navigateTo({ url: '/pages/login/login?from=circle' }); return; } // 正常跳转到圈子页面 wx.switchTab({ url }); return; } // 检查是否是"聊天"按钮(索引3) if (index === 3) { // 检查登录状态 const app = getApp(); if (!app.globalData.isLoggedIn) { wx.navigateTo({ url: '/pages/login/login?from=message' }); return; } // 已登录时正常跳转 wx.switchTab({ url }); return; } // 检查是否是"我的"按钮(索引4)- 跳转到好友列表页面 if (index === 4) { // 检查登录状态 const app = getApp(); if (!app.globalData.isLoggedIn) { wx.navigateTo({ url: '/pages/login/login?from=friends' }); return; } // 已登录时跳转到好友列表页面(使用 switchTab,因为它在 tabBar 中) wx.switchTab({ url: '/pages/social/friends/friends' }); return; } // 其他按钮正常跳转 wx.switchTab({ url }); }, // 显示拍照弹窗 showCameraActionSheet() { this.setData({ showCameraAction: true }); }, // 隐藏拍照弹窗 hideCameraActionSheet() { this.setData({ showCameraAction: false, cameraActive: false }); }, // 拍照 takePhoto() { this.hideCameraActionSheet(); wx.navigateTo({ url: '/subpackages/media/camera/camera', fail: (err) => { console.error('跳转拍照页面失败:', err); wx.showToast({ title: '跳转失败,请重试', icon: 'none' }); } }); }, // 从相册选择 chooseImage() { this.hideCameraActionSheet(); wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album'], success: (res) => { const tempFilePath = res.tempFilePaths[0]; wx.showLoading({ title: '上传中...', mask: true }); // 上传图片 this.uploadImage(tempFilePath) .then(imageUrl => { wx.hideLoading(); if (imageUrl) { wx.navigateTo({ url: `/subpackages/media/edits/edits?imagePath=${encodeURIComponent(imageUrl)}`, fail: (err) => { console.error('跳转编辑页失败:', err); wx.showToast({ title: '跳转失败,请重试', icon: 'none' }); } }); } }) .catch(error => { wx.hideLoading(); console.error('上传失败:', error); wx.showToast({ title: error.message || '上传失败,请重试', icon: 'none' }); }); }, fail: (error) => { console.error('选择图片失败:', error); if (error.errMsg && !error.errMsg.includes('cancel')) { wx.showToast({ title: '选择图片失败', icon: 'none' }); } } }); }, // 上传图片 uploadImage(tempFilePath) { return new Promise((resolve, reject) => { wx.uploadFile({ url: `${config.api.baseUrl}/api/v1/file/upload`, filePath: tempFilePath, name: 'file', formData: { file_type: 'image', usage_type: 'feed' }, header: { 'Authorization': `Bearer ${apiClient.getToken()}` }, success: (uploadRes) => { if (apiClient.is401Error(uploadRes)) { const app = getApp(); const isLoggedIn = app?.globalData?.isLoggedIn || false; apiClient.handle401Error(isLoggedIn); if (isLoggedIn) { reject(new Error('登录已过期,请重新登录')); } else { resolve(null); } return; } try { const data = JSON.parse(uploadRes.data); if (data.code === 0) { const fileData = data?.data?.data || data?.data || {}; const imageUrl = fileData.file_url || fileData.fileUrl || fileData.url; if (imageUrl) { resolve(imageUrl); } else { reject(new Error('上传成功但未获取到图片URL')); } } else { reject(new Error(data.message || '上传失败')); } } catch (error) { reject(new Error('响应解析失败')); } }, fail: (error) => { console.error('上传失败:', error); reject(new Error(error.errMsg || '上传失败')); } }); }); } } });