press-next
Version:
Vue3 组件库,支持 Composition API
461 lines (411 loc) • 13 kB
text/typescript
import { ref, reactive } from 'vue';
import { routerBack } from 'press-ui/common/utils/router';
import { treeDataList } from './list';
import type { TeamItem, TeamMap, TeamMember, ScoreItem, HistoryItem } from '../type';
// 类型定义
export interface RoundItem {
roundId: number;
started?: boolean;
}
export interface GroupItem {
id: string | number;
title: string;
teamCount?: number;
}
// 生成队员数据的辅助函数
function generateTeamMembers(count: number): TeamMember[] {
const members: TeamMember[] = [];
const nicknames = ['小明', '小红', '小刚', '小丽', '小华', '小强'];
const grades = [
{ gradePic: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/grade-bronze.png', gradeCnName: '青铜' },
{ gradePic: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/grade-silver.png', gradeCnName: '白银' },
{ gradePic: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/grade-gold.png', gradeCnName: '黄金' },
{ gradePic: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/grade-platinum.png', gradeCnName: '铂金' },
{ gradePic: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/grade-diamond.png', gradeCnName: '钻石' },
];
for (let i = 0; i < count; i++) {
const randomGrade = grades[Math.floor(Math.random() * grades.length)];
members.push({
uid: `uid_${Date.now()}_${i}`,
nick: `${nicknames[i % nicknames.length]}${i + 1}`,
head: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/teamate-default-avatar.png',
game_info: randomGrade,
});
}
return members;
}
// 模拟数据生成函数
function getTeamList(count = 20, startIndex = 0): TeamItem[] {
const list: TeamItem[] = [];
for (let i = 0; i < count; i++) {
const index = startIndex + i;
const memberCount = Math.floor(Math.random() * 6) + 1;
const members = generateTeamMembers(memberCount);
const capIndex = Math.floor(Math.random() * memberCount);
list.push({
id: index + 1,
teamname: `队伍${index + 1}`,
teamavatar: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/teamate-default-avatar.png',
storename: '网鱼网咖',
memnum: memberCount,
capuid: members[capIndex]?.uid,
members,
checked: Math.random() > 0.5,
disabled: Math.random() > 0.8,
isPass: Math.random() > 0.2,
});
}
return list;
}
function getGroupList(count = 10, startIndex = 0): GroupItem[] {
const list: GroupItem[] = [];
for (let i = 0; i < count; i++) {
const index = startIndex + i;
list.push({
id: index + 1,
title: `第${index + 1}组`,
teamCount: Math.floor(Math.random() * 8) + 3,
});
}
return list;
}
function getTeamScoreList(): ScoreItem[] {
return [
{
id: 1,
teamName: '王者战队',
teamAvatar: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/teamate-default-avatar.png',
totalScore: 2850,
allTotalIncome: 3200,
rank: 1,
historyScoreList: [
{ roundStr: '第1局', rank: 1, kill: 12, score: 950 },
{ roundStr: '第2局', rank: 2, kill: 10, score: 900 },
{ roundStr: '第3局', rank: 1, kill: 11, score: 1000 },
],
subwayBattleList: [
{ roundStr: '第1局', totalIncome: 1200, kill: 12, income: 400 },
{ roundStr: '第2局', totalIncome: 2100, kill: 10, income: 900 },
{ roundStr: '第3局', totalIncome: 3200, kill: 11, income: 1100 },
],
},
{
id: 2,
teamName: '无敌小队',
teamAvatar: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/teamate-default-avatar.png',
totalScore: 2720,
allTotalIncome: 2900,
rank: 2,
historyScoreList: [
{ roundStr: '第1局', rank: 2, kill: 9, score: 850 },
{ roundStr: '第2局', rank: 1, kill: 12, score: 1000 },
{ roundStr: '第3局', rank: 3, kill: 8, score: 870 },
],
subwayBattleList: [
{ roundStr: '第1局', totalIncome: 1000, kill: 9, income: 350 },
{ roundStr: '第2局', totalIncome: 1900, kill: 12, income: 900 },
{ roundStr: '第3局', totalIncome: 2900, kill: 8, income: 1000 },
],
},
{
id: 3,
teamName: '钢铁军团',
teamAvatar: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/teamate-default-avatar.png',
totalScore: 2650,
allTotalIncome: 2700,
rank: 3,
historyScoreList: [
{ roundStr: '第1局', rank: 3, kill: 8, score: 800 },
{ roundStr: '第2局', rank: 3, kill: 9, score: 850 },
{ roundStr: '第3局', rank: 2, kill: 10, score: 1000 },
],
subwayBattleList: [
{ roundStr: '第1局', totalIncome: 900, kill: 8, income: 300 },
{ roundStr: '第2局', totalIncome: 1700, kill: 9, income: 800 },
{ roundStr: '第3局', totalIncome: 2700, kill: 10, income: 1000 },
],
},
{
id: 4,
teamName: '闪电突击',
teamAvatar: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/teamate-default-avatar.png',
totalScore: 2580,
allTotalIncome: 2500,
rank: 4,
historyScoreList: [
{ roundStr: '第1局', rank: 4, kill: 7, score: 780 },
{ roundStr: '第2局', rank: 4, kill: 8, score: 800 },
{ roundStr: '第3局', rank: 4, kill: 9, score: 1000 },
],
subwayBattleList: [
{ roundStr: '第1局', totalIncome: 800, kill: 7, income: 280 },
{ roundStr: '第2局', totalIncome: 1600, kill: 8, income: 800 },
{ roundStr: '第3局', totalIncome: 2500, kill: 9, income: 900 },
],
},
{
id: 5,
teamName: '烈火战神',
teamAvatar: 'https://image-1251917893.file.myqcloud.com/tip-project/pubg/pubg-match/teamate-default-avatar.png',
totalScore: 2450,
allTotalIncome: 2300,
rank: 5,
historyScoreList: [
{ roundStr: '第1局', rank: 5, kill: 6, score: 750 },
{ roundStr: '第2局', rank: 5, kill: 7, score: 780 },
{ roundStr: '第3局', rank: 5, kill: 8, score: 920 },
],
subwayBattleList: [
{ roundStr: '第1局', totalIncome: 700, kill: 6, income: 250 },
{ roundStr: '第2局', totalIncome: 1500, kill: 7, income: 800 },
{ roundStr: '第3局', totalIncome: 2300, kill: 8, income: 800 },
],
},
];
}
function getDemoData() {
const teamList: TeamItem[] = getTeamList(20);
const groupMap: Record<number, { list: GroupItem[]; finished: boolean; loading: boolean }> = {
1: {
list: getGroupList(5),
finished: false,
loading: false,
},
2: {
list: getGroupList(3),
finished: false,
loading: false,
},
3: {
list: getGroupList(2),
finished: false,
loading: false,
},
};
const roundList: RoundItem[] = [
{ roundId: 1, started: true },
{ roundId: 2, started: false },
{ roundId: 3, started: false },
];
const toSelectNumList: number[] = [2, 3, 4, 5];
const groupList: GroupItem[] = getGroupList(8);
return {
teamList,
groupMap,
roundList,
toSelectNumList,
groupList,
};
}
// Vue3 Composition API 版本的 mixin 函数
export function getScheduleDemoMixin() {
// 响应式数据
const curTab = ref(0);
const teamMap = reactive<TeamMap>({
list: [],
finished: false,
loading: false,
});
const searchTeamMap = reactive<TeamMap>({
list: [],
finished: false,
loading: false,
});
const groupListMap = reactive<{ list: GroupItem[]; finished: boolean; loading: boolean }>({
list: [],
finished: false,
loading: false,
});
const groupMap = reactive<Record<number, { list: GroupItem[]; finished: boolean; loading: boolean }>>({
1: {
list: [],
finished: false,
loading: false,
},
});
const searchTeamMapInGroup = reactive<TeamMap>({
list: [],
finished: false,
loading: false,
});
const isAllConfirmed = ref(true);
const toSelectNumList = ref<number[]>([1]);
const numPerGroup = ref(1);
const curStartRoundId = ref(1);
const curScoreRoundId = ref(1);
const roundList = ref<RoundItem[]>([]);
const teamScoreMap = reactive<Record<string, { list: ScoreItem[]; loading: boolean; finished: boolean }>>({
['1-1']: {
list: getTeamScoreList(),
loading: false,
finished: false,
},
});
const teamMapInOneGroup = reactive<{ title: string; list: TeamItem[]; finished: boolean }>({
title: '第1组-5支队伍',
list: getTeamList(10),
finished: true,
});
const onlyShowDisabledButton = ref<number[]>([]);
const curGoingTab = ref(0);
const publishedScore = ref(false);
const showConfirmStartOB = ref(true);
const showPublishScoreOB = ref(true);
const historyScoreMap = reactive<Record<number, HistoryItem[]>>({});
// 使用 treeDataList 作为 scheList
const scheList = ref(treeDataList);
// 初始化数据
const initData = () => {
const { teamList, groupMap: gMap, roundList: rList, toSelectNumList: tsList, groupList } = getDemoData();
setTimeout(() => {
teamMap.list = teamList;
Object.assign(groupMap, gMap);
roundList.value = rList;
toSelectNumList.value = tsList;
groupListMap.list = groupList;
}, 1000);
};
const loadMoreCommon = (key: string, moreList: any, key2?: string | number) => {
const dataMap: any = {
teamMap,
searchTeamMap,
groupListMap,
groupMap,
searchTeamMapInGroup,
teamScoreMap,
};
let data = dataMap[key];
if (key2 !== undefined) {
data = data[key2];
}
return new Promise((resolve) => {
setTimeout(() => {
data.list = data.list.concat(moreList);
data.loading = false;
data.finished = data.list.length >= 50;
resolve(1);
}, 300);
});
};
// 事件处理函数
const close = () => {
routerBack();
};
const loadMoreData = (key: string) => {
const loadMoreMap: Record<string, () => void> = {
teamMap: loadMoreTeamList,
searchTeamMap: loadMoreSearchTeamList,
groupListMap: loadMoreGroupListMap,
groupMap: loadMoreGroupMap,
searchTeamMapInGroup: loadMoreSearchTeamMapInGroup,
teamScoreMap: loadMoreTeamScoreMap,
};
if (typeof loadMoreMap[key] === 'function') {
loadMoreMap[key]();
}
};
const loadMoreTeamList = () => {
loadMoreCommon(
'teamMap',
getTeamList(10, teamMap.list.length),
);
};
const loadMoreSearchTeamList = () => {
loadMoreCommon(
'searchTeamMap',
getTeamList(10, searchTeamMap.list.length),
);
};
const loadMoreSearchTeamMapInGroup = () => {
loadMoreCommon(
'searchTeamMapInGroup',
getTeamList(10, searchTeamMapInGroup.list.length),
);
};
const loadMoreTeamScoreMap = () => {
loadMoreCommon(
'teamScoreMap',
getTeamScoreList(),
'1-1',
);
};
const loadMoreGroupListMap = () => {
loadMoreCommon(
'groupListMap',
getGroupList(10, groupListMap.list.length),
).then(() => {
console.log('groupMap', groupListMap);
});
};
const loadMoreGroupMap = () => {
const currentRoundData = groupMap[curStartRoundId.value];
if (currentRoundData) {
loadMoreCommon(
'groupMap',
getGroupList(10, currentRoundData.list.length),
curStartRoundId.value,
).then(() => {
console.log('groupMap', groupMap);
});
}
};
const updateLoading = (key: string, value: boolean) => {
console.log('[updateLoading]', key, value);
if (key === 'groupMap') {
const currentRoundData = groupMap[curStartRoundId.value];
if (currentRoundData) {
currentRoundData.loading = value;
}
return;
}
if (key === 'teamScoreMap') {
const scoreData = teamScoreMap['1-1'];
if (scoreData) {
scoreData.loading = value;
}
return;
}
const dataMap: any = {
teamMap,
searchTeamMap,
groupListMap,
searchTeamMapInGroup,
};
if (dataMap[key]) {
dataMap[key].loading = value;
}
};
const treeList = treeDataList;
return {
// 响应式数据
curTab,
teamMap,
searchTeamMap,
groupListMap,
groupMap,
searchTeamMapInGroup,
isAllConfirmed,
toSelectNumList,
numPerGroup,
curStartRoundId,
curScoreRoundId,
roundList,
teamScoreMap,
teamMapInOneGroup,
onlyShowDisabledButton,
curGoingTab,
publishedScore,
showConfirmStartOB,
showPublishScoreOB,
historyScoreMap,
scheList,
treeList,
// 方法
initData,
close,
loadMoreData,
updateLoading,
};
}
// 导出工具函数
export { getTeamList, getGroupList, getTeamScoreList, getDemoData };