115 lines
3.6 KiB
JavaScript
115 lines
3.6 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 : '', projectIndex: 0, 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 groups = Array.isArray(res && res.groupNames) ? res.groupNames : [];
|
||
this.setData({
|
||
projects,
|
||
projectNames: ['全部项目'].concat(projects.map((item) => item.projectName || item.name || '未命名项目')),
|
||
projectIndex: 0,
|
||
groupNames: ['全部组别'].concat(groups),
|
||
groupIndex: 0
|
||
});
|
||
});
|
||
},
|
||
|
||
onProjectChange(e) {
|
||
this.setData({ projectIndex: Number(e.detail.value) || 0 });
|
||
},
|
||
|
||
onGroupChange(e) {
|
||
this.setData({ groupIndex: Number(e.detail.value) || 0 });
|
||
},
|
||
|
||
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();
|
||
this.setData({ loading: true, expandedId: '' });
|
||
return competition.queryScores({
|
||
eventId: this.data.selectedEventId,
|
||
projectId: this.data.projectIndex > 0 ? this.data.projects[this.data.projectIndex - 1].projectId : undefined,
|
||
groupName: this.data.groupIndex > 0 ? this.data.groupNames[this.data.groupIndex] : undefined,
|
||
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 });
|
||
}
|
||
});
|