ChooseRefundOrderDialog.vue 12.3 KB
<!-- 选择订单 -->
<template>
  <baseDialog :dialog-config="dialogConfig" :is-show.sync="visible" @handleClick="handleAddConfirm">
    <d2-search ref="showSearch" class="show-search" :search-form="searchForm" :label-width="labelWidth" :form-data="showFormData">
      <template slot="orderCodeKeyword">
        <el-input v-model="searchForm.orderCodeKeyword" maxlength="50" placeholder="请输入订单编码" @input="handleOrderCode"></el-input>
      </template>
    </d2-search>
    <div class="up-btns">
      <el-button @click="handleSearch" size="small" type="primary">
        查询
      </el-button>
      <el-button size="small" @click="handleReset">
        重置
      </el-button>
    </div>
    <el-table ref="orderTable" :data="tableData" class="order_table" border @select="handleChange" @select-all="handleAll">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" width="60px" label="序号" type="index" :index="
          (index) =>
            index +
            ((page.pageNum
              ? page.pageNum
              : page.pageNum) -
              1) * page.pageSize + 1
        " />
      <el-table-column label="订单编号" prop="orderCode" align="center"></el-table-column>
      <el-table-column label="客户名称" prop="customerName" align="center"></el-table-column>
      <el-table-column label="商品名称" prop="" align="center">
        <template slot-scope="scope">
          <div v-for="(item, index) in scope.row.goodsNameList" class="goods-name-item" :key="index">
            <el-tooltip class="item" :content="item" placement="right">
              <span>
                {{ item ? (item.length > 10 ? item.substring(0, 10) + '...' : item) : '-' }}
              </span>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="订单总价" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.totalPrice ? `¥${scope.row.totalPrice}` : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center">
        <template slot-scope="scope">
          <span>{{ payTypeOptions[scope.row.payType] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" prop="successDate" align="center"></el-table-column>
      <el-table-column label="流量方" prop="merchantName" align="center"></el-table-column>
      <el-table-column label="服务对接" prop="sellerMerchantName" align="center"></el-table-column>
      <el-table-column label="流量方总金额" align="center">
        <template slot-scope="scope">
          <el-input class="wid_120" @input="event => { handleInput(event, scope.$index, scope.row) }" placeholder="请输入" v-model="scope.row.shopDivisionMoneyInput" v-if="isShowInput(scope.row)"></el-input>
          <span v-else>{{ scope.row.shopDivisionMoney ? `¥${scope.row.shopDivisionMoney}` : '-' }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background @current-change="jumpPage" :current-page.sync="page.pageNum" :page-size="page.pageSize" layout="total, prev, pager, next" :total="tableTotal" />
    <div class="selected_box" v-if="selectedData.length">
      <span>已选订单:</span>
      <el-tooltip class="item" :content="formatSelected(selectedData, 'all')" placement="right">
        <span v-html="formatSelected(selectedData)"></span>
      </el-tooltip>
      <span class="totle_price">{{ selectedTotalPrice ? `共${selectedTotalPrice}元` : '--元' }}</span>
    </div>
  </baseDialog>
</template>
<script>
import { payTypeList, payType } from "@/common/constants"
// import * as api from "@api/finance"
import { getRefundOrderPage } from "@api/accountManage"
import { Util, deepClone } from '@/common/util'
import BigNumber from 'bignumber.js'
import { limitPrice } from '@/common/format'
export default {
  name: 'ChooseRefundOrderDialog',
  props: {
    chooseVisible: {
      type: Boolean,
      default: false
    },
    infoForm: {
      type: Object,
      default: () => { }
    },
    chooseData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      labelWidth: '90px',
      dialogConfig: {
        title: '选择订单',
        width: '75%'
      },
      tableData: [],
      searchForm: {
        orderCodeKeyword: '',
        customerName: '',
        payDate: '',
        payType: '',
        orderGoodsName: ''
      },
      showFormData: [{
        label: '订单编码',
        name: '',
        type: 'slot',
        addHtml: 'orderCodeKeyword'
      }, {
        label: '客户名称',
        name: 'customerName',
        type: 'input',
        maxlength: '20',
        isClear: true
      }, {
        label: '支付时间',
        name: 'payDate',
        type: 'datePicker',
        isClear: true,
        pickerOptions: 'rangePickerOptions'
      }, {
        label: '商品名称',
        name: 'orderGoodsName',
        type: 'input',
        maxlength: '50',
        isClear: true
      }],
      selectedData: [],
      tableTotal: 0,
      page: {
        pageSize: 10,
        pageNum: 1
      },
      baseData: {
        searchPayTypeList: deepClone(payTypeList)
      },
      payTypeOptions: deepClone(payType),
      selectedTotalPrice: 0
    }
  },
  watch: {
    selectedData: {
      handler(newVal) {
        const numberList = newVal?.map(val => { return val.shopDivisionMoneyInput }) || []
        if (numberList?.length) {
          this.selectedTotalPrice = numberList.reduce((preVal, curVal) => {
            return `${new BigNumber(preVal || 0).plus(new BigNumber(curVal || 0)).toNumber()}`
          })
        }
      },
      deep: true
    }
  },
  computed: {
    visible: {
      get() {
        return this.chooseVisible
      },
      set(newVal) {
        this.$emit('dialogClose', { flag: newVal, attrName: 'chooseVisible' })
      }
    }
  },
  mounted() {
    // this.selectedData = deepClone(this.infoForm.orderCodeScope)
    this.selectedData = []
    this.getOrderList()
    this.getOptionsList()
  },
  methods: {
    getOptionsList() {
      this.showFormData.forEach((item, index) => {
        if (item.type === 'select') {
          if (this.baseData[item.optionsName].length && this.baseData[item.optionsName][0][this.showFormData[index].optionLabel] !== '全部') {
            const temp = {}
            temp[this.showFormData[index].optionId] = ''
            temp[this.showFormData[index].optionLabel] = '全部'
            this.baseData[item.optionsName].unshift(temp)
          }
          this.$set(this.showFormData[index], 'optionList', this.baseData[item.optionsName])
        }
      })
    },
    // 订单编号查询条件 仅输入数字
    handleOrderCode(e) {
      let newValue = e.replace(/[^\d.]/g, '')
      this.searchForm.orderCodeKeyword = newValue
    },
    handleChange(selection, row) {
      // 不再选中后将输入框的值重置
      if (!selection?.map(val => val.orderCode)?.includes(row?.orderCode)) {
        row.shopDivisionMoneyInput = row.shopDivisionMoneyInput || row.shopDivisionMoney
        row.oldshopDivisionMoneyInput = row.shopDivisionMoney
      }
      var addOrDelete = !!selection.find(
        (item) => item.orderCode === row.orderCode
      )
      addOrDelete
        ? this.selectedData.push(row)
        : (this.selectedData = this.selectedData.filter(
          (item) => item.orderCode !== row.orderCode
        ))
    },
    handleAll(selection) {
      // 不再选中后将输入框的值重置
      if (!selection?.length) {
        this.tableData?.forEach(row => {
          row.shopDivisionMoneyInput = row.shopDivisionMoney
          row.oldshopDivisionMoneyInput = row.shopDivisionMoney
        })
      }
      this.tableData.forEach((item) => {
        var isExit = this.selectedData.findIndex(
          (subItem) => subItem.orderCode === item.orderCode
        )
        if (selection.length && isExit === -1) {
          this.selectedData.push(item)
        } else if (!selection.length && isExit !== -1) {
          this.selectedData.splice(isExit, 1)
        }
      })
    },
    // 处理显示已选的订单
    formatSelected(data, all) {
      let text = data?.map(val => { return val.orderCode })?.join(' / ')
      return all ? text : text?.length > 40 ? `<span>${text.substring(0, 40)}... <span style="color: #999;">共${data.length}个订单</span></span>` : `<span>${text}</span>`
    },
    // 输入框输入限制
    handleInput(e, index, row) {
      var max = Math.abs(row.totalPrice)
      var min = -max
      let newValue = e
      let oldValue = this.tableData[index].oldshopDivisionMoneyInput
      this.$set(this.tableData[index], 'shopDivisionMoneyInput', limitPrice(newValue, oldValue, 20, 2, min, max, 'priceReg_negative_input'))
      this.$set(this.tableData[index], 'oldshopDivisionMoneyInput', this.tableData[index].shopDivisionMoneyInput)
      const selIndex = this.selectedData?.findIndex(val => val.orderCode == row.orderCode)
      if (selIndex > -1) {
        this.$set(this.selectedData[selIndex], 'shopDivisionMoneyInput', this.tableData[index].shopDivisionMoneyInput)
      }
    },
    // 获取订单列表
    getOrderList() {
      const params = this.getParams()
      getRefundOrderPage(params).then(res => {
        res?.list?.forEach(val => {
          val.shopDivisionMoneyInput = val.shopDivisionMoney
          val.oldshopDivisionMoneyInput = val.shopDivisionMoney
        })
        this.tableData = res?.list || []
        this.tableTotal = res?.total
        this.$nextTick(() => {
          this.recoverySelection()
        })
      })
    },
    // 翻页时设置选中状态
    recoverySelection() {
      var exitIds = this.selectedData.map((item) => item.orderCode)
      this.tableData?.forEach((item) => {
        if (exitIds.includes(item.orderCode)) {
          this.$refs.orderTable && this.$refs.orderTable.toggleRowSelection(item, true)
          let obj = this.selectedData?.find(val => val.orderCode == item.orderCode)
          item.shopDivisionMoneyInput = obj?.shopDivisionMoneyInput || obj?.shopDivisionMoney
        }
      })
    },
    // 处理接口请求参数
    getParams() {
      const searchFormSearch = deepClone(this.searchForm)
      if (searchFormSearch.payDate) {
        searchFormSearch.paySuccessStartDate = Util.dateFormate(searchFormSearch.payDate[0], 'YYYY-MM-DD HH:mm:ss')
        searchFormSearch.paySuccessEndDate = Util.dateFormate(searchFormSearch.payDate[1], 'YYYY-MM-DD') + ' 23:59:59'
        delete searchFormSearch.payDate
      }
      return {
        sceneType: this.infoForm?.sceneType,
        ymerchantId: this.infoForm?.yqMerchantId, // 对账园区店
        merchantId: this.infoForm?.shopMerchantId, // 对账流量方
        ...searchFormSearch,
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize,
        // filterTransferAccounts: true, // 固定传参
        // shopTrafficMoneyFlag: 'Y',  // 固定传参
        orderStatuses: [4, 6]
      }
    },
    jumpPage(page) {
      this.page.pageNum = page
      this.getOrderList()
    },
    handleSearch() {
      this.getOrderList()
    },

    handleReset() {
      this.searchForm = {
        orderCodeKeyword: '',
        customerName: '',
        payDate: '',
        payType: '',
        orderGoodsName: ''
      }
      this.page.pageNum = 1
      this.handleSearch()
    },

    handleAddConfirm() {
      if (!this.selectedData?.length) return this.$message.warning('请选择订单')
      if (this.checkPriceEmpty(this.selectedData, 'shopDivisionMoneyInput')) {
        return this.$message.warning('请完整输入流量方总金额')
      }
      this.$emit('chooseOrderConfirm', this.selectedData)
    },
    // 校验流量方金额
    checkPriceEmpty(arr, propName) {
      const hasEmpty = arr.some(item => {
        return (item[propName] ?? '') === ''
      })
      if (hasEmpty) {
        return true
      }
      return false
    },
    isShowInput(item) {
      return this.selectedData?.map(val => { return val.orderCode })?.includes(item.orderCode)
    }
  }
}
</script>
<style lang="scss" scoped>
.goods-name-item {
  margin-top: 10px;
}
.order_table {
  margin-bottom: 10px;
}
.wid_120 {
  display: inline-block;
  width: 120px;
}
.selected_box {
  position: absolute;
  left: 30px;
  bottom: 30px;
  .totle_price {
    margin-left: 10px;
    color: #999;
  }
}
</style>