litian
2024-03-12 b7ed8541113f8fa116b19d13e34a70a929310077
pages/personalCenter/myOrder/index.js
@@ -1,17 +1,52 @@
const app = getApp();
import moment from 'moment'
import tool2 from "../../../assets/js/toolClass.js"
import { getPublicImage } from "../../../assets/js/middleGround/tool.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    tabList: [
      {
        label: '全部',
        value: 'all',
      },
      {
        label: '待支付',
        value: 'WaitPay',
      },
      {
        label: '已完成',
        value: 'Success',
      },
      {
        label: '已取消',
        value: 'Cancel',
      }
    ],
    list: [],
    //分页
    page: 1,
    limit: 12,
    pageTotalCount: 0,
    bottomLoading: false,
    isMoreData: false,
    // 返回顶部
    isBackTop: false,
    setScrollValue: 0,
    skeletonLoding: true,
    queryFilter: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.getDataList(false)
  },
  /**
@@ -20,39 +55,278 @@
  onReady() {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  tabBookClick(item) {
    console.log(item, 1)
    let that = this;
    let info = this.data.tabList[item.detail.value]
    that.setData({
      skeletonLoding: true,
      active: item.detail.value,
      queryFilter: info.value == 'all' ? [] : [{ field: 'State', value: info.value }],
      list: [],
      page: 1,
      bottomLoading: false,
      isMoreData: false,
    })
    that.getDataList(false);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  getDataList(isReachBottom) {
    const data = {
      start: this.data.page * this.data.limit - this.data.limit,
      size: this.data.limit,
      filterList: this.data.queryFilter,
      sort: {
        type: 'Desc',
        field: 'CreateDate'
      }
    }
    app.MG.store.getUserOrderList(data).then((res) => {
      try {
        if (res.datas.length > 0) {
          res.datas.forEach((item, index) => {
            item.payPrice = tool2.toDecimal2(item.payPrice)
            item.createDate = moment(item.createDate).format("YYYY-MM-DD HH:mm:ss");
            item.finishedDate = moment(item.finishedDate).format("YYYY-MM-DD HH:mm:ss");
            if (item.state == "Success") {
              item.colorName = 'success';
              item.CustomState = "已完成";
            }
            if (item.state == "Cancel") {
              item.colorName = 'cancal';
              item.CustomState = "已取消";
            }
            if (item.state == "WaitPay") {
              item.colorName = 'waitPay';
              item.CustomState = "待支付";
            }
            if (item.state == "WaitDeliver") {
              item.colorName = 'waitDeliver';
              item.CustomState = "正在支付";
            }
            if (item.state == "GroupPaySuccess") {
              item.colorName = 'success';
              item.CustomState = "等待拼团"
            }
            if (item.state == "ReFounding") {
              item.colorName = 'ReFounding';
              item.CustomState = "退款中"
            }
            if (item.state == "ReFoundFinished") {
              item.colorName = 'ReFoundFinished';
              item.CustomState = "退款完成"
            }
            let oldlist = [];
            oldlist = item.saleMethodLinks
            oldlist.forEach(istrue => {
              istrue.name = istrue.orderSaleMethod.product.name
              istrue.payPrice = tool2.toDecimal2(istrue.payPrice)
              istrue.icon = getPublicImage(istrue.orderSaleMethod.product.icon, "", 400)
            })
          })
          let dataList = res.datas;
          //触底加载新数据并保留老数据
          if (isReachBottom) {
            dataList = [...this.data.list, ...dataList] //将新数据加入老数据中
          }
          this.setData({
            list: dataList,
            pageTotalCount: res.totalSize,
            bottomLoading: false,
            isMoreData: dataList.length > 0 ? false : true,
            skeletonLoding: false,
            loading: false
          })
        } else {
          this.setData({
            skeletonLoding: false,
            loading: false
          })
        }
      } catch (error) {
        console.log(error)
      }
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    if (this._freshing) return
    this.setData({
      // list: [],
      page: 1,
      limit: 12,
      pageTotalCount: 0,
      bottomLoading: false,
      isMoreData: false
    })
    this._freshing = true;
    this.setData({
      triggered: false,
    })
    this.getDataList(false);
    this._freshing = false
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      bottomLoading: true,
      isMoreData: false
    })
    let bool = false;
    if (this.data.pageTotalCount > this.data.list.length) {
      bool = true;
      this.setData({
        page: this.data.page + 1,
      })
    } else {
      setTimeout(() => {
        this.setData({
          bottomLoading: false,
          isMoreData: true
        })
      }, 100)
      return false;
    }
    this.getDataList(bool);
  },
  onCancel(e) {
    let { orderNum } = e.currentTarget.dataset
    console.log(orderNum, 12)
    wx.showModal({
      title: '提示', //提示的标题
      content: '确定要取消订单?', //提示的内容
      success: function (res) {
        if (res.confirm) {
          app.MG.store.cancelOrder({ orderNum: orderNum }).then(() => {
            wx.showToast({
              title: "订单已取消",
              icon: 'success',
              duration: 1000
            })
            this.getDataList(false)
          })
        } else if (res.cancel) {
          console.log('用户点击了取消')
        }
      }
    })
  },
  //立即支付、再次购买
  onPayment(e) {
    var that = this;
    const {
      orderNum
    } = e.currentTarget.dataset
    const data = {
      orderNum: orderNum,
    };
    app.MG.store.getOrderByOrderNum(data).then(res => {
      if (res && res.state != "WaitPay") {
        app.MG.store.confirmOrder(data).then(conRes => {
          if (conRes.orderNumber) {
            const resOrderNum = {
              orderNum: conRes.orderNumber,
            };
            if (conRes.payPrice != 0) {
              app.MG.store.makeWeChatPay(resOrderNum).then(payRes => {
                const payVal = JSON.parse(payRes);
                wx.requestPayment({
                  "appId": payVal.appId,
                  "timeStamp": payVal.timeStamp,
                  "nonceStr": payVal.nonceStr,
                  "package": payVal.package,
                  "signType": payVal.signType,
                  "paySign": payVal.paySign,
                  "success": function (res) {
                    if (res.errMsg == 'requestPayment:ok') {
                      that.setData({
                        isPaySuccess: true
                      })
                    }
                    console.log(res, "成功")
                  },
                  "fail": function (res) {
                    console.log(res, "失败")
                  },
                  "complete": function (res) {
                    if (res.errMsg == 'requestPayment:ok') {
                      that.setData({
                        isPayComplete: true
                      })
                    }
                    if (that.data.isPaySuccess && that.data.isPayComplete) {
                      that.navToBack()
                    }
                  }
                })
              });
            } else {
              wx.showToast({
                title: '领取成功',
              })
              // 可添加返回上级
              setTimeout(() => {
                that.navToBack()
              }, 2000);
            }
          }
        });
      } else {
        app.MG.store
          .makeWeChatPay({
            orderNum: orderNum
          })
          .then(payRes => {
            const payVal = JSON.parse(payRes);
            wx.requestPayment({
              "appId": payVal.appId,
              "timeStamp": payVal.timeStamp,
              "nonceStr": payVal.nonceStr,
              "package": payVal.package,
              "signType": payVal.signType,
              "paySign": payVal.paySign,
              "success": function (res) {
                if (res.errMsg == 'requestPayment:ok') {
                  that.setData({
                    isPaySuccess: true
                  })
                }
              },
              "fail": function (res) {
                console.log(res, "失败")
              },
              "complete": function (res) {
                if (res.errMsg == 'requestPayment:ok') {
                  that.setData({
                    isPayComplete: true
                  })
                }
                if (that.data.isPaySuccess && that.data.isPayComplete) {
                  that.navToBack()
                }
              }
            })
          });
      }
    });
  },
  //删除订单
  onDel(e) {
    const {
      orderNum
    } = e.currentTarget.dataset
  },