刘嘉昕 | 9cdeca2 | 2025-06-03 16:54:30 +0800 | [diff] [blame] | 1 | import React, { useState, useEffect } from 'react'; |
| 2 | import { addFriend, deleteFriend, getFriendsByUserId } from '../api/friends'; |
| 3 | |
| 4 | const FriendManager = ({ currentUser, onSelectRelation }) => { |
| 5 | const [friendId, setFriendId] = useState(''); |
| 6 | const [friends, setFriends] = useState([]); |
| 7 | const [isLoading, setIsLoading] = useState(false); |
| 8 | const [isRefreshing, setIsRefreshing] = useState(false); |
| 9 | |
| 10 | useEffect(() => { |
| 11 | if (currentUser?.id) loadFriends(currentUser.id); |
| 12 | }, [currentUser]); |
| 13 | |
| 14 | const loadFriends = async (userid) => { |
| 15 | setIsRefreshing(true); |
| 16 | try { |
| 17 | const res = await getFriendsByUserId(userid); |
| 18 | setFriends(res.data); |
| 19 | } catch (err) { |
| 20 | console.error('加载好友失败', err); |
| 21 | alert('加载好友失败,请稍后重试'); |
| 22 | } |
| 23 | setIsRefreshing(false); |
| 24 | }; |
| 25 | |
| 26 | const handleFriendIdChange = (e) => { |
| 27 | const value = e.target.value; |
| 28 | if (/^\d*$/.test(value)) setFriendId(value); |
| 29 | }; |
| 30 | |
| 31 | const handleAddFriend = async () => { |
| 32 | if (!friendId) return alert('请输入好友ID'); |
| 33 | |
| 34 | const newFriendId = parseInt(friendId, 10); |
| 35 | if (newFriendId === currentUser.id) return alert('不能添加自己为好友'); |
| 36 | |
| 37 | if (friends.some(f => f.friend1 === newFriendId || f.friend2 === newFriendId)) { |
| 38 | return alert('该用户已是您的好友'); |
| 39 | } |
| 40 | |
| 41 | setIsLoading(true); |
| 42 | try { |
| 43 | const res = await addFriend({ friend1: currentUser.id, friend2: newFriendId }); |
| 44 | if (res.data) { |
| 45 | alert('添加成功'); |
| 46 | setFriendId(''); |
| 47 | loadFriends(currentUser.id); |
| 48 | } else { |
| 49 | alert('添加失败'); |
| 50 | } |
| 51 | } catch (err) { |
| 52 | alert('添加好友失败'); |
| 53 | console.error(err); |
| 54 | } |
| 55 | setIsLoading(false); |
| 56 | }; |
| 57 | |
| 58 | /* ---------- 这里开始:删除好友逻辑改为 friend1 + friend2 ---------- */ |
| 59 | const handleDelete = async (friend1, friend2) => { |
| 60 | if (!window.confirm('确认删除该好友吗?')) return; |
| 61 | setIsLoading(true); |
| 62 | try { |
| 63 | const res = await deleteFriend(friend1, friend2); |
| 64 | if (res.data) { |
| 65 | alert('删除成功'); |
| 66 | loadFriends(currentUser.id); |
| 67 | } else { |
| 68 | alert('删除失败'); |
| 69 | } |
| 70 | } catch (err) { |
| 71 | alert('删除好友失败'); |
| 72 | console.error(err); |
| 73 | } |
| 74 | setIsLoading(false); |
| 75 | }; |
| 76 | /* ------------------------------------------------------------------- */ |
| 77 | |
| 78 | return ( |
| 79 | <div className="max-w-xl mx-auto p-4"> |
| 80 | <h2 className="text-2xl font-bold mb-4">好友管理</h2> |
| 81 | |
| 82 | {/* 添加好友区域 */} |
| 83 | <div className="mb-6 space-y-2"> |
| 84 | <input |
| 85 | type="text" |
| 86 | placeholder="输入好友的用户ID" |
| 87 | value={friendId} |
| 88 | onChange={handleFriendIdChange} |
| 89 | className="border p-2 rounded w-full" |
| 90 | /> |
| 91 | <button |
| 92 | onClick={handleAddFriend} |
| 93 | disabled={isLoading} |
| 94 | className={`bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600 ${isLoading ? 'opacity-50 cursor-not-allowed' : ''}`} |
| 95 | > |
| 96 | {isLoading ? '添加中…' : '添加好友'} |
| 97 | </button> |
| 98 | </div> |
| 99 | |
| 100 | {/* 好友列表 */} |
| 101 | <div className="flex justify-between items-center mb-3"> |
| 102 | <h3 className="text-xl font-semibold">我的好友列表</h3> |
| 103 | <button |
| 104 | onClick={() => loadFriends(currentUser.id)} |
| 105 | disabled={isRefreshing} |
| 106 | className="text-sm text-blue-500 hover:underline disabled:text-gray-400" |
| 107 | > |
| 108 | {isRefreshing ? '刷新中…' : '刷新'} |
| 109 | </button> |
| 110 | </div> |
| 111 | |
| 112 | {friends.length === 0 ? ( |
| 113 | <p className="text-gray-500">暂无好友</p> |
| 114 | ) : ( |
| 115 | <ul className="space-y-3"> |
| 116 | {friends.map((f) => { |
| 117 | const friendUserId = |
| 118 | f.friend1 === currentUser.id ? f.friend2 : f.friend1; |
| 119 | return ( |
| 120 | <li |
| 121 | key={f.relationid} |
| 122 | className="border p-3 rounded flex justify-between items-center hover:bg-gray-100 cursor-pointer" |
| 123 | onClick={() => |
| 124 | onSelectRelation({ |
| 125 | relationid: f.relationid, |
| 126 | friendId: friendUserId, |
| 127 | }) |
| 128 | } |
| 129 | > |
| 130 | <div> |
| 131 | <p>好友用户ID:{friendUserId}</p> |
| 132 | <p className="text-sm text-gray-500"> |
| 133 | 添加时间:{new Date(f.requestTime).toLocaleString()} |
| 134 | </p> |
| 135 | </div> |
| 136 | <button |
| 137 | onClick={(e) => { |
| 138 | e.stopPropagation(); |
| 139 | /* ------- 传入正确的 friend1 & friend2 -------- */ |
| 140 | handleDelete(f.friend1, f.friend2); |
| 141 | }} |
| 142 | className="text-red-500 hover:underline" |
| 143 | disabled={isLoading} |
| 144 | > |
| 145 | 删除 |
| 146 | </button> |
| 147 | </li> |
| 148 | ); |
| 149 | })} |
| 150 | </ul> |
| 151 | )} |
| 152 | </div> |
| 153 | ); |
| 154 | }; |
| 155 | |
| 156 | export default FriendManager; |