yingsa/miniprogram/pages/match/ranking/index.js
Ethanfly d07ebb735a fix(env): Update API URL and enhance HTML structure
- Change VITE_API_URL in .env from localhost:3000 to localhost:3001 for backend access.
- Update index.html to replace favicon with logo.png and ensure proper HTML structure.
- Add new dependencies for WangEditor in package.json and package-lock.json to support rich text editing features.
2026-02-06 19:04:16 +08:00

169 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const app = getApp()
Page({
data: {
matchCode: '',
match: {
players: []
},
myPlayer: null,
currentGame: null,
myScoreInput: '',
opponentScoreInput: ''
},
onLoad(options) {
this.setData({ matchCode: options.code })
this.fetchMatchDetail()
},
onPullDownRefresh() {
this.fetchMatchDetail().then(() => {
wx.stopPullDownRefresh()
})
},
bindMyScoreInput(e) {
this.setData({ myScoreInput: e.detail.value })
},
bindOpponentScoreInput(e) {
this.setData({ opponentScoreInput: e.detail.value })
},
async handleSubmitScore() {
const { myScoreInput, opponentScoreInput, currentGame } = this.data
if (!myScoreInput || !opponentScoreInput) {
wx.showToast({ title: '请输入比分', icon: 'none' })
return
}
if (!currentGame) return
wx.showLoading({ title: '提交中...' })
try {
if (!this.data.match || !this.data.match.id) {
throw new Error('比赛信息缺失')
}
await app.request('/api/match/ranking/submit-score', {
match_id: this.data.match.id,
my_score: parseInt(myScoreInput),
opponent_score: parseInt(opponentScoreInput)
}, 'POST')
wx.showToast({ title: '已提交,等待确认' })
this.fetchCurrentGame()
} catch (e) {
console.error('提交比分失败:', e)
wx.showToast({ title: '提交失败,请重试', icon: 'none' })
} finally {
wx.hideLoading()
}
},
async handleConfirmScore() {
if (!this.data.currentGame) return
wx.showModal({
title: '确认比分',
content: '确认比分无误并结束本局?',
success: async (res) => {
if (res.confirm) {
wx.showLoading({ title: '确认中...' })
try {
await app.request('/api/match/ranking/confirm-score', {
game_id: this.data.currentGame.id,
confirm: true
}, 'POST')
wx.showToast({ title: '已确认' })
// 刷新页面以获取最新状态(包括新对局)
this.fetchMatchDetail()
} catch (e) {
console.error('确认比分失败:', e)
} finally {
wx.hideLoading()
}
}
}
})
},
async handleDisputeScore() {
if (!this.data.currentGame) return
wx.showModal({
title: '标记争议',
content: '标记为有争议将重置本局比分,是否继续?',
success: async (res) => {
if (res.confirm) {
wx.showLoading({ title: '提交中...' })
try {
await app.request('/api/match/ranking/confirm-score', {
game_id: this.data.currentGame.id,
confirm: false
}, 'POST')
wx.showToast({ title: '已标记争议' })
this.fetchCurrentGame()
} catch (e) {
console.error('提交失败:', e)
} finally {
wx.hideLoading()
}
}
}
})
},
async fetchMatchDetail() {
try {
const res = await app.request(`/api/match/ranking/${this.data.matchCode}`)
this.setData({ match: res.data })
// 找到我的参赛信息
const ladderUser = app.globalData.ladderUser
if (ladderUser) {
// 使用 == 比较,避免类型不一致问题
const myPlayer = res.data.players.find(p => p.ladderUserId == ladderUser.id)
this.setData({ myPlayer })
// 不再根据本地 myPlayer.status 决定是否拉取当前对局
// 后端 /my-game 接口内部会负责:
// - 如果当前有对局:返回对手信息
// - 如果没有对局但我在 waiting尝试自动匹配并返回新对局
// - 否则返回 currentGame: null
this.fetchCurrentGame()
}
} catch (e) {
console.error('获取排位赛详情失败:', e)
}
},
async fetchCurrentGame() {
try {
const res = await app.request(`/api/match/ranking/${this.data.matchCode}/my-game`)
const newGame = res.data.currentGame || null
// 如果切换到了新的对局(或上一局结束变为 null重置本地输入框
const prevGameId = this.data.currentGame && this.data.currentGame.id
const newGameId = newGame && newGame.id
const gameChanged = prevGameId !== newGameId
if (gameChanged) {
this.setData({
currentGame: newGame,
myScoreInput: '',
opponentScoreInput: ''
})
} else {
this.setData({ currentGame: newGame })
}
} catch (e) {
console.error('获取当前对局失败:', e)
}
}
})