Skip to content

Let's use AI to Earn!

选择账户新曾分组

daoyianf9824b982f1f
diff --git a/src/components/ChooseAccountModule/chooseAccountModule.module.scss b/src/components/ChooseAccountModule/chooseAccountModule.module.scss
index 7e8f4685..5ad10f8f 100644
--- a/src/components/ChooseAccountModule/chooseAccountModule.module.scss
+++ b/src/components/ChooseAccountModule/chooseAccountModule.module.scss
@@ -131,6 +131,59 @@
}
}
+ .simpleAccountChoose-groups {
+ flex: 1;
+ max-height: 400px;
+ overflow-y: auto;
+
+ .ant-collapse {
+ border: none;
+ background: transparent;
+
+ .ant-collapse-item {
+ border: 1px solid #e9ecef;
+ border-radius: 8px;
+ margin-bottom: 12px;
+ background: #fff;
+
+ .ant-collapse-header {
+ padding: 12px 16px;
+ background: #f8f9fa;
+ border-radius: 8px 8px 0 0;
+ border-bottom: 1px solid #e9ecef;
+
+ .ant-collapse-header-text {
+ flex: 1;
+ }
+
+ .ant-collapse-expand-icon {
+ color: #625BF2;
+ }
+ }
+
+ .ant-collapse-content {
+ border: none;
+ background: transparent;
+
+ .ant-collapse-content-box {
+ padding: 16px;
+ }
+ }
+ }
+ }
+
+ .simpleAccountChoose-group-header {
+ display: flex;
+ align-items: center;
+ width: 100%;
+
+ .ant-checkbox-wrapper {
+ font-weight: 500;
+ color: #333;
+ }
+ }
+ }
+
.simpleAccountChoose-accounts {
flex: 1;
display: grid;
diff --git a/src/components/ChooseAccountModule/components/SimpleAccountChoose.tsx b/src/components/ChooseAccountModule/components/SimpleAccountChoose.tsx
index 44789222..a233173c 100644
--- a/src/components/ChooseAccountModule/components/SimpleAccountChoose.tsx
+++ b/src/components/ChooseAccountModule/components/SimpleAccountChoose.tsx
@@ -14,6 +14,7 @@ import {
Checkbox,
Empty,
Tooltip,
+ Collapse,
} from "antd";
import { CheckOutlined } from "@ant-design/icons";
import { useShallow } from "zustand/react/shallow";
@@ -21,6 +22,17 @@ import { AccountPlatInfoMap } from "@/app/config/platConfig";
import { useAccountStore } from "@/store/account";
import Link from "next/link";
import { SocialAccount } from "@/api/types/account.type";
+import { getAccountGroupApi } from "@/api/account";
+import { AccountGroupItem } from "@/api/types/account.type";
+
+// 分组类型定义
+interface AccountGroup {
+ id: string;
+ name: string;
+ rank: number;
+ isDefault: boolean;
+ children?: SocialAccount[];
+}
export interface ISimpleAccountChooseRef {
/**
@@ -42,6 +54,8 @@ export interface ISimpleAccountChooseProps {
disableAllSelect?: boolean;
// 是否可以取消已经选择的账户,默认为 false
isCancelChooseAccount?: boolean;
+ // 是否显示分组,默认为 true
+ showGroup?: boolean;
}
const SimpleAccountChoose = memo(
@@ -52,11 +66,14 @@ const SimpleAccountChoose = memo(
choosedAccounts,
disableAllSelect = false,
isCancelChooseAccount = false,
+ showGroup = true,
}: ISimpleAccountChooseProps,
ref: ForwardedRef<ISimpleAccountChooseRef>,
) => {
// 当前选择的账户数据
const [choosedAccountsList, setChoosedAccountsList] = useState<SocialAccount[]>([]);
+ // 分组数据
+ const [accountGroupList, setAccountGroupList] = useState<AccountGroup[]>([]);
// 每次change操作的数据
const recentData = useRef<SocialAccount>();
const { accountList } = useAccountStore(
@@ -65,6 +82,53 @@ const SimpleAccountChoose = memo(
})),
);
+ // 获取分组数据
+ const fetchAccountGroups = async () => {
+ try {
+ const res = await getAccountGroupApi();
+ const groupList = res?.data;
+
+ if (!groupList || groupList.length === 0) return;
+
+ const accountGroupList: AccountGroup[] = [];
+ // key=组ID,val=账户组
+ const accountGroupMap = new Map<string, AccountGroup>();
+
+ const defaultGroup = groupList.find((v) => v.isDefault)!;
+
+ groupList.map((v) => {
+ const accountGroupItem = {
+ ...v,
+ children: [],
+ };
+ accountGroupList.push(accountGroupItem);
+ accountGroupMap.set(v.id, accountGroupItem);
+ });
+
+ accountList.map((v) => {
+ (
+ accountGroupMap.get(v.groupId!) ||
+ accountGroupMap.get(defaultGroup.id)!
+ ).children?.push(v);
+ });
+
+ accountGroupList.sort((a, b) => {
+ return a.rank - b.rank;
+ });
+
+ setAccountGroupList(accountGroupList);
+ } catch (error) {
+ console.error('获取账户分组失败:', error);
+ }
+ };
+
+ // 初始化分组数据
+ useEffect(() => {
+ if (showGroup && accountList.length > 0) {
+ fetchAccountGroups();
+ }
+ }, [showGroup, accountList]);
+
// change事件
useEffect(() => {
if (!recentData.current) return;
@@ -102,6 +166,122 @@ const SimpleAccountChoose = memo(
});
};
+ // 选择/取消选择分组内所有账户
+ const handleSelectGroup = (group: AccountGroup, checked: boolean) => {
+ if (!group.children || group.children.length === 0) return;
+
+ recentData.current = group.children[0];
+ setChoosedAccountsList(prev => {
+ const groupAccountIds = group.children!.map(account => account.id);
+ const selectedInGroup = prev.filter(account => groupAccountIds.includes(account.id));
+
+ if (checked) {
+ // 选择分组内所有账户
+ const otherAccounts = prev.filter(account => !groupAccountIds.includes(account.id));
+ return [...otherAccounts, ...group.children!];
+ } else {
+ // 取消选择分组内所有账户
+ return prev.filter(account => !groupAccountIds.includes(account.id));
+ }
+ });
+ };
+
+ // 渲染单个账户
+ const renderAccount = (account: SocialAccount) => {
+ const platInfo = AccountPlatInfoMap.get(account.type);
+ const isSelected = choosedAccountsList.some(item => item.id === account.id);
+ const isDisable = choosedAccounts?.find((k) => k.id === account.id) && isCancelChooseAccount;
+
+ return (
+ <div
+ key={account.id}
+ className={[
+ "simpleAccountChoose-accounts-item",
+ isSelected && "simpleAccountChoose-accounts-item--active",
+ isDisable && "simpleAccountChoose-accounts-item--disable",
+ ].join(" ")}
+ onClick={() => {
+ if (isDisable) return;
+ handleSelectAccount(account);
+ }}
+ >
+ <Tooltip
+ title={
+ <>
+ <p>昵称:{account.nickname}</p>
+ <p>平台:{platInfo?.name}</p>
+ </>
+ }
+ >
+ <div className="simpleAccountChoose-accounts-item-avatar">
+ <Avatar src={account.avatar} size="large" />
+ {platInfo && (
+ <div className="simpleAccountChoose-accounts-item-platform">
+ <img src={platInfo.icon} alt={platInfo.name} />
+ </div>
+ )}
+ </div>
+ <span className="simpleAccountChoose-accounts-item-nickname">
+ {account.nickname}
+ </span>
+ </Tooltip>
+
+ <div className="simpleAccountChoose-accounts-item-choose">
+ <CheckOutlined />
+ </div>
+ </div>
+ );
+ };
+
+ // 渲染分组内容
+ const renderGroupContent = () => {
+ if (!showGroup || accountGroupList.length === 0) {
+ return (
+ <div className="simpleAccountChoose-accounts">
+ {accountList.map(renderAccount)}
+ </div>
+ );
+ }
+
+ return (
+ <Collapse
+ defaultActiveKey={accountGroupList.map(group => group.id)}
+ className="simpleAccountChoose-groups"
+ >
+ {accountGroupList.map((group) => {
+ const groupAccountIds = group.children?.map(account => account.id) || [];
+ const selectedInGroup = choosedAccountsList.filter(account =>
+ groupAccountIds.includes(account.id)
+ );
+ const isGroupSelected = selectedInGroup.length === group.children?.length;
+ const isGroupIndeterminate = selectedInGroup.length > 0 && selectedInGroup.length < (group.children?.length || 0);
+
+ return (
+ <Collapse.Panel
+ key={group.id}
+ header={
+ <div className="simpleAccountChoose-group-header">
+ <Checkbox
+ indeterminate={isGroupIndeterminate}
+ checked={isGroupSelected}
+ onChange={(e) => handleSelectGroup(group, e.target.checked)}
+ onClick={(e) => e.stopPropagation()}
+ >
+ {group.name} ({group.children?.length || 0})
+ </Checkbox>
+ </div>
+ }
+ >
+ <div className="simpleAccountChoose-accounts">
+ {group.children?.map(renderAccount)}
+ </div>
+ </Collapse.Panel>
+ );
+ })}
+ </Collapse>
+ );
+ };
+
return (
<div className={styles.simpleAccountChoose}>
{accountList.length === 0 ? (
@@ -135,53 +315,7 @@ const SimpleAccountChoose = memo(
</span>
</div>
- <div className="simpleAccountChoose-accounts">
- {accountList.map((account) => {
- const platInfo = AccountPlatInfoMap.get(account.type);
- const isSelected = choosedAccountsList.some(item => item.id === account.id);
- const isDisable = choosedAccounts?.find((k) => k.id === account.id) && isCancelChooseAccount;
-
- return (
- <div
- key={account.id}
- className={[
- "simpleAccountChoose-accounts-item",
- isSelected && "simpleAccountChoose-accounts-item--active",
- isDisable && "simpleAccountChoose-accounts-item--disable",
- ].join(" ")}
- onClick={() => {
- if (isDisable) return;
- handleSelectAccount(account);
- }}
- >
- <Tooltip
- title={
- <>
- <p>昵称:{account.nickname}</p>
- <p>平台:{platInfo?.name}</p>
- </>
- }
- >
- <div className="simpleAccountChoose-accounts-item-avatar">
- <Avatar src={account.avatar} size="large" />
- {platInfo && (
- <div className="simpleAccountChoose-accounts-item-platform">
- <img src={platInfo.icon} alt={platInfo.name} />
- </div>
- )}
- </div>
- <span className="simpleAccountChoose-accounts-item-nickname">
- {account.nickname}
- </span>
- </Tooltip>
-
- <div className="simpleAccountChoose-accounts-item-choose">
- <CheckOutlined />
- </div>
- </div>
- );
- })}
- </div>
+ {renderGroupContent()}
</>
)}
</div>