initData.js 3.73 KB
import { initData } from '@/api/data'
import { isBlank, isInteger } from '@/utils/validate'
import { deepCopy } from '../utils/common-util'
export default {
  data() {
    return {
      loading: true, copyData: [], data: [], page: 0, size: 10, sort: 'id,desc', total: 0, url: '', params: {}, query: { type: '', value: '' }, time: 170, isAdd: false, curQueryType: undefined, downloadLoading: false
    }
  },
  methods: {
    async init(type) {
      if (!await this.beforeInit()) {
        this.loading = false
        return
      }
      // console.log(this.params, 'sdfhisdjkf')
      return new Promise((resolve, reject) => {
        this.loading = true
        initData(this.url, this.params).then(res => {
          this.total = res.totalElements
          this.data = res.content
          this.copyData = deepCopy(this.data)
          if (type && type === 'input') {
            this.data.forEach(item => {
              item.copyEpisodeC2Status = []
              for (const key in item.episodeC2Status) {
                const obj = {
                  name: key
                }
                const sobj = {}
                Object.assign(sobj, obj, item.episodeC2Status[key])
                sobj.successNum = sobj.episodeCount ? Math.ceil((sobj.successCount / sobj.episodeCount) * 130) : 0
                sobj.failNum = sobj.episodeCount ? Math.ceil((sobj.failureCount / sobj.episodeCount) * 130) : 0
                item.copyEpisodeC2Status.push(sobj)
              }
            })
          } else if (type && type === 'hasInput') {
            this.data.forEach(item => {
              item.successNum = item.cmdCount ? Math.ceil((item.successCount / item.cmdCount) * 130) : 0
              item.failNum = item.cmdCount ? Math.ceil((item.failureCount / item.cmdCount) * 130) : 0
            })
          }
          setTimeout(() => {
            this.loading = false
          }, this.time)
          resolve(res)
        }).catch(err => {
          this.loading = false
          reject(err)
        }).finally(() => {
          if (this.codesResultList !== null && this.codesResultList !== undefined) {
            this.codesResultList = []
          }
        })
      })
    },
    beforeInit() {
      return true
    },
    pageChange(e, type) {
      this.page = e - 1
      this.init(type)
    },
    sizeChange(e, type) {
      this.page = 0
      this.size = e
      this.init(type)
    },
    sortChange(data) {
      const { prop, order } = data
      let direction
      if (order === 'ascending') {
        direction = 'asc'
      } else {
        direction = 'desc'
      }
      this.sort = prop + ',' + direction
      this.page = 0
      // console.log(this.sort)
      this.init()
    },
    // 预防删除第二页最后一条数据时,或者多选删除第二页的数据时,页码错误导致请求无数据
    dleChangePage(size) {
      if (size === undefined) {
        size = 1
      }
      if (this.data.length === size && this.page !== 0) {
        this.page = this.page - 1
      }
    },
    toQuery(type) {
      if (this.curQueryType) {
        const value = this.query.value
        const display = this.curQueryType.display_name
        // 在queryTypeOptions需要校验的对象加上type='number'
        if (this.curQueryType.type === 'number') {
          // 如果值非空且不为数字则弹出错误提示
          if (!isBlank(value) && !isInteger(value)) {
            this.$message.error(display + '必须为整数')
            return
          }
        }
      }
      this.page = 0
      this.init(type)
    },
    // el-select change函数,为curQueryType赋值,为查询输入内容进行类型校验
    typeChange(val, options) {
      this.curQueryType = options.find(item => {
        return item.key === val
      })
    }
  }
}