<template>
<view class="page-container">
<view class="tabs">
<view v-for="(tab, index) in tabs" :key="index" class="tab-item" :class="{ active: activeTab === index }" @click="switchTab(index)">
<text class="tab-text">{{ tab }}</text>
</view>
</view>
<!-- 订单内容区域 -->
<view class="content">
<view class="order-list">
<view class="order-item" v-for="(item, index) in allList" :key="index" @click="goToOrderDetail(item)">
<view class="order-header">
<text class="order-number">订单号:{{ item.Reqsn }}</text>
<text class="order-status" :class="getStatusClass(item.StatusName)">{{ item.StatusName }}</text>
</view>
<view v-for="(itemA, indexA) in item.GoodsRelation" :key="indexA">
<!-- 商品信息 -->
<view class="product-info">
<image :src="resourceURL + itemA.Picture" class="product-img"></image>
<view class="product-details">
<text class="product-name">{{ itemA.Name }}</text>
<view class="product-attrs">
<text class="amount-value">¥{{ itemA.Price }}</text>
<text class="product-count">x{{ itemA.Num }}</text>
</view>
</view>
</view>
</view>
<view class="order-time">
<view>提交时间:{{ item.XkOrderTime }}</view>
<view class="order-amount">
<text class="amount-label">支付金额:</text>
<text class="amount-value">¥{{ item.Fee }}</text>
</view>
</view>
<view class="beizhu" v-if="item.Remark">
备注:
<text>{{ item.Remark }}</text>
</view>
<!-- 操作按钮 -->
<view class="order-actions" v-if="item.Btn.length > 0">
<uv-button
size="small"
v-for="(itemB, indexB) in item.Btn"
:key="indexB"
:text="itemB.Name"
@click.native.stop="handleOrderAction(itemB, item)"
></uv-button>
</view>
</view>
<!-- 空状态 -->
<view class="empty-state" v-if="allList.length === 0">
<text class="empty-text">暂无{{ tabs[activeTab] }}订单</text>
</view>
</view>
</view>
<!-- 取消订单弹窗 -->
<view v-if="cancelPopupVisible" class="popup-mask" @click="cancelPopupVisible = false">
<view class="cancel-popup" @click.stop>
<view class="popup-title">取消订单</view>
<view class="popup-content">
<text class="popup-desc">请输入取消订单的原因</text>
<textarea v-model="cancelReason" class="reason-input" placeholder="例如:暂时不需要了、价格不合适等" placeholder-style="color: #ccc;" rows="4"></textarea>
</view>
<view class="popup-footer">
<button class="popup-btn cancel-btn" @click="cancelPopupVisible = false">放弃</button>
<button class="popup-btn confirm-btn" @click="confirmCancel">确认取消</button>
</view>
</view>
</view>
</view>
</template>
<script>
import { defineComponent, reactive, toRefs } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { mineApi } from '@/api/user.js';
import { resourceURL } from '@/config/index';
export default defineComponent({
setup() {
const resourceUrl = typeof resourceURL === 'function' ? resourceURL() : resourceURL;
const state = reactive({
tabs: ['全部', '待提交', '待收货', '待评价', '已取消'],
statusMap: ['', 'WaitSubmit', 'WaitDone', 'WaitEvaluate', 'Cancel'],
activeTab: 0,
params: {
Page: 1,
Limit: 10,
Method: ''
},
paramsA: {
Id: '',
Method: '',
Reason: '',
File: [],
Content: ''
},
count: 0,
allList: [],
// 取消订单弹窗相关
cancelPopupVisible: false,
cancelReason: '',
currentOrder: null
});
// 获取订单列表
const getList = async () => {
try {
uni.showLoading({ title: '加载中...' });
const Data = await mineApi.getOrderList(state.params);
state.allList = Data.List || [];
state.count = Data.Count || 0;
} catch (error) {
console.error('获取订单列表失败', error);
uni.showToast({ title: '获取订单失败', icon: 'none' });
} finally {
console.log('11');
uni.hideLoading();
}
};
// 切换选项卡
const switchTab = (index) => {
state.activeTab = index;
state.params.Method = state.statusMap[index];
state.params.Page = 1;
getList();
};
// 根据订单状态获取对应的样式类
const getStatusClass = (statusName) => {
switch (statusName) {
case '待提交':
return 'pending-submit';
case '待收货':
return 'pending-receive';
case '待评价':
return 'pending-review';
case '已取消':
return 'cancelled';
default:
return '';
}
};
// 确认取消订单
const confirmCancel = async () => {
if (!state.cancelReason || state.cancelReason.trim() === '') {
uni.showToast({
title: '请输入取消原因',
icon: 'none',
duration: 2000
});
return;
}
state.cancelPopupVisible = false;
state.paramsA.Reason = state.cancelReason.trim();
try {
uni.showLoading({ title: '处理中...' });
await mineApi.getOrderOperation(state.paramsA);
uni.showToast({ title: '订单已取消', icon: 'success' });
getList();
} catch (error) {
console.error('取消订单失败', error);
uni.showToast({
title: '操作失败,请重试',
icon: 'none'
});
} finally {
console.log('22');
uni.hideLoading();
state.cancelReason = '';
state.currentOrder = null;
}
};
// 处理订单操作
const handleOrderAction = async (action, order) => {
state.paramsA = {
Id: order.Id,
Method: action.Method,
Reason: '',
File: [],
Content: ''
};
try {
if (action.Method === 'Cancel') {
state.currentOrder = order;
state.cancelReason = '';
state.cancelPopupVisible = true;
return;
} else if (action.Method === 'Evaluate') {
const evaluateData = await new Promise((resolve) => {
uni.navigateTo({
url: `/pages/mine/evaluate?orderId=${order.Id}`,
events: {
evaluateSubmit: (data) => {
resolve(data);
}
}
});
});
if (!evaluateData) return;
state.paramsA.Content = evaluateData.content;
state.paramsA.File = evaluateData.files;
} else if (action.Method === 'BuyAgain') {
// 调用再次购买接口并直接跳转
await handleBuyAgain(order);
return; // 不执行后续通用操作
}
// 调用接口执行操作
uni.showLoading({ title: '处理中...' });
await mineApi.getOrderOperation(state.paramsA);
// 操作成功后处理
switch (action.Method) {
case 'Submit':
submitOrder(order);
break;
case 'Delete':
applyRefund(order);
break;
case 'Evaluate':
writeReview(order);
break;
default:
console.log('未知操作', action.Name);
}
} catch (error) {
console.error('订单操作失败', error);
uni.showToast({
title: '操作失败,请重试',
icon: 'none'
});
} finally {
console.log('33');
uni.hideLoading();
}
};
// 处理再次购买 - 直接跳转页面
const handleBuyAgain = async (order) => {
try {
// 显示加载中
uni.showLoading({
title: '加载商品信息...'
});
// 调用再次购买接口
const response = await mineApi.getOrderOperation(state.paramsA);
console.log(response);
// 确保返回的数据结构正确
if (response && Array.isArray(response)) {
// 直接跳转到结算页面,携带商品数据
uni.navigateTo({
url: '/pages/order/shopCart/shopCart?products=' + encodeURIComponent(JSON.stringify(response))
});
} else {
// 处理数据格式异常的情况
uni.showToast({
title: '商品信息格式异常',
icon: 'none'
});
}
} catch (error) {
console.error('再次购买失败', error);
uni.showToast({
title: '获取商品信息失败',
icon: 'none'
});
} finally {
console.log('44');
uni.hideLoading();
}
};
// 订单操作方法
const cancelOrder = (order) => {
uni.showToast({
title: '订单已取消',
icon: 'success'
});
getList();
};
const goToOrderDetail = (order) => {
// 确保订单对象存在且有Id属性
if (order && order.Id) {
uni.navigateTo({
// 跳转到详情页并传递订单Id参数
url: `/pages/mine/orderDetail?id=${order.Id}`
});
} else {
uni.showToast({
title: '无法获取订单信息',
icon: 'none'
});
}
};
const payOrder = () => {
uni.showToast({
title: '跳转到支付页面',
icon: 'none'
});
};
const checkLogistics = () => {
uni.showToast({
title: '查看物流信息',
icon: 'none'
});
};
const confirmReceipt = () => {
uni.showModal({
title: '提示',
content: '确定已收到商品吗?',
success: (res) => {
if (res.confirm) {
uni.showToast({
title: '已确认收货',
icon: 'success'
});
getList();
}
}
});
};
const submitOrder = () => {
uni.showToast({
title: '订单提交成功',
icon: 'success'
});
getList();
};
const applyRefund = () => {
uni.showModal({
title: '提示',
content: '是否删除该订单?',
success: (res) => {
if (res.confirm) {
uni.showToast({
title: '已删除该订单',
icon: 'success'
});
getList();
}
}
});
};
const writeReview = () => {
uni.showToast({
title: '评价提交成功',
icon: 'success'
});
getList();
};
onLoad(async (option) => {
if (option && option.method) {
const tabIndex = state.statusMap.findIndex((item) => item === option.method);
if (tabIndex > -1) {
state.activeTab = tabIndex;
state.params.Method = option.method;
}
} else {
state.activeTab = 0;
state.params.Method = '';
}
getList();
});
return {
...toRefs(state),
switchTab,
resourceURL,
getStatusClass,
cancelOrder,
payOrder,
checkLogistics,
confirmReceipt,
submitOrder,
applyRefund,
writeReview,
handleOrderAction,
confirmCancel,
handleBuyAgain,
goToOrderDetail
};
}
});
</script>
<style lang="scss" scoped>
.page-container {
width: 100%;
min-height: 100vh;
background-color: #f5f5f5;
}
.tabs {
display: flex;
flex-direction: row;
background-color: #ffffff;
height: 40px;
border-bottom: 1px solid #eee;
}
.tab-item {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
position: relative;
.tab-text {
font-size: 14px;
color: #666;
padding: 3px 0;
}
&.active {
.tab-text {
color: #ff4400;
font-weight: 500;
}
&::after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 24px;
height: 2px;
background-color: #ff4400;
}
}
}
// 内容区域样式
.content {
padding-bottom: 20px;
}
// 订单列表样式
.order-list {
padding: 10px;
}
// 单个订单样式
.order-item {
background-color: #ffffff;
border-radius: 8px;
padding: 12px;
margin-bottom: 10px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
// 订单头部(订单号和状态)
.order-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
font-size: 13px;
.order-number {
color: #666;
}
.order-status {
font-weight: 500;
}
.pending-submit {
color: #ff4400; // 待提交-橙色
}
.pending-receive {
color: #007aff; // 待收货-蓝色
}
.pending-review {
color: #ffcc00; // 待评价-黄色
}
.cancelled {
color: #999; // 已取消-灰色
}
}
// 商品信息
.product-info {
display: flex;
margin-bottom: 12px;
padding-bottom: 12px;
border-bottom: 1px solid #f5f5f5;
.product-img {
width: 50px;
height: 50px;
border: 1px solid #eee;
border-radius: 4px;
margin-right: 10px;
flex-shrink: 0;
}
.product-details {
flex: 1;
display: flex;
flex-direction: column;
justify-content: space-between;
.product-name {
font-size: 14px;
color: #333;
line-height: 1.4;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
.product-attrs {
display: flex;
justify-content: space-between;
font-size: 12px;
color: #999;
.amount-value {
color: #ff4400;
}
.product-count {
color: #666;
}
}
}
}
.order-time {
display: flex;
align-items: center;
justify-content: space-between;
font-size: 13px;
color: #666;
}
.beizhu {
font-size: 13px;
color: #666;
margin-bottom: 12px;
}
// 订单金额
.order-amount {
display: flex;
justify-content: flex-end;
font-size: 13px;
.amount-label {
color: #666;
margin-right: 5px;
}
.amount-value {
color: #ff4400;
font-weight: 500;
}
}
// 订单操作按钮
.order-actions {
display: flex;
flex-direction: row;
justify-content: flex-end;
margin-top: 12px;
gap: 10px;
}
// 空状态样式
.empty-state {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 60px 0 40px;
.empty-img {
width: 120px;
height: 120px;
margin-bottom: 20px;
opacity: 0.5;
}
.empty-text {
color: #999;
font-size: 14px;
margin-bottom: 20px;
}
.empty-btn {
background-color: #ff4400;
color: white;
border: none;
width: 120px;
height: 36px;
line-height: 36px;
border-radius: 18px;
font-size: 14px;
padding: 0;
}
}
// 取消订单弹窗样式
.popup-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 999;
}
.cancel-popup {
width: 85%;
background-color: #fff;
border-radius: 12px;
padding: 20px;
box-sizing: border-box;
}
.popup-title {
font-size: 18px;
font-weight: 600;
color: #333;
text-align: center;
margin-bottom: 16px;
}
.popup-content {
margin-bottom: 20px;
}
.popup-desc {
display: block;
font-size: 14px;
color: #666;
margin-bottom: 10px;
}
.reason-input {
width: 100%;
padding: 10px;
border: 1px solid #eee;
border-radius: 8px;
font-size: 14px;
color: #333;
min-height: 80px;
box-sizing: border-box;
resize: none;
}
.popup-footer {
display: flex;
flex-direction: row;
justify-content: space-between;
gap: 10px;
}
.popup-btn {
flex: 1;
height: 44px;
line-height: 44px;
border-radius: 8px;
font-size: 16px;
border: none;
}
.cancel-btn {
background-color: #f5f5f5;
color: #666;
}
.confirm-btn {
background-color: #ff4400;
color: #fff;
}
</style>
点击的时候,我只想触发handleOrderAction事件,不想触发goToOrderDetail事件
最新发布