Newer
Older
baseResourceFront / src / views / deviceManage / busController.vue
zhangyingjie on 23 Mar 2021 9 KB 路灯样式整合
<!--设备列表-->
<template>
  <app-container>
    <search-area :need-clear="true" :need-search-more="false" type="seperate" search-more-type="default" @search="fetchData" @clear="clearInput">
      <search-item>
        <el-input v-model.trim="listQuery.controllerCode" placeholder="控制器编号" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.controllerType" placeholder="控制器类型" clearable>
          <el-option v-for="item in controllerTypeList" :key="item.value" :label="item.name" :value="item.value"/>
        </el-select>
      </search-item>
      <search-item>
        <dept-select v-model="listQuery.deptId" :dept-show="true" :need-top="true" placeholder="所属组织"/>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.status" placeholder="选择设备状态" clearable>
          <el-option v-for="item in statusList" :key="item.value" :label="item.name" :value="item.value"/>
        </el-select>
      </search-item>
    </search-area>
    <normal-table :list-loading="listLoading" :data="list" :query="listQuery" :total="total" :columns="columns" class="table" border @change="changePage" @selectionChange="handleSelectionChange">
      <template slot="btns">
        <el-button v-if="hasPerm('/controller/delete')" class="edit_btn" size="small" @click="del">删除</el-button>
        <el-button class="edit_btn" size="small" @click="add">新增</el-button>
        <el-button class="edit_btn" size="small" @click="batchExport">导出</el-button>
        <el-upload
          :limit="1"
          :show-file-list="false"
          :http-request="uploadFile"
          :file-list="fileList"
          class="edit_btn"
          style=" display: inline;"
          action="string"
          accept=".xls,.xlsx">
          <el-button size="small">批量导入</el-button>
        </el-upload>
        <download-template filename="controllerImport.xlsx" class="edit_btn" size="small" btnname="模板下载"/>
      </template>
      <template slot="preColumns">
        <!--拥有删除权限则显示复选框-->
        <el-table-column v-if="hasPerm('/controller/delete')" align="center" type="selection" width="55"/>
      </template>
      <template slot="columns">
        <!--操作列-->
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/controller/edit')" type="text" @click="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-controller ref="editController" @watchChild="fetchData"/>
  </app-container>
</template>

<script>
import DeptSelect from '@/components/DeptSelect'
import EditController from '@/views/deviceManage/editController'
import { getContrllerList, batchDelController, batchExportController } from '@/api/device'
import { getControllerType } from '@/api/allDict'
import { batchImportController } from '@/api/fileImport'
import DownloadTemplate from '@/components/DownloadTemplate/index'
import NormalTable from '../../components/NomalTable/index'
import AppContainer from '../../components/layout/AppContainer'
import SearchArea from '../../components/SearchArea/SearchArea'
import SearchItem from '../../components/SearchArea/SearchItem'
export default {
  name: 'BusController',
  components: { SearchItem, SearchArea, AppContainer, NormalTable, EditController, DeptSelect, DownloadTemplate },
  data() {
    return {
      fileList: [], // 批量导入
      listQuery: {
        controllerCode: '',
        controllerType: '',
        status: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '控制器编号',
          value: 'controllerCode',
          align: 'center',
          width: 140
        },
        {
          text: '控制器类型',
          value: 'controllerTypeName',
          align: 'center'
        },
        {
          text: '所属组织',
          value: 'deptName',
          align: 'center'
        },
        {
          text: '电信设备编号',
          value: 'teleId',
          align: 'center'
        },
        {
          text: '物联网平台',
          value: 'plantform',
          align: 'center'
        }
      ], // 显示列
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0, // 数据总数
      controllerTypeList: [], // 控制器类型列表
      statusList: [{ value: '1', name: '在线' }, { value: '0', name: '离线' }],
      listLoading: false, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      dialogFormVisible: false
    }
  },
  created() {
    this.fetchControllerType()// 获取控制器类型
    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)'
      })
      // 批量导入上传请求
      batchImportController(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$message.success('导入成功')
          this.fileList = []
          this.fetchData()
        } else {
          this.$message.error(res.message)
        }
      }).catch(err => {
        loading.close()
        this.$message.error(err.message)
      })
      this.fileList = []
    },
    // 批量导出
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '下载中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      batchExportController(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)
      })
    },
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    // 重置
    clearInput() {
      this.listQuery = {
        controllerCode: '',
        controllerType: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }
      this.fetchData(false)
    },
    // 新增设备
    add() {
      this.dialogFormVisible = true
      this.$refs.editController.initDialog('create', this.dialogFormVisible)
    },
    // 编辑设备信息
    edit(row) {
      this.dialogFormVisible = true
      this.$refs.editController.initDialog('update', this.dialogFormVisible, row)
    },
    // 删除设备
    del() {
      if (this.checkSelection()) {
        const devIds = []
        this.multipleSelection.forEach(function(value, index) {
          devIds.push(value.id)
        })
        this.$confirm(
          '确定要删除所选控制器吗?',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          batchDelController(devIds).then(response => {
            if (response.code === 200) {
              this.$message.success('删除成功')
              this.fetchData()
            }
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取设备列表
    fetchData(isNowPage = true) {
      this.listLoading = true
      console.log(this.listQuery)
      if (!isNowPage) { // 是否显示当前页,否则跳转第一页
        this.listQuery.offset = 1
      }
      getContrllerList(this.listQuery).then(response => {
        if (response.code === 200) {
          this.list = response.data.rows
          this.total = parseInt(response.data.total)
        } else {
          this.$message.error(response.message)
        }
        this.listLoading = false
      })
    },
    fetchControllerType() {
      getControllerType().then(response => {
        if (response.code === 200) {
          this.controllerTypeList = response.data
        }
      })
    },
    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>

</style>