当前位置:首页 > VUE

vue实现购票

2026-01-07 20:56:58VUE

Vue 实现购票功能

数据结构设计

购票功能通常需要以下数据结构:

  • 场次信息(时间、地点、价格)
  • 座位信息(可选座位、已售座位)
  • 用户订单信息

示例数据结构:

data() {
  return {
    showInfo: {
      id: 1,
      name: '演唱会',
      time: '2023-12-25 19:30',
      venue: '北京国家体育场',
      price: 380
    },
    seats: [
      { id: 'A1', status: 'available' },
      { id: 'A2', status: 'sold' },
      // 更多座位...
    ],
    selectedSeats: [],
    orderInfo: {
      name: '',
      phone: '',
      email: ''
    }
  }
}

座位选择组件

创建可交互的座位图组件:

<template>
  <div class="seat-map">
    <div 
      v-for="seat in seats" 
      :key="seat.id"
      :class="['seat', seat.status]"
      @click="selectSeat(seat)"
    >
      {{ seat.id }}
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    selectSeat(seat) {
      if (seat.status === 'available') {
        const index = this.selectedSeats.findIndex(s => s.id === seat.id)
        if (index === -1) {
          this.selectedSeats.push(seat)
        } else {
          this.selectedSeats.splice(index, 1)
        }
      }
    }
  }
}
</script>

<style>
.seat {
  width: 30px;
  height: 30px;
  margin: 5px;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}
.available { background-color: #4CAF50; }
.sold { background-color: #f44336; cursor: not-allowed; }
.selected { background-color: #FFC107; }
</style>

订单表单验证

实现表单验证确保用户输入有效信息:

methods: {
  validateForm() {
    if (!this.orderInfo.name) {
      alert('请输入姓名')
      return false
    }
    if (!/^1[3-9]\d{9}$/.test(this.orderInfo.phone)) {
      alert('请输入正确的手机号')
      return false
    }
    if (!this.selectedSeats.length) {
      alert('请选择至少一个座位')
      return false
    }
    return true
  }
}

提交订单

与后端API交互提交订单:

methods: {
  async submitOrder() {
    if (!this.validateForm()) return

    try {
      const response = await axios.post('/api/orders', {
        showId: this.showInfo.id,
        seats: this.selectedSeats.map(s => s.id),
        customerInfo: this.orderInfo
      })

      if (response.data.success) {
        this.$router.push(`/order/${response.data.orderId}`)
      }
    } catch (error) {
      console.error('订单提交失败:', error)
      alert('订单提交失败,请重试')
    }
  }
}

订单状态管理

使用Vuex管理全局订单状态:

// store/modules/order.js
export default {
  state: {
    currentOrder: null,
    orderHistory: []
  },
  mutations: {
    SET_CURRENT_ORDER(state, order) {
      state.currentOrder = order
      state.orderHistory.push(order)
    }
  },
  actions: {
    async createOrder({ commit }, orderData) {
      const response = await axios.post('/api/orders', orderData)
      commit('SET_CURRENT_ORDER', response.data)
      return response.data
    }
  }
}

支付集成

集成第三方支付接口:

methods: {
  async proceedToPayment() {
    const paymentData = {
      orderId: this.order.id,
      amount: this.order.total,
      paymentMethod: 'wechat'
    }

    try {
      const response = await axios.post('/api/payment', paymentData)
      if (response.data.paymentUrl) {
        window.location.href = response.data.paymentUrl
      }
    } catch (error) {
      console.error('支付处理失败:', error)
    }
  }
}

响应式布局

确保购票页面在不同设备上正常显示:

@media (max-width: 768px) {
  .seat-map {
    grid-template-columns: repeat(5, 1fr);
  }
  .order-form {
    flex-direction: column;
  }
}

vue实现购票

标签: vue
分享给朋友:

相关文章

vue实现单选

vue实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定一个变量,可以实现单选功能。单选按钮的 v…

vue实现年历

vue实现年历

Vue 实现年历的方法 使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式: 基于原生 Vue 的日历组件 创建 Calendar.vue 组件文件,…

vue实现多层评论回复

vue实现多层评论回复

Vue 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '父…

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <di…

vue实现点击页面切换

vue实现点击页面切换

实现点击页面切换功能 在Vue中实现点击页面切换功能,可以通过路由跳转或组件动态渲染两种方式完成。以下是具体实现方法: 使用Vue Router实现页面跳转 安装Vue Router后,在项目中配置…