order.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { defineStore } from 'pinia'
  2. export const useOrderStore = defineStore('order', {
  3. state: () => ({
  4. currentOrder: null,
  5. orderList: [],
  6. orderStatus: {
  7. 0: '待支付',
  8. 1: '已支付',
  9. 2: '制作中',
  10. 3: '已完成',
  11. 4: '已取消'
  12. }
  13. }),
  14. getters: {
  15. /**
  16. * 获取订单状态文本
  17. */
  18. getOrderStatusText: (state) => (status) => {
  19. return state.orderStatus[status] || '未知状态'
  20. },
  21. /**
  22. * 待支付订单数量
  23. */
  24. pendingCount: (state) => {
  25. return state.orderList.filter((order) => order.status === 0).length
  26. }
  27. },
  28. actions: {
  29. /**
  30. * 设置当前订单
  31. */
  32. setCurrentOrder(order) {
  33. this.currentOrder = order
  34. },
  35. /**
  36. * 设置订单列表
  37. */
  38. setOrderList(list) {
  39. this.orderList = list
  40. },
  41. /**
  42. * 添加订单
  43. */
  44. addOrder(order) {
  45. this.orderList.unshift(order)
  46. },
  47. /**
  48. * 更新订单状态
  49. */
  50. updateOrderStatus(orderId, status) {
  51. const order = this.orderList.find((o) => o.id === orderId)
  52. if (order) {
  53. order.status = status
  54. }
  55. if (this.currentOrder && this.currentOrder.id === orderId) {
  56. this.currentOrder.status = status
  57. }
  58. },
  59. /**
  60. * 清除订单数据
  61. */
  62. clearOrders() {
  63. this.currentOrder = null
  64. this.orderList = []
  65. }
  66. }
  67. })