yingsa/miniprogram/pages/match/challenge/index.js
2026-01-20 08:50:50 +08:00

198 lines
4.7 KiB
JavaScript

const app = getApp()
Page({
data: {
userInfo: null,
ladderUser: null,
currentStore: null,
pendingGames: []
},
onLoad() {
this.initData()
},
onShow() {
this.refreshData()
},
async initData() {
if (!app.globalData.token) {
try {
await app.login()
await app.getCurrentStore()
} catch (e) {
console.error('登录失败:', e)
}
}
this.refreshData()
},
refreshData() {
this.setData({
userInfo: app.globalData.userInfo,
ladderUser: app.globalData.ladderUser,
currentStore: app.globalData.currentStore
})
if (app.globalData.ladderUser) {
this.fetchPendingGames()
}
},
async fetchPendingGames() {
try {
const res = await app.request('/api/match/pending-confirm', {
store_id: this.data.currentStore?.storeId
})
this.setData({ pendingGames: res.data || [] })
} catch (e) {
console.error('获取待确认比赛失败:', e)
}
},
startChallenge() {
if (!this.data.ladderUser) {
wx.showToast({ title: '请先加入天梯系统', icon: 'none' })
return
}
wx.scanCode({
onlyFromCamera: false,
scanType: ['qrCode'],
success: async (res) => {
const memberCode = res.result
this.checkAndChallenge(memberCode)
},
fail: (err) => {
if (err.errMsg !== 'scanCode:fail cancel') {
wx.showToast({ title: '扫码失败', icon: 'none' })
}
}
})
},
async checkAndChallenge(memberCode) {
wx.showLoading({ title: '检查中...' })
try {
const res = await app.request(`/api/match/challenge/check/${memberCode}`, {
store_id: this.data.currentStore.storeId
})
wx.hideLoading()
if (!res.data.canChallenge) {
wx.showModal({
title: '无法挑战',
content: res.data.reason,
showCancel: false
})
return
}
// 显示确认弹窗
const target = res.data.targetUser
wx.showModal({
title: '确认挑战',
content: `确定要向 ${target.ladderUser.realName}(Lv${target.ladderUser.level}, 战力${target.ladderUser.powerScore}) 发起挑战吗?`,
success: async (modalRes) => {
if (modalRes.confirm) {
await this.createChallenge(memberCode)
}
}
})
} catch (e) {
wx.hideLoading()
console.error('检查挑战失败:', e)
}
},
async createChallenge(memberCode) {
wx.showLoading({ title: '发起挑战中...' })
try {
await app.request('/api/match/challenge/create', {
store_id: this.data.currentStore.storeId,
target_member_code: memberCode
}, 'POST')
wx.hideLoading()
wx.showToast({ title: '挑战已发起', icon: 'success' })
} catch (e) {
wx.hideLoading()
console.error('发起挑战失败:', e)
}
},
joinRankingMatch() {
if (!this.data.ladderUser) {
wx.showToast({ title: '请先加入天梯系统', icon: 'none' })
return
}
wx.scanCode({
onlyFromCamera: false,
scanType: ['qrCode'],
success: async (res) => {
const matchCode = res.result
wx.showLoading({ title: '加入中...' })
try {
const joinRes = await app.request('/api/match/ranking/join', {
match_code: matchCode
}, 'POST')
wx.hideLoading()
wx.showToast({ title: '加入成功', icon: 'success' })
// 跳转到排位赛详情
wx.navigateTo({
url: `/pages/match/ranking/index?code=${matchCode}`
})
} catch (e) {
wx.hideLoading()
console.error('加入排位赛失败:', e)
}
},
fail: (err) => {
if (err.errMsg !== 'scanCode:fail cancel') {
wx.showToast({ title: '扫码失败', icon: 'none' })
}
}
})
},
confirmGame(e) {
const game = e.currentTarget.dataset.game
wx.showModal({
title: '确认比分',
content: `确认比分 ${game.myScore} : ${game.opponentScore} 吗?`,
confirmText: '确认',
cancelText: '有争议',
success: async (res) => {
wx.showLoading({ title: '处理中...' })
try {
await app.request('/api/match/challenge/confirm-score', {
game_id: game.id,
confirm: res.confirm
}, 'POST')
wx.hideLoading()
wx.showToast({
title: res.confirm ? '确认成功' : '已标记争议',
icon: 'success'
})
this.fetchPendingGames()
} catch (e) {
wx.hideLoading()
console.error('确认比分失败:', e)
}
}
})
}
})