Newer
Older
smartcity_env_front / src / views / device / deviceList.vue
StephanieGitHub on 12 Aug 2021 11 KB MOD: 联调修改
<template>
  <app-container>
    <search-area :need-clear="true" :need-search-more="false" type="seperate" size="small" search-more-type="default" @search="fetchData" @clear="clearInput">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.keywords" size="small" placeholder="编号/点位名称" clearable/>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.position" size="small" placeholder="详细地址" clearable/>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.online" placeholder="在线状态" size="small" clearable="">
          <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value"></el-option>
        </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" @selectionChange="handleSelectionChange" @change="changePage">
      <template slot="btns">
        <el-button v-if="hasPerm('/environment/device/add')" size="small" class="edit_btn" @click.stop="add">新增</el-button>
        <el-button v-if="hasPerm('/environment/device/delete')" size="small" class="edit_btn" @click="batchDel">删除</el-button>
        <el-upload
          v-if="hasPerm('/environment/device/import')"
          :limit="1"
          :show-file-list="false"
          :http-request="uploadFile"
          :file-list="fileList"
          action="string"
          accept=".xls,.xlsx"
          class="edit_btn">
          <el-button slot="trigger" size="small">批量导入</el-button>
        </el-upload>
        <el-button v-if="hasPerm('/environment/device/export')" size="small" class="edit_btn" @click="batchExport">批量导出</el-button>
      </template>
      <template slot="preColumns">
        <el-table-column type="selection" width="45" align="center"></el-table-column>
      </template>
      <template slot="columns">
        <el-table-column label="在线状态" align="center" width="90">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.online=='1'" type="success" size="small">在线</el-tag>
            <el-tag v-else type="info" size="small">离线</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.stop="goDetail(scope.row)">详情</el-button>
            <el-button v-if="hasPerm('/environment/device/update')" type="text" size="small" @click.stop="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-device v-show="editShow" ref="editDevice" @watchChild="fetchData"/>
    <detail-device v-show="detailShow" ref="detailDevice"/>
  </app-container>
</template>

