findme-miniprogram-frontend/subpackages/social/friend-selector/friend-selector.js
2025-12-27 17:16:03 +08:00

176 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 选择好友页面
const app = getApp();
const apiClient = require('../../../utils/api-client.js');
Page({
data: {
// 好友列表
friends: [],
// 选中的好友列表(多选)
selectedFriends: [],
// 搜索关键词
searchKeyword: '',
// 加载状态
loading: false,
// 模式partial_visible 或 exclude_friends
mode: 'partial_visible'
},
onLoad(options) {
// 获取传递的参数
let selectedFriendsStr = options.selectedFriends || '[]';
const mode = options.mode || 'partial_visible';
// 如果参数被编码了,需要解码
try {
selectedFriendsStr = decodeURIComponent(selectedFriendsStr);
} catch (e) {
// 如果解码失败,使用原始值
console.log('参数未编码,使用原始值');
}
try {
const selectedFriends = JSON.parse(selectedFriendsStr);
this.setData({
selectedFriends: selectedFriends || [],
mode: mode
});
} catch (e) {
console.error('解析选中的好友失败:', e);
this.setData({
selectedFriends: [],
mode: mode
});
}
// 加载好友列表(在设置 selectedFriends 之后)
this.loadFriends();
},
// 加载好友列表
loadFriends() {
// TODO: 调用API加载好友列表
this.setData({ loading: true });
// 示例数据
setTimeout(() => {
const friends = [
{ id: 1, nickname: '张三', avatar: '/images/default-avatar.png', customId: 'user001' },
{ id: 2, nickname: '李四', avatar: '/images/default-avatar.png', customId: 'user002' },
{ id: 3, nickname: '王五', avatar: '/images/default-avatar.png', customId: 'user003' },
{ id: 4, nickname: '赵六', avatar: '/images/default-avatar.png', customId: 'user004' },
{ id: 5, nickname: '孙七', avatar: '/images/default-avatar.png', customId: 'user005' },
{ id: 6, nickname: '周八', avatar: '/images/default-avatar.png', customId: 'user006' }
];
// 为每个好友添加 isSelected 属性
this.updateFriendsSelection(friends);
this.setData({
friends: friends,
loading: false
});
}, 500);
},
// 更新好友的选中状态
updateFriendsSelection(friends) {
const { selectedFriends } = this.data;
if (!friends || !selectedFriends) return;
friends.forEach(friend => {
friend.isSelected = selectedFriends.some(selected => selected.id === friend.id);
});
},
// 选择好友(多选)
selectFriend(e) {
const friendId = e.currentTarget.dataset.friendId;
const friend = this.data.friends.find(f => f.id === friendId);
if (!friend) return;
const selectedFriends = [...this.data.selectedFriends];
const index = selectedFriends.findIndex(f => f.id === friendId);
if (index > -1) {
// 取消选择
selectedFriends.splice(index, 1);
friend.isSelected = false;
} else {
// 选择
selectedFriends.push(friend);
friend.isSelected = true;
}
this.setData({
selectedFriends: selectedFriends,
friends: this.data.friends
});
},
// 搜索输入
onSearchInput(e) {
this.setData({
searchKeyword: e.detail.value
});
},
// 确认选择
confirmSelection() {
const { selectedFriends, mode } = this.data;
// 检查是否选择了好友
if (!selectedFriends || selectedFriends.length === 0) {
wx.showToast({
title: '请选择好友',
icon: 'none',
duration: 2000
});
return;
}
// 计算好友总数
const friendsCount = selectedFriends.length;
// 生成显示文本:多个好友昵称,用逗号分隔
const displayText = selectedFriends.map(friend => friend.nickname).join('');
// 返回上一页并传递选中的好友
const pages = getCurrentPages();
const prevPage = pages[pages.length - 2];
if (prevPage) {
if (mode === 'exclude_friends') {
// 排除模式
if (typeof prevPage.updateExcludeFriends === 'function') {
prevPage.updateExcludeFriends(selectedFriends, friendsCount, displayText);
}
} else {
// 部分可见模式
if (typeof prevPage.updateSelectedFriends === 'function') {
prevPage.updateSelectedFriends(selectedFriends, friendsCount, displayText);
}
}
}
wx.navigateBack();
},
// 获取过滤后的好友列表
getFilteredFriends() {
const { friends, searchKeyword } = this.data;
if (!searchKeyword) {
return friends;
}
return friends.filter(friend => {
return friend.nickname.toLowerCase().includes(searchKeyword.toLowerCase());
});
},
// 返回
navigateBack() {
wx.navigateBack();
}
});