Newer
Older
GDT_FRONT / src / views / asset / assetList.vue
wangxitong on 9 Feb 2023 12 KB 0209
<template>
  <app-container>
    <search-area :need-clear="true" :need-search-more="false" type="seperate" size="small" search-more-type="default" @search="fetchData(false)" @clear="clearInput">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.keywords" size="small" placeholder="资产编号/名称" @change="fetchData(false)"/>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.assetType" placeholder="资产类型" size="small" clearable @change="fetchData(false)">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.status" size="small" placeholder="资产状态" clearable @change="fetchData(false)">
          <el-option value="1" label="在用" />
          <el-option value="0" label="弃用" />
        </el-select>
      </search-item>
    </search-area>
    <normal-table
      :data="list"
      :head="tableOption.head"
      :query="listQuery"
      :total="total"
      :columns="columns"
      :list-loading="listLoading"
      :options="tableOption.options"
      :tools-option="tableOption.toolsOption"
      size="small"
      @change="changePage"
      @selectionChange="handleSelectionChange"
    >
      <template slot="btns">
        <el-button size="small" class="edit_btn" icon="el-icon-edit" @click="add()">
          添加
        </el-button>
        <el-button size="small" class="edit_btn" icon="el-icon-delete" @click="batchDel()">
          删除
        </el-button>
        <el-button size="small" class="edit_btn" icon="el-icon-download" @click="exportFile()">
          导出
        </el-button>
        <download-template :filename="filename" />
        <el-upload
          :limit="1"
          :show-file-list="false"
          :http-request="uploadFile"
          :file-list="fileList"
          action="string"
          style="float:right; margin:0 0 0 3px;"
          accept=".xls,.xlsx"
        >
          <el-button slot="trigger" size="small" icon="el-icon-folder-add">
            批量导入
          </el-button>
        </el-upload>
      </template>
      <template slot="columns">
        <template slot="columns">
          <el-table-column label="资产照片" align="center" width="120">
            <template slot-scope="scope">
              <img :src="scope.row.picture ? `${scope.row.picture}`: defaultPhoto" width="50" height="50" style=" margin-top: 10px" @error="errorImg">
            </template>
          </el-table-column>
        </template>
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.stop="detail(scope.row)">
              详情
            </el-button>
            <el-button type="text" size="small" @click.stop="edit(scope.row)">
              编辑
            </el-button>
            <el-button type="text" size="small" @click.stop="del(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-asset v-show="dialogFormVisible" ref="editAsset" @watchChild="fetchData" />
  </app-container>
</template>

<script>
import DownloadTemplate from '@/components/DownloadTemplate/index'
import NormalTable from '@/components/NormalTable'
import AppContainer from '@/components/layout/AppContainer'
import SearchArea from '@/components/SearchArea/SearchArea'
import SearchItem from '@/components/SearchArea/SearchItem'
import { getAssetListPage, delAsset, batchDelAsset, batchImportAsset, getAssetExport } from '@/api/asset'
import EditAsset from './editAsset'
import { getDictByCode } from '@/api/system/dict'

