afterSale.js 10 KB
// pages/orders/afterSale/afterSale.js
import { GET } from '../../../../libs/request';
import { API_HOST } from '../../../../libs/config';
import { getImageUrlWithWH } from '../../../../utils/util.js';

const REFUND_COMPUTE = 'app.refund.compute';
const REFUND_GOODSLIST = 'app.refund.goodsList';
const REFUND_SUBMIT = 'app.refund.submit';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    refundProudectListTitle: '请选择退货商品',
    selectedImage: '/pages/addressManager/images/default-select@3x.png',
    unSelectedImage: '/pages/addressManager/images/default-normal@3x.png',
    arrows: '/pages/bindPhoneNumber/images/arrow-down@2x.png',
    tips: '/images/que_ic@3x.png',

    order_code: 0,
    refundData: {},
    goodsList: [],
    return_amount_mode: [],
    defaultWay: '',
    selectWay: {},
    return_reason: [],

    return_amount: '0.00',
    return_asset_desc: '最多0.00,以实际退款为准',
    isShowPrice: false,
    showInputAndUploadImageArrays: [
      4,
      6,
      8
    ],
    paymentInfo: {
      province: '',
      city: '',
      remark: '',
    },
    refundShipFeeDescription: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let { order_code } = options;
    console.log(order_code);
    this.setData({
      order_code
    });
    this.getRefundList(order_code);
  },

  getRefundList(order_code) {
    GET(API_HOST, {
      order_code,
      method: REFUND_GOODSLIST
    }).then(json => {
      if (json && json.code === 200 && json.data) {
        const list = this.handleList(json.data.goods_list);
        let paymentInfo = this.data.paymentInfo;
        let return_bank_mode = json.data.return_bank_mode;
        paymentInfo.return_amount_mode = json.data.return_amount_mode[0].id;
        paymentInfo.bank_id = return_bank_mode[0].id;
        paymentInfo.head_bank_name = return_bank_mode[0].title;
        paymentInfo.needSubBranch = return_bank_mode[0].needSubBranch;
        this.setData({
          refundData: json.data,
          goodsList: list,
          defaultWay: json.data.return_amount_mode[0].name,
          return_amount_mode: json.data.return_amount_mode,
          selectWay: json.data.return_amount_mode[0],
          return_amount_desc: json.data.return_amount_desc,
          return_reason: json.data.return_reason,
          paymentInfo,
          refundShipFeeDescription: json.data.refundShipFeeDescription
        })
      }
    }).catch(error => {
      console.log(error);
    });
  },

  onChangeSelected(e) {
    const index = e.detail;
    const goodsList = this.data.goodsList;
    const good = goodsList[index];
    good.selected = !good.selected;
    if (!good.selected) {
      good.evidence_images = []
    }
    this.setData({
      goodsList
    })
    this.getRefundPrice(goodsList);
  },

  handleSelectedGoods(goodsList) {
    let newGoods = []
    goodsList.forEach((item) => {
      if (item.selected) {
        let { product_sku, product_skn, product_skc, last_price, goods_type_id } = item;
        let obj = {
          product_sku,
          product_skn,
          product_skc,
          last_price,
          goods_type: goods_type_id
        };
        newGoods.push(obj);
      }
    });
    return newGoods;
  },

  handleSubmitData(goodsList) {
    let remark, returned_reason, evidence_images;
    let newGoods = [];
    let inputAndUploadImage = this.data.showInputAndUploadImageArrays;
    return new Promise((resolve, reject) => {
      goodsList.forEach((item) => {
        if (item.selected) {
          let { product_sku, product_skn, product_skc, last_price, goods_type_id } = item;
          if (item.returned_reason) {
            returned_reason = item.returned_reason;
            // 质量瑕疵、发错货、图片与实物不符这三个时,下方展开"退货原因说明"和上传"照片凭证"
            if (inputAndUploadImage.indexOf(returned_reason) !== -1) {
              if (item.remark && item.remark.length > 0) {
                remark = item.remark;
              } else {
                reject(new Error('请输入退款原因'));
              }
              if (item.evidence_images && item.evidence_images.length > 0) {
                evidence_images = item.evidence_images
              } else {
                reject(new Error('请输入上传图片'));
              }
            }
          } else {
            reject(new Error('请选择退款原因'));
          }

          let obj = {
            product_sku,
            product_skn,
            product_skc,
            last_price,
            goods_type: goods_type_id,
            returned_reason
          };

          if (remark) {
            obj.remark = remark;
          }

          if (evidence_images) {
            obj.evidence_images = evidence_images;
          }
          newGoods.push(obj);
        }
      });
      if (newGoods.length > 0) {
        resolve(newGoods);
      } else {
        reject(new Error('请选择要退货的商品'));
      }
    })
  },

  getRefundPrice(goodsList) {
    let isShowPrice = this.isShowPrice(goodsList);
    let order_code = this.data.order_code;
    if (isShowPrice) {
      let goods = this.handleSelectedGoods(goodsList);
      GET(API_HOST, {
        order_code,
        goods: JSON.stringify(goods),
        method: REFUND_COMPUTE
      }).then(json => {
        if (json && json.code === 200 && json.data) {
          this.setData({
            isShowPrice,
            return_amount: json.data.return_amount,
            return_asset_desc: json.data.return_asset_desc
          })
        }
      });
    } else {
      this.setData({
        isShowPrice
      })
    }    
  },

  isShowPrice(goodsList) {
    let isShow = false;
    goodsList.forEach((item) => {
      let selected = item.selected;
      isShow = isShow || selected;
    });
    return isShow;
  },

  handleList(goodsList) {
    const list = goodsList.map((item) => {
      item.goods_image = getImageUrlWithWH(item.goods_image, 180, 180 * 1.25);
      item.selected = false;
      item.reasonIndex = ''
      item.returned_reason = '';
      item.evidence_images = [];
      if (item.goods_type === 'gift') {
        item.returned_reason = '3'
      }
      return item;
    })
    return list;
  },

  showTips() {
    tt.showModal({
      title: '退款金额说明',
      content: this.data.return_amount_desc,
      showCancel: false,
      confirmColor: '#D0021B',
      confirmText: '我知道了'
    })
  },

  bindPickerChange(e) {
    const { return_amount_mode } = this.data;
    this.setData({
      defaultWay: return_amount_mode[e.detail.value].name,
      selectWay: return_amount_mode[e.detail.value]
    })
  },

  updateGoodsImage(e) {
    console.log('afterSale: ', e.detail);
    let { currentIndex, finalImageArray } = e.detail;
    const goodsList = this.data.goodsList;
    const good = goodsList[currentIndex];
    good.evidence_images = finalImageArray;
    this.setData({
      goodsList
    })
  },

  pickerSelectReason(e) {
    const reasonIndex = e.detail.reasonIndex;
    const index = e.detail.index;
    const goodsList = this.data.goodsList;
    const good = goodsList[index];
    good.reasonIndex = reasonIndex;
    good.returned_reason = e.detail.id;
    this.setData({
      goodsList
    });
  },

  confirmReasonInfo(e) {
    let { currentIndex, reason } = e.detail;
    const goodsList = this.data.goodsList;
    const good = goodsList[currentIndex];
    good.remark = reason;
    this.setData({
      goodsList
    })
  },

  submitRefundData(goods) {
    let order_code = this.data.order_code;
    let payment = this.data.paymentInfo;
    GET(API_HOST, {
      method: REFUND_SUBMIT,
      order_code,
      goods: JSON.stringify(goods),
      payment: JSON.stringify(payment),
    }).then(data => {
      if (data && data.code === 200 && data.data) {
        let apply_id = data.data.apply_id;
        tt.showModal({
          content: '退货申请已提交,\r\n请等待审核',
          cancelText: '返回订单',
          cancelColor: '#4A4A4A',
          confirmText: '查看进度',
          confirmColor: '#4A4A4A',
          success(res) {
            if (res.cancel) {
              tt.navigateBack({
                delta: 1
              })
            } else {
              tt.navigateTo({
                url: `../refundOrderProgress/refundOrderProgress?apply_id=${apply_id}`,
              })
            }
          }
        });
      } else {
        tt.showToast({
          title: data.message,
          icon: 'none'
        });
      }
    }).catch(error => {
      console.log(error);
    })
  },

  refundSubmit(goods) {
    let refundShipFeeDescription = this.data.refundShipFeeDescription;
    let that = this;
    tt.showModal({
      title: '关于赠品退货说明',
      content: refundShipFeeDescription,
      cancelColor: '#D0021B',
      cancelText: '我知道了',
      confirmColor: '#4A4A4A',
      confirmText: '赠品退货',
      success(res) {
        if (res.cancel) {
          that.submitRefundData(goods);
        }
      }
    });
  },

  submitRefund(e) {
    let that = this;
    const goodsList = this.data.goodsList;
    this.handleSubmitData(goodsList).then(goods => {
      if (goods.length > 0) {
        that.refundSubmit(goods);
      } else {
        tt.showToast({
          title: '请选择要退货的商品',
          icon: 'none'
        })
      }
    }).catch(error => {
      tt.showToast({
        title: error.message,
        icon: 'none'
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})