Commit 2b2de282 by liimin

refactor(OrderActionButtons): 重构订单操作按钮组件

- 新增 getButtons 和 updateButtons 方法,用于动态生成按钮
- 优化按钮渲染逻辑,使用 wx:for 循环生成按钮
- 重构按钮点击事件处理,统一使用 onButtonClick 方法
- 优化数据结构,将按钮配置移至组件数据中
- 添加组件生命周期钩子,初始化时更新按钮
1 parent 1e3f22ca
......@@ -7,25 +7,202 @@ Component({
item: {
type: Object,
value: {},
observer: function() {
// 当item变化时重新计算按钮
this.updateButtons();
}
},
hasUserInfo: {
type: Boolean,
value: false,
observer: function() {
// 当hasUserInfo变化时重新计算按钮
this.updateButtons();
}
},
},
/**
* 组件的初始数据
*/
data: {},
data: {
buttons: [],
orderItem: {}
},
/**
* 组件的方法列表
*/
methods: {
// 获取当前订单应该显示的按钮
getButtons() {
const { item, hasUserInfo } = this.data;
// 待付款状态按钮策略
if (item.orderStatus == 1) {
const buttons = [{
text: '取消付款',
type: 'cancelPay',
class: 'btn pay',
handler: this.onCancelPay.bind(this)
}];
if (item.orderSource != 4) {
buttons.push({
text: '去支付',
type: 'pay',
class: 'btn pay',
handler: this.onPay.bind(this)
});
}
return buttons;
}
// 特殊订单查看详情策略
if (item.orderSource == 5 && (item.orderStatus == 2 || item.orderStatus == 7)) {
return [{
text: '查看详情',
type: 'viewDetail',
class: 'btn pay',
handler: this.onToGoldDetail.bind(this)
}];
}
// 售后中订单按钮策略
if (item.afterSaleStatus) {
const buttons = [];
if (item.afterSaleStatus == 1) {
buttons.push({
text: '取消售后',
type: 'cancelOrder',
class: 'btn pay',
handler: this.onCancelOrder.bind(this)
});
}
buttons.push({
text: '售后详情',
type: 'afterSaleDetail',
class: 'btn',
handler: this.onAfterSaleDetail.bind(this)
});
return buttons;
}
// 进行中订单按钮策略
if (item.orderStatus > 2 && item.orderStatus < 9) {
const buttons = [];
// 售后按钮
if (item.payPrice > 0 && item.orderSource != 4 &&
!(item.relatedUserCouponStatus == 2 || item.relatedUserCouponStatus == 3)) {
buttons.push({
text: '售后',
type: 'afterSale',
class: 'btn',
handler: this.onAfterSale.bind(this)
});
}
// 确认收货按钮
if (item.deliveryType == 2 && item.orderStatus == 4) {
buttons.push({
text: '确认收货',
type: 'confirmReceipt',
class: 'btn confirm',
handler: this.onConfirmReceipt.bind(this)
});
}
// 待评价订单
if (item.orderGoods.length == 1 && !hasUserInfo &&
(item.orderStatus == 5 || item.orderStatus == 8)) {
if (item.evaluatedFlag == 'Y') {
buttons.push({
text: '查看评价',
type: 'viewEvaluate',
class: 'btn confirm',
handler: this.onLookEvaluate.bind(this)
});
} else {
if (!hasUserInfo) {
buttons.push({
text: '立即评价',
type: 'getUserProfile',
class: 'btn confirm userProfile',
handler: this.onGetUserProfile.bind(this)
});
} else {
buttons.push({
text: '立即评价',
type: 'evaluate',
class: 'btn confirm',
handler: this.onEvaluate.bind(this)
});
}
}
}
return buttons;
}
// 待评价订单(独立情况)
if (item.orderGoods.length == 1 && !hasUserInfo &&
(item.orderStatus == 5 || item.orderStatus == 8)) {
if (item.evaluatedFlag == 'Y') {
return [{
text: '查看评价',
type: 'viewEvaluate',
class: 'btn confirm',
handler: this.onLookEvaluate.bind(this)
}];
} else {
if (!hasUserInfo) {
return [{
text: '立即评价',
type: 'getUserProfile',
class: 'btn confirm userProfile',
handler: this.onGetUserProfile.bind(this)
}];
} else {
return [{
text: '立即评价',
type: 'evaluate',
class: 'btn confirm',
handler: this.onEvaluate.bind(this)
}];
}
}
}
// 默认返回空数组
return [];
},
// 更新按钮
updateButtons() {
const buttons = this.getButtons();
this.setData({
buttons,
orderItem: this.data.item
});
},
// 按钮点击事件处理
onButtonClick(e) {
const { index } = e.currentTarget.dataset;
const button = this.data.buttons[index];
if (button && button.handler) {
button.handler(e);
}
},
// 取消付款
onCancelPay(e) {
let { orderCode } = e.currentTarget.dataset;
let { orderCode } = e.currentTarget.dataset.item || e.currentTarget.dataset;
wx.showModal({
title: "温馨提示",
content: "请问您是否要取消付款",
......@@ -55,7 +232,7 @@ Component({
let _this = this;
wx.p.wxApi.showLoading("结算中...");
wx.p.api.shopmallApi
.payOrder(e.currentTarget.dataset)
.payOrder(e.currentTarget.dataset.item || e.currentTarget.dataset)
.then(({ needPay, payParams }) => {
wx.hideLoading();
if (needPay) {
......@@ -115,7 +292,7 @@ Component({
// 确认收货
onConfirmReceipt(e) {
let { orderCode } = e.currentTarget.dataset;
let { orderCode } = e.currentTarget.dataset.item || e.currentTarget.dataset;
wx.showModal({
title: "温馨提示",
content: "是否确认收货",
......@@ -165,7 +342,7 @@ Component({
// 取消订单
onCancelOrder(e) {
let { orderCode } = e.currentTarget.dataset;
let { orderCode } = e.currentTarget.dataset.item || e.currentTarget.dataset;
wx.showModal({
title: "温馨提示",
content: "请问您是否要取消售后",
......@@ -201,4 +378,11 @@ Component({
});
},
},
});
lifetimes: {
attached() {
// 组件实例进入页面节点树时执行
this.updateButtons();
}
}
});
\ No newline at end of file
<!-- 订单操作按钮组件 -->
<view class="btn-box">
<block wx:if="{{!item.afterSaleStatus}}">
<block wx:if="{{item.orderStatus == 1}}">
<view class="btn pay" catchtap="onCancelPay" data-order-code="{{item.orderCode}}">取消付款</view>
<block wx:if="{{item.orderSource != 4}}">
<view class="btn pay" catchtap="onPay" data-order-code="{{item.orderCode}}">去支付</view>
</block>
<block wx:for="{{buttons}}" wx:key="index">
<block wx:if="{{item.type === 'getUserProfile'}}">
<button catchtap="onGetUserProfile" class="{{item.class}}" data-item="{{orderItem}}">{{item.text}}</button>
</block>
<block wx:if="{{item.orderSource==5 && (item.orderStatus == 2 || item.orderStatus == 7)}}">
<view class="btn pay" data-item="{{item}}" bind:tap="onToGoldDetail">查看详情</view>
<block wx:else>
<view class="{{item.class}}" catchtap="onButtonClick" data-index="{{index}}" data-item="{{orderItem}}">{{item.text}}</view>
</block>
<block wx:elif="{{item.orderStatus > 2 && item.orderStatus < 9}}">
<!-- deliveryType 结算方式(1:到店核销,2:快递发货) -->
<block wx:if="{{item.payPrice > 0 && item.orderSource != 4}}">
<view class="btn" catchtap="onAfterSale" data-item="{{item}}" wx:if="{{!(item.relatedUserCouponStatus == 2 || item.relatedUserCouponStatus == 3)}}">售后</view>
</block>
<block wx:if="{{item.deliveryType == 2 && item.orderStatus == 4}}">
<view class="btn confirm" catchtap="onConfirmReceipt" data-order-code="{{item.orderCode}}">确认收货</view>
</block>
</block>
<!-- !hasUserInfo -->
<view wx:if="{{item.orderGoods.length == 1 && !hasUserInfo}}">
<block wx:if="{{item.orderStatus == 5 || item.orderStatus == 8}}">
<block wx:if="{{item.evaluatedFlag == 'Y'}}">
<view class="btn confirm" catchtap="onLookEvaluate" data-item="{{item}}">查看评价</view>
</block>
<block wx:else>
<button catchtap="onGetUserProfile" wx:if="{{!hasUserInfo}}" class="btn confirm userProfile" data-item="{{item}}"> 立即评价 </button>
<view class="btn confirm" catchtap="onEvaluate" wx:else data-item="{{item}}">立即评价</view>
</block>
</block>
</view>
</block>
<block wx:else>
<block wx:if="{{item.afterSaleStatus == 1}}">
<view class="btn pay" catchtap="onCancelOrder" data-order-code="{{item.orderCode}}">取消售后</view>
</block>
<view class="btn" data-item="{{item}}" catchtap="onAfterSaleDetail">售后详情</view>
</block>
</view>
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!