Newer
Older
sensorHubPlusFront / src / views / basic / group / list.vue
liyaguang 8 days ago 7 KB 页面搭建
<!-- 分组管理 列表 -->
<script name="GroupList" lang="ts" setup>
import { ElMessage, ElMessageBox, dayjs } from 'element-plus'
import type { DateModelType } from 'element-plus'
import type { IGroupInfo, IListQuery } from './group-info'
import AddGroupDialog from './addGroupDialog.vue'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { delGroup, getGroupListPage } from '@/api/basic/group'
import { getDictByCode } from '@/api/system/dict'
import { listIcons } from '@iconify/vue'
import { isNull } from 'lodash-es'

const { proxy } = getCurrentInstance() as any
const router = useRouter()

const refAddGroupDialog = ref()

// 查询条件
const searchQuery = ref<IListQuery>({
  groupName: '', // 分组名
  beginTime: '', // 创建时间-起始
  endTime: '', // 创建时间-结束
  offset: 1,
  limit: 20,
})
const dateRange = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据
const total = ref(0) // 数据条数
const loadingTable = ref(false) // 表格loading

// 表头
const columns = ref<TableColumn[]>([
  { text: '分组编号', value: 'groupNo', align: 'center' },
  { text: '分组名称', value: 'groupName', align: 'center' },
  { text: '所属组织', value: 'deptName', align: 'center' },
  { text: '设备数量', value: 'deviceCount', align: 'center', width: '120' },
  { text: '订阅数量', value: 'subscribeCount', align: 'center', width: '120' },
  { text: '分组创建时间', value: 'createTime', align: 'center', width: '200' },
])
const dataList = ref<Array<IGroupInfo>>([]) // 表格数据

// 逻辑

// 跳转到新建的页面
const addDeviceGroup = () => {
  refAddGroupDialog.value.initDialog({ type: 'create' })
}

// 详情
const detail = (row: IGroupInfo) => {
  router.push({
    query: {
      type: 'detail',
      id: row.id,
    },
    path: 'detail',
  })
}

// 编辑
const updateById = (row: IGroupInfo) => {
  sessionStorage.setItem('groupInfo', JSON.stringify(row))
  refAddGroupDialog.value.initDialog({ type: 'update', id: row.id })
}

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    searchQuery.value.offset = 1
  }
  getGroupListPage(searchQuery.value).then((response) => {
    if (response.code === 200) {
      dataList.value = response.data.rows
      total.value = parseInt(response.data.total)

    }
    loadingTable.value = false
    calcTableHeight()
  }).catch(() => {
    loadingTable.value = false
  })
}

const searchList = () => {
  fetchData(true)
}

// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.value.limit = val.size
  }
  if (val && val.page) {
    searchQuery.value.offset = val.page
  }
  fetchData(true)
}

// 重置
const reset = () => {
  dateRange.value = ['', '']
  searchQuery.value = {
    groupName: '', // 分组名
    beginTime: '', // 创建时间-起始
    endTime: '', // 创建时间-结束
    offset: 1,
    limit: 20,
  }
  fetchData(true)
}

// 删除
const deleteById = (row: IGroupInfo) => {
  ElMessageBox.confirm(`是否删除分组 ${row.groupName}`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    delGroup(row.id!).then((res) => {
      if (res.code === 200) {
        ElMessage.success(`分组 ${row.groupName} 删除成功`)
        fetchData(true)
      }
      else {
        ElMessage.error(`分组 ${row.groupName} 删除失败: ${res.message}`)
      }
    })
  })
}

const groupSavedHandler = () => {
  fetchData(true)
}

// 获取在用状态字典值
const getDeviceTypeDict = () => {
  getDictByCode('deviceType').then((res: any) => {
    if (res.code === 200) {
      sessionStorage.setItem('deviceType', JSON.stringify(res.data))
    }
  })
}
const getDict = async () => {
  getDeviceTypeDict()
}

watch(dateRange, (val) => {
  if (val) {
    searchQuery.value.beginTime = dayjs(val[0]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[0]).format('YYYY-MM-DD')
    searchQuery.value.endTime = dayjs(val[1]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[1]).format('YYYY-MM-DD')
  }
  else {
    searchQuery.value.beginTime = ''
    searchQuery.value.endTime = ''
  }
})

onMounted(async () => {
  await getDict()
  searchList()
  window.addEventListener('resize', calcTableHeight)
})
const tableHeight = ref(400)
const calcTableHeight = () => {
  // 顶部高度
  const topHeight = 50
  // app-container的 padding距离
  const appPadding = 20
  // 查询组件的高度
  const searchDiv = document.getElementById('search-div-id')
  const searchHeight = searchDiv ? searchDiv.clientHeight : 0
  // 表格顶部的文字提示高度
  const tableTopHeight = 32 + 10
  // 表格表头
  const tableHeaderHeight = 40
  // 分页器的高度
  const tablePaginationHeight = 40
  // 判断数据长度
  const height = window.innerHeight - topHeight - appPadding - searchHeight - tableTopHeight - tableHeaderHeight - tablePaginationHeight
  if (dataList.value.length * 40 >= height) {
    tableHeight.value = height
  }
  else {
    tableHeight.value = dataList.value.length ? (dataList.value.length + 1) * 40 : 200
  }
}
onBeforeUnmount(() => {
  window.removeEventListener('resize', calcTableHeight)
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="searchList" @clear="reset">
      <search-item>
        <el-input v-model="searchQuery.groupName" placeholder="分组名称" clearable />
      </search-item>
      <search-item>
        <el-date-picker v-model="dateRange" type="daterange" start-placeholder="创建时间(开始)" end-placeholder="创建时间(结束)" />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container title="分组管理">
      <!-- 表头区域 -->
      <template #btns-right>
        <!-- <icon-button v-if="proxy.hasPerm(`/basic/group/add`)" icon="icon-add" title="新建" @click="addDeviceGroup" /> -->
        <el-button v-if="proxy.hasPerm(`/basic/group/add`)" type="primary" @click="addDeviceGroup">新增</el-button>
      </template>

      <!-- 表格区域 -->
      <normal-table :data="dataList" :total="total" :columns="columns"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }" :list-loading="loadingTable"
        @change="changePage" :height="tableHeight">
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column fixed="right" label="操作" align="center" width="130">
            <template #default="{ row }">
              <el-button size="small" type="primary" link @click="detail(row)">
                查看
              </el-button>
              <el-button v-if="proxy.hasPerm(`/basic/group/edit`)" size="small" type="primary" link
                @click="updateById(row)">
                编辑
              </el-button>
              <el-button v-if="proxy.hasPerm(`/basic/group/del`)" size="small" type="danger" link
                @click="deleteById(row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>

    <add-group-dialog ref="refAddGroupDialog" @record-saved="groupSavedHandler" />
  </app-container>
</template>