Newer
Older
baseResourceFront / src / views / carinfo / listGpsDevice.vue
zhangyingjie on 24 Mar 2021 6 KB 合并master分支
<template>
  <app-container>
    <!--筛选条件-->
    <search-area size="small" @search="search">
      <search-item>
        <el-input v-model.trim="listQuery.imei" placeholder="IEMI号" size="small" clearable/>
      </search-item>
      <search-item>
        <el-input v-model="listQuery.iot" placeholder="物联网卡号" size="small" clearable/>
      </search-item>
    </search-area>

    <!--查询结果Table显示-->
    <normal-table :data="list" :query="listQuery" :total="total" :columns="columns" :list-loading="listLoading" class="table" border size="small" @selection-change="handleSelectionChange" @change="changePage">
      <template slot="btns">
        <download-template filename="deviceImportTemp.xlsx" class="edit_btn" size="small" icon="el-icon-document" btnname="模板下载"/>
        <el-upload
          :limit="1"
          :show-file-list="false"
          :http-request="uploadFile"
          :file-list="fileList"
          action="string"
          accept=".xls,.xlsx"
          class="edit_btn"
          style="margin-top: 0">
          <el-button slot="trigger" class="edit_btn" icon="el-icon-document-checked" size="small">批量导入</el-button>
        </el-upload>
        <el-button size="small" class="edit_btn" icon="el-icon-plus" @click="add">新增</el-button>
      </template>
      <template slot="columns">
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="detail(scope.row)">详情</el-button>
            <el-button type="text" size="mini" @click="edit(scope.row)">修改</el-button>
            <el-button type="text" size="mini" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>

    <!--编辑资源的对话框-->
    <edit-gps-device v-show="dialogFormVisible" ref="editGpsDevice" @watchChild="fetchData"/>
  </app-container>
</template>
<script>
import editGpsDevice from '@/views/carinfo/editGpsDevice'
import DownloadTemplate from '@/components/DownloadTemplate/index'
import { getDeviceInfoList, delDeviceInfo, batchImport } from '@/api/device'
export default {
  name: 'ListGpsDevice',
  components: { editGpsDevice, DownloadTemplate },
  data() {
    return {
      fileList: [], // 上传文件列表
      deptShow: true,
      listQuery: {
        imei: '',
        iot: '',
        carType: '',
        deptId: '',
        offset: 1,
        limit: 20
      }, // 查询条件
      columns: [
        {
          text: 'IMEI',
          value: 'imei',
          align: 'center'
        },
        {
          text: '物联网卡号',
          value: 'iot',
          align: 'center'
        },
        {
          text: 'SIM卡号',
          value: 'sim',
          align: 'center'
        },
        {
          text: '创建时间',
          value: 'createTime',
          align: 'center'
        }
      ], // 动态加载的表头
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      dialogFormVisible: false, // 是否显示编辑框
      cartypelist: [], // 类型列表
      deptlist: [] // 类型列表
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 批量导入
    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)'
      })
      // 发起导入请求
      batchImport(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$message.success('导入成功')
          this.fileList = []
          this.fetchData(false)
        }
      }).catch(() => {
        loading.close()
      })
      this.fileList = []
    },
    add() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$refs.editGpsDevice.initDialog(this.dialogStatus, this.dialogFormVisible)
    },
    edit(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$refs.editGpsDevice.initDialog(this.dialogStatus, this.dialogFormVisible, row)
    },
    detail(row) {
      this.dialogStatus = 'detail'
      this.dialogFormVisible = true
      this.$refs.editGpsDevice.initDialog(this.dialogStatus, this.dialogFormVisible, row)
    },
    del(row) {
      this.$confirm(
        '确定要删除' + row.imei + '吗?',
        '确认删除',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delDeviceInfo(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    search() {
      this.fetchData(false)
    },
    fetchData(isNowPage = true) {
      console.log('fetchData')
      this.listLoading = true
      if (!isNowPage) {
        this.listQuery.offset = 1
      }
      getDeviceInfoList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        // 如果当前页码数据为空,重新请求末页
        if (this.listQuery.offset > Math.ceil(this.total / this.listQuery.limit)) {
          this.listQuery.offset = Math.ceil(this.total / this.listQuery.limit)
          getDeviceInfoList(this.listQuery).then(response => {
            this.list = response.data.rows
            this.total = parseInt(response.data.total)
            this.listLoading = false
          })
        }
        this.listLoading = false
      })
    },
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .edit_btns{
    padding-top: 8px;
    .edit_btn{
      float:right;
      margin:0px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
</style>