147 lines
4.2 KiB
JavaScript
147 lines
4.2 KiB
JavaScript
const competition = require('../../../utils/competition');
|
||
|
||
Page({
|
||
data: {
|
||
events: [],
|
||
eventNames: [],
|
||
eventIndex: 0,
|
||
selectedEventId: '',
|
||
projects: [],
|
||
projectNames: [],
|
||
projectIndex: 0,
|
||
groupNames: [],
|
||
groupIndex: 0,
|
||
keyword: '',
|
||
rows: [],
|
||
searched: false,
|
||
loading: true,
|
||
expandedId: ''
|
||
},
|
||
|
||
onLoad() {
|
||
this.loadEvents();
|
||
},
|
||
|
||
onPullDownRefresh() {
|
||
this.loadEvents().finally(() => wx.stopPullDownRefresh());
|
||
},
|
||
|
||
loadEvents() {
|
||
this.setData({ loading: true });
|
||
return competition.fetchScoreEvents()
|
||
.then((events) => {
|
||
const list = Array.isArray(events) ? events : (events && events.rows) || [];
|
||
this.setData({
|
||
events: list,
|
||
eventNames: list.map((item) => item.name),
|
||
eventIndex: 0,
|
||
selectedEventId: list[0] ? list[0].id : ''
|
||
});
|
||
return this.loadFilters().then(() => this.queryScores());
|
||
})
|
||
.catch((err) => {
|
||
this.setData({ rows: [], searched: true });
|
||
wx.showToast({ title: (err && err.message) || '赛事加载失败', icon: 'none' });
|
||
})
|
||
.finally(() => this.setData({ loading: false }));
|
||
},
|
||
|
||
onEventChange(e) {
|
||
const eventIndex = Number(e.detail.value) || 0;
|
||
const selected = this.data.events[eventIndex];
|
||
this.setData({
|
||
eventIndex,
|
||
selectedEventId: selected ? selected.id : '',
|
||
projects: [],
|
||
projectNames: [],
|
||
projectIndex: 0,
|
||
groupNames: [],
|
||
groupIndex: 0,
|
||
rows: [],
|
||
searched: false
|
||
});
|
||
this.loadFilters()
|
||
.then(() => this.queryScores())
|
||
.catch((err) => wx.showToast({ title: (err && err.message) || '筛选条件加载失败', icon: 'none' }));
|
||
},
|
||
|
||
loadFilters() {
|
||
if (!this.data.selectedEventId) return Promise.resolve();
|
||
return competition.fetchScoreFilters(this.data.selectedEventId).then((res) => {
|
||
const projects = Array.isArray(res && res.projects) ? res.projects : [];
|
||
const groupNames = projects[0] && Array.isArray(projects[0].groupNames)
|
||
? projects[0].groupNames
|
||
: [];
|
||
this.setData({
|
||
projects,
|
||
projectNames: projects.map((item) => item.projectName || item.name || '未命名项目'),
|
||
projectIndex: 0,
|
||
groupNames,
|
||
groupIndex: 0
|
||
});
|
||
});
|
||
},
|
||
|
||
onProjectChange(e) {
|
||
const projectIndex = Number(e.detail.value) || 0;
|
||
const project = this.data.projects[projectIndex];
|
||
this.setData({
|
||
projectIndex,
|
||
groupNames: project && Array.isArray(project.groupNames) ? project.groupNames : [],
|
||
groupIndex: 0,
|
||
rows: [],
|
||
searched: false,
|
||
expandedId: ''
|
||
});
|
||
},
|
||
|
||
onGroupChange(e) {
|
||
this.setData({
|
||
groupIndex: Number(e.detail.value) || 0,
|
||
rows: [],
|
||
searched: false,
|
||
expandedId: ''
|
||
});
|
||
},
|
||
|
||
onKeywordInput(e) {
|
||
this.setData({ keyword: e.detail.value || '' });
|
||
},
|
||
|
||
onClearKeyword() {
|
||
this.setData({ keyword: '' });
|
||
},
|
||
|
||
onSearch() {
|
||
this.queryScores();
|
||
},
|
||
|
||
queryScores() {
|
||
// 拦住重复点击"查询"导致的并发请求;首次加载时 loadEvents 已置 loading=true,
|
||
// 故必须同时判 searched,否则初次查询会被自己拦掉。
|
||
if (this.data.loading && this.data.searched) return Promise.resolve();
|
||
const project = this.data.projects[this.data.projectIndex];
|
||
const groupName = this.data.groupNames[this.data.groupIndex];
|
||
if (!this.data.selectedEventId || !project || !groupName) {
|
||
this.setData({ rows: [], searched: true, loading: false, expandedId: '' });
|
||
return Promise.resolve();
|
||
}
|
||
this.setData({ loading: true, expandedId: '' });
|
||
return competition.queryScores({
|
||
eventId: this.data.selectedEventId,
|
||
projectId: project.projectId,
|
||
groupName,
|
||
keyword: this.data.keyword
|
||
}).then((res) => {
|
||
this.setData({ rows: (res && res.rows) || [], searched: true });
|
||
}).catch((err) => {
|
||
wx.showToast({ title: (err && err.message) || '查询失败', icon: 'none' });
|
||
}).finally(() => this.setData({ loading: false }));
|
||
},
|
||
|
||
onToggleDetail(e) {
|
||
const id = e.currentTarget.dataset.id;
|
||
this.setData({ expandedId: this.data.expandedId === id ? '' : id });
|
||
}
|
||
});
|