Files
JayuCloud/Frontend/src/views/OrderView.vue
T
2026-03-19 14:10:59 +08:00

344 lines
7.6 KiB
Vue

<template>
<div class="mobile-container">
<!-- 头部区域 -->
<header class="header">
<a-button class="back-btn" type="text" @click="handleBackClick">
<LeftOutlined :style="{ fontSize: '20px' }" />
</a-button>
<h1>我的订单</h1>
<div class="placeholder"></div>
</header>
<!-- 订单列表 -->
<div class="order-section">
<a-empty v-if="orders.length === 0" description="暂无订单">
<a-button type="primary" @click="handleBrowseProducts">去购物</a-button>
</a-empty>
<div v-else class="order-list">
<a-card v-for="order in orders" :key="order.id" class="order-card">
<div class="order-header">
<span class="order-no">订单号: {{ order.orderNo }}</span>
<span :class="['order-status', order.status === 1 ? 'pending' : order.status === 2 ? 'completed' : 'cancelled']">
{{ order.statusText }}
</span>
</div>
<div class="order-items">
<div v-for="item in order.items" :key="item.id" class="order-item">
<div class="item-image" v-if="item.productImage">
<img :src="item.productImage" :alt="item.productTitle" />
</div>
<div class="item-image" v-else :style="{ backgroundColor: getRandomColor() }">
<span>{{ item.productTitle[0] }}</span>
</div>
<div class="item-info">
<div class="item-title">{{ item.productTitle }}</div>
<div class="item-price">¥{{ item.price.toFixed(2) }} x {{ item.quantity }}</div>
<div v-if="item.artistName" class="item-artist">
<img v-if="item.artistAvatar" :src="item.artistAvatar" class="artist-avatar-small" />
<span>美甲师: {{ item.artistName }}</span>
</div>
</div>
</div>
</div>
<div class="order-footer">
<div class="order-total">
总计: <span class="total-price">¥{{ order.actualAmount.toFixed(2) }}</span>
</div>
<div class="order-actions">
<a-button v-if="order.status === 1" size="small" danger @click="handleCancelOrder(order.id)">
取消订单
</a-button>
</div>
</div>
</a-card>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { LeftOutlined } from '@ant-design/icons-vue'
import { message, Modal } from 'ant-design-vue'
import { orderApi } from '../api/order'
import { fileApi } from '../api/index'
const router = useRouter()
// 订单相关状态
const orders = ref<any[]>([])
const loading = ref(false)
// 图片缓存
const imageIdCache = ref<Record<string, string>>({})
// 加载订单列表
const loadOrders = async () => {
loading.value = true
try {
const response = await orderApi.getOrderList() as any
orders.value = response || []
await loadProductImages(orders.value)
} catch (error) {
message.error('获取订单列表失败')
} finally {
loading.value = false
}
}
// 加载作品图片
const loadProductImages = async (orders: any[]) => {
for (const order of orders) {
if (order.items && order.items.length > 0) {
for (const item of order.items) {
if (item.productImage) {
const cleanImageUrl = item.productImage.replace(/<[^>]*>/g, '')
const match = cleanImageUrl.match(/\/api\/file\/get\/(\d+)/)
if (match && match[1]) {
const imageId = match[1]
try {
if (!imageIdCache.value[imageId]) {
const response = await fileApi.getSignedUrl(Number(imageId)) as any
imageIdCache.value[imageId] = response
}
item.productImage = imageIdCache.value[imageId]
} catch (error) {
console.error('获取图片签名链接失败:', error)
item.productImage = ''
}
} else {
item.productImage = ''
}
}
}
}
}
}
// 处理取消订单
const handleCancelOrder = async (orderId: number) => {
Modal.confirm({
title: '确认取消订单',
content: '确定要取消这个订单吗?',
onOk: async () => {
try {
await orderApi.cancelOrder(orderId)
message.success('订单已取消')
loadOrders()
} catch (error) {
message.error('取消订单失败')
}
}
})
}
// 处理返回按钮点击
const handleBackClick = () => {
router.back()
}
// 处理浏览作品
const handleBrowseProducts = () => {
router.push('/products')
}
// 生成随机颜色
const getRandomColor = () => {
const colors = ['#ffe4d6', '#d6eaff', '#e2ffd6', '#fff2d6', '#d6ffe4', '#e4d6ff']
return colors[Math.floor(Math.random() * colors.length)]
}
// 页面加载时获取订单数据
onMounted(() => {
loadOrders()
})
</script>
<style scoped>
.mobile-container {
padding-top: 60px;
min-height: 100vh;
background: #f5f5f5;
}
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 15px;
border-bottom: 1px solid #f0f0f0;
background: white;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.header h1 {
font-size: 18px;
font-weight: bold;
margin: 0;
}
.back-btn,
.placeholder {
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
}
.order-section {
padding: 15px;
padding-bottom: 20px;
}
.order-list {
display: flex;
flex-direction: column;
gap: 15px;
}
.order-card {
border-radius: 12px;
overflow: hidden;
}
.order-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 1px solid #f0f0f0;
}
.order-no {
font-size: 14px;
color: #666;
}
.order-status {
font-size: 14px;
font-weight: 600;
padding: 4px 12px;
border-radius: 12px;
}
.order-status.pending {
background: #fff7e6;
color: #fa8c16;
}
.order-status.completed {
background: #f6ffed;
color: #52c41a;
}
.order-status.cancelled {
background: #fff1f0;
color: #ff4d4f;
}
.order-items {
display: flex;
flex-direction: column;
gap: 10px;
margin-bottom: 15px;
}
.order-item {
display: flex;
gap: 12px;
align-items: center;
}
.item-image {
width: 60px;
height: 60px;
border-radius: 8px;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
font-weight: bold;
color: rgba(0,0,0,0.1);
flex-shrink: 0;
overflow: hidden;
}
.item-image img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 8px;
}
.item-info {
flex: 1;
display: flex;
flex-direction: column;
gap: 4px;
}
.item-title {
font-size: 14px;
font-weight: 600;
color: #1a1a2e;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.item-price {
font-size: 13px;
color: #666;
}
.item-artist {
display: flex;
align-items: center;
gap: 6px;
font-size: 12px;
color: #1890ff;
}
.artist-avatar-small {
width: 20px;
height: 20px;
border-radius: 50%;
object-fit: cover;
}
.order-footer {
display: flex;
justify-content: space-between;
align-items: center;
padding-top: 10px;
border-top: 1px solid #f0f0f0;
}
.order-total {
font-size: 14px;
color: #666;
}
.total-price {
font-size: 16px;
font-weight: 700;
color: #ff4d4f;
}
.order-actions {
display: flex;
gap: 8px;
}
</style>