Newer
Older
xc-metering-front / src / views / system / label / list.vue
tanyue on 27 Jul 2023 6 KB 20230727 标签管理页面搭建
<!-- 标签列表 -->
<script lang="ts" setup name="Label">
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { ILabelItem, IUseStateList, IlistQuery } from './label'
import { getDictByCode } from '@/api/system/dict'
import { delLabel, getLabelList } from '@/api/system/label'
import { uploadApi } from '@/api/system/notice'

const { proxy } = getCurrentInstance() as any
const useStateList = ref<IUseStateList[]>([]) // 关联使用情况字典值
const listQuery = reactive({
  useState: '', // 使用情况
  offset: 1,
  limit: 20,
})
const fileRef = ref() // 文件上传input,获取input的引用
// 表头
const columns = ref([
  { text: 'RFID标签编号', value: 'rfid', width: '200' },
  { text: '使用情况', value: 'status', width: '100' },
  { text: '绑定设备编号', value: 'equipmentNo', width: '300' },
  { text: '绑定设备名称', value: 'equipmentName', width: '300' },
  { text: '使用岗位', value: 'equipmentDeptName' },
  { text: '负责人', value: 'equipmentDirectorName', width: '150' },
])
const list = ref([
  { rfid: '12341234', status: '未使用' },
  { rfid: '11267643211', status: '已使用', equipmentNo: '312020020007', equipmentName: '压力监测仪', equipmentDeptName: '十二室', equipmentDirectorName: '智慧计量' },
])
const total = ref(20)
const listLoading = ref(false)

// 获取标签使用情况字典值
const fetchLabelUseDict = () => {
  getDictByCode('labelUseState').then((res) => {
    useStateList.value = res.data
  })
}

// 获取流程列表
const fetchData = async (isNowPage: boolean) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQuery.offset = 1
  }
  const param = {
    useState: '', // 使用状态
    offset: listQuery.offset,
    limit: listQuery.limit,
  }
  const response = await getLabelList(param)
  total.value = parseInt(response.data.total)
  list.value = response.data.rows.map((item: any) => {
    return {
      ...item,
      rfid: item.rfid,
      status: item.useState === 1 ? '已使用' : '未使用',
      equipmentNo: item.equipmentNo,
      equipmentName: item.equipmentName,
      describe: item.formDesc,
    }
  })
  listLoading.value = false
}

// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
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 search = () => {
  fetchData(false)
}

// 重置
const reset = () => {
  listQuery.useState = '' // 使用情况
  fetchData(true)
}

// 点击导入
const upload = () => {
  fileRef.value.click()
}
const onFileChange = (event: any) => {
  // 原生上传
  // console.log(event.target.files)
  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)',
    })
    // TODO: 暂时用上传接口,将来改为导入的接口
    uploadApi(fd).then((res) => {
      if (res.code === 200) {
        ElMessage.success('上传成功')
        loading.close()
      }
      else {
        ElMessage.error(res.message)
        loading.close()
      }
    })
  }
}

// 删除
const del = (row: ILabelItem) => {
  ElMessageBox.confirm(
    `确认删除标签${row.rfid}吗?`,
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      delLabel(row.rfid).then((res) => {
        if (res.code === 200) {
          ElMessage.success('删除成功')
          fetchData(true)
        }
      })
    })
}

onMounted(() => {
  fetchLabelUseDict()
})
</script>

<template>
  <div class="label">
    <app-container>
      <!-- 筛选条件 -->
      <search-area :need-clear="true" @search="search" @clear="reset">
        <search-item>
          <el-select v-model="listQuery.useState" placeholder="请选择使用情况" clearable>
            <el-option
              v-for="item in useStateList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </search-item>
      </search-area>

      <!-- 查询结果Table显示 -->
      <table-container>
        <template #btns-right>
          <icon-button ref="fileRef" icon="icon-import" title="导入" type="primary" @click="upload" />
          <input v-show="false" ref="fileRef" type="file" accept=".xls,.xlsx" @change="onFileChange">
        </template>

        <div class="table-area">
          <normal-table
            :data="list"
            :total="total"
            :columns="columns"
            :query="listQuery"
            :list-loading="listLoading"
            @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="100" align="center" fixed="right">
                <template #default="scope">
                  <el-button type="danger" link size="small" class="table-text-button" @click="del(scope.row)">
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </template>
          </normal-table>
        </div>
      </table-container>
    </app-container>
  </div>
</template>

<style lang="scss" scoped>
.label {
  .table-area {
    background-color: #fff;
    padding: 10px;
    margin-top: 10px;
    border-radius: 7px;
  }
}
</style>

<style lang="scss">
  .list-login-log {
    .el-button + .el-button {
      margin-top: -10px;
    }
  }

  .el-message-box {
    overflow: auto;
  }
</style>