403 lines
12 KiB
JavaScript
403 lines
12 KiB
JavaScript
import { config } from '../../config/index';
|
||
|
||
/** 获取订单详情mock数据 */
|
||
function mockFetchOrderDetail(params) {
|
||
console.log('[订单详情服务] 使用Mock数据获取订单详情', {
|
||
params,
|
||
timestamp: new Date().toISOString()
|
||
});
|
||
|
||
const { delay } = require('../_utils/delay');
|
||
const { genOrderDetail } = require('../../model/order/orderDetail');
|
||
|
||
return delay().then(() => {
|
||
const result = genOrderDetail(params);
|
||
console.log('[订单详情服务] Mock数据生成完成', {
|
||
orderId: result.data?.orderId,
|
||
orderNo: result.data?.orderNo,
|
||
orderStatus: result.data?.orderStatus,
|
||
itemCount: result.data?.orderItemVOs?.length || 0
|
||
});
|
||
return result;
|
||
});
|
||
}
|
||
|
||
/** 获取订单详情数据 */
|
||
export function fetchOrderDetail(params) {
|
||
console.log('[订单详情服务] 开始获取订单详情', {
|
||
params,
|
||
useMock: config.useMock,
|
||
timestamp: new Date().toISOString()
|
||
});
|
||
|
||
if (config.useMock) {
|
||
console.log('[订单详情服务] 使用Mock模式');
|
||
return mockFetchOrderDetail(params);
|
||
}
|
||
|
||
return new Promise((resolve, reject) => {
|
||
// 从本地存储获取token
|
||
const token = wx.getStorageSync('token');
|
||
console.log('[订单详情服务] 检查登录状态', {
|
||
hasToken: !!token
|
||
});
|
||
|
||
if (!token) {
|
||
console.error('[订单详情服务] 用户未登录');
|
||
reject(new Error('未登录'));
|
||
return;
|
||
}
|
||
|
||
// 使用订单号或订单ID获取详情
|
||
const orderIdentifier = params.parameter || params.orderNo || params.orderId;
|
||
console.log('[订单详情服务] 解析订单标识', {
|
||
parameter: params.parameter,
|
||
orderNo: params.orderNo,
|
||
orderId: params.orderId,
|
||
orderIdentifier
|
||
});
|
||
|
||
if (!orderIdentifier) {
|
||
console.error('[订单详情服务] 缺少订单标识', { params });
|
||
reject(new Error('缺少订单标识'));
|
||
return;
|
||
}
|
||
|
||
const requestUrl = `${config.apiBase}/orders/${orderIdentifier}`;
|
||
console.log('[订单详情服务] 发送API请求', {
|
||
url: requestUrl,
|
||
orderIdentifier,
|
||
hasToken: !!token
|
||
});
|
||
|
||
wx.request({
|
||
url: requestUrl,
|
||
method: 'GET',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`,
|
||
'Content-Type': 'application/json'
|
||
},
|
||
success: (res) => {
|
||
console.log('[订单详情服务] API响应成功', {
|
||
statusCode: res.statusCode,
|
||
dataCode: res.data?.code,
|
||
orderId: res.data?.data?.orderId,
|
||
orderNo: res.data?.data?.orderNo,
|
||
orderStatus: res.data?.data?.orderStatus
|
||
});
|
||
|
||
if (res.statusCode === 200 && res.data.code === 200) {
|
||
console.log('[订单详情服务] 开始处理订单详情数据');
|
||
|
||
// 转换后端数据格式为前端需要的格式
|
||
const order = res.data.data;
|
||
|
||
console.log('[订单详情服务] 原始订单数据', {
|
||
orderId: order.orderId,
|
||
orderNo: order.orderNo,
|
||
orderStatus: order.orderStatus,
|
||
paymentAmount: order.paymentAmount,
|
||
itemCount: order.orderItemVOs?.length || 0,
|
||
hasLogistics: !!order.logisticsVO
|
||
});
|
||
|
||
// 构建符合前端期望的数据结构
|
||
// 注意:后端API已经返回正确格式的数据,直接使用
|
||
const orderDetail = {
|
||
data: {
|
||
orderId: order.orderId,
|
||
orderNo: order.orderNo,
|
||
parentOrderNo: order.parentOrderNo,
|
||
storeId: order.storeId,
|
||
storeName: order.storeName,
|
||
orderStatus: order.orderStatus,
|
||
orderStatusName: order.orderStatusName,
|
||
paymentAmount: order.paymentAmount, // 后端已经是分为单位的整数
|
||
goodsAmountApp: order.goodsAmountApp,
|
||
totalAmount: order.totalAmount,
|
||
payAmount: order.payAmount,
|
||
couponAmount: order.couponAmount || 0, // 优惠券金额(分为单位)
|
||
freightFee: 0,
|
||
discountAmount: 0,
|
||
createdAt: order.createdAt,
|
||
createTime: new Date(order.createdAt).getTime(),
|
||
autoCancelTime: order.orderStatus === 1 ? Date.now() + 30 * 60 * 1000 : null, // 30分钟后自动取消
|
||
|
||
// 订单商品列表 - 直接使用后端返回的数据
|
||
orderItemVOs: order.orderItemVOs || [],
|
||
|
||
// 物流信息 - 直接使用后端返回的数据
|
||
logisticsVO: order.logisticsVO || {
|
||
logisticsNo: '',
|
||
logisticsCompanyName: '',
|
||
receiverName: '',
|
||
receiverPhone: '',
|
||
receiverProvince: '',
|
||
receiverCity: '',
|
||
receiverCountry: '',
|
||
receiverAddress: '',
|
||
sendTime: null,
|
||
arrivalTime: null
|
||
},
|
||
|
||
// 支付信息
|
||
paymentVO: {
|
||
payStatus: order.payStatus || 0,
|
||
amount: order.payAmount || order.totalAmount,
|
||
paySuccessTime: order.paidAt || null,
|
||
payWayName: '微信支付'
|
||
},
|
||
|
||
// 操作按钮
|
||
buttonVOs: getOrderButtons(order.orderStatus),
|
||
|
||
// 发票信息
|
||
invoiceStatus: 2, // 暂不开发票
|
||
invoiceDesc: '暂不开发票',
|
||
invoiceVO: null
|
||
},
|
||
code: 'Success',
|
||
success: true
|
||
};
|
||
|
||
console.log('[订单详情服务] 订单详情数据处理完成', {
|
||
orderId: orderDetail.data.orderId,
|
||
orderNo: orderDetail.data.orderNo,
|
||
orderStatus: orderDetail.data.orderStatus,
|
||
orderStatusName: orderDetail.data.orderStatusName,
|
||
paymentAmount: orderDetail.data.paymentAmount,
|
||
itemCount: orderDetail.data.orderItemVOs.length,
|
||
buttonCount: orderDetail.data.buttonVOs.length,
|
||
hasLogistics: !!orderDetail.data.logisticsVO.logisticsNo,
|
||
autoCancelTime: orderDetail.data.autoCancelTime
|
||
});
|
||
|
||
resolve(orderDetail);
|
||
} else {
|
||
const errorMsg = res.data.message || '获取订单详情失败';
|
||
console.error('[订单详情服务] API响应错误', {
|
||
statusCode: res.statusCode,
|
||
dataCode: res.data?.code,
|
||
message: errorMsg,
|
||
orderIdentifier
|
||
});
|
||
reject(new Error(errorMsg));
|
||
}
|
||
},
|
||
fail: (error) => {
|
||
console.error('[订单详情服务] API请求失败', {
|
||
error,
|
||
orderIdentifier,
|
||
url: requestUrl,
|
||
timestamp: new Date().toISOString()
|
||
});
|
||
reject(new Error('网络请求失败'));
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
// 获取订单操作按钮
|
||
function getOrderButtons(status) {
|
||
console.log('[订单详情服务] 获取订单操作按钮', { status });
|
||
|
||
const buttons = [];
|
||
|
||
switch (status) {
|
||
case 1: // 待付款
|
||
buttons.push(
|
||
{ primary: false, type: 1, name: '取消订单' },
|
||
{ primary: true, type: 2, name: '立即付款' }
|
||
);
|
||
break;
|
||
case 2: // 已付款/待发货
|
||
buttons.push(
|
||
{ primary: false, type: 7, name: '申请退款' },
|
||
{ primary: true, type: 3, name: '提醒发货' }
|
||
);
|
||
break;
|
||
case 3: // 待发货
|
||
buttons.push(
|
||
{ primary: false, type: 7, name: '申请退款' },
|
||
{ primary: true, type: 3, name: '提醒发货' }
|
||
);
|
||
break;
|
||
case 4: // 已发货/待收货
|
||
buttons.push(
|
||
{ primary: true, type: 4, name: '确认收货' }
|
||
);
|
||
break;
|
||
case 5: // 待收货
|
||
buttons.push(
|
||
{ primary: true, type: 4, name: '确认收货' }
|
||
);
|
||
break;
|
||
case 6: // 已完成
|
||
buttons.push(
|
||
{ primary: false, type: 5, name: '申请售后' },
|
||
{ primary: true, type: 6, name: '评价' }
|
||
);
|
||
break;
|
||
case 7: // 已取消
|
||
// 无操作按钮
|
||
break;
|
||
}
|
||
|
||
console.log('[订单详情服务] 生成的操作按钮', {
|
||
status,
|
||
buttonCount: buttons.length,
|
||
buttons: buttons.map(btn => ({ name: btn.name, type: btn.type, primary: btn.primary }))
|
||
});
|
||
|
||
return buttons;
|
||
}
|
||
|
||
// 获取商品操作按钮
|
||
function getItemButtons(orderStatus) {
|
||
console.log('[订单详情服务] 获取商品操作按钮', { orderStatus });
|
||
|
||
if (orderStatus === 3) { // 已完成
|
||
const buttons = [
|
||
{ primary: false, type: 4, name: '申请售后' }
|
||
];
|
||
console.log('[订单详情服务] 生成商品按钮', { buttons });
|
||
return buttons;
|
||
}
|
||
|
||
console.log('[订单详情服务] 无商品操作按钮', { orderStatus });
|
||
return [];
|
||
}
|
||
|
||
// 状态描述映射
|
||
function getStatusDesc(status) {
|
||
// 与后端order.go中的状态映射保持一致
|
||
const statusMap = {
|
||
1: '未付款',
|
||
2: '待发货', // 统一为待发货
|
||
3: '待发货', // 统一为待发货
|
||
4: '已发货',
|
||
5: '待收货',
|
||
6: '已完成',
|
||
7: '已取消',
|
||
8: '退货中',
|
||
9: '已退款'
|
||
};
|
||
const statusDesc = statusMap[status] || '未知状态';
|
||
|
||
console.log('[订单详情服务] 获取状态描述', {
|
||
status,
|
||
statusDesc
|
||
});
|
||
|
||
return statusDesc;
|
||
}
|
||
|
||
/** 获取客服mock数据 */
|
||
function mockFetchBusinessTime(params) {
|
||
console.log('[订单详情服务] 使用Mock数据获取客服信息', {
|
||
params,
|
||
timestamp: new Date().toISOString()
|
||
});
|
||
|
||
const { delay } = require('../_utils/delay');
|
||
const { genBusinessTime } = require('../../model/order/orderDetail');
|
||
|
||
return delay().then(() => {
|
||
const result = genBusinessTime(params);
|
||
console.log('[订单详情服务] Mock客服数据生成完成', result);
|
||
return result;
|
||
});
|
||
}
|
||
|
||
/** 获取客服数据 */
|
||
export function fetchBusinessTime(params) {
|
||
console.log('[订单详情服务] 开始获取客服信息', {
|
||
params,
|
||
useMock: config.useMock,
|
||
timestamp: new Date().toISOString()
|
||
});
|
||
|
||
if (config.useMock) {
|
||
console.log('[订单详情服务] 使用Mock模式获取客服信息');
|
||
return mockFetchBusinessTime(params);
|
||
}
|
||
|
||
return new Promise((resolve, reject) => {
|
||
const requestUrl = `${config.apiBase}/business/time`;
|
||
const token = wx.getStorageSync('token') || '';
|
||
|
||
console.log('[订单详情服务] 发送客服信息API请求', {
|
||
url: requestUrl,
|
||
hasToken: !!token
|
||
});
|
||
|
||
wx.request({
|
||
url: requestUrl,
|
||
method: 'GET',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`
|
||
},
|
||
success: (res) => {
|
||
console.log('[订单详情服务] 客服信息API响应', {
|
||
statusCode: res.statusCode,
|
||
dataCode: res.data?.code,
|
||
hasBusinessTime: !!res.data?.data?.business_time,
|
||
hasPhone: !!res.data?.data?.phone
|
||
});
|
||
|
||
if (res.statusCode === 200 && res.data.code === 200) {
|
||
const result = {
|
||
data: {
|
||
businessTime: res.data.data.business_time || ['周一至周日: 9:00-18:00'],
|
||
telphone: res.data.data.phone || '400-000-0000',
|
||
saasId: res.data.data.saas_id || '88888888',
|
||
},
|
||
code: 'Success',
|
||
msg: null,
|
||
success: true,
|
||
};
|
||
|
||
console.log('[订单详情服务] 客服信息获取成功', result.data);
|
||
resolve(result);
|
||
} else {
|
||
console.warn('[订单详情服务] 客服信息API失败,使用默认数据', {
|
||
statusCode: res.statusCode,
|
||
dataCode: res.data?.code,
|
||
message: res.data?.message
|
||
});
|
||
|
||
// 如果API失败,返回默认数据
|
||
resolve({
|
||
data: {
|
||
businessTime: ['周一至周日: 9:00-18:00'],
|
||
telphone: '400-000-0000',
|
||
saasId: '88888888',
|
||
},
|
||
code: 'Success',
|
||
msg: null,
|
||
success: true,
|
||
});
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
console.error('[订单详情服务] 客服信息请求失败,使用默认数据', {
|
||
error: err,
|
||
url: requestUrl
|
||
});
|
||
|
||
// 网络错误时返回默认数据
|
||
resolve({
|
||
data: {
|
||
businessTime: ['周一至周日: 9:00-18:00'],
|
||
telphone: '400-000-0000',
|
||
saasId: '88888888',
|
||
},
|
||
code: 'Success',
|
||
msg: null,
|
||
success: true,
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|