const app = getApp() Page({ data: { playerId: null, player: null, matches: [], loadingMatches: false }, onLoad(options) { const playerId = options && options.id ? String(options.id) : null this.setData({ playerId }) const eventChannel = this.getOpenerEventChannel ? this.getOpenerEventChannel() : null if (eventChannel) { eventChannel.on('player', (player) => { if (player) this.setData({ player }) }) } this.refresh() }, async onPullDownRefresh() { try { await this.refresh() } finally { wx.stopPullDownRefresh() } }, async refresh() { await Promise.all([this.fetchPlayer(), this.fetchMatches()]) }, async fetchPlayer() { if (!this.data.playerId) return try { const res = await app.request('/api/ladder/player', { id: this.data.playerId }) if (res && res.data) this.setData({ player: res.data }) } catch (e) { } }, async fetchMatches() { if (!this.data.playerId) return this.setData({ loadingMatches: true }) try { const res = await app.request('/api/match/history', { player_id: this.data.playerId }) const list = Array.isArray(res && res.data) ? res.data : (res && res.data && res.data.list) || [] const matches = list.map((item) => { return Object.assign({}, item, { timeText: item.timeText || item.createTime || item.matchTime || '', resultClass: item.resultClass || (item.result === 'win' ? 'win' : item.result === 'lose' ? 'lose' : ''), resultText: item.resultText || (item.result === 'win' ? '胜' : item.result === 'lose' ? '负' : item.resultName || '') }) }) this.setData({ matches }) } catch (e) { this.setData({ matches: [] }) } finally { this.setData({ loadingMatches: false }) } } })