dapaijizhang3/pages/game/join/join.js

144 lines
2.9 KiB
JavaScript

// pages/game/join/join.js
import { onLoginReady } from '@jdmini/api'
import request from '../../../utils/request'
Page({
data: {
inviteCode: '',
joining: false,
recentSessions: [],
navbarHeight: 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.code) {
console.log(options)
this.setData({
inviteCode: options.code
})
// 自动加入
}
// 检查登录状态
onLoginReady(()=>{
//判断是否登录
if (!getApp().getUserInfo()) {
wx.navigateTo({
url: '/pages/login/login'
})
return
}
if (options) {
this.joinSession()
}
//this.loadRecentSessions()
})
// const app = getApp()
// if (!app.getUserInfo()) {
// wx.showToast({
// title: '请先登录',
// icon: 'none'
// })
// setTimeout(() => {
// wx.navigateBack()
// }, 1500)
// return
// }
// if (options) {
// this.joinSession()
// }
// // 加载最近的牌局
// this.loadRecentSessions()
},
// 加载最近的牌局
async loadRecentSessions() {
try {
const data = await request.get('/rooms/my-sessions', {
page: 1,
pageSize: 5
})
this.setData({
recentSessions: data.list || []
})
} catch (error) {
console.error('加载牌局失败:', error)
}
},
// 加入牌局
async joinSession() {
const { inviteCode } = this.data
if (!inviteCode) {
wx.showToast({
title: '请输入邀请码',
icon: 'none'
})
return
}
if (this.data.joining) return
this.setData({ joining: true })
wx.showLoading({ title: '加入中...' })
try {
const data = await request.post('/rooms/join', {
room_code: inviteCode
})
wx.hideLoading()
wx.showToast({
title: '加入成功',
icon: 'success'
})
// 更新全局牌局
const app = getApp()
app.setCurrentSession(data.session)
// 跳转到牌局详情
setTimeout(() => {
wx.redirectTo({
url: `/pages/game/detail/detail?id=${data.session.id}`
})
}, 1500)
} catch (error) {
wx.hideLoading()
wx.showToast({
title: error.message || '加入失败',
icon: 'none'
})
} finally {
this.setData({ joining: false })
}
},
// 进入牌局
goToSession(e) {
const id = e.currentTarget.dataset.id
wx.navigateTo({
url: `/pages/game/detail/detail?id=${id}`
})
}
})