OrderActionButtons.js 11.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
// packageMine/pages/order/index/components/orderActionButtons.js

// 定义枚举常量
const OrderStatus = {
  PENDING_PAYMENT: 1, // 待付款
  PROCESSING: 2, // 处理中
  SHIPPED: 3, // 已发货
  DELIVERED: 4, // 已送达
  COMPLETED: 5, // 已完成
  CANCELLED: 6, // 已取消
  REFUNDED: 7, // 已退款
  CLOSED: 8, // 已关闭
  AFTER_SALE: 9, // 售后中
};

const OrderSource = {
  SPECIAL: 5, // 特殊订单(如金币兑换)
};

const AfterSaleStatus = {
  PROCESSING: 1, // 售后处理中
};

const DeliveryType = {
  EXPRESS: 2, // 快递发货
};

const CouponStatus = {
  USED: 2, // 已使用
  EXPIRED: 3, // 已过期
};

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 == OrderStatus.PENDING_PAYMENT) {
        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 == OrderSource.SPECIAL &&
        (item.orderStatus == OrderStatus.PROCESSING ||
          item.orderStatus == OrderStatus.REFUNDED)
      ) {
        return [
          {
            text: "查看详情",
            type: "viewDetail",
            class: "btn pay",
            handler: this.onToGoldDetail.bind(this),
          },
        ];
      }

      // 售后中订单按钮策略
      if (item.afterSaleStatus) {
        const buttons = [];

        if (item.afterSaleStatus == AfterSaleStatus.PROCESSING) {
          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 > OrderStatus.PENDING_PAYMENT &&
        item.orderStatus < OrderStatus.AFTER_SALE
      ) {
        const buttons = [];

        // 售后按钮
        if (
          item.payPrice > 0 &&
          item.orderSource != 4 &&
          !(
            item.relatedUserCouponStatus == CouponStatus.USED ||
            item.relatedUserCouponStatus == CouponStatus.EXPIRED
          )
        ) {
          buttons.push({
            text: "售后",
            type: "afterSale",
            class: "btn",
            handler: this.onAfterSale.bind(this),
          });
        }

        // 确认收货按钮
        if (
          item.deliveryType == DeliveryType.EXPRESS &&
          item.orderStatus == OrderStatus.SHIPPED
        ) {
          buttons.push({
            text: "确认收货",
            type: "confirmReceipt",
            class: "btn confirm",
            handler: this.onConfirmReceipt.bind(this),
          });
        }

        // 待评价订单
        if (
          item.orderGoods?.length == 1 &&
          !hasUserInfo &&
          (item.orderStatus == OrderStatus.COMPLETED ||
            item.orderStatus == OrderStatus.CLOSED)
        ) {
          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 == OrderStatus.COMPLETED ||
          item.orderStatus == OrderStatus.CLOSED)
      ) {
        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();
    },
  },
});