dapaijizhang3/pages/game/create/create.js

270 lines
6.3 KiB
JavaScript
Raw Permalink Normal View History

2025-11-20 16:42:59 +08:00
// pages/game/create/create.js
import request from '../../../utils/request'
Page({
data: {
roomName: '',
gameType: 'mahjong', // 默认麻将
gameTypes: [
{ value: 'mahjong', label: '麻将' },
{ value: 'poker', label: '扑克' },
{ value: 'other', label: '其他' }
],
tableFee: '0',
quickSettings: ['10', '20', '30'],
showSuccessModal: false,
inviteCode: '',
roomCode: '',
roomId: null,
creating: false,
navbarHeight: 0,
qrcodeUrl: ''
},
onLoad() {
// 计算导航栏高度
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 })
// 检查登录状态
const app = getApp()
const userInfo = app.getUserInfo()
if (!userInfo) {
wx.showToast({
title: '请先登录',
icon: 'none'
})
setTimeout(() => {
wx.navigateBack()
}, 1500)
return
}
// 设置默认房间名称为"昵称的房间"
this.setData({
roomName: `${userInfo.nickname}的房间`
})
},
// 游戏类型选择
onGameTypeChange(e) {
const value = e.currentTarget.dataset.value
this.setData({
gameType: value
})
},
// 桌位费输入
onTableFeeInput(e) {
let value = e.detail.value
// 只允许正整数
value = value.replace(/[^\d]/g, '')
this.setData({
tableFee: value
})
},
// 快捷设置输入
onQuickSettingInput(e) {
const index = e.currentTarget.dataset.index
const value = e.detail.value
const quickSettings = [...this.data.quickSettings]
quickSettings[index] = value
this.setData({ quickSettings })
},
// 添加快捷设置
addQuickSetting() {
if (this.data.quickSettings.length >= 10) {
wx.showToast({
title: '最多添加10个快捷设置',
icon: 'none'
})
return
}
const quickSettings = [...this.data.quickSettings]
quickSettings.push('')
this.setData({ quickSettings })
},
// 删除快捷设置
removeQuickSetting(e) {
const index = e.currentTarget.dataset.index
if (this.data.quickSettings.length <= 1) {
wx.showToast({
title: '至少保留一个快捷设置',
icon: 'none'
})
return
}
const quickSettings = [...this.data.quickSettings]
quickSettings.splice(index, 1)
this.setData({ quickSettings })
},
// 创建房间
async createRoom() {
const roomName = this.data.roomName.trim()
const tableFee = parseFloat(this.data.tableFee) || 0
const quickSettings = this.data.quickSettings
.map(val => parseInt(val))
.filter(val => !isNaN(val) && val > 0)
if (!roomName) {
wx.showToast({
title: '请输入房间名称',
icon: 'none'
})
return
}
if (quickSettings.length === 0) {
wx.showToast({
title: '请至少设置一个有效的快捷输入',
icon: 'none'
})
return
}
if (this.data.creating) return
this.setData({ creating: true })
wx.showLoading({ title: '创建中...' })
try {
const data = await request.post('/rooms/create', {
room_name: roomName,
game_type: this.data.gameType,
table_fee: tableFee,
quick_settings: quickSettings
})
wx.hideLoading()
this.setData({
inviteCode: data.invite_code,
roomCode: data.room_code,
roomId: data.room_id,
showSuccessModal: true
})
// 延迟生成二维码确保canvas已渲染
setTimeout(() => {
this.generateQRCode(data.invite_code)
}, 100)
// 更新全局房间信息
const app = getApp()
app.setCurrentSession({
id: data.room_id,
room_code: data.room_code,
room_name: roomName
})
} catch (error) {
wx.hideLoading()
wx.showToast({
title: error.message || '创建失败',
icon: 'none'
})
} finally {
this.setData({ creating: false })
}
},
// 生成二维码
async generateQRCode(inviteCode) {
try {
const app = getApp()
// 构建scene参数使用邀请码
const scene = `code=${inviteCode}`
console.log('【创建页】开始生成二维码, scene:', scene, 'invite_code:', inviteCode)
wx.showLoading({ title: '生成二维码...' })
// 调用app.js中的getQrcode方法获取二维码
const qrcodeBuffer = await app.getQrcode(scene)
console.log('【创建页】二维码数据返回:', qrcodeBuffer)
wx.hideLoading()
// 检查返回数据类型
if (!qrcodeBuffer) {
throw new Error('二维码数据为空')
}
// 将arraybuffer转换为base64
const base64 = wx.arrayBufferToBase64(qrcodeBuffer)
const qrcodeUrl = `data:image/png;base64,${base64}`
console.log('【创建页】base64长度:', base64.length)
// 更新页面数据,显示二维码图片
this.setData({
qrcodeUrl: qrcodeUrl
})
} catch (error) {
wx.hideLoading()
console.error('【创建页】生成二维码失败:', error)
wx.showToast({
title: error.message || '二维码生成失败',
icon: 'none'
})
}
},
// 复制邀请码
copyInviteCode() {
if (!this.data.inviteCode) return
wx.setClipboardData({
data: this.data.inviteCode,
success: () => {
wx.showToast({
title: '已复制邀请码',
icon: 'success'
})
}
})
},
// 进入房间
goToRoom() {
if (!this.data.roomId) return
wx.redirectTo({
url: `/pages/game/detail/detail?id=${this.data.roomId}`
})
},
// 关闭弹窗
closeModal() {
this.setData({
showSuccessModal: false
})
},
// 阻止事件冒泡
stopPropagation() {
// 阻止点击弹窗内容时关闭
},
// 分享给朋友
onShareAppMessage() {
if (this.data.inviteCode) {
return {
title: `邀请你加入房间:${this.data.roomName}`,
path: `/pages/game/join/join?code=${this.data.inviteCode}`,
imageUrl: '/images/share-bg.png'
}
}
}
})