OrderActionButtons.js 10.2 KB
// packageMine/pages/order/index/components/orderActionButtons.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object,
      value: {},
      observer: function() {
        // 当item变化时重新计算按钮
        this.updateButtons();
      }
    },
    hasUserInfo: {
      type: Boolean,
      value: false,
      observer: function() {
        // 当hasUserInfo变化时重新计算按钮
        this.updateButtons();
      }
    },
  },

  /**
   * 组件的初始数据
   */
  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.item || e.currentTarget.dataset;
      wx.showModal({
        title: "温馨提示",
        content: "请问您是否要取消付款",
        confirmText: "确认",
        cancelText: "取消",
        confirmColor: "#F13C3D",
      }).then((res) => {
        if (res.confirm) {
          wx.p.api.shopmallApi
            .cancelPay(orderCode)
            .then(({ code, message }) => {
              if (code == "0") {
                wx.p.wxApi.showToast("操作成功");
                setTimeout(() => {
                  this.clearData();
                }, 1.5e3);
              } else {
                wx.p.wxApi.showToast(message);
              }
            });
        }
      });
    },

    // 去支付
    onPay(e) {
      let _this = this;
      wx.p.wxApi.showLoading("结算中...");
      wx.p.api.shopmallApi
        .payOrder(e.currentTarget.dataset.item || e.currentTarget.dataset)
        .then(({ needPay, payParams }) => {
          wx.hideLoading();
          if (needPay) {
            let params = {
              nonceStr: payParams.nonceStr,
              paySign: payParams.paySign,
              signType: payParams.signType,
              timeStamp: payParams.timeStamp,
              package: payParams.packages,
            };
            wx.requestPayment({
              ...params,
              success(res) {
                setTimeout(() => {
                  _this.clearData();
                }, 1.5e3);
                _this.requestSubscribeMessage();
              },
              fail(res) {
                if (res.errMsg == "requestPayment:fail cancel") {
                  wx.p.wxApi.showToast("取消支付");
                } else {
                  wx.p.router.go("/packageMine/pages/order/complete/complete", {
                    params,
                  });
                }
              },
            });
          } else {
            _this.requestSubscribeMessage();
          }
        });
    },

    // 查看详情
    onToGoldDetail(e) {
      const { item } = e.currentTarget.dataset;
      wx.p.router.go("/packageEquity/pages/kaqDetail/orderDetail", {
        orderCode: item.orderCode,
      });
    },

    // 售后
    onAfterSale(e) {
      let {
        item: { orderCode, payPrice,goodsNumber },
      } = e.currentTarget.dataset;
      wx.p.router.go(
        "/packageMine/pages/order/afterSale_step_two/afterSale_step_two",
        {
          orderCode,
          refundPrice: payPrice,
          refundCount:goodsNumber
        }
      );
    },

    // 确认收货
    onConfirmReceipt(e) {
      let { orderCode } = e.currentTarget.dataset.item || e.currentTarget.dataset;
      wx.showModal({
        title: "温馨提示",
        content: "是否确认收货",
        confirmText: "确认",
        cancelText: "取消",
        confirmColor: "#F13C3D",
      }).then((res) => {
        if (res.confirm) {
          wx.p.api.shopmallApi.confirmReceipt(orderCode).then((data) => {
            wx.p.wxApi.showToast("操作成功");
            setTimeout(() => {
              this.clearData();
            }, 1.5e3);
          });
        }
      });
    },

    // 查看评价
    onLookEvaluate(e) {
      let {
        item: { orderCode },
      } = e.currentTarget.dataset;
      wx.p.router.go(
        "/packageMall/pages/commentDetails/commentDetails",
        orderCode
      );
    },

    // 获取用户信息
    onGetUserProfile(e) {
      const item = e.currentTarget.dataset.item;
      this.triggerEvent("getUserProfile", { item });
    },

    // 立即评价
    onEvaluate(e) {
      let {
        item: { orderCode },
      } = e.currentTarget.dataset;
      let { userInfo } = this.data;
      wx.p.router.go("/packageMall/pages/addComment/addComment", {
        orderCode,
        userInfo,
      });
    },

    // 取消订单
    onCancelOrder(e) {
      let { orderCode } = e.currentTarget.dataset.item || e.currentTarget.dataset;
      wx.showModal({
        title: "温馨提示",
        content: "请问您是否要取消售后",
        confirmText: "确认",
        cancelText: "取消",
        confirmColor: "#F13C3D",
      }).then((res) => {
        if (res.confirm) {
          wx.p.api.shopmallApi
            .cancelOrder(orderCode)
            .then(({ code, message }) => {
              if (code == "0") {
                wx.p.wxApi.showToast("操作成功");
                setTimeout(() => {
                  this.clearData();
                }, 1.5e3);
              } else {
                wx.p.wxApi.showToast(message);
              }
            });
        }
      });
    },

    // 售后详情
    onAfterSaleDetail(e) {
      let url = "/packageMine/pages/order/afterSaleDetail/afterSaleDetail";
      let {
        item: { orderCode },
      } = e.currentTarget.dataset;
      wx.p.router.go(url, {
        orderCode,
      });
    },
  },
  
  lifetimes: {
    attached() {
      // 组件实例进入页面节点树时执行
      this.updateButtons();
    }
  }
});