134 lines
3.1 KiB
JavaScript
134 lines
3.1 KiB
JavaScript
|
|
// pages/game/settlement/settlement.js
|
||
|
|
import request from '../../../utils/request'
|
||
|
|
|
||
|
|
Page({
|
||
|
|
data: {
|
||
|
|
roomId: null,
|
||
|
|
navbarHeight: 0,
|
||
|
|
isHost: false,
|
||
|
|
userInfo: null,
|
||
|
|
settlements: [], // 所有结算记录(房主)
|
||
|
|
mySettlements: [], // 我的结算记录(房客)
|
||
|
|
myTotal: 0 // 我的总盈亏
|
||
|
|
},
|
||
|
|
|
||
|
|
onLoad(options) {
|
||
|
|
// 计算导航栏高度
|
||
|
|
const windowInfo = wx.getWindowInfo()
|
||
|
|
const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
|
||
|
|
const statusBarHeight = windowInfo.statusBarHeight
|
||
|
|
const menuButtonTop = menuButtonInfo.top
|
||
|
|
const navbarHeight = menuButtonInfo.bottom + (menuButtonTop - statusBarHeight)
|
||
|
|
this.setData({ navbarHeight })
|
||
|
|
|
||
|
|
if (!options.id) {
|
||
|
|
wx.showToast({
|
||
|
|
title: '房间不存在',
|
||
|
|
icon: 'none'
|
||
|
|
})
|
||
|
|
setTimeout(() => {
|
||
|
|
wx.navigateBack()
|
||
|
|
}, 1500)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
this.setData({ roomId: options.id })
|
||
|
|
|
||
|
|
// 获取用户信息
|
||
|
|
const app = getApp()
|
||
|
|
const userInfo = app.getUserInfo()
|
||
|
|
if (!userInfo) {
|
||
|
|
wx.showToast({
|
||
|
|
title: '请先登录',
|
||
|
|
icon: 'none'
|
||
|
|
})
|
||
|
|
setTimeout(() => {
|
||
|
|
wx.navigateBack()
|
||
|
|
}, 1500)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
this.setData({ userInfo })
|
||
|
|
|
||
|
|
// 加载结算数据
|
||
|
|
this.loadSettlementData()
|
||
|
|
},
|
||
|
|
|
||
|
|
// 加载结算数据
|
||
|
|
async loadSettlementData() {
|
||
|
|
wx.showLoading({ title: '加载中...' })
|
||
|
|
|
||
|
|
try {
|
||
|
|
const data = await request.get(`/rooms/${this.data.roomId}/settlement`)
|
||
|
|
|
||
|
|
this.setData({
|
||
|
|
isHost: data.is_host,
|
||
|
|
settlements: data.settlements || [],
|
||
|
|
mySettlements: data.my_settlements || [],
|
||
|
|
myTotal: data.my_total || 0
|
||
|
|
})
|
||
|
|
|
||
|
|
wx.hideLoading()
|
||
|
|
} catch (error) {
|
||
|
|
wx.hideLoading()
|
||
|
|
wx.showToast({
|
||
|
|
title: error.message || '加载失败',
|
||
|
|
icon: 'none'
|
||
|
|
})
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
// 确认结算
|
||
|
|
confirmSettle() {
|
||
|
|
const title = this.data.isHost ? '确认关闭房间?' : '确认退出房间?'
|
||
|
|
const content = this.data.isHost
|
||
|
|
? '房间将被关闭,结算记录已保存,所有玩家将被移除'
|
||
|
|
: '您将退出房间,稍后可以重新加入。您的结算记录已保存'
|
||
|
|
|
||
|
|
wx.showModal({
|
||
|
|
title: title,
|
||
|
|
content: content,
|
||
|
|
confirmText: '确认',
|
||
|
|
cancelText: '取消',
|
||
|
|
success: (res) => {
|
||
|
|
if (res.confirm) {
|
||
|
|
this.doSettle()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
},
|
||
|
|
|
||
|
|
// 执行结算
|
||
|
|
async doSettle() {
|
||
|
|
wx.showLoading({ title: '结算中...' })
|
||
|
|
|
||
|
|
try {
|
||
|
|
const endpoint = this.data.isHost
|
||
|
|
? `/rooms/${this.data.roomId}/close`
|
||
|
|
: `/rooms/${this.data.roomId}/leave`
|
||
|
|
|
||
|
|
await request.post(endpoint)
|
||
|
|
|
||
|
|
wx.hideLoading()
|
||
|
|
wx.showToast({
|
||
|
|
title: this.data.isHost ? '房间已关闭' : '已退出房间',
|
||
|
|
icon: 'success'
|
||
|
|
})
|
||
|
|
|
||
|
|
setTimeout(() => {
|
||
|
|
// 返回首页
|
||
|
|
wx.reLaunch({
|
||
|
|
url: '/pages/index/index'
|
||
|
|
})
|
||
|
|
}, 1500)
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
wx.hideLoading()
|
||
|
|
wx.showToast({
|
||
|
|
title: error.message || '结算失败',
|
||
|
|
icon: 'none'
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|