当前位置:首页 > 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: ''
    }
  }
}

座位选择组件

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

vue实现购票

<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交互提交订单:

vue实现购票

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实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDi…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…

vue实现treeselect

vue实现treeselect

Vue TreeSelect 实现方法 使用 Vue 实现 TreeSelect 组件可以通过以下几种方式完成,包括使用现成的第三方库或自行封装组件。 使用第三方库 推荐使用成熟的第三方 Tree…

vue实现treeview

vue实现treeview

Vue 实现 TreeView 的方法 使用递归组件实现 TreeView 递归组件是 Vue 中实现 TreeView 的常见方式。通过组件调用自身,可以轻松处理嵌套的树形结构数据。 <t…