export default {
  name: 'AssetList',
  components: { SearchItem, SearchArea, AppContainer, NormalTable, EditAsset, DownloadTemplate },
  data() {
    return {
      defaultPhoto: require('@/assets/global_images/photo_error.png'),
      errorImg: require('@/assets/global_images/photo_error.png'),
      typeList: [],
      listQuery: {
        keywords: '',
        assetType: '',
        status: '',
        offset: 1,
        limit: 20
      }, // 筛选条件
      columns: [
        {
          text: '资产名称',
          value: 'assetName',
          align: 'center',
          width: 150
        },
        {
          text: '资产类别',
          value: 'assetTypeName',
          align: 'center',
          width: 150
        },
        {
          text: '资产编号',
          value: 'assetCode',
          align: 'center'
        },
        {
          text: '存放位置',
          value: 'position',
          align: 'center'
        },
        {
          text: '设备状态',
          value: 'statusName',
          align: 'center',
          width: 80
        },
        {
          text: '品牌型号',
          value: 'brand',
          align: 'center'
        },
        {
          text: '设备序列号',
          value: 'serial',
          align: 'center'
        },
        {
          text: '备注',
          value: 'remarks',
          align: 'center'
        }
      ], // 显示列
      dialogFormVisible: false, // 是否显示编辑框
      timeRange: [], // 时间范围
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      filename: 'asset_template.xlsx',
      fileList: [],
      tableOption: {
        head: {
          show: true, // 是否需要标题栏,
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false, // 是否需要刷新按钮
          needCheckBox: true
        }
      } // 表格属性
    }
  },
  created() {
    this.fetchOptions()
    this.fetchData()
  },
  methods: {
    fetchOptions() {
      getDictByCode('assetType').then(response => {
        if (response.code === 200) {
          this.typeList = response.data
        }
      })
    },
    uploadFile(param) {
      // 判断文件大小是否符合要求
      const _file = param.file
      const isLt5M = _file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('请上传5M以下的excel文件')
        return false
      }
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '导入中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      // 发起导入请求
      batchImportAsset(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$message.success('导入成功')
          this.fetchData(false)
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        loading.close() // 关闭加载动画
      })
      this.fileList = []
    },
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页,否则跳转第一页
        this.listQuery.offset = 1
      }
      // getAssetListPage(this.listQuery).then(response => {
      //   if (response.code === 200) {
      //     this.list = response.data.rows.map(item =>{
      //       item.statusName = item.status === '1'? '在用': '弃用'
      //       return item
      //     })
      //     this.total = parseInt(response.data.total)
      //     this.listLoading = false
      //   }
      // })
      const that = this
      setTimeout(function() {
        that.list = [
          { id: '', assetName: '数字彩色四分割器', assetType: '0', assetTypeName: '视频监控设备', assetCode: '0024214365421111', position: '一期消防监控中心', status: '1', brand: '松下 WJ-MS424', serial: 'WJ-MS424', picture: '', remarks: '' },
          { id: '', assetName: '数字彩色四分割器', assetType: '0', assetTypeName: '视频监控设备', assetCode: '0024214365421111', position: '一期消防监控中心', status: '1', brand: '松下 WJ-MS424', serial: 'WJ-MS424', picture: '', remarks: '' },
          { id: '', assetName: '数字彩色四分割器', assetType: '0', assetTypeName: '视频监控设备', assetCode: '0024214365421111', position: '一期消防监控中心', status: '1', brand: '松下 WJ-MS424', serial: 'WJ-MS424', picture: '', remarks: '' },
          { id: '', assetName: '数字彩色四分割器', assetType: '0', assetTypeName: '视频监控设备', assetCode: '0024214365421111', position: '一期消防监控中心', status: '1', brand: '松下 WJ-MS424', serial: 'WJ-MS424', picture: '', remarks: '' },
          { id: '', assetName: '数字彩色四分割器', assetType: '0', assetTypeName: '视频监控设备', assetCode: '0024214365421111', position: '一期消防监控中心', status: '1', brand: '松下 WJ-MS424', serial: 'WJ-MS424', picture: '', remarks: '' },
          { id: '', assetName: '数字彩色四分割器', assetType: '0', assetTypeName: '视频监控设备', assetCode: '0024214365421111', position: '一期消防监控中心', status: '1', brand: '松下 WJ-MS424', serial: 'WJ-MS424', picture: '', remarks: '' }
        ]
        that.list = that.list.map(item => {
          item.statusName = item.status === '1' ? '在用' : '弃用'
          return item
        })
        that.total = 200
        that.listLoading = false
      }, 100)
    },
    exportFile() {
      const loading = this.$loading({
        lock: true,
        text: '数据处理中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      getAssetExport(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        console.log('download===', res)
        const blob = new Blob([res.data])
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = `资产列表.xlsx` // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放blob对象
      }).catch((res) => {
        loading.close()
        this.$message.error(res.message)
      })
    },
    // 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    batchDel() {
      const ids = this.multipleSelection.map(item => item.id)
      if (ids.length === 0) {
        this.$message.warning('请至少选择一行')
        return
      }
      this.$confirm(
        '确定要删除所选数据吗?',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        batchDelAsset(ids).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    // 打开详情对话框
    detail(row) {
      this.$refs.editAsset.initDialog('detail', row)
    },

    // 新增区域
    add() {
      this.dialogFormVisible = true
      this.$refs.editAsset.initDialog('create')
    },
    // 编辑区域信息
    edit(row) {
      this.dialogFormVisible = true
      this.$refs.editAsset.initDialog('update', row)
    },
    del(row) {
      this.$confirm(
        '确定要删除该条数据吗?',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delAsset(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    clearInput() {
      this.listQuery = {
        keywords: '',
        assetType: '',
        status: '',
        offset: 1,
        limit: 20
      }
      this.fetchData()
    }
  }
}
</script>

<style scoped>

</style>