Newer
Older
smart-metering-front / src / views / measure / source / list.vue
lyg on 26 Mar 2024 10 KB 证书作废等修改完成
<script lang="ts" setup name="ListSource">
import { getCurrentInstance, ref } from 'vue'
import type { Ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { ISupplier, IlistQuery, IlistType } from './list_interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { batchImportSource, exportSourceList, getSoucreList, getSoucreListDelete } from '@/api/measure/source'
import { uploadApi } from '@/api/system/notice'
import { printJSON } from '@/utils/printUtils'
import { exportFile } from '@/utils/exportUtils'
import { SCHEDULE } from '@/utils/scheduleDict'
import useTemplateDownload from '@/utils/useTemplateDownload'
import { keepSearchParams, renewSearchParams } from '@/utils/keepQuery'

// 查询条件
const listQuery: Ref<IlistQuery> = ref({
  supplierNo: '', // 业务内容
  supplierName: '', // 溯源供方名称
  businessContent: '', // 溯源供方编号
  formId: SCHEDULE.SUPPLIER_APPROVAL,
  offset: 1,
  limit: 20,
})
// 页面跳转之前保存参数
onBeforeRouteLeave((to: any) => {
  keepSearchParams(to.path, 'measure-source', listQuery.value)
})
// 重新赋值
listQuery.value = renewSearchParams('measure-source') || {
  supplierNo: '', // 业务内容
  supplierName: '', // 溯源供方名称
  businessContent: '', // 溯源供方编号
  formId: SCHEDULE.SUPPLIER_APPROVAL,
  offset: 1,
  limit: 20,
}
// 表格数据
const list = ref<ISupplier[]>([])
const { proxy } = getCurrentInstance() as any
// 总数
const total = ref(0)
// 表头
const columns = ref<TableColumn[]>([
  {
    text: '溯源供方编号',
    value: 'supplierNo',
    width: '160',
    align: 'center',
  },
  {
    text: '溯源供方名称',
    value: 'supplierName',
    width: '120',
    align: 'center',
  },
  {
    text: '业务内容',
    value: 'businessContent',
    align: 'center',
  },
  {
    text: '公司业务范围',
    value: 'businessScope',
    align: 'center',
  },
  {
    text: '负责人',
    value: 'director',
    align: 'center',
  },
  {
    text: '联系方式',
    value: 'mobile',
    align: 'center',
  },
  {
    text: '地址',
    value: 'briefName',
    align: 'center',
  },
  {
    text: '创建时间',
    value: 'createTime',
    align: 'center',
    width: '180px',
  },
  // {
  //   text: '备注',
  //   value: 'remark',
  //   align: 'center',
  // },
])

// 选中的内容
const checkoutList = ref<string[]>([])
const loadingTable = ref(false)
const fetchData = (isNowPage: boolean) => {
  loadingTable.value = true
  // if (!isNowPage) {
  //   // 是否显示当前页,否则跳转第一页
  //   listQuery.value.offset = 1
  // }
  getSoucreList(listQuery.value).then((response) => {
    list.value = response.data.rows.map((item: ISupplier) => {
      if (item.companyProvinceName && item.companyCityName) {
        item.briefName = `${item.companyProvinceName}${item.companyCityName}`
      }
      else {
        item.briefName = item.companyProvinceName || item.companyCityName
      }
      return item
    })
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}
fetchData(true)
// 多选发生改变时
const handleSelectionChange = (e: any) => {
  checkoutList.value = e.map((item: { id: string }) => item.id)
}
// 点击编辑id和删除row类型
interface rowReturn {
  id: string
  supplierName: string
}
const $router = useRouter()
// 点击编辑/详情
const handleEdit = (row: rowReturn, pageType: 'edit' | 'detail') => {
  $router.push(`/source/${pageType}/${row.id}`)
}
// 点击删除
const handleDelete = (index: string, row: rowReturn) => {
  ElMessageBox.confirm(
    `确认删除${row.supplierName}吗?`,
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      getSoucreListDelete({ id: row.id as string }).then((res) => {
        if (res.code === 200) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          fetchData(true)
        }
      })
    })
}
// 点击搜索
const searchList = () => {
  fetchData(true)
}

// 点击重置
const clearList = () => {
  listQuery.value = {
    supplierNo: '', // 业务内容
    supplierName: '', // 溯源供方名称
    businessContent: '', // 溯源供方编号
    formId: SCHEDULE.SUPPLIER_APPROVAL,
    offset: 1,
    limit: 20,
  }
  fetchData(true)
}

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

// 添加溯源供方
const add = () => {
  $router.push('/source/add')
}
const exportAll = () => {
  const loading = ElLoading.service({
    lock: true,
    text: '下载中请稍后',
    background: 'rgba(255, 255, 255, 0.8)',
  })
  if (list.value.length > 0) {
    const params = {
      supplierNo: listQuery.value.supplierNo, // 业务内容
      supplierName: listQuery.value.supplierName, // 溯源供方名称
      businessContent: listQuery.value.businessContent, // 溯源供方编号
      formId: SCHEDULE.SUPPLIER_APPROVAL,
      ids: checkoutList.value,
    }
    exportSourceList(params).then((res) => {
      const blob = new Blob([res.data])
      exportFile(blob, '溯源供方列表.xlsx')
    })
  }
  else {
    ElMessage.warning('无数据可导出数据')
  }
  loading.close()
}

// 打印列表
function printList() {
  // 打印列
  const properties = columns.value.map((item) => {
    return {
      field: item.value,
      displayName: item.text,
    }
  })
  if (checkoutList.value.length <= 0 && list.value.length > 0) {
    printJSON(list.value, properties, '溯源供方列表')
  }
  else if (checkoutList.value.length > 0) {
    const printList = list.value.filter((item: ISupplier) => checkoutList.value.includes(item.id))
    printJSON(printList, properties, '溯源供方列表')
  }
  else {
    ElMessage.warning('无可打印内容')
  }
}

// -------------------------------------模板下载、批量导入-----------------------------------
// 模板下载
const templateDownload = () => {
  useTemplateDownload('溯源供方模块')
}
const fileRef = ref() // 文件上传input
const onFileChange = (event: any) => {
  // 原生上传
  // console.log(event.target.files)
  // if (event.target.files[0].type === 'application/pdf') {
  if (event.target.files?.length !== 0) {
    // 创建formdata对象
    const fd = new FormData()
    fd.append('multipartFile', event.target.files[0])
    const loading = ElLoading.service({
      lock: true,
      background: 'rgba(255, 255, 255, 0.8)',
    })
    batchImportSource(fd).then((res) => {
      if (res.code === 200) {
        ElMessage.success('导入成功')
        fetchData(true)
        loading.close()
        event.target.value = ''
      }
      else {
        ElMessage.error(res.message)
      }
    })
  }
//   }
//   else {
//     ElMessage.error('请上传pdf格式')
//   }
}
// 批量导入
const batchImport = () => {
  fileRef.value.click()
}
// --------------------------------------------------------------------------------------------

onActivated(() => {
  fetchData(true)
})
</script>

<template>
  <app-container>
    <search-area
      :need-clear="true"
      @search="searchList" @clear="clearList"
    >
      <search-item>
        <el-input
          v-model.trim="listQuery.supplierNo"
          placeholder="溯源供方编号"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.supplierName"
          placeholder="溯源供方名称"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.businessContent"
          placeholder="业务内容"
          clearable
        />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm('/measure/measureSource/import')" icon="icon-import" title="批量导入" type="primary" @click="batchImport" />
        <icon-button v-if="proxy.hasPerm('/measure/measureSource/mould')" icon="icon-template" title="模板下载" type="primary" @click="templateDownload" />
        <icon-button v-if="proxy.hasPerm('/measure/measureSource/add')" icon="icon-add" title="新建" type="primary" @click="add" />
        <icon-button v-if="proxy.hasPerm('/measure/measureSource/export')" icon="icon-export" title="导出" type="primary" @click="exportAll" />
        <icon-button v-if="proxy.hasPerm('/measure/measureSource/print')" icon="icon-print" title="打印" type="primary" @click="printList" />
      </template>
      <input v-show="false" ref="fileRef" type="file" accept="pdf/*" @change="onFileChange">
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" is-showmulti-select @change="changePage" @multi-select="handleSelectionChange"
      >
        <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="操作" align="center" width="90" fixed="right">
            <template #default="{ row }">
              <!-- <el-button
                  v-if="proxy.hasPerm('/measure/measureSource/edit')"
                  size="small"
                  type="primary"
                  link
                  @click="handleEdit(row, 'edit')"
                >
                  编辑
                </el-button> -->
              <el-button size="small" link type="primary" @click="handleEdit(row, 'detail')">
                详情
              </el-button>
              <el-button v-if="proxy.hasPerm('/measure/measureSource/delete')" size="small" link type="danger" @click="handleDelete(row.$index, row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

<style lang="scss" scoped>
.pagination {
  position: fixed;
  bottom: 0;
}
</style>