Newer
Older
xc-metering-front / src / views / tested / document / list / index.vue
<!-- 文档管理列表 -->
<script lang="ts" setup name="DocumentList">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { IDocumentList, IlistQuery } from './document-interface'
import { delDocument, getListPage } from '@/api/eqpt/document/index'
import { urlToBlob } from '@/utils/download'
import { getPhotoUrl } from '@/api/system/tool'
import { getDictByCode } from '@/api/system/dict'
import { getAdminDept } from '@/api/system/user'
import { keepSearchParams } from '@/utils/keepQuery'
const { proxy } = getCurrentInstance() as any
const listQuery = reactive<IlistQuery>({
  fileNo: '',
  fileName: '',
  fileType: '',
  createCompanyId: '',
  createUserName: '',
  updateTimeStart: '',
  updateTimeEnd: '',
  implementationStatus: '0',
  offset: 1,
  limit: 20,
})
onBeforeRouteLeave((to: any) => {
  keepSearchParams(to.path, 'DocumentList')
})
const columns = ref([
  {
    text: '文件号',
    value: 'fileNo',
    align: 'center',
  },
  {
    text: '文件名称',
    value: 'fileName',
    align: 'center',
  },
  {
    text: '类别',
    value: 'fileTypeName',
    align: 'center',
  },
  {
    text: '发布单位',
    value: 'createCompanyName',
    align: 'center',
  },
  {
    text: '发布人',
    value: 'createUserName',
    align: 'center',
  },
  {
    text: '发布时间',
    value: 'createTime',
    align: 'center',
  },
  {
    text: '实施状态',
    value: 'implementationStatusName',
    align: 'center',
  },
])
const list = ref<IDocumentList[]>([])
const total = ref(0)
const listLoading = ref(true)

// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQuery.offset = 1
  }
  getListPage(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 发布开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  listQuery.updateTimeStart = ''
  listQuery.updateTimeEnd = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.updateTimeStart = `${newVal[0]} 00:00:00`
      listQuery.updateTimeEnd = `${newVal[1]} 23:59:59`
    }
  }
})
// 查询数据
const search = () => {
  fetchData(false)
}
// 重置
const reset = () => {
  listQuery.fileName = ''
  listQuery.fileType = ''
  listQuery.updateTimeStart = ''
  listQuery.updateTimeEnd = ''
  listQuery.fileNo = ''
  listQuery.createUserName = ''
  listQuery.createCompanyId = ''
  listQuery.implementationStatus = ''
  listQuery.offset = 1
  listQuery.limit = 20
  search()
}
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
const $router = useRouter()
// 新建编辑操作
const handler = (row: IDocumentList | any, type: string) => {
  $router.push({
    path: `/documentlist/${type}`,
    query: {
      row: JSON.stringify(row),
      id: row.id,
    },
  })
}
// 删除
const delHandler = (row: IDocumentList) => {
  ElMessageBox.confirm(
    '确认删除此文档信息吗?',
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    delDocument(row.id).then((res) => {
      ElMessage.success('操作成功')
      // close()
      search()
    })
  })
}
// 下载
const download = (row: IDocumentList) => {
  // 获取文件地址
  // downloadFile
  getPhotoUrl(row.attachment).then((res) => {
    urlToBlob(res.data, row.fileName)
  })
}
const permUrl = ref({
  add: '/tested/document/add',
  edit: '/tested/document/edit',
  del: '/tested/document/delete',
  download: '/tested/document/download',
})
// 类别
const fileTypeList = ref<{ id: string; value: string; name: string }[]>([])
// 发布单位
const createCompanyList = ref<{ id: string; value: string; name: string }[]>([])
// 实施状态
const implementationStatusList = ref<{ id: string; value: string; name: string }[]>([])
const fetchDict = () => {
  getDictByCode('eqptFileType').then((res) => {
    fileTypeList.value = res.data
  })
  getDictByCode('eqptImplementStatus').then((res) => {
    implementationStatusList.value = res.data
  })
  getAdminDept({}).then((res) => {
    createCompanyList.value = res.data.map((item: any) => ({ id: item.id, value: item.id, name: item.fullName }))
  })
}
fetchDict()

onActivated(() => {
  // 从编辑或者新增页面回来需要重新获取列表数据
  // $router.options.history.state.forward 上一次路由地址
  if (!($router.options.history.state.forward as string || '').includes('detail')) {
    console.log('需要重新获取列表')
    fetchData(false)
  }
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.fileNo" placeholder="文件号" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.fileName" placeholder="文件名称" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.fileType" clearable filterable placeholder="类别">
          <el-option v-for="item in fileTypeList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.createCompanyId" clearable filterable placeholder="发布单位">
          <el-option v-for="item in createCompanyList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.createUserName" placeholder="发布人" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.implementationStatus" clearable filterable placeholder="实施状态">
          <el-option v-for="item in implementationStatusList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="datetimerange" type="daterange" value-format="YYYY-MM-DD"
          format="YYYY-MM-DD" range-separator="至" start-placeholder="发布开始时间" end-placeholder="发布结束时间"
        />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm(permUrl.add)" icon="icon-add" title="新增" @click="handler({}, 'create')" />
      </template>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns as any" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="true" @change="changePage"
      >
        <template #columns>
          <el-table-column label="操作" width="160" align="center">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="handler(scope.row, 'detail')">
                查看
              </el-button>
              <el-button v-if="proxy.hasPerm(permUrl.edit)" link type="primary" size="small" @click="handler(scope.row, 'update')">
                编辑
              </el-button>
              <el-button v-if="proxy.hasPerm(permUrl.del)" link type="danger" size="small" @click="delHandler(scope.row)">
                删除
              </el-button>
              <el-button v-if="proxy.hasPerm(permUrl.download)" link type="primary" size="small" @click="download(scope.row)">
                下载
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>