Newer
Older
jh-business-system / src / views / resource / customer / list.vue
<!-- 委托方名录 -->
<script name="list" lang="ts" setup>
import type { DateModelType } from 'element-plus'
import { ElLoading, ElMessage, ElMessageBox, dayjs } from 'element-plus'
import type { ICustomerInfo, IListQuery } from './components/customer-info'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import useTemplateDownload from '@/utils/useTemplateDownload'
import { batchImport, deleteCustomer, getCustomerList } from '@/api/resource/customer'

const { proxy } = getCurrentInstance() as any
const router = useRouter()
// 查询条件
const searchQuery = ref<IListQuery>({
  customerNo: '', // 委托方编号
  customerName: '', // 委托方名字
  contacts: '', // 联系人
  offset: 1,
  limit: 20,
})
const dateRange = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据
const total = ref(0) // 数据条数
const loadingTable = ref(false) // 表格loading

// 表头
const columns = ref<TableColumn[]>([
  { text: '委托方代码', value: 'customerNo', align: 'center', width: '175' },
  { text: '委托方名称', value: 'customerName', align: 'center' },
  { text: '联系人', value: 'contacts', align: 'center', width: '120' },
  { text: '联系电话', value: 'mobile', align: 'center', width: '140' },
  { text: '通信地址', value: 'address', align: 'center' },
])
const list = ref<Array<ICustomerInfo>>([]) // 表格数据

// 逻辑
// 跳转到新建的页面
const addCustomerInfo = () => {
  router.push({ path: 'customer/add' })
}

// 点击编辑按钮
const update = (row: ICustomerInfo) => {
  sessionStorage.setItem('customerInfo', JSON.stringify(row))
  router.push({
    path: `customer/edit/${row.id}`,
    query: {
    },
  })
}

// 点击查看按钮
const detail = (row: ICustomerInfo) => {
  // 将行数据存入缓存 用来在路由之间传递数据
  sessionStorage.setItem('customerInfo', JSON.stringify(row))

  router.push({
    path: `customer/detail/${row.id}`,
    query: {
    },
  })
}

// 删除委托方名录 或 删除草稿箱中的审批单
const deleteList = (row: any) => {
  const confirmStr = `委托方${row.customerName}`
  ElMessageBox.confirm(`是否删除${confirmStr}`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    deleteCustomer({ id: row.id }).then((res) => {
      if (res.code === 200) {
        ElMessage.success(`${confirmStr} 删除成功`)
        fetchData(true)
      }
      else {
        ElMessage.error(`${confirmStr} 删除失败: ${res.message}`)
      }
    })
  })
}

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    searchQuery.value.offset = 1
  }
  getCustomerList(searchQuery.value).then((response) => {
    if (response.code === 200) {
      list.value = response.data.rows.map((item: { createTime: string }) => {
        return {
          ...item,
        }
      })
      total.value = parseInt(response.data.total)
    }
    loadingTable.value = false
  }).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 = () => {
  searchQuery.value = {
    customerNo: '', // 委托方编号
    customerName: '', // 委托方名字
    contacts: '', // 联系人
    offset: 1,
    limit: 20,
  }
  dateRange.value = ['', '']
  fetchData(true)
}
const tableRef = ref() // 表格组件ref

// -------------------------------------表格上方工具栏-----------------------------------
const fileRef = ref() // 文件上传input
const onFileChange = (event: any) => {
  // 原生上传
  if (event.target.files?.length !== 0) {
    // 创建formdata对象
    const fd = new FormData()
    fd.append('file', event.target.files[0])
    const loading = ElLoading.service({
      lock: true,
      background: 'rgba(255, 255, 255, 0.8)',
    })
    // 调批量导入接口
    batchImport(fd).then((_res: any) => {
      ElMessage.success('上传成功')
      event.target.value = null
      loading.close()
      fetchData(true)
      loading.close()
    }).catch(() => {
      event.target.value = null
      loading.close()
    })
  }
}
// 批量导入
const handleBatchImport = () => {
  fileRef.value.click()
}
// -------------------------------------------------------------------------------------------
onMounted(() => {
  fetchData()
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="searchList" @clear="reset">
      <search-item>
        <el-input v-model="searchQuery.customerNo" placeholder="委托方代码" clearable />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.customerName" placeholder="委托方名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.contacts" placeholder="联系人" clearable />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container>
      <!-- 表头区域 -->
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm('/resource/customer/infoList/add')" icon="icon-add" title="新建" @click="addCustomerInfo" />
        <icon-button v-if="proxy.hasPerm('/resource/customer/infoList/import')" icon="icon-import" title="批量导入" type="primary" @click="handleBatchImport" />
        <icon-button v-if="proxy.hasPerm('/resource/customer/infoList/import')" icon="icon-template" title="模板下载" type="primary" @click="useTemplateDownload('委托方名录')" />
      </template>
      <input v-show="false" id="fileInput" ref="fileRef" type="file" accept="*" @change="onFileChange">

      <!-- 表格区域 -->
      <normal-table
        id="customerInfoTabel"
        ref="tableRef"
        :data="list" :total="total" :columns="columns"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }"
        :list-loading="loadingTable"
        @change="changePage"
      >
        <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="180">
            <template #default="{ row }">
              <el-button size="small" type="primary" link @click="detail(row)">
                查看
              </el-button>

              <el-button v-if="proxy.hasPerm('/resource/customer/infoList/edit')" size="small" type="primary" link @click="update(row)">
                编辑
              </el-button>

              <el-button v-if="proxy.hasPerm('/resource/customer/infoList/del')" size="small" type="danger" link @click="deleteList(row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>