index.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <view class="create-room-page">
  3. <view class="room-header">
  4. <view class="game-info" v-if="gameInfo">
  5. <image class="game-image" :src="gameInfo.image" mode="aspectFill" />
  6. <view class="game-detail">
  7. <view class="game-title">{{ gameInfo.title }}</view>
  8. <view class="game-meta">
  9. <text>{{ gameInfo.minPlayers }}人-{{ gameInfo.maxPlayers }}人</text>
  10. <text>{{ gameInfo.duration }}分钟</text>
  11. </view>
  12. </view>
  13. </view>
  14. </view>
  15. <view class="room-settings">
  16. <nut-divider
  17. content-position="center"
  18. :style="{ color: '#3C92FB', borderColor: '#3C92FB', padding: '0 16px', margin: '10px 0 20px 0' }"
  19. >
  20. 房间设置
  21. </nut-divider>
  22. <view class="setting-item">
  23. <view class="setting-label">房间名称</view>
  24. <nut-input v-model="roomName" placeholder="请输入房间名称..." />
  25. <view class="random-name" @click="generateRandomName">随机名称</view>
  26. </view>
  27. <view class="setting-item" v-if="gameInfo">
  28. <view class="setting-label">最大人数</view>
  29. <view class="setting-slider">
  30. <nut-range
  31. v-model="maxPlayers"
  32. :min="gameInfo.minPlayers"
  33. :max="gameInfo.maxPlayers"
  34. inactive-color="#E5E5E5"
  35. button-color="#3C92FB"
  36. active-color="#3C92FB">
  37. </nut-range>
  38. <view class="slider-value">{{ maxPlayers }}人</view>
  39. </view>
  40. </view>
  41. <view class="setting-item">
  42. <view class="setting-label">房间可见性</view>
  43. <view class="visibility-options">
  44. <nut-radio-group v-model="roomVisibility" direction="horizontal">
  45. <nut-radio label="private" icon-name="check" icon-active-color="#3C92FB">仅限密码</nut-radio>
  46. <nut-radio label="public" icon-name="check" icon-active-color="#3C92FB">公开房间</nut-radio>
  47. </nut-radio-group>
  48. </view>
  49. </view>
  50. <view class="setting-item" v-if="roomVisibility === 'private'">
  51. <view class="setting-label">房间密码</view>
  52. <nut-input v-model="roomPassword" placeholder="请设置房间密码..." />
  53. </view>
  54. </view>
  55. <view class="action-buttons">
  56. <nut-button block color="#3C92FB" class="create-button" @click="createRoom">
  57. 创建并开始
  58. </nut-button>
  59. </view>
  60. </view>
  61. <Tabbar></Tabbar>
  62. </template>
  63. <script lang="ts">
  64. import Taro from '@tarojs/taro'
  65. import { ref } from 'vue'
  66. import { useGameStore } from '@/stores/game'
  67. import { type Game } from '@/types/game'
  68. import { useUserStore } from '@/stores/user'
  69. import { useRoomStore } from '@/stores/room'
  70. import { RoomRole, RoomStatus, RoomVisibility } from '@/types/room'
  71. import Tabbar from '@/components/Tabbar.vue'
  72. // 随机房间名称列表
  73. const ROOM_NAME_PREFIXES = ['欢乐', '快乐', '奇妙', '有趣', '精彩', '神秘', '魔法', '激情', '疯狂', '幻想']
  74. const ROOM_NAME_SUFFIXES = ['小队', '团队', '俱乐部', '联盟', '聚会', '战队', '家族', '帮派', '协会', '同盟']
  75. export default {
  76. components: {
  77. Tabbar
  78. },
  79. // 生命周期钩子 - 页面显示
  80. onShow() {
  81. // 隐藏返回首页按钮
  82. Taro.hideHomeButton()
  83. console.log('已隐藏返回首页按钮')
  84. },
  85. // Composition API
  86. setup() {
  87. // 初始化store
  88. const gameStore = useGameStore()
  89. const userStore = useUserStore()
  90. const roomStore = useRoomStore()
  91. // 房间设置
  92. const roomName = ref('')
  93. const maxPlayers = ref(0) // 初始值会在游戏加载后设置
  94. const roomVisibility = ref('private')
  95. const roomPassword = ref('')
  96. // 添加明确的类型注解
  97. const gameInfo = ref<Game | null>(null)
  98. // 生成随机房间名称
  99. const generateRandomName = () => {
  100. const prefix = ROOM_NAME_PREFIXES[Math.floor(Math.random() * ROOM_NAME_PREFIXES.length)]
  101. const suffix = ROOM_NAME_SUFFIXES[Math.floor(Math.random() * ROOM_NAME_SUFFIXES.length)]
  102. roomName.value = `${prefix}${gameInfo.value?.title || '游戏'}${suffix}`
  103. }
  104. // 获取游戏信息
  105. const initGameInfo = async () => {
  106. const pages = Taro.getCurrentPages()
  107. const currentPage = pages[pages.length - 1]
  108. const gameId = currentPage.$taroParams?.gameId
  109. if (!gameId) {
  110. Taro.showToast({
  111. title: '游戏ID不存在',
  112. icon: 'none'
  113. })
  114. return
  115. }
  116. try {
  117. // 获取游戏详情
  118. const game = await gameStore.getGameDetail(gameId)
  119. if (game) {
  120. gameInfo.value = game
  121. // 设置默认房间名
  122. roomName.value = `${game.title}房间`
  123. // 设置最大人数默认值为游戏的最大人数
  124. maxPlayers.value = game.maxPlayers
  125. }
  126. } catch (error) {
  127. console.error('获取游戏信息失败:', error)
  128. Taro.showToast({
  129. title: '获取游戏信息失败',
  130. icon: 'none'
  131. })
  132. }
  133. }
  134. // 创建房间
  135. const createRoom = async () => {
  136. if (!roomName.value) {
  137. Taro.showToast({
  138. title: '请输入房间名称',
  139. icon: 'none'
  140. })
  141. return
  142. }
  143. if (roomVisibility.value === 'private' && !roomPassword.value) {
  144. Taro.showToast({
  145. title: '请设置房间密码',
  146. icon: 'none'
  147. })
  148. return
  149. }
  150. if (!gameInfo.value) {
  151. Taro.showToast({
  152. title: '游戏信息不存在',
  153. icon: 'none'
  154. })
  155. return
  156. }
  157. try {
  158. Taro.showLoading({ title: '创建房间中...' })
  159. // 构建房间数据 - 使用滑块选择的最大人数
  160. const roomData = {
  161. id: `room_${Date.now()}_${Math.floor(Math.random() * 1000)}`,
  162. name: roomName.value,
  163. gameId: gameInfo.value.id || '',
  164. gameTitle: gameInfo.value.title || '',
  165. maxPlayers: maxPlayers.value, // 使用滑块选择的人数
  166. visibility: roomVisibility.value as RoomVisibility,
  167. password: roomVisibility.value === 'private' ? roomPassword.value : undefined,
  168. hosterId: userStore.openid,
  169. createTime: Date.now(),
  170. status: RoomStatus.WAITING,
  171. users: [] // 空数组,store内部会添加主持人信息
  172. }
  173. const result = await roomStore.createRoom(roomData)
  174. if (result.success && result.roomId) {
  175. // 导航到房间页面
  176. roomStore.navigateToRoomPage(result.roomId)
  177. } else {
  178. throw new Error(result.message || '创建房间失败')
  179. }
  180. } catch (error) {
  181. console.error('创建房间失败:', error)
  182. Taro.showToast({
  183. title: error instanceof Error ? error.message : '创建房间失败',
  184. icon: 'none'
  185. })
  186. } finally {
  187. Taro.hideLoading()
  188. }
  189. }
  190. // 页面初始化时获取游戏信息
  191. initGameInfo()
  192. return {
  193. roomName,
  194. maxPlayers,
  195. roomVisibility,
  196. roomPassword,
  197. gameInfo,
  198. createRoom,
  199. generateRandomName
  200. }
  201. },
  202. // 生命周期钩子 - 页面加载
  203. onLoad() {
  204. // 其他初始化逻辑
  205. }
  206. }
  207. </script>
  208. <style lang="scss">
  209. /* 保持样式不变 */
  210. .create-room-page {
  211. padding: $spacing-base;
  212. background-color: $background-color-base;
  213. min-height: 100vh;
  214. padding-bottom: $spacing-large * 4; // 为底部tabbar留出空间
  215. .room-header {
  216. margin-bottom: $spacing-small;
  217. .game-info {
  218. display: flex;
  219. background-color: $background-color-light;
  220. border-radius: $border-radius-small;
  221. overflow: hidden;
  222. box-shadow: $shadow-light;
  223. .game-image {
  224. width: 120px;
  225. height: 120px;
  226. object-fit: cover; // 修复图片显示问题
  227. }
  228. .game-detail {
  229. flex: 1;
  230. padding: $spacing-large;
  231. display: flex;
  232. flex-direction: column;
  233. justify-content: center;
  234. .game-title {
  235. font-size: $font-size-medium;
  236. font-weight: $font-weight-medium;
  237. color: $text-color-primary;
  238. margin-bottom: $spacing-base;
  239. }
  240. .game-meta {
  241. font-size: $font-size-small;
  242. color: $text-color-secondary;
  243. text {
  244. margin-right: $spacing-large;
  245. display: inline-block;
  246. &:before {
  247. content: '•';
  248. margin-right: 4px;
  249. }
  250. &:first-child:before {
  251. content: '';
  252. margin-right: 0;
  253. }
  254. }
  255. }
  256. }
  257. }
  258. }
  259. .room-settings {
  260. background-color: $background-color-light;
  261. border-radius: $border-radius-small;
  262. padding: $spacing-large;
  263. margin-bottom: $spacing-large;
  264. box-shadow: $shadow-light;
  265. .setting-item {
  266. margin-bottom: $spacing-large;
  267. position: relative;
  268. .setting-label {
  269. font-size: $font-size-small;
  270. color: $text-color-secondary;
  271. margin-bottom: $spacing-mini;
  272. }
  273. .random-name {
  274. position: absolute;
  275. right: 0;
  276. top: 0;
  277. font-size: $font-size-small;
  278. color: $primary-color;
  279. padding: $spacing-mini $spacing-small;
  280. cursor: pointer;
  281. }
  282. .setting-slider {
  283. display: flex;
  284. align-items: center;
  285. margin-top: $spacing-xlarge;
  286. .slider-value {
  287. width: 60px;
  288. text-align: center;
  289. font-size: $font-size-small;
  290. color: $text-color-primary;
  291. font-weight: $font-weight-medium;
  292. }
  293. }
  294. .visibility-options {
  295. margin-top: $spacing-large;
  296. }
  297. }
  298. }
  299. .action-buttons {
  300. padding: $spacing-large 0;
  301. .create-button {
  302. height: 44px;
  303. font-size: $font-size-medium;
  304. border-radius: $border-radius-base;
  305. }
  306. }
  307. }
  308. </style>