<script>
import { getDeviceListPage, delDevice, batchImportDevice, batchExportDevice } from '@/api/environment/device'
import EditDevice from './editDevice'
import { exportFile } from '@/utils/exportutils'
import DetailDevice from "./detailDevice";
export default {
  name: 'DeviceList',
  components: {DetailDevice, EditDevice },
  data() {
    return {
      listQuery: {
        keywords: '', // 关键字
        online: '', // 在线状态
        position: '', // 位置
        offset: 1,
        limit: 20,
        sort: '',
        order: 'desc'
      }, // 筛选条件
      columns: [
        {
          text: '设备编号',
          value: 'deviceNo',
          align: 'center',
          width: 160
        },
        {
          text: '点位名称',
          value: 'deviceName',
          align: 'center'
        },
        // {
        //   text: '市',
        //   value: 'responsiblePerson',
        //   align: 'center',
        //   width: 80
        // },
        {
          text: '区/县',
          value: 'area',
          align: 'center'
        },
        {
          text: '详细地址',
          value: 'position',
          align: 'center'
        },
        {
          text: '权属单位',
          value: 'deptName',
          align: 'center'
        },
        {
          text: '备注',
          value: 'notes',
          align: 'center'
        },
        {
          text: '安装时间',
          value: 'installDate',
          align: 'center'
        }
      ], // 显示列
      options:[
        {value:'1',name:'在线'},
        {value:'2',name:'离线'}
      ],
      timeRange: [], // 时间范围
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      typeList: [],
      fileList: [],
      tableOption: {
        head: {
          show: true, // 是否需要标题栏,
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      }, // 表格属性
      multipleSelection:[], // 多选选中项
      editShow: false, // 编辑页面是否显示
      detailShow: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getDeviceListPage(this.listQuery).then(response => {
        this.list = response.data.rows
        // this.list = [
        //     {id:"1", deviceNo:"2021030803100001", deviceName:"王泥岭", area:'崇仁县', online:'1', deptName:"城管局", areaCode:'361024', lat:27.77740600, lng:116.05673800, notes:"无", position:"王泥岭", ts:"2021-01-12", deptid:'24', installDate:"2021-07-01"},
        //     {id:"2", deviceNo:"2021030803100002", deviceName:"王泥岭", area:'崇仁县', online:'1', deptName:"城管局", areaCode:'361024', lat:27.77740600, lng:116.05673800, notes:"无", position:"王泥岭", ts:"2021-01-12", deptid:'24', installDate:"2021-07-01"},
        //     {id:"3", deviceNo:"2021030803100003", deviceName:"王泥岭", area:'崇仁县', online:'1', deptName:"城管局", areaCode:'361024', lat:27.77740600, lng:116.05673800, notes:"无", position:"王泥岭", ts:"2021-01-12", deptid:'24', installDate:"2021-07-01"},
        //     {id:"4", deviceNo:"2021030803100004", deviceName:"王泥岭", area:'崇仁县', online:'1', deptName:"城管局", areaCode:'361024', lat:27.77740600, lng:116.05673800, notes:"无", position:"王泥岭", ts:"2021-01-12", deptid:'24', installDate:"2021-07-01"},
        //     {id:"5", deviceNo:"2021030803100005", deviceName:"王泥岭", area:'崇仁县', online:'2', deptName:"城管局", areaCode:'361024', lat:27.77740600, lng:116.05673800, notes:"无", position:"王泥岭", ts:"2021-01-12", deptid:'24', installDate:"2021-07-01"},
        //     {id:"6", deviceNo:"2021030803100006", deviceName:"王泥岭", area:'崇仁县', online:'2', deptName:"城管局", areaCode:'361024', lat:27.77740600, lng:116.05673800, notes:"无", position:"王泥岭", ts:"2021-01-12", deptid:'24', installDate:"2021-07-01"}
        // ]
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 点击编辑
    edit(row) {
      this.editShow = true
      this.$refs.editDevice.initDialog('update', true, row)
    },
    // 点击详情
    goDetail(row) {
      this.detailShow = true
      this.$refs.detailDevice.initDialog(row)
    },
    // 点击新增
    add() {
      this.editShow = true
      this.$refs.editDevice.initDialog('create', true)
    },
    // 删除单个设备
    del(row) {
      const deleteIds = [row.id]
      this.$confirm(
        '确定要删除该设备吗?',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delDevice(deleteIds).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    handleSelectionChange(val){
      console.log('device Selection Change')
      this.multipleSelection = val
      console.log(this.multipleSelection)
    },
    // 删除多个设备
    batchDel() {
      if (this.checkSelection()) {
        const deleteIds = []
        this.multipleSelection.forEach(function(value, index) {
          deleteIds.push(value.id)
        })
        this.$confirm(
          '确定要删除所选设备吗?',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          delDevice(deleteIds).then(response => {
            if (response.code === 200) {
              this.$message.success('删除成功')
              this.fetchData()
            }
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    // 批量导入
    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)'
      })
      // 发起导入请求
      batchImportDevice(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$message.success('导入成功')
          this.fetchData()
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        loading.close() // 关闭加载动画
      })
      this.fileList = []
    },
    // 批量导出
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '数据处理中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      batchExportDevice(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        const blob = new Blob([res.data])
        const fileName = `设备列表.xlsx`
        exportFile(blob, fileName)
      }).catch((res) => {
        loading.close()
      })
    },
    // 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 重置后的操作, 若不需要显示重置按钮则不需要写
    clearInput() {
      this.listQuery = {
        keywords: '', // 关键字
        online: '', // 在线状态
        position: '', // 位置
        offset: 1,
        limit: 20,
        sort: '',
        order: 'desc'
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .edit_btns{
    .edit_btn{
      float:right;
      margin-left:5px;
    }
  }
</style>