Newer
Older
laserPTZFrontV2.0 / src / views / station / list.vue
wangxitong on 26 Sep 4 KB 流媒体
<script lang="ts" setup name="ResourceList">
import type { Ref } from 'vue'
import { getCurrentInstance, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import dayjs from 'dayjs'
import type { IStationInfo, IStationTypeInfo } from './interface'
import StationEdit from './edit.vue'
import { delStation, getStationListPage } from '@/api/ptz/station'
import { getDeptTreeList } from '@/api/system/dept'
import type { TableColumn } from '@/components/NormalTable/table_interface'

const { proxy } = getCurrentInstance() as any
const editDialog = ref() // 组件

// 默认查询条件
const defaultQuery = {
  keyword: '',
  offset: 1,
  limit: 20,
}
// 总数
const total = ref(0)
// 查询条件
const listQuery = reactive({ ...defaultQuery })

// 搜索重置
function reset() {
  Object.assign(listQuery, defaultQuery)
  fetchData()
}
// 表格表头
const columns: Ref<TableColumn[]> = ref([])
columns.value = [
  { text: '场站名称', value: 'stationName', align: 'center' },
  { text: '权属', value: 'deptName', width: 200, align: 'center' },
  { text: '修改时间', value: 'ts', width: 200, align: 'center' },
]
// 数据列表
const list: Ref<IStationInfo[]> = ref([])
const getDepList = ref([])
const loading = ref(false)

// 搜索按钮
function search() {
  fetchData()
}

// 获取部门树
function getDepData() {
  getDeptTreeList().then((res) => {
    getDepList.value = res.data
  })
}

// 查询数据
function fetchData(isNowPage = false) {
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQuery.offset = 1
  }
  loading.value = true
  getStationListPage(listQuery).then((res: any) => {
    list.value = res.data.rows.map((item: {
      ts: string
      deptName: string
      deptId: any
    }) => {
      item.ts = dayjs(item.ts).format('YYYY-MM-DD HH:mm:ss')
      item.deptId = `${item.deptId}`
      item.deptName = getDepList.value.find((depItem: { id: any }) => depItem.id === item.deptId.toString())?.name as string
      return item
    })
    total.value = res.data.total
    loading.value = false
  }).catch(() => {
    loading.value = false
  })
}

// 编辑资源
function edit(row: IStationInfo) {
  editDialog.value.initDialog('update', row)
}
// 删除资源
function del(row: IStationInfo) {
  ElMessageBox.confirm(
    `确定要删除${row.stationName}吗?`,
    '确认删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    delStation({ id: row.id }).then((res: any) => {
      ElMessage.success('删除成功')
      fetchData()
    })
  })
}
// 添加资源
function add() {
  editDialog.value.initDialog('create')
}

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

onMounted(() => {
  getDepData()
  fetchData() // 获取列表
})
</script>

<template>
  <app-container>
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model="listQuery.keyword" placeholder="场站名称" clearable style="width: 200px;" />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button icon="icon-add" title="新增" @click="add" />
      </template>
      <normal-table :query="listQuery" :list-loading="loading" :columns="columns" :data="list" :total="total" :border="false" @change="changePage">
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="操作" width="200" align="center">
            <template #default="scope">
              <el-button type="primary" link size="small" class="table-text-button" @click="edit(scope.row)">
                编辑
              </el-button>
              <el-button type="primary" link size="small" class="table-text-button" @click="del(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
    <station-edit ref="editDialog" @close-refresh="search" />
  </app-container>
</template>